﻿$(document).ready(function() {

    $.ajaxSettings.cache = false;


    // //viewProd();
    // //seeAll();
    soundToggle();
    styleSheetToggler();

     $("#sound a").mousedown(function() {
        //myMovie("header").sendToActionScript();
		 swfobject.getObjectById("header").sendToActionScript();
     });


    /* MODALS */
    $("#rw-launch").click(function() {
        $("#rw-modal").modal();
    });
    $("#fw-launch").click(function() {
        $("#fw-modal").modal();
    });
    $("#cg-launch").click(function() {
        $("#cg-modal").modal();
    });
    $("#lr-launch").click(function() {
        $("#lr-modal").modal();
    });
    $("#sde-launch").click(function() {
        $("#sde-modal").modal();
    })
    $("#hsg-launch").click(function() {
        $("#hsg-modal").modal();
    })


    $("#gr-launch").click(function() {
        $("#gr-modal").modal();
    })
    $("#ag-launch").click(function() {
        $("#ag-modal").modal();
    })
    $("#wm-launch").click(function() {
        $("#wm-modal").modal();
    })
    $("#sd-launch").click(function() {
        $("#sd-modal").modal();
    })
    
    
    
    /*Seasonal - spring */
    $("#mf-info-launch").click(function() {
        $("#mf-info-modal").modal();
    })
    
    $(".spring-recipes-btn").click(function() {
        $("#spr-recipes-modal").modal();
    });
    $(".mf-recipes-btn").click(function() {
        $("#spr-recipes-modal").modal();
    });    
    
    

    $("#contact-link").click(function() {
        $("#contact-modal").modal();
    });


    $(".climate").click(function() {
        $("#climate-modal").modal();
    });

    $(".hug").click(function() {
        $("#hug-modal").modal();
    });

    $(".fastfood").click(function () {
        $("#fastfood-modal").modal();
    });


    // Load the comments in
    $('#comments').load('CommunityBoard.aspx');

    /* Bread drop modal */
    $(".bread-drop").click(function() {
        
        $("#bread-drop-modal").modal(/*{ position: ["2%", "25%"] }*/);
        
        $('.bread-drop-scroll-pane').jScrollPane();
    });

    /* Community board modal */
    $(".community-board").click(function() {

        ResetError(true);

        $('#comments').load('CommunityBoard.aspx');

        $("#community-board-modal").modal(/*{ position: ["2%", "20%"] }*/);

//        $('.left-image').pngfix();

        $('.community-board-scroll-pane').jScrollPane();

        $('#comments').fadeIn('slow');
    });

    // IE family of browsers have issues with A links and click() event
    if ($.browser.msie) {
        $("#postcomment").mouseup(function() { PostComment(); });
    }
    else {
        $("#postcomment").click(function() { PostComment(); });
    }

    function PostComment() {

        // Grab the values entered by the user
        var firstName = $("#firstName").val();
        var email = $("#email").val();
        var state = $("#state").val();
        var comment = $("#comment").val();

        // Reset the error state
        ResetError(false);

        var inputErrors;

        // Validate the submission
        if (firstName == "") {
            $("#nameRow").removeClass("item");
            $("#nameRow").addClass("error");
            inputErrors = true;
        }
        if (email == "" || !IsValidEmail(email)) {
            $("#emailRow").removeClass("item");
            $("#emailRow").addClass("error");
            inputErrors = true;
        }
        if (state == "") {
            $("#stateRow").removeClass("item");
            $("#stateRow").addClass("error");
            inputErrors = true;
        }
        if (comment == "") {
            $("#commentRow").removeClass("item");
            $("#commentRow").addClass("error");
            inputErrors = true;
        }

        if (inputErrors) {
            return;
        }

        // Submit the comment

        var data = {
            a: "PostComment",
            n: firstName,
            e: email,
            s: state,
            c: comment
        }

        try 
        {
            var responseText = jQuery.ajax({
                url: "services/CommunityBoard.ashx",
                data: data,
                type: "POST",
                async: false
            }).responseText;

            var response = JSON.parse(responseText);

            if (response != null) {
                if (response.value == true) {
                    // Post OK!
                    
                    // Load the comments list anew
                    $('#comments').fadeOut('slow');
                    $('#comments').load('CommunityBoard.aspx', function() {
                        $('.community-board-scroll-pane').jScrollPane();
                        $('#comments').fadeIn('slow');
                    });
                }
                else {
                    // Something failed on the server end
                }
            }
        }
        catch(ex)
        {
            // Problem with the ajax call
        }
        
        // Clear/reset the fields
        ResetError(true);
    }

    function ResetError(bClearFields) {

        if (bClearFields == true) {
            $("#firstName").val("");
            $("#email").val("");
            $("#state").val("ACT");
            $("#comment").val("");
        }

        $("#nameRow").removeClass("error");
        $("#emailRow").removeClass("error");
        $("#stateRow").removeClass("error");
        $("#commentRow").removeClass("error");

        $("#nameRow").addClass("item");
        $("#emailRow").addClass("item");
        $("#stateRow").addClass("item");
        $("#commentRow").addClass("item");
    }

    function IsValidEmail(email) {
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        return filter.test(email);
    }

    RangeTabs_Init();
    initRecipes();
    
    //$(".product-list.granary").hide();
    //SetPage(".product-list.granary", 0);
    SetPage(".product-list.everyday", 0);
});

