﻿ProductDetails_OnResponseEnd = function(sender, arguments) {
    trace('ProductDetails_OnResponseEnd');

    try {
        if (typeof initialiseFonts != 'undefined') {
            initialiseFonts();
        }
    } catch (e) {
        trace(e);
    }

    var hasErrors = checkForException(); trace(hasErrors);
};

ProductDetails_OnResponseStart = function(sender, arguments) {
    try {
        var eventTarget = arguments.EventTarget; trace(typeof eventTarget); trace(eventTarget);
        if (typeof eventTarget == 'string') {
            if (/lbAddToCart/i.test(eventTarget)) { addToCart(); }
            if (/btnAddToWishList/i.test(eventTarget)) { addToWishlist(); }
        }
    } catch (e) {
        trace(e);
    }
};

roundUpToMultiplier = function(i, factor) {
    return i + (factor - (i % factor));
};

var customUnorderedListScrollClass = "ul.custom-ul-scroll";
var customScrollingDuration = 750;
showMoreImages = function(up) {
    // make sure there are more images;
    if ($(customUnorderedListScrollClass + ' li:visible').length <= 6) return;

    var sectionHeight = (roundUpToMultiplier($(customUnorderedListScrollClass + ' li:visible').length, 3) * $(customUnorderedListScrollClass + ' li:visible').outerHeight()) / 3;
    var currentTopMargin = parseInt($(customUnorderedListScrollClass).css('marginTop')); trace(currentTopMargin);
    if (up) {
        trace('scrolling up...');
        if (currentTopMargin >= 0) {
            $(customUnorderedListScrollClass).animate({
                marginTop: '0px'
            },
            customScrollingDuration,
            'swing');
        } else {
            // actual scrolling
            var newTopMargin = (currentTopMargin + ($(customUnorderedListScrollClass + ' li:visible').outerHeight() * 2)) + 'px';
            trace('scrolling up by {0}'.format(newTopMargin));
            $(customUnorderedListScrollClass).animate({
                marginTop: newTopMargin
            },
            customScrollingDuration,
            'swing');
        }
    } else {
        trace('scrolling down...');
        if ((currentTopMargin * -1) >= sectionHeight - ($(customUnorderedListScrollClass + ' li:visible').outerHeight() * 2)) { return; }
        else {
            // actual scrolling
            var newTopMargin = (currentTopMargin - ($(customUnorderedListScrollClass + ' li:visible').outerHeight() * 2)) + 'px';
            trace('scrolling down by {0}'.format(newTopMargin));
            $(customUnorderedListScrollClass).animate({
                marginTop: newTopMargin
            },
            customScrollingDuration, 
            'swing');
        }
    }
};

addToWishlist = function() {
    trace('addToWishlist');
    $('#Header_MyCart_hlWishlist').effect('highlight', {}, 1000);
};

addToCart = function() {
    trace('addToCart');

    // find the big product image and move it to somewhere
    // jquery ui animation transfer -> puff
    var CART_ID = '#mycart-pulsate';
    var PRODUCT_IMG_ID = '#zoom1';
    var CLONED_PRODUCT_IMG_ID = generateUniqueElementId(PRODUCT_IMG_ID);

    var IMG_WIDTH = 270;
    var IMG_ANIMATE_DURATION = 1500;
    var IMG_DISAPPEAR_DURATION = 500;
    var CART_PULSATE_DURATION = 60;

    try {
        trace('animating...');

        $('#zoom1 > img')
        .clone()
        .attr('id', CLONED_PRODUCT_IMG_ID)
        .css({
            'visibility': 'visible',
            'display': 'block',
            'position': 'absolute',
            'left': $(PRODUCT_IMG_ID).offset().left + 'px',
            'top': $(PRODUCT_IMG_ID).offset().top + 'px',
            'z-index': '99999'
        })
        .appendTo('body')
        .animate({
            'left': '+=' + (($(PRODUCT_IMG_ID).offset().left) - $(CART_ID).offset().left) * -1 + 'px',
            'top': '-=' + ($(PRODUCT_IMG_ID).offset().top - $(CART_ID).offset().top) + 'px',
            'width': '0%',
            'height': '0%'
        }, IMG_ANIMATE_DURATION, 'swing', function() {
            $(this).remove();
            $(CART_ID).css({
                'display': 'block',
                'position': 'absolute',
                'left': ($(CART_ID).offset().left - $('#header').offset().left) + 'px',
                'top': $(CART_ID).offset().top + 'px',
                'z-index': '99999'
            }).effect('shake', { times: 5, direction: 'up', distance: 5 }, CART_PULSATE_DURATION);
        });

        //        trace((($(PRODUCT_IMG_ID).offset().left + IMG_WIDTH) - $(CART_ID).offset().left) * -1 + 'px');
        //        trace($(PRODUCT_IMG_ID).offset().left + IMG_WIDTH);
        //        trace($(CART_ID).offset().left);
        //        trace('----');
        //        trace($(PRODUCT_IMG_ID).offset().top);
        //        trace($(CART_ID).offset().top);
        //        trace(($(PRODUCT_IMG_ID).offset().top - $(CART_ID).offset().top) + 'px');
    } catch (e) {
        trace(e);
    }
};

generateUniqueElementId = function(id) {
    if (typeof id != 'string') throw 'argument specified is wrong: id';
    return id + Math.floor(Math.random() * 99999);
};

loadTabs = function() {
    // ie
    var isIE = $.browser.msie;
    //hide second tab
    $("div.tabs-a-two").css("display", "none");
    $(".tabs-a ul li.item").bind('click', function(e) {
        switch (this.id) {
            case "tabs-a-one":
                //change status & style menu
                $("#tabs-a-one").addClass("active");
                $("#tabs-a-two").removeClass("active");
                //display selected division, hide others
                $(".tabs-a-one").fadeIn();
                $(".tabs-a-two").css("display", "none");
                break;
            case "tabs-a-two":
                //change status & style menu
                $("#tabs-a-one").removeClass("active");
                $("#tabs-a-two").addClass("active");
                //display selected division, hide others
                $(".tabs-a-two").fadeIn();
                $(".tabs-a-one").css("display", "none");
                break;
        }
        return false;
    });
}

$(function() {
    // load tabs
    loadTabs();

    if ($(customUnorderedListScrollClass + ' li:visible').length <= 6) { $('#more-images-control').hide(); }

    // vertical scrollables
    $('.scrollable').scrollable({
        size: 3,
        vertical: true,
        clickable: false,
        loop: true
    });

    checkForException();
});