﻿$(document).ready(function()
{

    //PNG fix 
    if ($.browser.msie && $.browser.version == '6.0')
    {
        jqPngFix();
    }


    // logo click
    //**************************************************
    var shouldRedirectToHome = false;

    // redirect to webmail
    $(".sel-logo-img").dblclick(function()
    {
        shouldRedirectToHome = false;
    });

    $(".sel-logo-img").click(function()
    {
        // make sure i'm still waiting
        if (!shouldRedirectToHome)
        {
            shouldRedirectToHome = true;
            setTimeout(redirectToMail, 300);
        }

    });

    function redirectToMail()
    {
        if (shouldRedirectToHome)
            window.location.href = $(".sel-logo-img").attr("data-url");
        else
            window.location.href = "http://webmail.cergis.com";

        // reset
        shouldRedirectToHome = false;

    }
    //**************************************************


    // home page rotator: font end image fading
    //**************************************************
    $('#front-page-container').cycle(
    {
        fx: 'fade',
        timeout: 8000,
        random: 1
    });
    //**************************************************


    // selecting a size when shopping
    //**************************************************
    $(".sel_prod_item").click(function()
    {
        // set the hidden field to the selected value
        $("[id$=hidProductItem_ID]").val($(this).attr("data-itemid"));

        // toggle the css classes
        $(".sel_prod_item").removeClass("linkbutton_catalogue_selected");
        $(this).addClass("linkbutton_catalogue_selected");

        // cancel event for redirecting

    });

    $(".sel_prod_item").each(function()
    {
        if ($(this).attr("data-itemid") == $("[id$=hidProductItem_ID]").val())
        {
            $(this).addClass("linkbutton_catalogue_selected");
        }
    })
    //**************************************************

    // add to basket
    //**************************************************
    $(".sel-add-to-basket").click(function()
    {

        if ($("[id$=hidProductItem_ID]").val() <= 0)
        {
            $(".sel-add-err").html("You must select a size");
            return false;
        }

        if (isNaN($(".sel-add-qty").val()))
        {
            $(".sel-add-err").html("Quantity must be a number");
            return false;
        }

        // process the webservice method using jquery
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "/Services/BexterwearService.asmx/AddToBasket",
            data: "{iProductItem_ID:" + $("[id$=hidProductItem_ID]").val() + ",iQty:" + $(".sel-add-qty").val() + "}",
            success: function(result) { document.location = result.d; },
            error: function(err) { alert("Error: " + err.responseText); }
        });

    });
    //**************************************************


    // product images
    //**************************************************
    $(".sel-product-img").click(function()
    {
        fadeImage($(".sel-product-img-main"), "/image.aspx?w=320&h=320&q=100&i=" + $(this).attr("title"));
    });
    //**************************************************


    // images in product group list
    //**************************************************
    $(".sel-catalogue-img").click(function()
    {
        var imgname = $(this).attr("data-image-name");
        var imgid = $(this).attr("data-image-id");

        $(".sel-catalogue-popup-img-con").html("");

        $find("programmaticModalPopupBehavior").show();

        // fisrt set the source to nothing and then assign the true value
        // $(".sel-catalogue-popup-img").attr("src", "");
        $(".sel-catalogue-popup-img").css("display", "none");
        fadeImage($(".sel-catalogue-popup-img"), "/image.aspx?i=" + imgname + "&w=320&h=320&q=100");

        // process the webservice method using jquery
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "/Services/BexterwearService.asmx/ListProductImages",
            data: "{iProductID:" + imgid + "}",
            success: loadProductImages_success,
            error: loadProductImages_error
        });

    });

    function loadProductImages_success(result)
    {
        var imgResult = "";
        $.each(result.d, function(intIndex, objValue)
        {
            imgResult = imgResult + "<img style='cursor:pointer;' data-image-name='" + objValue + "' class='sel-catalogue-popup-images' alt='product image' src='/image.aspx?w=100&h=100&q=100&i=" + objValue + "' />";
        });
        $(".sel-catalogue-popup-img-con").html(imgResult);

        $(".sel-catalogue-popup-images").click(function()
        {
            fadeImage($(".sel-catalogue-popup-img"), "/image.aspx?i=" + $(this).attr("data-image-name") + "&w=320&h=320&q=100");
        });
    }

    function loadProductImages_error(err)
    {
        alert(err.responseText);
    }
    //**************************************************


    // cache menu images
    //**************************************************
    $("img.sel-menu-img").each(function()
    {
        var img = new Image();
        img.src = $(this).attr('data-src');
    });
    $("img.sel-menu-img").each(el_fnFixPng2);
    //**************************************************


    // switch text image on hover
    //**************************************************
    $("div.sel-home-prod-group").hover(SwitchImg, SwitchImg);
    $("img.sel-menu-img").hover(SwitchImgMenu, SwitchImgMenu);
    function SwitchImg()
    {
        DoSwitch($("img.sel-menu-img",this));
    }
    function SwitchImgMenu()
    {
        DoSwitch($(this));
    }

    function DoSwitch(img)
    {
        // test if we are i.e. 6
        if ($.browser.msie && $.browser.version == '6.0')
        {
            $(img).css("width", $(img).width() + "px");
            $(img).css("height", $(img).height() + "px");

            if ($(img).data("src-cur") == 1)
            {
                $(img).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + escape($(img).data("src-type2")) + "', sizingMethod='scale')");
                $(img).data("src-cur", 2);
            }
            else
            {
                $(img).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + escape($(img).data("src-type1")) + "', sizingMethod='scale')");
                $(img).data("src-cur", 1);
            }
        }
        else
        {
            var src, data

            src = img.attr('src');
            data = img.attr('data-src');

            img.attr('src', data);
            img.attr('data-src', src);
        }
    }
    //**************************************************

    // Payment Processing
    //**************************************************
    $(".sel-button-payment").click(function()
    {
        $(this).val("Processing...");
        $(this).attr("disabled", "true");
        $(".sel-button-payment1").trigger('click');
    });
    //**************************************************


});


/** Basket Functionality **/
function fadeImage(elem, src)
{
    $(elem).fadeOut(0, function()
    {
        $(elem).attr("src", src);
        $(elem).fadeIn(0);
    });
}

var shim = 'png-fix/blank.gif';

function jqPngFix() {
    try {
        //ie6 png transparency fix
        $.each($("img[src$=.png],img[src$=.PNG]"),
        function() 
        {
            var img = $(this);
            img.css({ "width": img.width(), "height": img.height(), "filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.attr("src") + "', sizingMethod='scale')" });
            img.attr("src", shim);
        });
    } catch (e) {
        alert(e.description)
    }
}

function el_fnFixPng2() 
{
    if ($.browser.msie && $.browser.version == '6.0')
    {
        var src = $(this).attr("src");
        $(this).css("width", $(this).width() + "px");
        $(this).css("height", $(this).height() + "px");
        $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + escape(src) + "', sizingMethod='scale')");

        $(this).data("src-cur", 1);
        $(this).data("src-type1", $(this).attr("src"));
        $(this).data("src-type2", $(this).attr('data-src'));

        $(this).attr("src", shim);
    }
};


function DisableButton(b)
{
//  b.disabled = true;
//  b.value = 'Processing Payment';
//  var obj = document.getElementById('ctl00_conTop_contentControl_212_btnSubmit1');
//  obj.click();
}


 
