/*
 *  Fix IE background image flicker (http://www.mister-pixel.com/)
 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

function showNutrition(productId) {
    window.open('nutrition.aspx?productid='+productId,'nutrition','width=562,height=602,resize=yes,scrollbars=yes');
}
function windowPop(a,b,c,d,e) {
    window.open(a,b,'width='+c+',height='+d+',resize='+e+',scrollbars='+e);
}

function openUPC() {
	var wt=325;
	var ht=195;
	var str="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,screenx=30,screeny=20,width="+wt+",height="+ht+",top=50,left=50";
	HomePop=window.open("upc.aspx","upcPop",str);
	HomePop.focus();
}

function openManu() {
	var wt=450;
	var ht=300;
	var str="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,screenx=30,screeny=20,width="+wt+",height="+ht+",top=50,left=50";
	HomePop=window.open("manufacturingcode.aspx","manuPop",str);
	HomePop.focus();
}

function openUTLfaq() {
	var wt=600;
	var ht=584;
	var str="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,screenx=30,screeny=20,width="+wt+",height="+ht+",top=50,left=50";
	UTLfaqPop=window.open("hand-pick-win-faq.aspx","UTLfaqPop",str);
	UTLfaqPop.focus();
}
function openUTLrules() {
	var wt=600;
	var ht=584;
	var str="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,screenx=30,screeny=20,width="+wt+",height="+ht+",top=50,left=50";
	UTLrulesPop=window.open("hand-pick-win-rules.aspx","UTLrulesPop",str);
	UTLrulesPop.focus();
}
function openNewsletter(newsletter) {
	var wt=641;
	var ht=700;
	var str="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenx=30,screeny=20,width="+wt+",height="+ht+",top=50,left=50";
	NewsletterPop=window.open(newsletter,"newsletter",str);
	NewsletterPop.focus();
}

function is_child_of(parent, child) {
	if( child != null ) {
	    try{
            while( child.parentNode ) {
                if( (child = child.parentNode) == parent ) {   
				    return true;
			    }
	        }     
	    }
	    catch(err) {return false;}
	}
	return false;
}
function openPopup(e,idNum){
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.fromElement;
	var container = document.getElementById("container"+idNum);
    //var reportStr = "open>>>from: " + tg.nodeName + " " + tg.id + " - to:" + reltg.nodeName + " " + reltg.id;
    //reportStr += "<br />parent node = " + tg.parentNode.id;
    //reportStr += "<br />ico: " + is_child_of(container,reltg);
    //reportStr += "<br />container: " + container.id + " - reltg: " + reltg.nodeName + " " + reltg.id;
    if (!is_child_of(container,reltg)){        
        container.className += " hasMouseOver";     
    }
    //reportStr += "<br />classnames: 1 - " + document.getElementById("container1").className + " ... 2 - " + document.getElementById("container2").className;
    //document.getElementById("report").innerHTML = reportStr;
}
function closePopup(e,idNum){
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;    
	var container = document.getElementById("container"+idNum);
    //var reportStr = "close>>>from: " + tg.nodeName + " " + tg.id + " - to:" + reltg.nodeName + " " + reltg.id;
    //reportStr += "<br />parent node = " + tg.parentNode.id;
    //reportStr += "<br />ico: " + is_child_of(container,reltg);
    //reportStr += "<br />container: " + container.id + " - reltg: " + reltg.nodeName + " " + reltg.id;
    if (!is_child_of(container,reltg)){
        container.className = "roll";
    }
    //reportStr += "<br />classnames: 1 - " + document.getElementById("container1").className + " ... 2 - " + document.getElementById("container2").className;
    //document.getElementById("report").innerHTML = reportStr;
}


//Under The Lid promotion
function changeIframeHeight(height) {
    parent.document.getElementById("strobeLandingFrame").style.height=height + "px";
}

function tagDownload(obj, type){
	var s_click = s_gi(s_account);
	s_click.events='event14';
	s_click.linkTrackVars = 'events,eVar9';
	s_click.linkTrackEvents = 'event14';
	s_click.eVar9 = type;
	s_click.tl(this, 'd', type);
}

function tagExit(obj, type){
	var s_click = s_gi(s_account);
	s_click.tl(this, 'e', type);
}

function tagExternal(obj, type){
    tagExit(obj,type);
}

function tagCustom(obj, type){
	var s_click = s_gi(s_account);
	s_click.tl(this, 'o', type);
}

var lwapiAuth;

function checkLWAuthAndRedirect(liveWorldURL){
    //lwapiAuth = new LWAPI("http://community.pacefoods.com");    //PROD
    lwapiAuth = new LWAPI("http://community.pacefoods.com"); //STAGING

    var communityURL = '/recipes/community.aspx?redirect=' + encodeLiveWorldURL(liveWorldURL);
    
    lwapiAuth.callContentService("isAuthenticated", "",
          function(auth) { 
                if (eval(auth)) {
                    location.href=liveWorldURL; 
                } else {
                      // not logged in: prompt for login
                      location.href=communityURL;
                }
          });
}

function encodeLiveWorldURL(liveWorldURL)
{
    var encodedInputString=escape(liveWorldURL);
    
    encodedInputString = encodedInputString.replace(new RegExp("\/", "g"), "%2F");
    
    return encodedInputString;
}
