﻿initStarRating = function(selector, captionSelector) {
    try {

        $(selector).children().not(":radio").hide();
        $(selector).stars({
            captionEl: $(captionSelector),
            oneVoteOnly: true
        });
    } catch (e) {
        trace(e);
    }


};

ProductDetails_OnResponseEnd = function(sender, arguments) {
    trace('ProductDetails_OnResponseEnd');

    try {
        if (typeof initialiseFonts != 'undefined') {
            initialiseFonts();
        }
        if (typeof initialiseOverlays != 'undefined') {
            initialiseOverlays();
        }

        initStarRating('#ratings', '#stars-cap');

        // the star rating is typically hidden after the control has posted...Show elements...
        $('div.ui-stars-star').show();
        
    } 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 (/btnAddToCart/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 = 300;
initialiseImageScrolling = function() {
    var 

    // constants that define micro-structure of scrollable images
    IMAGE_CONTROL_CLASS = '.show-more-images',
    IMAGE_CONTROL_SCROLL_UP = 'up';

    $(IMAGE_CONTROL_CLASS).bind('click', function(event) {
        var 

        $this = $(this),

        /* the scrolling part */
        $SCROLLER = $(customUnorderedListScrollClass),

        queued = $SCROLLER.queue('fx').length > 0;

        if (queued) {
            return false;
        } else {
            var 

            /* it's either up or down */
            SCROLL_UP = $this.hasClass(IMAGE_CONTROL_SCROLL_UP),

            /* top margin of the images */
            TOP_MARGIN = parseInt($SCROLLER.css('marginTop')),
            SECTION_HEIGHT = (roundUpToMultiplier($SCROLLER.find('li:visible').length, 3) * $SCROLLER.find('li:visible').outerHeight()) / 3;

            if (SCROLL_UP) {
                if (TOP_MARGIN >= 0) {
                    return false;
                } else {
                    TOP_MARGIN = (TOP_MARGIN + ($SCROLLER.find('li:visible').outerHeight() * 2)) + 'px';
                }
            } else {
                if ((TOP_MARGIN * -1) >= SECTION_HEIGHT - ($SCROLLER.find('li:visible').outerHeight() * 2)) {
                    return false;
                } else {
                    TOP_MARGIN = (TOP_MARGIN - ($SCROLLER.find('li:visible').outerHeight() * 2)) + 'px';
                }
            }

            /* finally animate */
            $SCROLLER.animate({
                marginTop: TOP_MARGIN
            }, customScrollingDuration, 'swing');

            return false;
        }
    });
};

initialiseRating = function() {
    try {
        rating = parseInt($('#ratings').attr('data-init'));
        if (rating > 0) {
            $('.ui-stars-star').each(function(index) {

                if (index == rating)
                    return false;
                else {
                    //alert(index);
                    $(this).addClass('ui-stars-star-on');
                }

            });
        }

    } catch (e) {
        trace(e);
    }
};


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).effect('shake', { times: 5, direction: 'up', distance: 5 }, CART_PULSATE_DURATION);
            initialisefadeTabs();
        });
    } 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;
    });
    //size guide tab
    //hide second tab and third tab
    $("div.tabs-b-two").css("display", "none");
    $("div.tabs-b-three").css("display", "none");
    $(".tabs-a ul li.item").bind('click', function(e) {
        switch (this.id) {
            case "tabs-b-one":
                //change status &amp; style menu
                $("#tabs-b-one").addClass("active");
                $("#tabs-b-two").removeClass("active");
                $("#tabs-b-three").removeClass("active");
                //display selected division, hide others
                $(".tabs-b-one").fadeIn();
                $(".tabs-b-two").css("display", "none");
                $(".tabs-b-three").css("display", "none");
                break;
            case "tabs-b-two":
                //change status &amp; style menu
                $("#tabs-b-one").removeClass("active");
                $("#tabs-b-two").addClass("active");
                $("#tabs-b-three").removeClass("active");
                //display selected division, hide others
                $(".tabs-b-two").fadeIn();
                $(".tabs-b-one").css("display", "none");
                $(".tabs-b-three").css("display", "none");
                break;
            case "tabs-b-three":
                //change status &amp; style menu
                $("#tabs-b-one").removeClass("active");
                $("#tabs-b-two").removeClass("active");
                $("#tabs-b-three").addClass("active");
                //display selected division, hide others
                $(".tabs-b-three").fadeIn();
                $(".tabs-b-one").css("display", "none");
                $(".tabs-b-two").css("display", "none");
                break;
        }
        return false;
    });
}


