﻿var vote;

$(document).ready(function () {
    if (screen.width >= 481) {
        if ($("div#recipe-search").length) {
            CreateRecipeSearch();
        }
        if ($("#recipe-search-landing").length) {
            CreateRecipeSearch();
        }

        if ($("div#homepage-rotator").length) {
            CreateRecipeSlideShow();
        }

        if ($(".popup-controls").length) {
            InitPopupControls();
        }

        $('a[rel$="popup"]').click(function () {
            window.open($(this).attr('href'), 'popup', 'width=512,height=550, scrollbars=yes');
            return false;
        });

        if ($('div#recipe-content').length) {
            InitRecipePageContent();
        }

        if ($('#products-landing-page').length) {
            InitProductSlideshow();
        }

        if ($('.where-to-buy-default').length) {
            InitWhereToBuyMap();
        }

        if ($('#hqm-media-player').length) {
            InitHQMMediaPlayer();
        }

        if ($('#single-video-player').length) {
            InitSingleVideoPlayer();
        }

        if ($('a[rel$="jobconfirm"]').length) {
            ViewJobsPopup();
        }

        if ($('#ck-scroll-pane').length) {
            ChefsCornerRecipeScrollPane();
        }
    }

});

function ChefsCornerRecipeScrollPane() {
    $('#ck-scroll-pane').jScrollPane({
        showArrows : true,
        verticalDragMinHeight: 7,
        verticalDragMaxHeight: 7,
        hijackInternalLinks: true
    });
}

function ViewJobsPopup()
{
    $('a[rel$="jobconfirm"]').click(function() {
        
        var c = confirm("You have requested to view our Job Openings Site. Press OK to continue or cancel to remain on this site.");

        if (c) {
            window.open($(this).attr('href'), 'popup', 'width=1024,height=800,scrollbars=1, resizable=1');
            return false;
        }
        else {
            return false;
        }


    });
}

function InitSingleVideoPlayer() {
    if ($('.about-press-press-10show-05-08').length) {
        NBCShowMay2008();
    }
    else if ($('.about-press-press-10show-09-08').length)
    {
        NBCShowSept2008();
    }
    else if ($('.about-press-press-10show-12-08').length)
    {
        NBCShowDec2008();
    }
}

function NBCShowMay2008() {
    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    params.menu = "false";
    params.allowScriptAccess = "always";
    var attributes = {};
    swfobject.embedSWF("/assets/flash/10!Hatfield-5.23.08.swf", "single-video-player-content", "320", "280", "10.0.0", "false", flashvars, params, attributes);
    swfobject.createCSS("single-video-player-content", "outline:none");
}
function NBCShowSept2008() {
    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    params.menu = "false";
    params.allowScriptAccess = "always";
    var attributes = {};
    swfobject.embedSWF("/assets/flash/10!Hatfield-9-26-08.swf", "single-video-player-content", "320", "280", "10.0.0", "false", flashvars, params, attributes);
    swfobject.createCSS("single-video-player-content", "outline:none");

}
function NBCShowDec2008() {
    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    params.menu = "false";
    params.allowScriptAccess = "always";
    var attributes = {};
    swfobject.embedSWF("/assets/flash/10!hatfield-12.18.08.swf", "single-video-player-content", "320", "280", "10.0.0", "false", flashvars, params, attributes);
    swfobject.createCSS("single-video-player-content", "outline:none");

}

function InitHQMMediaPlayer() {
    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    params.menu = "false";
    params.allowScriptAccess = "always";
    var attributes = {};
    swfobject.embedSWF("/assets/flash/HQMMediaPlayer.swf", "hqm-media-player-content", "700", "400", "10.0.0", "false", flashvars, params, attributes);
    swfobject.createCSS("hqm-media-player-content", "outline:none");

}

