


var RecentComparisonHistory = new Object;
RecentComparisonHistory.data = new Array;


RecentComparisonHistory.settings = {
    cookieName: 'RecentComparisonHistory',
    cookieHost: '.pricegrabber.com',
    pageTitle: null,
    langId: null,
    tokens: {
        pages:     ',',
        attrs:     ':',
        masterIds: {
            pre:   'all{',
            split: ';',
            post:  '}'
        }
    }
};


RecentComparisonHistory.createCookie = function() {
    var pages = new Array();
    for (var i=0; i<RecentComparisonHistory.data.length; i++) {
        var attrs = new Array();
        var page  = RecentComparisonHistory.data[i];
        attrs[0] = page.pageId;
        attrs[1] = page.title;
        attrs[2] = page.langId;
        attrs[3] = page.lastModified;
        var pre  = RecentComparisonHistory.settings.tokens.masterIds.pre;
        var post = RecentComparisonHistory.settings.tokens.masterIds.post;
        attrs[4] = pre+page.masterIds.join(RecentComparisonHistory.settings.tokens.masterIds.split)+post;
        pages[i] = attrs.join(RecentComparisonHistory.settings.tokens.attrs);
    }
    var cookieVal = pages.join(RecentComparisonHistory.settings.tokens.pages);
    util.createCookie(RecentComparisonHistory.settings.cookieName, cookieVal, null, '/', RecentComparisonHistory.settings.cookieHost);

}

RecentComparisonHistory.parseCookie = function() {
    var cookieVal = util.readCookie(RecentComparisonHistory.settings.cookieName);
    if (cookieVal) {
        //alert(cookieVal);
        var pages = cookieVal.split(RecentComparisonHistory.settings.tokens.pages);
        for (var i=0; i<pages.length; i++) {
            var pageStr = pages[i];
            var attrs = pageStr.split(RecentComparisonHistory.settings.tokens.attrs);
            var pageIds = attrs[4].substring(RecentComparisonHistory.settings.tokens.masterIds.pre.length,
                                             attrs[4].length - RecentComparisonHistory.settings.tokens.masterIds.post.length);
            var page = {
                pageId:       attrs[0],
                title:        attrs[1],
                langId:       attrs[2],
                lastModified: attrs[3],
                masterIds:    pageIds.split(RecentComparisonHistory.settings.tokens.masterIds.split)
            };
            RecentComparisonHistory.data.push(page);
        }
    }
}

RecentComparisonHistory.toggleMasterId = function(pageId, masterId, catName, langId) {
    var pageIds = new Array();
    for (var i=0; i<RecentComparisonHistory.data.length; i++) {
        pageIds.push(RecentComparisonHistory.data[i].pageId);
    }
    var idx = pageIds.inArray(pageId);
    if (idx === false) {
        idx = RecentComparisonHistory.data.length;
        pageIds[idx] = pageId;
        page = {
            pageId:    pageId,
            title:     catName,
            langId:    langId,
            masterIds: new Array(masterId),
            timestamp: null
        };
    } else {
        page = RecentComparisonHistory.data[idx];
        if (page.masterIds.inArray(masterId) !== false) {
            // Remove the master id from the list
            var tmpMasterIds = new Array();
            for (i=0; i<page.masterIds.length; i++) {
                 if (page.masterIds[i] != masterId) {
                     tmpMasterIds.push(page.masterIds[i]);
                 }
            }
        } else {
            // Add the master id to the list
            page.masterIds.push(masterId);
        }
    }
    page.lastModified = (new Date()).valueOf();
    RecentComparisonHistory.data[idx] = page;
}


