(function() {
    /**
     *  Fix IE background image flicker
     *  http://www.mister-pixel.com/
     */
    try
    {
        document.execCommand('BackgroundImageCache', false, true);
    }
    catch(e)
    {}
})();


/*
Dessert Kits flash - will have 1 of 2 productname passed in:
    product_mc0 – decadent fudge
    product_mc1 – magic cookie bars
*/
function showProductBackground(productname) {
    if (productname == "product_mc0") {
        $("body.dessert_kits #content_bkgd").css('backgroundImage','url(/UI/images/dessert-kits-fudge-bg.jpg)'); 
        $("body.dessert_kits #content_bkgd").show();
    }
    if (productname == "product_mc1") {
        $("body.dessert_kits #content_bkgd").css('backgroundImage','url(/UI/images/dessert-kits-mcb-bg.jpg)'); 
        $("body.dessert_kits #content_bkgd").show();
    }
}
function hideProductBackground() {
    $("body.dessert_kits #content_bkgd").hide();
}


//Homepage flash - sidebar should appear for recipe rotations but not for intro
function showHomeSidebar() {
    $("body.home #sidebar").show();
}


//How To Video page-specific function
function loadHowToVideo(videoFileName,videoName) {
    $("body.how_to_videos #video_container #video img").hide();
    $("body.how_to_videos #video_container #video p").show();
    $("body.how_to_videos #video_container h2").show();

    var flashvars = {
        thevideo: "/UI/flash/HowToVideos/4x3/" + videoFileName, //CherryCheesePie.flv
        gacategory: "Video",
        gaaction: "Recipes",
        galabel: videoFileName, //"CherryCheesePie.flv",
        gaaccount: gaaccount
    };
    var params = { allowScriptAccess: "always" };
    var attributes = {};
    swfobject.embedSWF("/UI/flash/HowToVideos/EagleBrandVideoPlayer_4x3ratio.swf", "video", "320", "280", "9.0.0", false, flashvars, params, attributes);    
    
    $("body.how_to_videos #video_container h2").html(unescape(videoName));
}


function loadHowToVideoShort(videoFileName, videoName) {
    $("body.how_to_videos #video_container #video img").hide();
    $("body.how_to_videos #video_container #video p").show();
    $("body.how_to_videos #video_container h2").show();

    var flashvars = {
        thevideo: "/UI/flash/HowToVideos/16x9/" + videoFileName, //ButterPecanIceCreamLoaf.flv
        gacategory: "Video",
        gaaction: "Recipes",
        galabel: videoFileName, //"ButterPecanIceCreamLoaf.flv",
        gaaccount: gaaccount
    };

    var params = { allowScriptAccess: "always" };
    var attributes = {};
    swfobject.embedSWF("/UI/flash/HowToVideos/EagleBrandVideoPlayer_16x9ratio.swf", "video", "320", "226", "9.0.0", false, flashvars, params, attributes);

    $("body.how_to_videos #video_container h2").html(videoName);
}

var newwindow;
function printPop(theURL,popWidth,popHeight) {
	newwindow = window.open(theURL,"popup","width="+popWidth+",height="+popHeight+",resizable=true,scrollbars=yes");
	if (window.focus) {newwindow.focus()};
}

/* Check to see if a particular querystring parameter exists */
function getAnchor() {
  var regexS = "#[A-Za-z0-9]+"; //"[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results;
};



/* Define a namespace */
var EagleBrand = {};

/* jQuery unobtrusive window pop */
EagleBrand.enableWinPop = function() {
	$("a.poptrigger").click(function() {
		var destination = $(this).attr("href");
		var winheight;
		var winwidth;
		if ($(this).hasClass("shortwin")) {
		    winheight = 225;
		} else {
		    winheight = 400;
		};
		
		if ($(this).hasClass("emailfriend")) {
		    winwidth = 500;
		    winheight = 475;
		} else {
		    winwidth = 540;
		}
		
		if ($(this).hasClass("newsletter")) {
		    winwidth = 668;
		    winheight = 800;
		}
		
		var newwindow = window.open(destination,"popup","width="+winwidth+",height="+winheight+",resizable=true,scrollbars=yes");
	    if (window.focus) {newwindow.focus()};
		return false;
	});	
};

EagleBrand.enableAccordion = function() {
	if ($("#topten").length > 0) {
	    var anchor = getAnchor();
        if (anchor != "") {
            $("#topten").accordion({
	            header: "dt",
	            collapsible: false,
	            animated: "slide",
	            active: "'" + anchor + "'",
	            autoHeight: false
	        });
        } else {
            $("#topten").accordion({
	            header: "dt",
	            collapsible: false,
	            animated: "slide",
	            autoHeight: false
	        });
        }
	};

	if ($("#expansion_list").length > 0) {
	    var anchor = getAnchor();
        if (anchor != "") {
            $("#expansion_list").accordion({
	            header: "dt",
	            collapsible: false,
	            animated: "slide",
	            active: "'" + anchor + "'",
	            autoHeight: false
	        });
        } else {
            $("#expansion_list").accordion({
	            header: "dt",
	            collapsible: false,
	            animated: "slide",
	            autoHeight: false
	        });
        }
	};
	
	/*
	$("#expansion_list dl dt").click(function () {
		if ($(this).parent("dl").hasClass('showing')) {
			$("dl").removeClass('showing');
			$(this).siblings("dd").slideUp("fast");
			//alert('test');
		} else {
			$("dl").removeClass('showing');
			$("dd").slideUp("fast");
			$(this).parent("dl").addClass('showing');
			$(this).siblings("dd").slideDown("slow");
		}
	});*/
};

EagleBrand.enablePrint = function() {
	$('img.printbutton').click(function() {
		if(window.print) {
			window.print();
		} else {
			alert('Your browser (or its current settings) does not support this print button. You may need to manually print this page.');
		}
		return false;
	});
};

 
/* Initialize everything when page is ready */
$(document).ready(function() {
    EagleBrand.enableWinPop();
    EagleBrand.enableAccordion();
    EagleBrand.enablePrint();

    //add extra spacing above recipe detail instructions footnotes
    $("#instructions li.heading:first").css("padding-top", "16px");

    //fix problem with Firefox not checking radio buttons on load
    if ($.browser.mozilla) $("form").attr("autocomplete", "off");


    //randomizes bottom right homepage callout
    var count = $('body.home #callouts div.rndm').size();
    var randNum = (Math.floor(Math.random() * count));
    $('body.home #callouts div.rndm').eq(randNum).show();


});









