/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 
    le.js - core namespace and web page initialization
   
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

var DEBUG = false;
var PAGE_TYPE_LE_CUSTOM = 6;
var buyGrids = new Array();
var pViewers = new Array();
var collectionBuyGrid;
var inlineBag;
var messages = new Object();
var hostname;
var ppPath;
var firstBuyGridKey = null;
var buyGridKeyOrder = new Array();
var previouslySelectedBuyGridKey = null;
var currentlySelectedFit = new Object();
var multiItemBuyGridColors = new Array();
var currentBuyGridUrl = null;

/*=====================
    Initialize the page  
=====================*/
function init() {
    
    checkDebugState();
    theSid = getSID();
    getHostname();
    getMessageContent();
    loadBasicCustomerData();
    
    if (environmentContext == "production") {
        cmSetProductCategories(getCategoryID());
    }    
    
    // Start building buy grids 
    doDebug("[init] sA.length = " + sA.length);
   
   	// build the buy grids
   	var buyGridI; 
   	var orderedKeyValueIndex;
   	for(orderedKeyValueIndex = 0;orderedKeyValueIndex < buyGridKeyOrder.length;orderedKeyValueIndex++){   	
   		buyGridI = buyGridKeyOrder[orderedKeyValueIndex];
    	var action = getQueryVariable("action");
    	
    	doDebug("[init] making new sbg(" + sA[buyGridI].buyGridNum + ")");
    	buyGrids[buyGridI] = new sbg(sA[buyGridI].buyGridNum,buyGridI);
  		
    	//if (document.getElementById("sizeWaist_prd_" + sA[buyGridI].buyGridNum) != null) {
    		//buyGrids[buyGridI].pantsHemmingSelector = new hem(sA[buyGridI].buyGridNum, buyGrids[buyGridI]);
    	//}
        if (action == "modify") {
            buyGrids[buyGridI].modifyFromShoppingBag();
        } else if (action == "order_more" || getQueryVariable("fitChange") == "true") {
        	buyGrids[buyGridI].makeSelectionsFromQueryString();
        }
    }
    doDebug("[init] done making buy grids!");

	  if(multiItem == true){
			multiItem = new MultiItem();
	  }

		// requires multi-item to be setup before calling, due to the url refer logic
    inlineBag = new inlineShoppingBag(); 

   	if (document.getElementById("fullCollection") != null) {
      doDebug("[init] Start building collection buy grids");
   		collectionBuyGrid = new cbg();
      doDebug("[init] done making collection buy grids!");
   	}
   	
   	// build the bread crumb from the cookie values
   	buildBreadCrumb();
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function addErrorEvent(func) {
  var oldonerror = window.onerror;
  if (typeof window.onerror != 'function') {
    window.onerror = func;
  } else {
    window.onerror = function() {
      oldonerror();
      func();
    }
  }
}

function leErrorHandler(msg, url, lno) {
	var el = document.createElement("img");
  	el.setAttribute("height", 1);
  	el.setAttribute("width", 1);
  	var now = new Date();
  	var eSrc = hostname + "/images/error.gif?onerr=true&ts="+now.getTime()+"&file="+escape(url)+"&line="+lno+"&msg="+escape(msg)+"&sid="+theSid+"";
  	el.setAttribute("src", eSrc);
  	if (document.body) {
  	    document.body.appendChild(el);
  	}
   doDebug("[ERROR]: "+msg+" at line "+lno+" of "+url);
   return true;
}

// This function builds the bread crumb from the cookies. But ONLY if the page request 
// includes the parameter bcc=y on the query string AND there is at least 1 breadcrumb 
// in the cookie.
function buildBreadCrumb() {
    var bcc = getQueryVariable("bcc");
    var breadCrumbEle = document.getElementById("breadcrumbs");
    var productAlternatesALink = document.getElementById("productAlternatesALink");
    
    if (bcc == "y" && breadCrumbEle != null && breadCrumbEle != undefined) {
    	var bcString = "<a href='/'>Homepage</a> ";
	    var bcNumURLs = (getCookie("bcNumURLs") == null) ? 0 : getCookie("bcNumURLs");
	    var channel = getCookie("channel");
	    
	    if (bcNumURLs > 0) {
	    	bcString = bcString + "> ";
		    var i = 0;
		    var lastURL = bcNumURLs - 1;
		    var bcURL = null;
		    for (i = 0; i < bcNumURLs; i++) {
		       bcURL = getCookie("bcURL_"+i);
		       var bcLinkText = decodeURIComponent(getCookie("bcLinkText_"+i));
		       bcString = bcString + "<a href='" + bcURL + "'>" + bcLinkText + "</a>";
		       if (i != lastURL) {
		         bcString = bcString + "&nbsp;&gt;&nbsp;";
		       }
		    }
		    breadCrumbEle.innerHTML = bcString;
		    // collections don't have a product alternates link
		    if (productAlternatesALink) {
			    var queryOrigin = getQueryVariable("origin");
			    var productAlternateALinkText = "view similar items";
			    if(queryOrigin == "search"){
			    	productAlternateALinkText = "back to search results";
			    }else if(queryOrigin == "index"){
			    	productAlternateALinkText = "back to product category";
			    }
		    	productAlternatesALink.innerHTML = "<a href='" + bcURL + "'>" + productAlternateALinkText + "</a>"
		    }
		}
	
		if (channel !== null && channel != "") {
			doDebug("[buildBreadCrumb] s_omtr.channel = " + channel);
			s_omtr.channel = channel;
		}		
    }
}

addErrorEvent(leErrorHandler);