var $j = jQuery.noConflict();

function customHandler(desc,page,line,chr)  {		// Display Errors in status	 
//	console.log("JAVASCRIPT ERROR : " + desc + " : PAGE=" + page + " : LINE=" + line);
//	return true;
}
window.onerror=customHandler;

var newsdefValue = "ENTER E-MAIL";
var isiPad = navigator.userAgent.match(/iPad/i) != null;

function setupMenu() {
	menuSet(["faq", "shipping", "returns", "your-account"],"mcusto");
	menuSet(["what-we-do", "services", "press","pressarticles","contact"],"mabout");		
}

function menuSet(wArray,wId) {
	for(var i = 0; i < wArray.length; i++) {
		if (location.href.indexOf(wArray[i]) != -1) {
			mmenu = wId;
		}
	}		
}
cattext = "";
prodtext = "";
function setcategoryhints() {
	// add category hints for google analytics
	productname = $j(".product-shop .product-name h1 .product-named");
	prodtext =  productname.text();
	prodtext = $j.trim(prodtext.toLowerCase());
	prodtext = prodtext.replace("'","");		
	prodtext = prodtext.replace("& ","");	
	prodtext = prodtext.replace(" ","");		
	
	activecat = $j(".sidebar .block-layered-nav .active:not(.parent)");
	cattext = activecat.text();
	cattext = $j.trim(cattext.toLowerCase());
	cattext = cattext.replace("'","");
	cattext = cattext.replace("& ","");	
	
	if (prodtext != "" & cattext != "") {
		console.log("setcookie");
		Mage.Cookies.set(prodtext,cattext);
	}
}

var showsize = true;
$j(function () {
	$j(document).ready(function () {	
		
		//if (!issecure) {
			// add code for SID appendage
			$j("BODY A").not('.nosid').attr("href", function (i, val) {
				if (typeof(val) != 'undefined') {
				if (val.indexOf(storecross) != -1) {			
					if (val.indexOf("?") == -1) {				
						val = val + "?SID=" + sessionid;
					}
				}
				return val;
				}
					
			});	
			
			// add code for SID appendage
			$j("BODY A").not('.nosid').attr("href", function (i, val) {
				if (typeof(val) != 'undefined') {
				if (val.indexOf("https://www.sidmashburn.com") != -1) {			
					if (val.indexOf("?") == -1) {				
						val = val + "?SID=" + sessionid;
					}
				}
				return val;
				}
					
			});				
			
			
			
//		}
			
		
		// select form element on contact page
		contactelem = $j("#contactForm #sender option")
		if (contactelem && storeid == "am") { 
			$j(contactelem[1]).attr("selected","selected");
		}
		
				
		if (showsize) { 
			// show sizing on pages with size
			sizeop = $j(".product-options LABEL").html();
			if (sizeop) {
				if (sizeop.toLowerCase().indexOf("size") != -1) {
					$j(".product-shop #sizepopup").show();
					
				}
			}
		}
		
		$j("#sizepopup .sizeviewer").attr("href","/media/sizecharts/" + storeid + "chart.html");


		// setup size viewer
		$j(".sizeviewer").yoxview({			
				defaultDimensions: { iframe: { width: 600 }},	
				backgroundColor: '#FFFFFF',
				popupResizeTime: 1000,
				menuBackgroundColor: '#ffffff',
				titleDisplayDuration: 0,
				autoHideInfo: false,
				renderButtons: true,
				renderInfoPin: false,
				renderInfo: false,
				renderMenu:true,
				autoHideMenu: false,
				buttonsFadeTime: 0,
				menuBackgroundOpacity: 1,
				allowInternalLinks: true,
				textLinksSelector: "" 
			}); 			
		
				
		
		// setup menus for CMS PAGES
		setupMenu();

		if (typeof(mmenu) != 'undefined') {
			cmenu = $j("#" + mmenu);	
			cmenu.addClass("activemenu");			
		}
		else {	
			if (location.href.indexOf("gift-certificate") != -1) {
				$j("#mgiftc").addClass("activemenu");
				$j(".nav-1").removeClass("active");		
				$j(".nextprevholder").hide();		
			}

		}
		
		
		$j("#newsletter").val(newsdefValue);
		$j("#newsletter").focus(function() { clearTextJ(this,newsdefValue) });
		$j("#newsletter").blur(function() { clearTextJBlur(this,newsdefValue) });		
	
		
		// TOP MENUS BEGIN
		$j(".recently-viewed-link").mouseenter(function(e) {
			 $j('.block-viewed').fadeIn(300);
			 $j("a", this).addClass("link-active");
		});
				
		$j(".top-link-cart").mouseenter(function(e) {
		    $j('.block-cart').fadeIn(200);
		    $j(this).addClass("link-active");
		}); 
			
		$j(".block-viewed .toppop-block").mouseenter(function(e) {	showrecent();		});
		$j(".block-viewed .toppop-title").mouseenter(function(e) {	showrecent();		});
		$j(".block-viewed .toppop-block").mouseleave(function(e) {	hiderecent();		});
		$j(".block-viewed .toppop-title").mouseleave(function(e) {	hiderecent();		});				
		configcart();

	
	});
});

var cart_timeout;
var recent_timeout;

function configcart() {
		$j(".block-cart .toppop-block").mouseenter(function(e) {	showcart();		});
		$j(".block-cart .toppop-title").mouseenter(function(e) {	showcart();		});
		$j(".block-cart .toppop-block").mouseleave(function(e) {	hidecart();		});
		$j(".block-cart .toppop-title").mouseleave(function(e) {	hidecart();		});		
}
/////////////////////
function showcart(wspeed) {	
	setcategoryhints();
	console.log("showcart");
	if (wspeed == null) { wspeed = 300; }
	clearTimeout(cart_timeout);	
	$j(".top-link-cart").addClass("link-active");
	 $j('.block-cart').fadeIn(wspeed);							
}
function hidecart(wspeed) {
	console.log("hidecart");			
	if (wspeed == null) { wspeed = 200; }
	clearTimeout(cart_timeout);	
	cart_timeout = setTimeout(function() { 
			hidecartnow(wspeed);
		}, 100);
}		

function hidecartnow(wspeed) {
	if (wspeed == null) { wspeed = 200; }	
	console.log("hidecartnow");	
	clearTimeout(cart_timeout);
	$j(".block-cart").fadeOut(wspeed); 
	$j(".top-link-cart").removeClass("link-active"); 
}

/////////////////////
function showrecent() {	
console.log("showrecent");
	clearTimeout(recent_timeout);	
	$j('.block-recent').fadeIn(200);
	j("a", ".top-link-cart").addClass("link-active");				
}
function hiderecent() {
	console.log("hiderecent");
	clearTimeout(recent_timeout);	
	recent_timeout = setTimeout(function() { 
			hiderecentnow();
		}, 100);
}		
function hiderecentnow() {
	clearTimeout(recent_timeout);
	$j(".block-viewed").fadeOut(200); 
	$j(".recently-viewed-link > a").removeClass("link-active"); 
}	
