/*
 *  Fix IE background image flicker (http://www.mister-pixel.com/)
 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

/*
 *  Pace Global namespace
 */
var Pace = {};

/*
 *  sIFR (http://wiki.novemberborn.net/sifr/)
 */
Pace.SIFR = Class.create({
    // Constructor
    initialize: function()
    {
        if(typeof sIFR != "function" || sIFR.UA.bIsIEMac || document.location.toString().indexOf('awards-press') > 0) return;
        
        this.attachEvents();
        this.build();
    },
    
    // Private Methods
    attachEvents: function()
    {
        document.observe('sIFR:refresh', this.build.bind(this));
    },
    
    build: function()
    {
        sIFR.replaceElement("#main-content ul li h3", named({sFlashSrc: "UI/flash/stymieT.swf", sColor: "#000000", sWmode: "transparent", sFlashVars: "textalign=center"}));
        sIFR.replaceElement("h3", named({sFlashSrc: "UI/flash/stymieT.swf", sColor: "#D22435",sWmode: "transparent"}));
    }
});

/*
 *  DOM/Window load methods
 */
if (Prototype.Browser.IE) Event.observe(window, 'load', function() { document.fire('sIFR:refresh'); }); // refresh sIFR in IE 6 b/c of rendering issues

document.observe("dom:loaded", function()
{
    new Pace.SIFR();
});

/*
 *  Single use methods
 */
function showNutrition(productId)
{
    window.open('nutrition.aspx?productid='+productId,'nutrition','width=565,height=610,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 openRecipeSweepsRules() {
	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";
	RecipeSweepsRulesPop=window.open("RecipeSweepsRules.aspx","RecipeSweepsRulesPop",str);
	RecipeSweepsRulesPop.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)
        {
            location.href = (eval(auth)) ? liveWorldURL : communityURL;
        }
    );
}

function encodeLiveWorldURL(liveWorldURL)
{
    var encodedInputString = escape(liveWorldURL);
    encodedInputString = encodedInputString.replace(new RegExp("\/", "g"), "%2F");
    
    return encodedInputString;
}
