﻿//
// create closure
//
(function($) {

  //private variable to hold options
  var opts = {};
  
  //private Variable to hold all datarows
  var $dataRows;
  //
  // plugin definition
  //
  $.fn.gridviewex = function(options) {
	// build main options before element iteration
	
	opts = $.extend({}, $.fn.gridviewex.defaults, options);
	
	// iterate and reformat each matched element
	var i = 0;
	return this.each(function() {
	  $this = $(this);
	  if(!$this.is('table')){
	    alert('Your selection must be element of type table');
	    return;
	  }
	 
	  //Header row must contain th tag
	  var $headerRow = $this.children('tbody').children('tr:has(th)');
	  
	  //Data rows, should execlude header row.
	  $dataRows = ($headerRow.length > 0) ? $this.children('tbody').children('tr:not(:has(th))') : $this.children('tbody').children('tr');
	 
	  //If the table contains pager row, this flag should be set to true by developer
	  /*if(!opts.hasPagerRow)
	    $dataRows = $dataRows.not(':last');*/
	  
	  var rowIndex = 0;
	  //Attache dataRowClass & altDataRowClass to rows
	  $dataRows.each(function(){

	    if(rowIndex%2==0){
	        if(!$(this).hasClass(opts.dataRowClass))
	            $(this).addClass(opts.dataRowClass);}
	    else{
	        if(!$(this).hasClass(opts.altDataRowClass))
	            $(this).addClass(opts.altDataRowClass);}
	       
	    //Columns    
	    var $dataCells = $(this).children();
	    
	    var cellIndex = 0;
	    
	    //Bind mouseover, mouseout and click events handlers to column cells
	    $dataCells.each(function(){
	        //Extra data to be sent to event handler. Can be accessed like this e.data.propertyName.
	        var args = {rowIndex:rowIndex,cellIndex:cellIndex};
	        $(this).bind('mouseover', args, cellHover);
	        $(this).bind('mouseout', args, cellOut);
	        $(this).bind('click', args, cellClick);
	        cellIndex++;
	    });
	    rowIndex++;
	  });
	});
  };
  //
  // private functions
  //
  
  //mouseover event handler 
  function cellHover(e)
  {
    //$(this) is the current hover cell jQuery Object.
    //$dataRows is a private jGuery variable holding all data rows
    //$column is a jQuery local variable holds all cells on the same column (having same cellIndex) as current cell
    
    //get all cells in the current row including current hovering cell.
    var $cells = $(this).siblings().andSelf();
    
    //Accessing additional data sent to the event handler.
    var index = e.data.cellIndex+1;
    
    //Equation to select cells of the same column.
    var eq = ':nth-child('+index+')';
    
    //Column Cells
    var $column =  $dataRows.children(eq);
    
    //Attache rowHoverClass to each cell.
    $cells.each(function(){
        if(notEmptyOrNull(opts.rowHoverClass) && !$(this).hasClass(opts.rowHoverClass))
            $(this).addClass(opts.rowHoverClass);
    });
    
    //Attache columnHoverClass to each cell on the column.   
    $column.each(function(){
        if(notEmptyOrNull(opts.columnHoverClass) && !$(this).hasClass(opts.columnHoverClass))
            $(this).addClass(opts.columnHoverClass);
    });
    
    //If cellHoverClass Applied, remove columnHoverClass from current hover cell.
    if(notEmptyOrNull(opts.cellHoverClass) && !$(this).hasClass(opts.cellHoverClass)){
        $(this).removeClass(opts.columnHoverClass);
        $(this).addClass(opts.cellHoverClass);
    }
  };
  //mouseout event handler 
  function cellOut(e)
  {
    //$(this) is the current hover cell jQuery Object.
    //$dataRows is a private jGuery variable holding all data rows
    //$column is a jQuery local variable holds all cells on the same column (having same cellIndex) as current cell
    
    //get all cells in the current row including current hovering cell.
    var $cells = $(this).siblings().andSelf();
    
    //Accessing additional data sent to the event handler.
    var index = e.data.cellIndex+1;
    
    //Equation to select cells of the same column.
    var eq = ':nth-child('+index+')';
    
    //Column Cells
    var $column =  $dataRows.children(eq);
    
    //Remove rowHoverClass from each cell in the current row.
    $cells.each(function(){
        if(notEmptyOrNull(opts.rowHoverClass) && $(this).hasClass(opts.rowHoverClass))
            $(this).removeClass(opts.rowHoverClass);
    });
    
    //Remove columnHoverClass from each cell in the current column.
    $column.each(function(){
        if(notEmptyOrNull(opts.columnHoverClass) && $(this).hasClass(opts.columnHoverClass))
            $(this).removeClass(opts.columnHoverClass);
    });
    
    //Remove cellHoverClass current cell.
    if(notEmptyOrNull(opts.cellHoverClass) && $(this).hasClass(opts.cellHoverClass))
        $(this).removeClass(opts.cellHoverClass);
  };
  //click event handler 
  function cellClick(e)
  {
     //$(this) is the current hover cell jQuery Object.
     //$dataRows is a private jquery variable holding all data rows
     //Remove all attached css classes to be ready to apply rowSelectClass
        
     //you can find the same code in the main.js   
      var tempStr=MyArrayOfMarker[this.id-1].data.split(':');
      map.panTo(MyArrayOfMarker[this.id-1].getPoint());
      GEvent.trigger(MyArrayOfMarker[this.id-1], "click");    
      Ajax_GetRelatedTheme(tempStr[10],tempStr[12]);//get the logos
      
    $dataRows.each(function(){
        $dataRows.children().each(function(){
            if(notEmptyOrNull(opts.rowSelectClass) && $(this).hasClass(opts.rowSelectClass))
                $(this).removeClass(opts.rowSelectClass);
            
            if(notEmptyOrNull(opts.cellHoverClass) && $(this).hasClass(opts.cellHoverClass))
                $(this).removeClass(opts.cellHoverClass);
                
            if(notEmptyOrNull(opts.cellSelectClass) && $(this).hasClass(opts.cellSelectClass))
                $(this).removeClass(opts.cellSelectClass);
                
                  
        });
    });
        
    var $cells = $(this).siblings().andSelf();
    
    //Attache rowSelectClass to current selected row.
    $cells.each(function(){
        if(notEmptyOrNull(opts.rowSelectClass) && !$(this).hasClass(opts.rowSelectClass))
            $(this).addClass(opts.rowSelectClass);
    });
    
    if(notEmptyOrNull(opts.cellSelectClass) && !$(this).hasClass(opts.cellSelectClass)){
        $(this).removeClass(opts.rowSelectClass);
        $(this).removeClass(opts.columnHoverClass);
        $(this).removeClass(opts.columnSelectClass);
        $(this).addClass(opts.cellSelectClass);
    }
  };
  
  function notEmptyOrNull(value){
    return value != '' && value != null;
  };
  //
  // plugin defaults
  //
  $.fn.gridviewex.defaults = {
    hasPagerRow: false,
    dataRowClass: '',
	altDataRowClass: '',
	rowHoverClass: '',
	rowSelectClass: '',
	columnHoverClass: '',
	columnSelectClass: '',
	cellHoverClass: '',
	cellSelectClass: '',
	headerRowClass: '',
	headerCellHoverClass: '',
	headerCellSelectClass: ''
  };
//
// end of closure
//
})(jQuery);