function InitWhereToBuyMap() {

    $('#NorthEast area').click(function() {
        $('#where-to-buy-image').removeClass('w-t-b-i-' + $(this).attr('id') + "");
        $('#r-b-rows a[href$=' + $(this).attr('href') + ']').removeClass('active');
    });

    $('#r-b-rows a').click(function() {
        var id = $('#NorthEast area[href$=' + $(this).attr('href') + ']').attr('id');
        $('#where-to-buy-image').removeClass("w-t-b-i-" + id + "");
    });

    $('#NorthEast area').hover(
        function() {
            $('#where-to-buy-image').addClass("w-t-b-i-" + $(this).attr('id') + "");
                $('#r-b-rows a[href$=' + $(this).attr('href') + ']').addClass('active');
        },
        function() {
            $('#where-to-buy-image').removeClass('w-t-b-i-' + $(this).attr('id') + "");
            $('#r-b-rows a[href$=' + $(this).attr('href') + ']').removeClass('active');
        }
    );
        $('#r-b-rows a').hover(
            function() {
                var id = $('#NorthEast area[href$=' + $(this).attr('href') + ']').attr('id');
                $('#where-to-buy-image').addClass("w-t-b-i-" + id + "");
            },
            function() {
                var id = $('#NorthEast area[href$=' + $(this).attr('href') + ']').attr('id');
                $('#where-to-buy-image').removeClass("w-t-b-i-" + id + "");
            }
        );
}

function InitProductSlideshow() {
    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    params.menu = "false";
    params.allowScriptAccess = "always";
    var attributes = {};
    swfobject.embedSWF("/assets/flash/HQMProductCategories.swf", "subpage-content-navigation", "950", "572", "10.0.0", "false", flashvars, params, attributes);
    swfobject.createCSS("subpage-content-navigation", "outline:none");
}

function InitRecipePageContent() {
    $('div#reviews').hide();
    $('div#nutrition').hide();

    $('a.read-reviews-link').click(function() {
        if ($('#reviews').is(':visible')) {
            return false;
        }
        else {
            $('div.recipe-content-panel:visible').hide();
            $('#reviews').show();
            $('#recipe-content-navigation a.active').removeClass('active');
            $('.r-c-n-reviews a').addClass('active');
            return false;
        }
    });

    $('#nutritional-information-link a').click(function() {
        if ($('#nutrition').is(':visible')) {
            location.href = location.href + "#nutrition";
        }
        else {
            $('div.recipe-content-panel:visible').hide();
            $('#nutrition').show();
            $('#recipe-content-navigation a.active').removeClass('active');
            $('.r-c-n-nutrition a').addClass('active');
            return false;
        }
    });
    
    $('span.review-this-recipe-button').click(function() {
        if ($('#reviews').is(':visible')) {
            location.href = location.href + "#leave-your-review";
        }
        else {
            $('div.recipe-content-panel:visible').hide();
            $('#reviews').show();
            $('#recipe-content-navigation a.active').removeClass('active');
            $('.r-c-n-reviews a').addClass('active');
            location.href = location.href + "#leave-your-review";
        }
    });

    $('div#recipe-content-navigation a').click(function() {
        clicked = $(this).attr('href');
        if ($(clicked).is(':visible')) {
            return false;
        }
        else {
            $('div.recipe-content-panel:visible').hide();
            $(clicked).show();
            $('#recipe-content-navigation a.active').removeClass('active');
            $(this).addClass('active');
            return false;
        }

    });

    $(".star-image").mouseover(function() {
        var span = $(this).parent("span");
        var newRating = $(this).attr("value");
        setRating(span, newRating);
    });
    $(".star-image").mouseout(function() {
        var span = $(this).parent("span");
        var rating = span.attr("rating");
        setRating(span, rating);
    });

    $(".star-image").click(function() {

        var span = $(this).parent("span");
        var NewRating = $(this).attr("value");
        var RecipeID = span.attr("recipe");
        var UserID = span.attr("post");
        var text = span.children("span");
        vote = NewRating;
        
        $.ajax({
            type: "POST",
            url: "vote.aspx/Vote",
            contentType: "application/json; charset=utf-8",
            data: "{recipeID: " + RecipeID + ", rating: " + NewRating + "}",
            dataType: "json",
            success: VoteSuccess,
            error: VoteFailed
        });

    });

      $("#favorite-recipe a").click(function() {

          var temp = new Array();
          temp = $(this).attr('href').split("RecipeID=");
          var XRecipeID = temp[temp.length - 1];
          
          $.ajax({
              type: "POST",
              url: "favorite.aspx/Favorite",
              contentType: "application/json; charset=utf-8",
              data: "{recipeIDString: " + XRecipeID + "}",
              dataType: "json",
              success: AjaxSucceededFavoriteRecipe,
              error: AjaxFailed
          });

          return false;

      });

      $("#remove-favorite-recipe a").click(function() {

          var temp = new Array();
          temp = $(this).attr('href').split("RecipeID=");
          var XRecipeID = temp[temp.length - 1];
          
          $.ajax({
              type: "POST",
              url: "remove-favorite.aspx/UnFavorite",
              contentType: "application/json; charset=utf-8",
              data: "{recipeIDString: " + XRecipeID + "}",
              dataType: "json",
              success: AjaxSucceededRemoveFavoriteRecipe,
              error: AjaxFailed
          });

          return false;

      });

  }

  function VoteSuccess(result) {
      $(".star-image").unbind('mouseover');
      $(".star-image").unbind('mouseout');
      $(".star-image").unbind('click');
      $(".star-image").css('cursor', 'default');
      $(".rate-this-recipe-link").replaceWith('<span class="rate-this-recipe-link">Thanks for rating this recipe</span>');
      setRating($('.rating'), vote);
  }

  function VoteFailed(result) {
      $(".rate-this-recipe-link").replaceWith('<span class="rate-this-recipe-link">Sorry, there was an error rating this recipe</span>');
  }
  
  function AjaxSucceededFavoriteRecipe(result) {
      $('#favorite-recipe').hide();
      $('#remove-favorite-recipe').show();
  }

  function AjaxSucceededRemoveFavoriteRecipe(result) {
      $('#favorite-recipe').show();
      $('#remove-favorite-recipe').hide();
  }

  function AjaxSucceeded(result) {
      alert(result.d);
  }
  function AjaxFailed(result) {
      alert(result.status + ' ' + result.statusText + ' ' + result.responseText);
  }  


