$(document).ready(init);

var bIphone = false;
var bTouchScreen = false;
var months = {
	"01": "Jan.",
	"02": "Feb.",
	"03": "March",
	"04": "April",
	"05": "May",
	"06": "June",
	"07": "July",
	"08": "Aug.",
	"09": "Sep.",
	"10": "Oct.",
	"11": "Nov.",
	"12": "Dec."
};

function init() {
	initNav();
	firstLoadBG();
	//var collection = window.location.hash;  //jQuery.url.param("collection");
	var openVideo = true;
    /*
	if (collection) {

	    collection = collection.replace("#!", "");
	    if (collectionShortcuts[collection]) {
	        openVideo = false;
	    }
	//    //console.log(collection);
		loadPage(collectionShortcuts[collection], null, 1);
    }
    */
    if (openVideo) {
        setTimeout("toggleVideo();", 2500);
    }
	setTimeout("initNewsTab();", 3500);


	$("#btnSignup").click(function () {
	    var email = $("#tb_Email").val();
	    $.post("/_xml/signupReceiver.aspx", { "e": email },
            function (data) {
                if (data == "1") {
                    $("#signup").hide();
                    $("#signup_complete").show();
                } else {
                    $("#sp_Result").show();
                }

            });
	});

}


$().ready(function () {
    checkHash();
});

function setupHashHandler() {


}
function checkHash() {
    var hash = window.location.hash;
    if (hash.length > 0) {
        var substr = hash.split('/');
        //console.log(substr);
        switch (substr.length) {
            case 2:
                //console.log("2 def");
                navName = pages[deepLinks[substr[1].replace(/[^a-z0-9]/g, '').toLowerCase()] + "_" + gender.toLowerCase()];
                CurrentSectionIdx = 0;
                //console.log(" CurrentIndex 0 " + CurrentSectionIdx);
                //console.log(" NAV NAME 0 " + navName);
                var bgName = substr[1].replace(/[^a-z0-9]/g, '').toLowerCase() + "_" + substr[0].replace(/[^a-z0-9]/g, '').toLowerCase();
                bgName = bgName.replace('thebrand', 'the_brand');
                CurrentBackgroundID = pageCats[bgName];

                initBackground();
                //console.log("bgName: " + bgName);
                //console.log("STR 0 " + substr[1].replace(/[^a-z0-9]/g, '').toLowerCase());
                //console.log("DL 0 " + deepLinks[substr[1].replace(/[^a-z0-9]/g, '').toLowerCase()]);
                //console.log(navName);

                //console.log(pageCats[navName]);

        

                loadPage(navName);
                break;
            case 3:
                //console.log("3 def");
                var box2 = substr[1].replace(/[^a-z0-9]/g, '').toLowerCase();
                //console.log(box2);
                if (box2 == "collections") {
                    var box3 = substr[2].replace(/[^A-Za-z0-9]/g, '').toLowerCase();
                    //console.log(box3);
                    var o = eval("collectionDeepLinks." + box3);
                    var i = 0;
                    for (i in collectionPagesOrder) {
                        if (collectionPagesOrder[i] == o) {
                            currentCollectionPage = i;
                            currentCollectionIndex = i;
                            break;
                        }
                        i++;
                    }

                    //console.log(collectionDeepLinks.augustcollection);
                    loadPage(o, null, 1);
                } else {


                    navName = pages[deepLinks[substr[1].replace(/[^a-z0-9]/g, '').toLowerCase()] + "_" + gender.toLowerCase()];
                    CurrentSectionIdx = deepLinks[gender.toLowerCase() + "_" + substr[2].replace(/[^a-z0-9]/g, '')];
                    //console.log(" CurrentIndex 0 " + CurrentSectionIdx);
                    //console.log(" NAV NAME 0 " + navName);
                    //console.log("STR 0 " + substr[1].replace(/[^a-z0-9]/g, '').toLowerCase());
                    //console.log("DL 0 " + deepLinks[substr[1].replace(/[^a-z0-9]/g, '').toLowerCase()]);


                    var bgName = substr[1].replace(/[^a-z0-9]/g, '').toLowerCase() + "_" + substr[0].replace(/[^a-z0-9]/g, '').toLowerCase();
                    bgName = bgName.replace('thebrand', 'the_brand');
                    CurrentBackgroundID = pageCats[bgName];

                    initBackground();

                    loadPage(navName);
                }
                break;
            case 4:
                //console.log("4 def");
                prevPage = eval("pages." + deepLinks[substr[1].replace(/[^a-zA-Z0-9]/g, '').toLowerCase()] + "_" + gender.toLowerCase());
                //prevPage = pages[deepLinks[substr[1].replace(/[^a-zA-Z0-9]/g, '').toLowerCase()] + "_" + gender.toLowerCase()];
                //console.log("subst 0: " + substr[1].replace(/[^a-zA-Z0-9]/g, '').toLowerCase());

                CurrentSectionIdx = deepLinks[gender.toLowerCase() + "_" + substr[2].replace(/[^a-z0-9]/g, '')];
                navName = deepLinks["r" + substr[3].replace(/[^a-zA-Z0-9]/g, '').toLowerCase()];
               
                var bgName = substr[1].replace(/[^a-z0-9]/g, '').toLowerCase() + "_" + substr[0].replace(/[^a-z0-9]/g, '').toLowerCase();
                bgName = bgName.replace('thebrand', 'the_brand');
                CurrentBackgroundID = pageCats[bgName];
                initBackground();
                //console.log("PREV PAGE VAL:" + prevPage);
                //console.log("navName VAL:" + navName);
                //console.log("PREV PAGE:" + pages[prevPage]);
                //  loadPage(prevPage);

                var productPath = products[navName];
                setTimeout("loadPage('" + productPath + "', null)", 2000);
                break;
            default:
                //console.log("def");
                hash = hash.replace(/[^a-z]/g, '').toLowerCase();
                //console.log("<2 hash " + hash);
                navName = deepLinks[hash] + "_" + gender.toLowerCase();
                //console.log("default nav 2< "  + navName);
                var pageURL = pages[navName];
                prevPage = eval("pages." + navName);
                //   //console.log("PREV PAGE SINGLE:" + pages[prevPage]);
                var bgName = substr[1].replace(/[^a-z0-9]/g, '').toLowerCase() + "_" + substr[0].replace(/[^a-z0-9]/g, '').toLowerCase();
                bgName = bgName.replace('thebrand', 'the_brand');
                CurrentBackgroundID = pageCats[bgName];

                initBackground();
                loadPage(pageURL);
                break;

        }





    }
}


