﻿/**
* some custom jquery extensions
*/
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

/**
* douglas crockford's variable substitution extension
*/
String.prototype.supplant = function (o) {
    return this.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
};



/**
* on DOM ready handler
*/

$(document).ready( function() {
       // apply pngfix in case of ie < 7
       if ($.browser.msie === true && $.browser.version < 7) {
        pngfix();
       }
       // user jquery to set some convenient styles with CSS3 selectors
       setCSS3styles();
       // set some event handlers
       setBehaviors();
       // do image replacement on some textual titles
       textReplacement();
       
       setHTMLTrackingListener(); // for all events outside flash
       
       
        // embed flash
        var flashvars = {
	        baseUrl		: "global/swf/"
        };
        var params = {
	        menu			: "false",
	        scale			: "noScale",
	        wmode			: "transparent"
        };
        var attributes = {
	        id				: "website"
        };
        swfobject.embedSWF("global/swf/mute.swf", "soundFlashHolder", "62", "38", "9.0.115", "global/swf/expressInstall.swf", flashvars, params, attributes);
     
})

function sendToJavaScript (value) {
  
    sendToActionScript (value);
  
}

function sendToActionScript (value) { 

    var obj = document.getElementById("website");
    obj.sendToActionScript(value);  

}

/**
* 
*/
function navOver(arg) {
    $("#nav" + arg).css("visibility","visible");
}

/**
* 
*/
function navOut(arg) {
    $("#nav" + arg).css("visibility","hidden");
}

/**
* 
*/
function setCSS3styles() {

    // bottom buckets
    $("#touts li:last").css("background","none"); // get rid of divider on rightmost bucket (last)


}

/**
* 
*/
function setSIFR() {
    $("h1").sifr( { font:'HelveticaNeueBlackCond_97',path: 'global/fonts'   } );
    $("#bottomBuckets h3").sifr( { font:'HelveticaNeueBlackCond_97',path: 'global/fonts'   } );
       
}

/**
*  attach action handlers to dom items
*/
function setBehaviors() {
    
    // send a spoonogram over state for spoon image in upper right
    $("#spoon").hover( function() { $("#spoon-overstate").css("visibility","visible"); }, function() { $("#spoon-overstate").css("visibility","hidden"); });
    $("#spoon-overstate").hover( function() { $(this).css("visibility","visible"); }, function() { $(this).css("visibility","hidden"); });



}

/**
*  handle image replacement on SEO sensitive titles
*/
function textReplacement() {
    var bgsrc = $("*[replacepath]").each( function() {
           var src = $(this).attr("replacepath");
           $(this).css("background-image","url(" + src.toString() + ")");
           $(this).css("background-repeat","no-repeat");
           $(this).css("text-indent","-1000px");
           $(this).css("overflow","hidden");  
    });
}


/**
*  tracking
*/

function trackEvent( param1, param2, param3 ) {
    pageTracker._trackEvent(param1, param2, param3); // pageTracker is instantiated in body block
}

function trackPage( param1 ) {
    pageTracker._trackPageview(param1); // pageTracker is instantiated in body block
}

function setHTMLTrackingListener() {
           //console.time("live event attachment")
           $("*").live("click", function() {        
                if (typeof $(this).attr("track") !== 'undefined') {
                            var key = $(this).attr("track");    
                            if (key in trackData) {
                                var data = trackData[key];
                                switch(data.gaMode) {
                                    case "page":
                                        var finalParam;
                                        if ("function1" in data) {
                                            // this NEEDS to be refactored
                                            var text1 = data.function1.call(this);
                                            finalParam = data.param1.supplant( { function1: text1  } );
                                            var text2 = data.function2.call(this);
                                            finalParam = finalParam.supplant( { function2: text2  } );                                   
                                        } else {
                                            finalParam = data.param1;
                                        }   
                                        pageTracker._trackPageview(finalParam);  
                                        break;
                                    case "event":
                                        pageTracker._trackEvent(data.param1,data.param2,data.param3);  
                                        break;                                   
                                } // end switch
                            } // end if                            
                } // end if
           } ); // end live handler
} // end function



/**
*  tracking data
*  this provides parameters corresponding to the keys which are embedded in the clicked elements
*/

var trackData = {

    breakfastTab: { 
        gaMode : "page",   
        param1 : "/recipes/breakfast/"
    }, // end record
    snacksTab: { 
        gaMode : "page",   
        param1 : "/recipes/snacks/"
    }, // end record
    sweetsTab: { 
        gaMode : "page",   
        param1 : "/recipes/sweets/"
    }, // end record
    recipe: { 
        gaMode : "page",   
        param1 : "/recipes/{function2}/{function1}",
        function1 : function() { return $(this).parent().find("h4").text(); },
        function2 : function() { return $(this).parents(".catWrapper").attr("cat"); }
    }, // end record
    featuredRecipe: { 
        gaMode : "page",   
        param1 : "/recipes/{function2}/{function1}",
        function1 : function() { return $(this).parent().parent().find("h4").text(); },
        function2 : function() { return $(this).parents(".catWrapper").attr("cat"); }
    }, // end record
    emailPreview: { 
        gaMode : "page",   
        param1 : "/email preview"
    }, // end record
    emailSubmit: { 
        gaMode : "page",   
        param1 : "/email signup thank you"
    }, // end record
    pollSubmit: { 
        gaMode : "page",   
        param1 : "/talktous/thanksforvoting/"
    }, // end record 
    talktousLink: {
        gaMode : "page",   
        param1 : "/talktous/talktous/"
    } // end record
    /*
    pollSubmit: { 
        gaMode : "event",   
        param1 : "poll",
        param2 : "submission",
        param3 : "submission"
    } // end record    */    
} // end data

function popRules() {
    window.open ("Rules.aspx", "ruleswindow","location=0,status=0,scrollbars=1, width=800,height=600");  
}