var menuTimer;
var bodyClass;

function initialize(){
	
	$("#crumb").jBreadCrumb();
	$("ul.tabs").css('display','block');
	
	// Product menu
	bodyClass = $('body').attr('class');
	$('#t-prod span.click').click(
		function(){
			if($('#menu').is(':visible')){
				menuHide(true);
			}else{
				menuShow();
			}
		}
	);
	$('#t-prod').mouseover( function(){clearTimeout(menuTimer)} );
	$('#menu').mouseover( function(){clearTimeout(menuTimer)} );
	$('#t-prod').mouseleave( function(){menuHide()} );
	$('#menu').mouseleave( function(){menuHide()} );
	
	// Pre load images
	jQuery.preLoadImages(
		"/com/grp/ajax-32.gif",
		"/com/grp/ajax-16.gif",
		"/com/grp/ajax-16-grey.gif"
	);
	
	// fix missing images
	$("img").error(function () {
	 $(this).unbind("error").attr("src", "/com/grp/missing.gif");
	});
	
	$("div.product-img img").error(function () {
	  $(this).unbind("error").attr("src", "/com/grp/missingmainimage.gif");
	});
	
	
	
}


$(function() {
	$("ul.tabs").tabs("div.panes > div",{
		onBeforeClick: function(event,i){
			var pane = this.getPanes().eq(i);
			var targ = this.getTabs().eq(i).attr("href");
			var live = this.getTabs().eq(i).attr("rel");
		
			if(targ != '#'){
				if (pane.is(":empty") || live == 'live') {
					loadtoobj(pane,this.getTabs().eq(i).attr("href"));	
				}
			}
		}	
	});
});

function loadtoobj(obj,url,params){
	
	var loadDelay = setTimeout(
		function(){
			obj.html('<p class="loading"><img src="/com/grp/ajax-16-grey.gif" width="16" height="16"/> LOADING</p>');
		},300
	);
	
	obj.load(
		url,
		params,
		function(response,status,xhr){
			clearTimeout(loadDelay);
			if (status=="error") {
				var msg = "Error: ";
				obj.html('<p class="error"><strong>Error: '+ xhr.status + ' ' + xhr.statusText +'</strong>, Please try again later');
			}
		}	
	);
	
}


function menuShow(){
	clearTimeout(menuTimer);
	if($('#menu').is(':empty')){
		$('#menu').html('<p class="menuloading"><img src="/com/grp/ajax-32.gif" width="32" height="32"/></p>');
		$('#menu').load('./search/ajax/categories');
	}
	
	var overlayH = $(document).height() - $('#header').height();
	
	if(bodyClass != 'prdt')	$('body').removeClass(bodyClass).addClass('prdt');
	$('#menu').slideDown('fast');
	$('#overlay')
		.fadeIn('fast')
		.height(
			$(document).height() - $('#header').height()
		);
}

function menuHide(fromClick){
	if(fromClick){
		menuHideGo();
	}else{
		menuTimer = setTimeout(function(){menuHideGo()},300);
	}
}

function menuHideGo(){
	if(bodyClass != 'prdt') $('body').removeClass('prdt').addClass(bodyClass);
	$('#menu').slideUp('fast');	
	$('#overlay').fadeOut('fast');
}


function langwarnHide(){
	$.ajax({
		url: './utilities/ajax/togglelangwarn',
		success: function(data) {
			$('#langwarn').slideUp('fast');	
		}
	});
	return false;
}



(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);


(function($){

	$.fn.alphanumeric = function(p) { 

		p = $.extend({
			ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>?~`.- ",
			nchars: "",
			allow: ""
		  }, p);	

		return this.each
			(
				function() 
				{

					if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
					if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyz";
					
					s = p.allow.split('');
					for ( i=0;i<s.length;i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
					p.allow = s.join('|');
					
					var reg = new RegExp(p.allow,'gi');
					var ch = p.ichars + p.nchars;
					ch = ch.replace(reg,'');

					$(this).keypress
						(
							function (e)
								{
								
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
										
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();
									
								}
								
						);
						
					$(this).bind('contextmenu',function () {return false});
									
				}
			);

	};

	$.fn.numeric = function(p) {
		
		var az = "abcdefghijklmnopqrstuvwxyz";
		az += az.toUpperCase();

		p = $.extend({
			nchars: az
		  }, p);	
		  	
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};
	
	$.fn.alpha = function(p) {

		var nm = "1234567890";

		p = $.extend({
			nchars: nm
		  }, p);	

		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};	

})(jQuery);