/**
* Setup click events for the range tab selection
*/
function RangeTabs_Init() {
	if(!$(".everyday .page .intro").hasClass('active')){
		//console.log('RangeTabs_Init');
		$(".everyday .page .intro").addClass('active');
	}
    $(".rangetab").click(function() {
        $(".rangetab").removeClass("active");
        $(this).addClass("active");
/*
        if ($(this).hasClass("rangetab-granary")) {
            //Show Granary
            $(".product-list.granary .intro .on, .product-list.granary .intro .off").show();
            $(".product-list").css({ display: "none" });
            $(".product-list.granary").css({ display: "block" });
            SetPage(".product-list.granary", 0);
        } else {
            //Show Everyday
            $(".product-list").css({ display: "none" });
            $(".product-list.everyday").css({ display: "block" });
            SetPage(".product-list.everyday", 0);
        }
*/
      /*  
      $('rangetab').each(function(){
    	  $(this).removeClass('rangetab-selected');
      }); 
      $(this).addClass('rangetab-selected');
      */

        if ($(this).hasClass("rangetab-seasonal")) {
            //Show Seasonal 
            $(".product-list.seasonal .intro .on, .product-list.seasonal .intro .off").show();
            $(".product-list").css({ display: "none" });
            $(".product-list.seasonal").css({ display: "block" });
            SetPage(".product-list.seasonal", 0);
        } else {
            //Show Everyday
            $(".product-list").css({ display: "none" });
            $(".product-list.everyday").css({ display: "block" });
            SetPage(".product-list.everyday", 0);
        }        
    });

/*
    $(".btn.ag, #access-ag").click(function() {
        SetPage(".product-list.granary", 1);
    });
    $(".btn.wm, #access-wm").click(function() {
        SetPage(".product-list.granary", 2);
    })
    $(".btn.sd, #access-sd").click(function() {
        SetPage(".product-list.granary", 3);
    })
 */   
    
    $(".btn.spr, #access-spr").click(function() {
        SetPage(".product-list.seasonal", 1);
    })
    
    
    
    $(".btn.rw, #access-rw").click(function() {
        SetPage(".product-list.everyday", 1);
    });
    $(".btn.fw, #access-fw").click(function() {
        SetPage(".product-list.everyday", 2);
    });
    $(".btn.cg, #access-cg").click(function() {
        SetPage(".product-list.everyday", 3);
    });
    $(".btn.lr, #access-lr").click(function() {
        SetPage(".product-list.everyday", 4);
    });
    $(".btn.hsg, #access-hsg").click(function() {
        SetPage(".product-list.everyday", 5);
    });
    
}