RecentComparisonHistory.newToggleMasterId = function(pageId, masterId, catName, langId, comparText0, comparText1, comparText2) {
    
	var pageIds = new Array();

    for (var i=0; i<RecentComparisonHistory.data.length; i++) {
        pageIds.push(RecentComparisonHistory.data[i].pageId);
    }
    var idx = pageIds.inArray(pageId);

	if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}


    if (idx === false) {
        idx = RecentComparisonHistory.data.length;
        pageIds[idx] = pageId;
        page = {
            pageId:    pageId,
            title:     catName,
            langId:    langId,
            masterIds: new Array(masterId),
            timestamp: null
        };
    } else {
       page = RecentComparisonHistory.data[idx];
	   if( page.masterIds.length == 1 && page.masterIds == ''){
		   page.masterIds = new Array(); 
	   }
       if( document.getElementById('productCompareCheckbox'+ masterId).checked == false){
            // Remove the master id from the list 			
			page.masterIds.splice(page.masterIds.indexOf(masterId), 1);

        } else if(page.masterIds.inArray(masterId) == false) {
            // Add the master id to the list
            page.masterIds.push(masterId);
        }
    }

    page.lastModified = (new Date()).valueOf();
    RecentComparisonHistory.data[idx] = page;
	if (page.masterIds.length == 0 || document.getElementById('productCompareCheckbox'+ masterId).checked == false) {
		document.getElementById('productCompareLink'+masterId).innerHTML = unescape(comparText2);
		document.getElementById('productCompareLink'+masterId).className = "";
		document.getElementById('productCompareDiv'+ masterId).className = "compareDiv";
		document.getElementById('productCompareLink'+masterId).onclick  = function() {
			compare_toggle_check(document.getElementById('productCompareLink'+masterId));
			return newToggleCompareCbx(document.getElementById('productCompareCheckbox' + masterId)) ;
		};
		if (page.masterIds.length == 1 && document.getElementById('productCompareLink'+ page.masterIds[0]) ){
			document.getElementById('productCompareLink'+ page.masterIds[0]).innerHTML = unescape(comparText1);
			document.getElementById('productCompareLink'+ page.masterIds[0]).className = "compareAnother";
			document.getElementById('productCompareDiv'+ page.masterIds[0]).className = "compareDiv selectAnother";
			
		}
	} else if (page.masterIds.length == 1) {
		document.getElementById('productCompareLink'+masterId).innerHTML = unescape(comparText1);
		document.getElementById('productCompareLink'+masterId).className = "compareAnother";
		document.getElementById('productCompareDiv'+ masterId).className = "compareDiv selectAnother";
	} else {
		document.getElementById('productCompareLink'+masterId).onclick  = function() {};
		//document.getElementById('productCompare'+masterId).onclick  = function() {};
		document.getElementById('productCompareLink'+masterId).innerHTML = '<span onClick="setSubmitTimeout()", 50);">'+unescape(comparText0)+'</span>';
		document.getElementById('productCompareLink'+masterId).className = "compareNow";
		//IE6 hack. Form won't submit unless you submit call this function through a timeout...
		

		document.getElementById('productCompareDiv'+ masterId).className = "compareDiv selectCompareNow";
		for (var i=0; i<page.masterIds.length; i++) {
			if(document.getElementById('productCompareLink'+page.masterIds[i]) && document.getElementById('productCompareDiv'+ page.masterIds[i])){
				document.getElementById('productCompareLink'+page.masterIds[i]).onclick  = function() {};
				//document.getElementById('productCompare'+page.masterIds[i]).onclick  = function() {};
				document.getElementById('productCompareLink'+page.masterIds[i]).innerHTML = '<span onClick="setSubmitTimeout();", 50);">'+ unescape(comparText0)+'</span>';
				document.getElementById('productCompareLink'+page.masterIds[i]).className = "compareNow";
				document.getElementById('productCompareDiv'+ page.masterIds[i]).className = "compareDiv selectCompareNow";
			}
		}
	}

}


function setSubmitTimeout(){
	setTimeout("submitFormFunction();", 50);
}

//IE6 hack function
submitFormFunction = function() {
	document.getElementById("productCompareForm").submit();	
}




RecentComparisonHistory.parseCookie();

