jQuery.fn.extend({
	filterDisabled : function(){ 
			return this.filter(
							function() {
								return (typeof(this.disabled)!=undefined)
							})
	},
	
	disabled: function(h) {
   		if (h!=undefined) return this.filterDisabled().each(function(){this.disabled=h});
   		this.filterDisabled().each(function() {h=((h||this.disabled)&&this.disabled)});return h;
	},
	
	toggleDisabled: function() {return this.filterDisabled().each(function(){this.disabled=!this.disabled});}
});

jQuery(document).ready(function(){
		$j(".gridTable input[type='text']").bind("click", function(event) {
			stopEvent(event);			
		});
});


function timerOutMenu(subID) {
	if(currentMenuActive > 0) return false;
	if(currentMenuActive > 0 && currentMenuActive == subID ) return false;
	if(currentMenuActive == 0 && selectedSection == subID) return false;
	
	$j("#subm_" + subID).hide();
	$j("#mainM_" + subID).removeClass("mod_active");
	
	/*
	var s = $j("#logsStatus").html();
	s = s + ' out:' + subID + ' ';
	$j("#logsStatus").html(s);
	*/
	
	if(currentMenuActive == 0) {
		currentMenuActive = selectedSection;
		$j("#secondLineMenu ul[subid!="+selectedSection+"]").hide();
		$j("#topLineMenu li[subid!="+selectedSection+"]").removeClass("mod_active");
		
		$j("#subm_" + selectedSection).fadeIn('fast');
		$j("#mainM_" + selectedSection).addClass("mod_active");
		
		/*
			var s = $j("#logsStatus").html();
			s = s + ' a:' + selectedSection + ' ';
			$j("#logsStatus").html(s);
		*/
	}
}

$j.fn.onEnterMenu = function () {
	return this.each(
		function() {

			var subID = $j(this).attr("subId");
			currentMenuActive = subID;

			$j("#secondLineMenu ul[subid!="+subID+"]").hide();
			$j("#topLineMenu li[subid!="+subID+"]").removeClass("mod_active");

			menuInTimerID = 0;

			$j("#subm_" + subID).fadeIn('fast');
			$j("#mainM_" + subID).addClass("mod_active");
		}
	);
}


$j.fn.onOutMenu = function () {
	return this.each(
		function() {
			currentMenuActive = 0;
			var subID = $j(this).attr("subId");
			menuInTimerID = subID;
			setTimeout("timerOutMenu("+subID+")", 500);
		}
	);
}


$j.fn.hoverMenu = function () {
	currentMenuActive = selectedSection;
	menuInTimerID = 0;
	return this.each(
		function(){
			var subID = $j(this).attr("subId");
			if( !$j("#subm_" + subID) ) {
				subID = 0;

			}
			if(subID) {				
				$j(this).hover(
					function() {
						$j("#subm_" + subID).onEnterMenu();
					},
					function() {
						$j("#subm_" + subID).onOutMenu();
					}
					
				);
				
				$j("#subm_" + subID).hover(
					function() {
						$j(this).onEnterMenu();
					},
					function() {
						$j(this).onOutMenu();
					}
				);
			}
		}
	);
}

$j.fn.subMenuIn = function (subID, pos) {
	$j("#subm_" + subID).show();
	// position it
	var parentpos = $j(this).parent().position();

	var finalleft = pos.left - parentpos.left;
	$j("#subm_" + subID).css('left', finalleft);

}


$j.fn.subMenuOut = function (subID) {
	$j("#subm_" + subID).hide();
}


function startNav()  {
	$j("#sub_menu_wrapper").show();	
	$j("#subm_" + selected_module).subNavIn(selected_module);

}


$j.fn.hoverNav = function () {
	
	return this.each(
		function(){
			var subID = $j(this).attr("module_id");
			var pos = $j(this).position();
						
			$j(this).hover(
					function() {
						// clear all menus except this one you are hovering over
						$j("#top_menu_wrapper div[topm_!="+subID+"]").removeClass("module_selected");
						$j("#top_menu_wrapper div[topm_!="+subID+"]").addClass("module_unselected");
						$j("#sub_menu_wrapper div[subm_!="+subID+"]").removeClass("sub_module_selected");
						$j("#sub_menu_wrapper div[subm_!="+subID+"]").addClass("sub_module_unselected");

						 //show this submenu
						$j("#subm_" + subID).subNavIn(subID, pos);
						$j("#subm_" + subID).addClass("module_selected");
					},
					function() {												
						$j("#subm_" + subID).subNavOut(subID, pos);
						
					}
				);


			$j("#subm_" + subID).hover(
					function() {
						$j(this).subNavIn(subID,pos);
					},
					function() {
						$j(this).subNavOut(subID,pos);
					}
				);
		}
	);
}