/**
* Activates the index page in the productList
* @param {Object} productList
* @param {Object} index
*/
function SetPage(container, index) {
    $(container).children().removeClass("active").eq(index).addClass("active");
}



function seeAll() {
    $("#accessible-open").toggle(
		function() {
		    $("#accessible-open").addClass("all-open");
		    $("#accessible-open").removeClass("all-closed");
		    $(".rw .on").css("display", "block");
		    $(".fw .on").css("display", "block");
		    $(".cg .on").css("display", "block");
		    $(".lr .on").css("display", "block");
		    $(".spr .on").css("display", "block");
		    $(".modal").css("display", "block");
		    $("#contact-modal").css("display", "none");
		    $(".moreinfo-btn").css("display", "none");
		    $(".spring-recipes-btn").css("display", "none");
		    $(".mf-recipes-btn").css("display", "none");
		    $(".simplemodal-close").css("display", "none");
		},
			function() {
			    $("#accessible-open").removeClass("all-open");
			    $("#accessible-open").addClass("all-closed");
			    $(".intro .on").css("display", "block");
			    $(".rw .on").css("display", "none");
			    $(".fw .on").css("display", "none");
			    $(".cg .on").css("display", "none");
			    $(".spr .on").css("display", "none");
			    $(".mf .on").css("display", "none");
			    $(".modal").css("display", "none");
			    $(".moreinfo-btn").css("display", "block");
			    $(".spring-recipes-btn").css("display", "block");
			    $(".mf-recipes-btn").css("display", "block");
			    $(".simplemodal-close").css("display", "block");
			}
			);
}

function soundToggle() {
    $("#sound a").toggle(
		function() {
		    $("#sound").addClass("sound-off");
		    $("#sound").removeClass("sound-on");
		},
				function() {
				    $("#sound").addClass("sound-on");
				    $("#sound").removeClass("sound-off");
				}
				);
}

// ***************************** flash header embed ***************************** \\
var flashvars = {};
var params = {
    menu: "false",
    wmode: "transparent"
};
swfobject.embedSWF("header.swf", "header", "290", "160", "9.0.0", "expressInstall.swf", flashvars, params);

// ***************************** sound callback ***************************** \\
function myMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
        return document[movieName];
    }
}

function styleSheetToggler() {
    // Call stylesheet init so that all stylesheet changing functions 
    // will work.
    $.stylesheetInit();

    // This code loops through the stylesheets when you click the link with 
    // an ID of "accessibility-toggle" below.
    $('#accessibility-toggle').bind(
			'click',
			function(e) {
			    $.stylesheetToggle();
			    return false;
			}
		);
}

/*Recipes modal page*/
//Show the selected recipe
function initRecipes(){
	//Map links to showRecipe fn to show rel. recipe
	$('#spr-recipes-nav span').each(function(idx,val){
		//console.log(val);
		$(this).click(function(e) {
			e.preventDefault();
			showRecipe($('#spr-recipes-modal-page2'), idx);
	    })
	});
	showRecipe($('#spr-recipes-modal-page2'), 0);	
}
function showRecipe(container,recipeIndex){
	//Select recipe
	//console.log('container:'+container + ' idx:'+recipeIndex);
	$(container).find('.recipe-page').each(function(idx,val){
		if(idx ==recipeIndex){
			$(this).show();
		}else {	
			$(this).hide();
		}
	});
/*	
	//Highlight link
	$('#spr-recipes-nav span').each(function(){
		$(this).css('color','#000000');
	});
	$('#spr-recipes-nav span').eq(recipeIndex).css('color','#b2bb1c');
*/
	
	$('#spr-recipes-nav span').each(function(){
		$(this).removeClass('recipe-nav-selected');
	});
	$('#spr-recipes-nav span').eq(recipeIndex).addClass('recipe-nav-selected');;

}

