//document.domain = location.hostname;
var bagEmpty = true;
var cartHeight = -1;
var numCartImages = 1;
var imagesLoaded = 0;
var s7imgPath = 'DieselAsset';
var closeButton = '<button name="closeButton" class="closeButton" id="closeButton"><span class="buttonLeft"><span class="buttonRight"><span class="buttonContent">CLOSE</span></span></span></button>';

var hostname = document.location.hostname;
var shoppingBag_local = '/AssetStore/Diesel/_common/object_library/shoppingBagContents.php?store_name=Diesel&local_lang=en_US';
var shoppingBag = '/webapp/wcs/stores/servlet/FSAJAXService?service=getMiniCart&URL=MiniCartView&langId=-1&storeId=33053&catalogId=21503';
var shoppingBag_URL;

if(hostname.match( /local/ ) || hostname.match(/172.30/) || hostname.match(/127./)){ 
	shoppingBag_URL = shoppingBag_local; 
} else {
	shoppingBag_URL = shoppingBag;
}

//Onload function in shell, loads on every page load.
$(document).ready(function(){
	//if cartButtonWrapper element exists, get shopping bag
	var cartButtonWrapperEl = document.getElementById("cartButtonWrapper");
	if( cartButtonWrapperEl != null ) {
		$.ajax({
			url: shoppingBag_URL,
			cache:false,	
			success: function(html){
				if (document.getElementById('cartButtonWrapper') != null && html.indexOf('<!--  FSMiniCart.jsp  -->') != -1) {
				cartButtonWrapperEl.innerHTML = html;
				bagEmpty = false;
				// cart dropdown
				$("div.espot img").each( function() {
					if ($(this)[0].complete) {
						imagesLoaded++;
					}

					if(numCartImages <= imagesLoaded) {
						cartHeight = $('#cartDropdown').height();
					}
				});
			
				$("#cartWrapper").hover(function () {
					slideDown();
				}, function () {
					slideUp();
				});
				
				//enableEspotModals();
			}}
		});

	$('#global a').colorbox({width:"565px", height:"560px", opacity: 0.3, iframe: true, title:''+closeButton, style:'overflow:hidden'});
	$('#sweep a').colorbox({width:"503px", height:"515px", opacity: 0.3, iframe: true, title:''+closeButton, style:'overflow:hidden'});
	$('#storeLocator a').colorbox({width:"881px", height:"664px", opacity: 0.3, iframe: true, title:''+closeButton, style:'overflow:hidden'});
	$('.storeLocator a').colorbox({width:"881px", height:"664px", opacity: 0.3, iframe: true, title:''+closeButton, style:'overflow:hidden'});		
	}

	// account dropdown
	$("#accountWrapper").hover(function () {
		accountSlideDown();
	}, function () {
		accountSlideUp();
	});

	//header search box
	$('#headerSearchInput, #bannerSearch').val('SEARCH').focus(function() {
		if (this.value=="SEARCH") {
			this.value='';
		}
	}).bind('blur',function() {
		if (this.value=='') {
			this.value="SEARCH";
		}
	});

	// Setup default validation
	$('.validate').FSvalidate();

	/* opens external links in a new window if they have class="external" defined */
	$("a.external").click(function(){
		window.open(this.href,'external','location=yes,scrollbars=yes,menubar=yes,resizable=yes,toolbar=yes,status=yes,width=700,height=500,left='+(screen.availWidth/2-350)+',top='+(screen.availHeight/2-250)+'' );
		return false;
	});

	/*adds tooltips to image map <a> tags with alt attribute */
	/*$("area").each(function(){
		var mytip =  $(this).attr("alt");
		if(mytip != '' && mytip != undefined){
			$(this).wTooltip({content: mytip});
		}
	});*/
	
	/* kill all menu /# links be default */
	$(".nav .sub").click(function(){
		return false;
	});

	var dropdownOffset = 28;
	var accountWidth=$('#accountButtonWrapper').width() + $('#headerAccWrap').width() + $('#cartWrapper').width();

	$('#accountDropdown').width(accountWidth + dropdownOffset);
	$('#accountDropdownContent').width((accountWidth + dropdownOffset)-10);

});//end document ready function