function setRating(span, rating) {
    span.find('img').each(function() {
        var value = parseFloat($(this).attr("value"));
        var imgSrc = $(this).attr("src");
        if (value <= rating) {
            $(this).attr("src", imgSrc.replace("-off.png", "-on.png"));
        }
        else {
            $(this).attr("src", imgSrc.replace("-on.png", "-off.png"));
        }

    });
}


function InitPopupControls() {
    $('.print-window').click(function() {
        window.print();
    });

    $('.close-window').click(function() {
        window.close(); 
    });
}

function CreateRecipeSearch() {
    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    params.menu = "false";
    params.allowScriptAccess = "always";
    var attributes = {};
    swfobject.embedSWF("/assets/flash/HQMRecipeBox.swf", "recipe-search-content", "950", "551", "10.0.0", "false", flashvars, params, attributes);
    swfobject.createCSS("recipe-search-content", "outline:none");
}
function CreateRecipeSlideShow() {
    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    params.menu = "false";
    params.allowScriptAccess = "always";
    var attributes = {};
    swfobject.embedSWF("/assets/flash/HQMHomepage.swf", "homepage-rotator-content", "702", "402", "10.0.0", "false", flashvars, params, attributes);
    swfobject.createCSS("homepage-rotator-content", "outline:none");
}

function recipeExpand() {
    $("div#recipe-search").css("width", "950px");
    $("div#recipe-search").css("height", "551px");
    $("div#recipe-search").css("top", "0");
    if($('#recipe-search-landing').length) {
        $("div#recipe-search-landing").css("width", "950px");
        $("div#recipe-search-landing").css("height", "551px");
        $("div#recipe-search-landing").css("top", "0");
        $("div#recipe-search-landing").css("left", "16px");
    }
    $("div#recipe-search-content-box").css("top", 0);
    showModal();

}
function recipeShrink() {
    $("div#recipe-search").css("width", "379px");
    $("div#recipe-search").css("height", "250px");
    $("div#recipe-search").css("top", "300px");
    $("div#recipe-search-content-box").css("top", "-300px");
    if ($('#recipe-search-landing').length) {
        $("div#recipe-search-landing").css("width", "379px");
        $("div#recipe-search-landing").css("height", "250px");
        $("div#recipe-search-landing").css("top", "150px");
        $("div#recipe-search-landing").css("left", "30px");
    }
    hideModal();
}

function showModal() {
    $("div#recipe-modal").show();
//    if ($('#homepage-rotator').length) {
//        $('#homepage-rotator').hide();
//    }
}
function hideModal() {
    $("div#recipe-modal").hide();
//    if ($('#homepage-rotator').length) {
//        $('#homepage-rotator').show();
//    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

function MM_openBrWindow(theURL, winName, features) { //v2.0
    window.open(theURL, winName, features);
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}


function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}