// this method and entire nav system needs cleanup - gbh 10/22/10
$j.fn.subNavIn = function (subID) {
	$j("#subm_" + subID).show();


	if (selected_module != subID) {
		$j("#subm_" + selected_module).hide();
	}
	
	var topm_container = $j("#top_menu_wrapper");
	//console.log('this is '+topm_container.attr('id')+"  -- top = "+topm_container.position().top+'-- width = '+topm_container.width()+"  -- left = "+topm_container.position().left+"  -- right = "+(topm_container.position().left+topm_container.width()));
	
	var subm_container = $j("#sub_menu_wrapper");
	//console.log('this is '+subm_container.attr('id')+"  -- top = "+subm_container.position().top+'-- width = '+subm_container.width()+"  -- left = "+subm_container.position().left+"  -- right = "+(subm_container.position().left+subm_container.width()));

	var topm_item  = $j("#topm_" + subID);
	//console.log('this is '+topm_item.attr('id')+"  -- top = "+topm_item.position().top+' -- width = '+topm_item.width()+"  -- left = "+topm_item.position().left+"  -- right = "+(topm_item.position().left+topm_item.width()));

	if (!topm_item.position()) {
		return false;
	}

	var subm_item  = $j("#subm_" + subID);
	//console.log('this is '+subm_item.attr('id')+"  -- top = "+subm_item.position().top+' -- width = '+subm_item.width()+"  -- left = "+subm_item.position().left+"  -- right = "+(subm_item.position().left+subm_item.width()));

	var nav_wrap = $j("#nav_wrapper");
	//console.log('this is '+nav_wrap.attr('id')+' --- width'+nav_wrap.width()+' -- left = '+nav_wrap.position().left);

	var abs_left = $j("#top_menu_wrapper").children('div:first').position().left;
	//console.log("abs left = "+$j("#top_menu_wrapper").children('div:first').position().left);
	var abs_right = $j("#top_menu_wrapper").children('div:last').position().left+$j("#top_menu_wrapper").children('div:last').width();
	//console.log("abs right = "+($j("#top_menu_wrapper").children('div:last').position().left+$j("#top_menu_wrapper").children('div:last').width()));
	
	// here is where we do math to get it to center
	// subtract half the width to get static centering, but also add 1/2 width of the referring
	// orig center var ff = topm_item.position().left-(subm_item.outerWidth()/2)+(topm_item.outerWidth()/2);
	
	var var_offset = 0;

	var half_sub_width = subm_item.width()/2;

	var l_dtc = ((topm_item.position().left-abs_left)+(topm_item.width()/2));

	var l_overlap = half_sub_width - l_dtc;

	var l_perce = 1-(l_overlap/half_sub_width);

	var r_dtc = abs_right - (topm_item.position().left+topm_item.width());
	//console.log('distance to right = '+r_dtc);
	var r_overlap = half_sub_width-r_dtc;
	//console.log('all = '+subm_item.width());
	//console.log('hafl = '+half_sub_width);
	//console.log('overlap = '+r_overlap);
	var r_perce = r_overlap/half_sub_width;
	//console.log('per = '+r_perce);

	if ( half_sub_width >= l_dtc )  {
		var_offset = .5*l_perce;
	} else if (half_sub_width >= r_dtc ) {
	
		var_offset = (.5*(1+r_perce))-.45;
	}else {
		var_offset = .5;
	}

	// at 0, the submenu left aligns with the center of the top menu
	// at 1, the submenu right aligns with the center of the top menu
	// at .5, the submenu center aligns with the center of the top menu

	var final_pos = topm_item.position().left-(subm_item.outerWidth()*var_offset)+(topm_item.outerWidth()*.5);
	
	$j("#subm_" + subID).css('left', final_pos);

	return true;

}


$j.fn.subNavOut = function (subID) {
	/* hide the submenu */
	$j("#subm_" + subID).hide();

	/* show the selected menu*/	
	$j("#subm_" + selected_module).show();
}


function redirect(url) {
	location.href = url
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
	//return (((sign)?'':'-') + num + '.' + cents);
}

function updateCartBox(qty, price, productID, prodQty) {	
	
	if(qty >1 || qty == 0) {
		$("topBarCartQty").innerHTML	= qty+' items in Cart ';
	} else {
		$("topBarCartQty").innerHTML	= qty+' item in Cart ';
	}
	

	$("hoverProdNo").innerHTML = qty;
	currentTotal = parseInt($("hoverTotal").innerHTML);
	
	$("hoverTotal").innerHTML = formatCurrency(price);
	
	if($("qty_prod_" + productID)) {
		var newQty = $("qty_prod_" + productID).innerHTML;
		newQty = parseInt(newQty);
		newQty -= prodQty;
		$("qty_prod_" + productID).innerHTML = newQty;
	}
}


function stopEvent(e) {
	var e = e || window.event;
		
	if(e.stopPropagation){
		e.stopPropagation();
	}
		
	//if (e.cancelBubble)
  e.cancelBubble = true;
      
	//if (e.preventDefault) e.preventDefault(); 
	//else
	//e.returnValue = false;               
}

function staticPageLink(link) {
	if(window.isPopup && window.opener) {
		window.opener.location = link;
		window.close()
	} else{
		window.location = link;
	}
}


function clearForm(form) {

	$j.ajax({
			 type: "POST",
			 url: "loader.php?modAct=clear_delivery_search",
			 data: "formname="+form.name,
			 success: function(rets){
					window.location.reload()
			 }
		 });

		 return true;
}

function clearInput(item) {

	item.value = '';

	item.setAttribute('style', '');
}


function check_set_price() {
	var val = $j('#zero_price_type_id').val();	
	switch_set_price(val);

}


function switch_set_price(val) {

	var foundin = $j('td:contains("$usd")');
	
	if (!val) {
		foundin.html('Set Price ($usd):');
	} else {
		foundin.html('Value ($usd):');
	}

}


/* clear out any values in input fields, chrome and safari tend to hang onto them */
function clear_form(sel) {
	$j(sel).val('');	
}


/*
 * this gets called when being viewed on the iPad
 */
function setInputWidth(sel) {
	$j(sel).width('20px');
}



