﻿function HideMsgs() {
    return true;
}


$(document).ready(function() {

    function SetupPage() {

        // Lightbox - load only for non ie6 users
        if (typeof document.body.style.maxHeight === "undefined") {
            // do nothing
        } else {
            $("a[rel^='prettyPhoto']").prettyPhoto({
                animationSpeed: 'normal',
                padding: 20,
                opacity: 0.65,
                showTitle: false,
                allowresize: false,
                counter_separator_label: '/',
                theme: 'violetmaylondon'
            });
        }

        $(imgProductClientId).data('originalImg', $(imgProductClientId).attr('src'));

        $(ddlVariantsClientId).change();

        $(imgProductClientId).load(function() {
            $(this).animate({ opacity: 1 }, 'slow');
        });

    }

    // Information boxes
    function CreateInformationBoxes() {
        $("#info-box h3").hide();
        $("#info-box .tabnav").show();
        $("#info-box").tabs({ fx: { opacity: 'toggle'} });
    }

    // Switch Variant
    function switchVariant(mainImage, largeImage, variantId) {
        var currentGallery = $('#hdnCurrentGallery').val();

        $(imgProductClientId).stop().animate({ opacity: 0 }, 'fast', '', function() {
            $(currentGallery).animate({ opacity: 0 }, 'slow', '', function() {
                $(currentGallery).hide();
                $('#gallery' + variantId).css('opacity', '0').show().animate({ opacity: 1 }, 'slow');
            });

            $(imgProductClientId).attr('src', mainImage)
                                                .animate({ opacity: 1 }, 'fast')
                                                .data('originalImg', $(imgProductClientId).attr('src'));
            $('#hdnCurrentGallery').val('#gallery' + variantId);

            $('#pricing').animate({ opacity: 0 }, 'fast', '', function() {
                $.get('/Ajax/Product.ashx?type=price&variant=' + variantId, function(xml) {
                    $('#pricing').html('<span>' + xml + '</span>')
                                             .animate({ opacity: 1 }, 'fast');
                });
            });

            //                    $('#productTitle').animate({ opacity: 0 }, 'fast', '', function() {
            //                        $.get('/Ajax/Product.ashx?type=title&variant=' + variantId, function(xml) {
            //                            $('#productTitle').html(xml)
            //                                                  .animate({ opacity: 1 }, 'fast');
            //                        });

            //                    });

            $.get('/Ajax/Product.ashx?type=link&variant=' + variantId, function(xml) {
                $(mainImageLinkClientId).attr('href', xml);
                $(enlargeLinkClientId).attr('href', xml); ;
            });

            $('#information').animate({ opacity: 0 }, 'fast', '', function() {
                $.get('/Ajax/Product.ashx?type=boxes&variant=' + variantId, function(xml) {
                    $('#information').html(xml);
                    CreateInformationBoxes();
                    $('#information').animate({ opacity: 1 }, 'fast');
                });
            });

        });
    }

    CreateInformationBoxes();
    SetupPage();

    var prm = Sys.WebForms.PageRequestManager.getInstance();

    prm.add_endRequest(function() {
        CreateInformationBoxes();
        SetupPage();
        $(ddlVariantsClientId).change();
    });

    $('#imageGalleries a').live('click', function(e) {
        e.preventDefault();
        var link = this;
        $(imgProductClientId).stop('true').animate({ opacity: 0 }, 'fast', '', function() {
            $(this).attr('src', $(link).attr('rel'));
            $(mainImageLinkClientId).attr('href', $(link).attr('href'));
            $(enlargeLinkClientId).attr('href', $(link).attr('href'));
        });
    });

    $(ddlVariantsClientId).live('change', function() {
        //                  the variant id is the first group separated by ~
        var variantId = $(this).attr('value').split('~')[0];
        var mainImageRef = $('a[rel=#gallery' + variantId + '].pictureHref').attr('href');
        var enlargeRef = $('a[rel=#gallery' + variantId + '].enlargeHref').attr('href');
        switchVariant(mainImageRef, enlargeRef, variantId);
    });
    
    $('.option a').live('click', function() {
        //                  y is the last letter of gallery as in the gallery id. e.g. gallery56. therefore, this would split the two into
        //                  galler and 56.
        var variantId = $(this).attr('rel').split('y')[1];
        var mainImageRef = $('a[rel=#gallery' + variantId + '].pictureHref').attr('href');
        var enlargeRef = $('a[rel=#gallery' + variantId + '].enlargeHref').attr('href');
        switchVariant(mainImageRef, enlargeRef, variantId);

        $(ddlVariantsClientId + ' > option').attr('selected', '');

        $(ddlVariantsClientId + ' > option[value^=' + variantId + ']').attr('selected', 'true');

        return false;
    });

});