var CurrentBackgroundSrc = "";
var BackgroundTimer = null;
var IsResize = false;
function firstLoadBG() {
initBackground();
}
// Initializes the background image thing.
function initBackground() {

    $('#loading_graphic').show();
    $("#bg0").fadeOut('fast');
    BackgroundTimer = setTimeout("doReload();", 500);

  }
  var isLoadingBg = false;
  function doReload() {
    //  console.log("wanna load bg: " + CurrentBackgroundID);
      if (!isLoadingBg) {
          isLoadingBg = true;
       //    console.log("loading bg: " + CurrentBackgroundID);
          $('#supersize').load('/_xml/gimmebe.aspx?f=' + Failover + '&bg=' + CurrentBackgroundID, function () {
              resizeBG();
              isLoadingBg = false;
             // console.log("setting timeout");
              clearTimeout(BackgroundTimer);
              BackgroundTimer = setTimeout("doReload()", 20000);
              //     setTimeout("doReload()", 20000);
          });
      } 
}

$(window).bind('resize', function () {
    IsResize = true;
    resizeBG();
});
function resizeBG() {
    $('#loading_graphic').hide();
        //Define image ratio
    var ratio = 9 / 16;//  3/4;
   // $('#bg0').fadeOut('slow');
        var obj = document.getElementById("bg0");  //$("#bgX");
        imgSrc = obj.src;
        
        var aImg = new Image();

        aImg.src = imgSrc;
            CurrentBackgroundSrc = imgSrc;
            //Gather browser and current image size
            var imagewidth = aImg.width;
            var imageheight = aImg.height;
            var browserwidth = $(window).width();
            var browserheight = $(window).height();
            var offset;
            //    //console.log("obj", obj);
            //   //console.log("h x w", imageheight, imagewidth);
            //Resize image to proper ratio
            if ((browserheight / browserwidth) > ratio) {
                $("#bg0").height(browserheight);
                $("#bg0").width(browserheight / ratio);
                $("#bg0").children().height(browserheight);
                $("#bg0").children().width(browserheight / ratio);
            } else {
                $("#bg0").width(browserwidth);
                $("#bg0").height(browserwidth * ratio);
                $("#bg0").children().width(browserwidth);
                $("#bg0").children().height(browserwidth * ratio);
            }
            // if (options.vertical_center == 1) {
            $("#bg0").children().css('left', (browserwidth - $("#bg0").width()) / 2);
            $("#bg0").children().css('top', (browserheight - $("#bg0").height()) / 2);
            //}
            //       //console.log("h x w", $("#bg0").height(), $("#bg0").width());

            $('#bg0').fadeIn('fast');
   
};
	      