scrollImages = function(data) {
    var settings;
    this.direction = { up: 1, down: 2, left: 3, right: 4 },
    this.defaultSettings = {
        className: 'ul.custom-scroll',
        scrollDuration: 750,
        visibleItemCount: 5,
        scrollItemCount: 5
    },
    this.getSettings = function(d, c) {
        if (c == null)
            return d;
        d.className = (c.className != null ? c.className : d.className);
        d.scrollDuration = (c.scrollDuration != null ? c.scrollDuration : d.scrollDuration);
        d.visibleItemCount = (c.visibleItemCount != null ? c.visibleItemCount : d.visibleItemCount);
        d.scrollItemCount = (c.scrollItemCount != null ? c.scrollItemCount : d.scrollItemCount);
        return d;
    },
    this.init = function() {
        settings = this.getSettings(this.defaultSettings, data);
    },
    this.scroll = function(dir) {
        if (!settings) { this.init(); }
        if ($(settings.className + ' li:visible').length <= settings.visibleItemCount) return;

        var sectionHeight = $(settings.className + ' li').length * $(settings.className + ' li:visible').outerHeight();
        var currentTopMargin = parseInt($(settings.className).css('marginTop')); trace(currentTopMargin);
        var sectionWidth = $(settings.className + ' li').length * $(settings.className + ' li:visible').outerWidth();
        var currentLeftMargin = parseInt($(settings.className).css('marginLeft')); trace(currentLeftMargin);
        var newTopMargin, newLeftMargin;

        switch (dir) {
            case this.direction.up:
                trace('scrolling up...');
                if (currentTopMargin >= 0) {
                    $(settings.className).animate({
                        marginTop: '0px'
                    },
                    settings.scrollDuration,
                    'swing');
                } else {
                    // actual scrolling
                    newTopMargin = (currentTopMargin + ($(settings.className + ' li:visible').outerHeight() * settings.scrollItemCount)) + 'px';
                    trace('scrolling up by {0}'.format(newTopMargin));
                    $(settings.className).animate({
                        marginTop: newTopMargin
                    },
                    settings.scrollDuration,
                    'swing');
                }
                break;
            case this.direction.down:
                trace('scrolling down...');
                if ((currentTopMargin * -1) >= sectionHeight - ($(settings.className + ' li:visible').outerHeight() * settings.scrollItemCount)) { return; }
                else {
                    // actual scrolling
                    newTopMargin = (currentTopMargin - ($(settings.className + ' li:visible').outerHeight() * settings.scrollItemCount)) + 'px';
                    trace('scrolling down by {0}'.format(newTopMargin));
                    $(settings.className).animate({
                        marginTop: newTopMargin
                    },
                    settings.scrollDuration,
                    'swing');
                }
                break;
            case this.direction.left:
                trace('scrolling left...');
                if (currentLeftMargin >= 0) {
                    $(settings.className).animate({
                        marginLeft: '0px'
                    },
                    settings.scrollDuration,
                    'swing');
                } else {
                    // actual scrolling
                    newLeftMargin = (currentLeftMargin + ($(settings.className + ' li:visible').outerWidth() * settings.scrollItemCount)) + 'px';
                    trace('scrolling left by {0}'.format(newLeftMargin));
                    $(settings.className).animate({
                        marginLeft: newLeftMargin
                    },
                    settings.scrollDuration,
                    'swing');
                }
                break;
            case this.direction.right:
                trace('scrolling right...');

                if ((currentLeftMargin * -1) >= sectionWidth - ($(settings.className + ' li:visible').outerWidth() * settings.scrollItemCount)) { return; }
                else {
                    // actual scrolling
                    newLeftMargin = (currentLeftMargin - ($(settings.className + ' li:visible').outerWidth() * settings.scrollItemCount)) + 'px';
                    trace('scrolling right by {0}'.format(newLeftMargin));
                    $(settings.className).animate({
                        marginLeft: newLeftMargin
                    },
                    settings.scrollDuration,
                    'swing');
                }
                break;
        }
    }
}

$(function() {
    
    

    // check query string and play the video
    if (/pv=1/i.test(window.location.hash)) {
        $('[data-video=true]').trigger('click');
    }
    
   

    // vertical scrollables
    $('.scrollable').scrollable({
        size: 3,
        vertical: true,
        clickable: false,
        loop: true
    });

    initStarRating('#ratings', '#stars-cap');
    initialiseRating();

    checkForException();
});

$(window).load(function() {
    
    if ($(customUnorderedListScrollClass + ' li:visible').length <= 6) {
        $('#more-images-control').hide();
    } else {
        initialiseImageScrolling();
    }
    // load tabs
    loadTabs();

});
