// Initialize some useful global variables
// The locale variable is initialized by the CMS before inclusion of this
// script. If this is not the case, try extracting it from the URL path
if (typeof(locale) == "undefined")
locale = window.location.pathname.split("/")[1];
languageCode = locale.split("_")[0];
countryCode = locale.split("_")[1];

s_account="leviseu" + countryCode.toLowerCase() + "dev"

var IE6 = false;

var strChUserAgent = navigator.userAgent;
var intSplitStart = strChUserAgent.indexOf("(",0);
var intSplitEnd = strChUserAgent.indexOf(")",0);
var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);

if(strChMid.indexOf("MSIE 6") != -1) IE6 = true;

// Group country codes into geographical regions
regions = new Array();
regions["BE"] = "EUROPE"; regions["IE"] = "EUROPE"; regions["FR"] = "EUROPE";
regions["PL"] = "EUROPE"; regions["AT"] = "EUROPE"; regions["FI"] = "EUROPE";
regions["CH"] = "EUROPE"; regions["RU"] = "EUROPE"; regions["CH"] = "EUROPE";
regions["GB"] = "EUROPE"; regions["SE"] = "EUROPE"; regions["IT"] = "EUROPE";
regions["DE"] = "EUROPE"; regions["CZ"] = "EUROPE"; regions["GR"] = "EUROPE";
regions["NL"] = "EUROPE"; regions["ES"] = "EUROPE"; regions["DK"] = "EUROPE";
regions["HU"] = "EUROPE"; regions["NO"] = "EUROPE"; regions["PT"] = "EUROPE";
regions["CH"] = "EUROPE";
regions["ID"] = "ASIA"; regions["NZ"] = "ASIA"; regions["IN"] = "ASIA";
regions["SA"] = "ASIA"; regions["MY"] = "ASIA"; regions["PH"] = "ASIA";
regions["VN"] = "ASIA"; regions["VN"] = "ASIA"; regions["CN"] = "ASIA";
regions["PK"] = "ASIA"; regions["SG"] = "ASIA"; regions["JP"] = "ASIA";
regions["HK"] = "ASIA"; regions["AU"] = "ASIA"; regions["AU"] = "ASIA";
regions["TH"] = "ASIA"; regions["KR"] = "ASIA"; regions["TW"] = "ASIA";
regions["EC"] = "SOUTH_AMERICA"; regions["PY"] = "SOUTH_AMERICA";
regions["UY"] = "SOUTH_AMERICA"; regions["CL"] = "SOUTH_AMERICA";
regions["PE"] = "SOUTH_AMERICA"; regions["VE"] = "SOUTH_AMERICA";
regions["CO"] = "SOUTH_AMERICA"; regions["MX"] = "SOUTH_AMERICA";
regions["AR"] = "SOUTH_AMERICA"; regions["CR"] = "SOUTH_AMERICA";
regions["BR"] = "SOUTH_AMERICA";
regionCode = regions[countryCode];
//if (regionCode == null)
//    alert("Unsupported country code: " + countryCode);

////////////////////////////////////////////////////////////////////////////////

// Highlights the appropriate nav button in the header
function highlightHeader () {
	var current_url = location.href;
	var toHighlight;
	if(current_url.indexOf("collections") > -1){
		toHighlight = $("header-collections");
	} else if(current_url.indexOf("news") > -1){
		toHighlight = $("header-news");
	} else if(current_url.indexOf("media") > -1){
		toHighlight = $("header-mediacenter");
	} else if(current_url.indexOf("store-locator") > -1){
		toHighlight = $("header-storelocator");
	} else if(current_url.indexOf("shop.eu.levi.com") > -1){
		toHighlight = $("header-shop");
	} else if(current_url.indexOf("shop") > -1){
		toHighlight = $("header-shop");
	} else {
		toHighlight = $("header-logo");
	}
	if (toHighlight)
		toHighlight.addClass("highlight");
}

function clearSearchField(event) {
    if (typeof(searchFocusFirstTime) == "undefined") {
	document.getElementById("search").value = "";
	searchFocusFirstTime = "shay";
    }
}


function executeEshopSearch(){
	Cookie.dispose('GLOBAL_SEARCH_STATE',{path:'/'});
	document.location.href = "http://eu.levi.com/"+locale+"/search.html?levisquery="+$('search').value;
	return false;
}

function searchKeyPressed(event) {
    var code;
    if (event.which == null)
	code = event.keyCode;    // IE
    else if (event.which > 0)
	code = event.which;      // All others

    if (code == 13 || code == 14)
	executeEshopSearch();
}

window.addEvent('domready', function() {
	highlightHeader();
	$('main').getElements('a.productFinderlink').each(function(link, index){
		link.addEvent('click', function() {
			navigateAndClearProductFinder(link.get('href'));
			return false;
		});
	});
	$('header').getElements('a').addEvent('focus', function(e) {
		e.target.blur();
	});
	$('footer').getElements('a').addEvent('focus', function(e) {
		e.target.blur();
	});
});

/* This function says whether we're in Fatwire's InSite editing mode. It allows Javascript code to do different things in InSite */
function checkFatwireInsiteMode()
{
        return window.location.search.indexOf("rendermode=preview-") == -1 ? false : true;
}

function openPopup(url, parameters) {
	window.open(url, "LevisPopup", parameters);
}

function openMinimalPopup(url, parameters) {
	parameters += "status=0,toolbar=0,location=0,menubar=0";
	openPopup(url, parameters);
}

function navigateAndClearProductFinder(url) {
	var gridCookie = Cookie.dispose('SEARCH_PARAMETERS',{path:'/'});
	var topbottomCookie = Cookie.dispose('TOP_BOTTOM_SEARCH_STATE',{path:'/'});
	window.location = url;
}