//Global Functions - used by addToBag.js
var enableEspotModals = function(){
	$('.freeGiftEspot').colorbox({width:"678px", height:"622px", opacity: 0.3, iframe: true, title:'<p>Free Gift with Purchase</p>'+closeButton});
	$('.eSpotDetailsGlobal').colorbox({width:"867px", height:"622px", opacity: 0.3, iframe: true, title:'<p>Special Offers Details</p>'+closeButton});
};

var isAccountDropdownAnimated = false;

//slideDown
var accountHeight = 90;
var accountSlideDown = function() {
	if(isDropdownAnimated){
		$('#cartDropdown').css('visibility', 'hidden').hide();
		$('#cartButton').removeClass('showBag');
		isDropdownAnimated = false;
		$('#cartDropdown').stop().clearQueue();
	}
	if(!isAccountDropdownAnimated){
		isAccountDropdownAnimated = true;

		//swap style
		$('#accountDropdown').css('visibility', 'visible');
		$('#accountButton').addClass('showBag');
		$('#accountDropdownContent').css('height', (accountHeight-20));
		$('#accountDropdown').show().animate({height: accountHeight},200);
	}else{
		var newTime = 200 * ((accountHeight-$('#accountDropdown').height())/ accountHeight);
		$('#accountDropdown').stop().clearQueue().animate({height: accountHeight},newTime);
	}
};

//slideUp
var accountSlideUp = function() {
	$('#accountDropdown').animate({height: 0}, 300, function() {
		//swap style
		$('#accountButton').removeClass('showBag');
		isAccountDropdownAnimated = false;
		$('#accountDropdown').hide();
		//$('#accountDropdownShadow').hide();
	});
}

var timeout;

//Cart Dropdown
var isDropdownAnimated = false;

//slideDown
var slideDown = function() {
	if(isAccountDropdownAnimated){
		$('#accountDropdown').css('visibility', 'hidden');
		$('#accountButton').removeClass('showBag');
		isAccountDropdownAnimated = false;
	}
	if(!isDropdownAnimated){
		isDropdownAnimated = true;
		//Only calculate height on first hover. Delayed calculation because Safari does not return proper image height immediately after ajax loads.
		//This is now used as a redundancy only! cart Height should be calculated on AJAX Success
		if( cartHeight == -1 ) {
			cartHeight = $('#cartDropdown').height();
		}
		//swap style
		
		$('#cartDropdown').css('visibility', 'visible');
		$('#cartDropdown').show();
		
		$('#cartButton').addClass('showBag');
		$('#cartDropdown').height(0).animate({height: cartHeight},300);
	}else{
		//isDropdownAnimated = false;
		var newTime = 300 * ((cartHeight-$('#cartDropdown').height())/ cartHeight);
		$('#cartDropdown').stop().clearQueue().animate({height: cartHeight},newTime);

	}
};

//slideUp
var slideUp = function() {
	$('#cartDropdown').animate({height: 0},300,
		function() {
			$('#cartDropdown').hide();
			//swap style
			$('#cartButton').removeClass('showBag');
			isDropdownAnimated = false;
		}
	);
}

function addScript(source){
	var s = document.createElement("script");
	s.type = "text/javascript";
	s.src = source;
	$("head").append(s);
};

function getParameterByName( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	return "";
	else
	return decodeURIComponent(results[1].replace(/\+/g, " "));
}

String.prototype.substringBefore = function( original, matchString ) {
	var matchIndex = original.indexOf( matchString );
	var returnString = original;
	if( matchIndex != -1 ) {
		returnString = returnString.substr(0, matchIndex);
	}
	return returnString;
};
