﻿var firstRun = false;

function ProductClick(imgId) {

    BindProduct(imgId);         
}

function BindProduct(imgId) {

    $.ajax({
        type: "POST",
        url: "/ClientServices/ProductJsonMethods.aspx/GetProduct",
        data: "{productId:" + imgId + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            BindProductDetail(msg.d);
        }
    });
}

function BindProductDetail(product) {

    $("#detail").fadeOut('slow', function() {

        //animation complete, bind product

        $("#image").css("background-image", "url(" + product.ImageLarge.replace('~', '') + ")");

        $("#details h1#title").html(product.Name);
        $("#details h1#title").css("color", "#" + product.ForeColor);

        $("#details h2#subtitle").html(product.ProductCategory.CategoryName);
        $("#details div#desc").html(product.Description);

        $("#details a#facts-img").css("color", "#" + product.ForeColor);

        $("div#caption h5").html(product.ImageCaption);

        Cufon.replace('#details h1', { fontFamily: 'Device', textShadow: '-1.8px -1.8px #ffd24f' });
        Cufon.replace('#details h2', { fontFamily: 'Device' });
        Cufon.replace('div#caption h5', { fontFamily: 'Device' });

        //set facebook like iframe
        var fbUrl = "http://"  + window.location.hostname + "/snacks/" + product.ProductCategory.CategoryName.replace(' ', '-').toLowerCase() + "/" + product.SeoPageId + ".aspx";
        var fbIframe = "<iframe id='facebook' name='facebook' src='http://www.facebook.com/plugins/like.php?href={0}&layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=35' scrolling='no' frameborder='0' style='border: none; overflow: hidden; width: 300px; height: 35px;'></iframe>";

        //nutrition
        if (product.ImageNutritionFacts != null) {
            $("a#facts-img").attr('href', product.ImageNutritionFacts.replace('~', '')).fancybox();
            $("#nut").show();
        } else {
            $("#nut").hide();
        }

        if (firstRun == false) {

            $('#detail').slideDown({
                duration: 1000,
                easing: 'easeOutBounce'
            }).show();

        } else {

            $("#detail").fadeIn('slow');

            document.title = product.Name + " - " + product.ProductCategory.CategoryName;
            $('meta[name=Description]').attr("content", product.Name + " - " + $('meta[name=Description]').attr("content"));
        }

        $("#facebook_iframe").html(fbIframe.replace("{0}", fbUrl));

        if (firstRun == false) firstRun = true;
    });

}

$(document).ready(function() {
    $(document).pngFix();
}); 