﻿/**
* constants
*/
WIDGET_WIDTH_FEATURED = "355px";
WIDGET_WIDTH_WIDE = "712px";


/**
* on DOM ready handler
*/   
 $(document).ready( function() {   
    
    $("#recipeModule .backToMain").click(function(){
        rWidget.ToFeatured();
    });

    $("#recipeModule a.seeAllRecipes").click( function() {
        rWidget.OpenAll();
    });
    
    rWidget.Init();
 
 
    // embed flash
	var flashvars = {
		baseUrl		: "global/swf/"
	};
	var params = {
		menu			: "false",
		scale			: "noScale",
		wmode			: "transparent",
		swliveconnect   : "true"
	};
	var attributes = {
		id              : "clickSound", 
		name            : "clickSound"
	};
	swfobject.embedSWF("global/swf/Recipe.swf", "recipeSoundHolder", "1", "1", "9.0.115", "global/swf/expressInstall.swf", flashvars, params, attributes);
	
    $(".getrecipe").each(function(){
        if($(this).attr("link")=="internal")$(this).attr("target","_self");
    
    });
    
    $(".getrecipethumb").each(function(){
        if($(this).attr("link")=="internal")$(this).attr("target","_self");
    
    });
});

function sendToJavaScript (value) {
  
    sendToActionScript (value);
  
}

function sendToActionScript (value) { 

    var obj = document.getElementById("clickSound");
    obj.sendToActionScript(value);  

}  
  
/**
*
*/
var rWidget = {

    CurrentCat:"snacks", // defaults to breakfast
    Mode:0, // 0 for closed, 1 for open

    
    // init method
    Init: function() {        
        var wrapper = $("div.catWrapper:eq(1)").show();
        this.ToFeatured();
         
        $("#hero").css("background-image","url(global/images/" + $(wrapper).attr("bgpath") + ")");  
        
        // set tab click behavior
        $("#recipeTabs img").click( function() {
            recipeClickAudioTab('playTab');
            rWidget.GoToCat($(this).attr("cat"));
        });  
        
    },    
    
    // methods
    
    /**
    * switch to category
    */

    
    /**
    * switch to category
    */
    OpenAll: function() {      
        this.Mode = 1;
        this.ResetButtons();
        $('div.featuredBox').hide();
        // and now turn on appropriate button
        var button = $("#recipeTabs img[cat='" + this.CurrentCat + "']");
        var onSrc = $(button).attr("src");
        onSrc = onSrc.replace("-off","-open");
        onSrc = onSrc.replace("-on","-open");
        $(button).attr("src",onSrc);         
        
        $("#recipeModule").animate(
            { width: WIDGET_WIDTH_WIDE },
              300, "swing" 
        ).addClass("recipeWideBg");
                
        setTimeout( function() {  
            var wrapper = $("div.catWrapper:visible div.allRecipesBox").fadeIn();
            $('div.catWrapper:visible .scroll-pane').jScrollPane({scrollbarWidth:14});
         }, 200);    
    },
    
    /**
    * switch to category
    */
    ToFeatured: function() {      
        this.Mode = 0;
        this.ResetButtons();

        // and now turn on appropriate button
        var button = $("#recipeTabs img[cat='" + this.CurrentCat + "']");
        var onSrc = $(button).attr("src");
        onSrc = onSrc.replace("-off","-on"); // get off
        onSrc = onSrc.replace("-over","-on"); // or get over
        $(button).attr("src",onSrc);

        $('div.allRecipesBox').hide(); // disappear all content        
        // slide closed the view all panel to the featured width
        $("#recipeModule").animate(
            { width: WIDGET_WIDTH_FEATURED },
              300, "swing", function() { $(this).removeClass("recipeWideBg");
        });
        
        // wait .2 seconds and then fade in featured content        
        setTimeout( function() {                
          var wrapper = $("div.catWrapper:visible div.featuredBox").fadeIn();
        }, 200);   
              
    },
   
    
    GoToCat: function(catName) {

        this.CurrentCat = catName;
        $("div.catWrapper").hide(); // hide all cats
        $("div.allRecipesBox").hide(); // hide all recipes container
        var wrapper = $("div.catWrapper[cat='" + catName + "']").show();
        $(wrapper).find('div.featuredBox').show();
            
        if (this.Mode === 1) {
            this.OpenAll(); 
        } else {
            this.ToFeatured();
        }
        
        $("#hero").css("background-image","url(global/images/" + $(wrapper).attr("bgpath") + ")");
//                          
//        // animate transition (it only animates if we're in 'view all' mode
//        $("#recipeModule").animate({ 
//            width: WIDGET_WIDTH_WIDE
//        }, 500, function() { 
//            // deal with background image
//            $("#hero").css("background-image","url(global/images/" + $(wrapper).attr("bgpath") + ")");             
//        } );// end animate
    },
    
    /**
    * set all buttons to off
    */
   ResetButtons: function() {
        // set all buttons to off stage
        $("#recipeTabs img").each( function() {
            var src = $(this).attr("src");
            src = src.replace("-on","-off"); // get on
            src = src.replace("-open","-off"); // also get overs
            $(this).attr("src",src);
        });
   
   }



} // end recipe widget object

// sound for buttons and tabs

function recipeClickAudio(value) {
    var flashObj = document.getElementById("clickSound");
    if (flashObj && typeof flashObj.recipeClickAudio != "undefined") {
	    flashObj.recipeClickAudio(value);
    }
}

function recipeClickAudioTab(value) {
    var flashObj = document.getElementById("clickSound");
    if (flashObj && typeof flashObj.recipeClickAudioTab != "undefined") {
	    flashObj.recipeClickAudioTab(value);
    }
} 



// click event handlers



  
  

//            ocontainer = $('div.scrollContainer');
//			ocontent = $('div.scrollArea');
//			
//			containerSize = jQuery.iUtil.getSize(ocontainer.get(0));
//			containerPosition = jQuery.iUtil.getPosition(ocontainer.get(0));
//			containerInner = jQuery.iUtil.getClient(ocontainer.get(0));
//			
//			contentSize = jQuery.iUtil.getSize(ocontent.get(0));
//			
//			$('div.slider1')
//				.css('top', containerPosition.y + 'px')
//				.css('left', containerPosition.x + containerSize.wb + 'px')
//				.css('height', containerSize.hb + 'px');
//			
//			spaceToScroll = contentSize.hb - containerInner.h;
//			
//			$('div.indicator')
//				.css('height', containerInner.h * containerSize.hb / contentSize.hb + 'px')



//  // configure slider
//    $('.slider1').Slider(
//	    {
//		    accept : '.indicator',
//		    onSlide : function( cordx, cordy, x , y)
//		    {
//			    //ocontent
//			    //	.css('top', - spaceToScroll * cordy / 100 + 'px');
//		    }
//	    }
//    );




//}); // end document.ready

    

















        