$(document).ready(function () {
    // RECIPE FINDER
    var time = 600;
    var start = '-695px';
    var finish = '0';

    $('#recipe-finder ul li a').live('click',function () {
        $("#toppane .container").css({ 'z-index': '99' });
        $("#toppane #sidebar.home").css({ 'z-index': '99' });
        $("header").css({ 'z-index': '100' });
        $("#recipe-finder").animate({
            'right': finish,
            'padding-left': '10px',
            'width': '906px'
        }, { duration: time, easing: 'easeOutExpo'
        });
    });

    $('#recipe-finder a.close').live('click', function(){
        $("header").css({'z-index':'auto'});
        $("#recipe-finder").animate({
            'right':start,
            'padding-left':'0px',
            'width':'916px'
        }, {duration:time, easing:'easeOutExpo',complete:function(){
            $("#toppane .container").css({'z-index':'1'});
            $("#toppane #sidebar.home").css({'z-index':'6'});
            }
        });
    });

    //$('#recipe-finder a.close').click(function () { window.location.href = "http://dev.chefs.com" });

    // ACCORDION WIDGET
    $("#accordion3").msAccordion({
        //defaultid:0, 
        //autodelay:8, 
        vertical: true
    });


    // NAVIGATION
    $("#dnn_menu .NavXpRoot ul li ul").addClass("subnav");
    $("#dnn_menu .NavXpRoot ul li ul div.child_container ul").removeClass("subnav");
    $("#dnn_menu .NavXpRoot ul li ul div.child_container ul").addClass("childnav");

    $("#dnn_menu .NavXpRoot ul li ul").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav

    $("#dnn_menu .NavXpRoot ul li a").hover(function () { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function () {
        }, function () {
            $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        });

        //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function () {
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function () {	//On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });


    $("#dnn_menu .NavXpRoot ul li ul.subnav a").hover(function () { //When trigger is clicked...
        $("#dnn_menu .NavXpRoot ul li ul.subnav li div.child_container").css({ display: 'block' });
        $("#dnn_menu .NavXpRoot ul li ul.subnav li ul.childnav li div.child_container").css({ display: 'none' });
        var ulWidth = $('#dnn_menu .NavXpRoot ul li ul.subnav').css('width');
        $("#dnn_menu .NavXpRoot ul li ul.subnav li div.child_container ul").css({ 'left': ulWidth });

        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.childnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function () {
        }, function () {
            $(this).parent().find("ul.childnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        });

        //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function () {
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function () {	//On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

    $("#dnn_menu .NavXpRoot ul li ul.subnav li ul.childnav li a").hover(function () { //When trigger is clicked...
        $("#dnn_menu .NavXpRoot ul li ul.subnav li ul.childnav li div.child_container").css({ display: 'block' });
        var ulWidth = $('#dnn_menu .NavXpRoot ul li ul.subnav li ul.childnav li').css('width');
        $("#dnn_menu .NavXpRoot ul li ul.subnav li ul.childnav li div.child_container ul").css({ 'left': ulWidth });

        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.childnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function () {
        }, function () {
            $(this).parent().find("ul.childnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        });

        //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function () {
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function () {	//On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });


});

// RECIPES POPUP
document.getElementsByTagName('body')[0].className+=' js';
$(document).ready(function () {
    buildGallery();

    var $loading = $("img[alt=ProgressBar]").parent();
    $loading.addClass("loading");

    $("#dnn_recipeSearch .result:last").addClass("result-last");
    $(".tag:last").addClass("tag-last");
    $(".primaryTag:last").addClass("primaryTag-last");

    $("ul#sort").live('click', function (e) {
        var $dropdown = $(this).find("ul.sort-dropdown");
        $dropdown.toggleClass("recipe-overlay");
        hideOverlays($dropdown);
        positionLoader($("div.results table:first"), $loading);
    });

    $("ul#searchMode select").live('change', function (e) {
        positionLoader($("div.results table:first"), $loading);
    });

    $('.target').change(function () {
        alert('Handler for .change() called.');
    });

    //Clicking anywhere on the body of the page will do a test to see if any open menus need to be closes
    $("body").click(function (e) {
        if (!$(e.target).hasClass("recipe-parent") && !$(e.target).parentsUntil("recipe-parent").hasClass("recipe-parent") && !$(e.target).hasClass("filter-one") && !$(e.target).parentsUntil("recipe-parent").hasClass("filter-one")) {
            $(".recipe-overlay").removeClass("recipe-overlay");
            $("li.filter-one").removeClass("overlay");
            $("header nav").removeClass("overlay");
        }
    });

    //This is the dropdown for the search types in the search bar 
    $("ul.search-type li.search-label").click(function (e) {
        var $dropdown = $(this).children("ul#search-select");
        $dropdown.toggleClass("recipe-overlay");
        hideOverlays($dropdown);

        $("ul#search-select li").click(function () {
            $(this).find("input").attr("checked", true);
            $("li.search-label span.label-text").text($(this).find("label").text());
        });


    });

    //This is the left-side filter menu
    $("li.filter-one").click(function (e) {
        $(this).toggleClass("overlay");
        $("header nav").toggleClass("overlay");
        var $dropdown = $(this).children("ul");
        $dropdown.toggleClass("recipe-overlay").removeAttr("style");
        var offset = $dropdown.offset();
        var position = $(this).position();

        if ((offset.top + 350) > ($(window).height() + $(window).scrollTop())) {
            var cssObj = {
                'top': position.top - 322,
                'left': position.left
            }
            $dropdown.css(cssObj);
            //$dropdown.offset({top: , left: offset.left});
        } else {
            var cssObj = {
                'top': position.top + 29,
                'left': position.left
            }
            $dropdown.css(cssObj);

        }
        hideOverlays($dropdown);
    });

    $("li.filter-one a").not("li.filter-one ul a").click(function (e) {
        e.preventDefault();
    });


    //Handles the dropdown for the review areas
    $(".actions a.review").live('click', function (e) {
        e.preventDefault();
        var $dropdown = $(this).next();
        $dropdown.toggleClass("recipe-overlay");
        hideOverlays($dropdown);
    });
    //Handles the dropdown for the review areas
    $(".actions a.save").live('click', function (e) {
        if (!$(this).attr('disabled')) {
            var $dropdown = $(this).next();
            $dropdown.toggleClass("recipe-overlay");
            hideOverlays($dropdown);
        }
    });

    $(".review .write a").live('click', function (e) {
        e.preventDefault();
        var $dropdown = $(this).next();
        $dropdown.toggleClass("recipe-overlay");
        hideOverlays($dropdown);
    });

    //Handles the star clicks on the reviews
    $(".review .star, .review-box .star").click(function () {
        $(this).parent().find(".star").removeClass("active");
        $(this).prevUntil("label").addClass("active");
        $(this).addClass("active");
        //Add star click function here
        var rating = $(this).prevUntil("label").length + 1;
//        document.getElementById('dnn_ctr1108_nRecipeReview_hdnRating').value = rating;
//        document.getElementById('dnn_ctr1106_nRecipeDetails_hdnRating').value = rating;
//        document.getElementById('dnn_ctr1108_nRecipeReview_hdnRating2').value = rating;
        document.getElementById('dnn_ctr1126_nRecipeDetails_hdnRating').value = rating;
        document.getElementById('dnn_ctr1131_nRecipeReview_hdnRating').value = rating;
    });


    //Handles the hover actions for the star reviews.
    $(".review .star, div.review-box .star").hover(
  	  function () {
  	      $(this).prevUntil("label").addClass("hov");
  	      $(this).addClass("hov");
  	  },
  	  function () {
  	      $(this).prevUntil("label").removeClass("hov");
  	      $(this).removeClass("hov");
  	  }
  	);



    $("ul#search-select input").change(function () {
        if ($("li.site-search input").is(":checked")) {
            $("li.search-label span.label-text").text("Site Search");
        } else {
            $("li.search-label span.label-text").text("Recipe Search");
        }
    });

    $("ul#search-select input").change(function () {
        if ($("li.site-search input").is(":checked")) {
            $("li.search-label span.label-text").text("Site Search");
        } else {
            $("li.search-label span.label-text").text("Recipe Search");
        }
    });



    //Opens up the Flagging interface on the Recipe Details pages
    $("li.flag a").click(function (e) {
        $("li.flag .problem,li.flag h5").show();
        $("li.flag .thankyou").hide();
        $("li.flag textarea").val("");
        e.preventDefault();
        var $dropdown = $(this).next();
        $dropdown.toggleClass("recipe-overlay");
        hideOverlays($dropdown);
    });


    $('#imgNav li').live('click', function () {
        $this = $(this);
        if (!$this.hasClass("active")) {
            $('#imgNav li').removeClass("active");
            var thisClass = $this.attr("class").replace(" active", "");
            $(".images .active").removeClass("active").fadeOut("fast", function () {
                $(".images li." + thisClass).fadeIn("fast").addClass("active");
                $("#imgNav li." + thisClass).addClass("active");
            });
        }
    });
    //Expands the tag area on the add recipe page
    $(".tag h3, .tagList .tag h3").live('click', function () {
        $(this).toggleClass("active");
        var $slider = $(this).parent().find("ul")
        $slider.slideToggle('fast');
    });

    $("#close").live('click', function () {
        $(".recipe-overlay").removeClass("recipe-overlay");
        $("li.filter-one").removeClass("overlay");
        $("header nav").removeClass("overlay");
    });


});
 
 function hideOverlays(e){
   	$(".recipe-overlay").not(e).removeClass("recipe-overlay");
  }
function positionLoader(obj, loading){
 	var $results = obj.position();
	var cssObj = {
	      'left' : $results.left + 'px',
	      'top' : $results.top +  'px'
	 }
	loading.width(obj.width()).height(obj.height()).css(cssObj);
	
	cssObj = {
	      'left' : ($results.left+(obj.width()/2)-50) + 'px',
	      'top' : ($(window).height())/2 +  'px'
	      
	 }
  	loading.find("img").css(cssObj);
 } 
 
 function buildGallery(){
  	var parent = $("div.aside div.image ul");
 	var imgHeight = 0;
 	var imgCount = 1;
 	var imgNav = "";
 	
 		$("div.aside div.image ul li").each(function(){
   			$this = $(this);
   			if ($this.height() > imgHeight){
 				imgHeight = $this.height();
 			}
 			$this.addClass("img-"+imgCount);
   			if(imgCount == 1){
   				$this.addClass("active");
   				imgNav += "<li class='img-"+imgCount+" active'><span>"+imgCount+"</span></li>"
   				
   			}else{
   				imgNav += "<li class='img-"+imgCount+"'><span>"+imgCount+"</span></li>"
   			}
 			imgCount++;
 		});
 		
 		$("div.aside .image ul li").height(imgHeight);
 		
 		$("div.aside .image ul li img").each(function(){
 			$(this).css('top', (imgHeight-2-$(this).height())/2);
 		});
  	if(imgCount > 2){	
	 	imgNav = "<ul id='imgNav' class='imgNav' style='top: "+(imgHeight-30)+"px'>" + imgNav + "</ul>"	
	 	parent.height(imgHeight);	
	 	$("div.aside div.image").append(imgNav);
	 	$("div.aside .images li").not("div.aside .images li.active").hide();
 	}
   }
  
 
