﻿var HSA_default_imagesPath = "/cmsglobalfiles/asics/images";

function clearReviewInputs()
{
    $('.text-field:eq(0) input').val("YOUR NAME");
    $('.text-field:eq(1) input').val("YOUR EMAIL (will not be published)");
    $('.text-field:eq(2) input').val("SUMMARY");
    $('.textarea-field textarea').val("REVIEW");
    $('input:text, textarea').each(function() {
        var _el = $(this);
        _el.data('val', _el.val());

        _el.bind('focus', function() {
            if (_el.val() == _el.data('val')) _el.val('');
        }).bind('blur', function() {
            if (_el.val() == '') _el.val(_el.data('val'));
        });
    });
}

function initReviews(){
	var _speed = 500; //in ms
	var _hold = $('#reviews');
	if(_hold.length){
		var _btn = _hold.find('a.btn-review');
		_btn.click(function() {
		    $('.img-ajax-loader').addClass('hidden');	
		    var _box = _hold.find('div.add-review-hold');
		    var _h = _box.outerHeight();
		    var _sbox = $('div.add-review-success[rel]');
		    if (_sbox.length) { //reinit all controls
		        initRating();		       
		        $('div.reviews input:radio').customRadio();
		        $('div.reviews select').customSelect({ maxHeight: 197 });
		        clearReviewInputs();
		        _hold.removeClass('opened');
		        _sbox.removeAttr('rel');
		        _sbox.hide();
		    }
		    if (_hold.hasClass('opened')) _box.show();
		    else _box.hide();
		    if (_hold.hasClass('opened')) {
		        _hold.removeClass('opened');
		        _box.stop().animate({ height: 0 }, _speed, function() { _box.css({ display: 'none', height: 'auto' }); });
		    }
		    else {
		        _hold.addClass('opened');
		        if (_box.is(':hidden')) {
		            _box.css({ height: 0, display: 'block' });
		            $('div.reviews input:radio').customRadio();
		            $('div.reviews select').customSelect({ maxHeight: 197 });
		            $('div.progress-el').customSlider();
		            _h = _box.children().outerHeight(true);
		        }
		        _box.stop().animate({ height: _h }, _speed, function() {
		            _box.height('auto');
		            $('body').scrollTo(_box.offset().top, _speed);
		        });
		    }
		    if ($('.btnSubmitAddReview').length) {
		        //var prm = Sys.WebForms.PageRequestManager.getInstance();
		        //prm.add_pageLoaded(initReviews);
		        $('.btnSubmitAddReview').click(function(event) {
		            $('.img-ajax-loader').removeClass('hidden');
		            if (!ValidateReviewForms()) {
		                $('.img-ajax-loader').addClass('hidden');
		                return false;
		            }
		            else
		                TrackAddProductReview();
		        });
		    }
		    return false;
		});
	}
}

/*--- customised rating ---*/
function initRating() {
    var t_w = 31;
    $('div.rating-holder').each(function() {
        var _hold = $(this);
        var _list = _hold.find('ul.star-rating > li');
        var info_hold = _hold.find('p.stars-col');
        var _info = _hold.find('input.rating-info');
        _info.removeAttr('value');
        var _a = parseInt(_info.val());
        _list.removeClass('active');
        info_hold.html('');
        if (!isNaN(_a)) {
            _list.eq(_a - 1).addClass('active');
            if (_a == 1) info_hold.html('<strong>1</strong> star');
            else info_hold.html('<strong>' + _a + '</strong> stars');
            info_hold.css('marginLeft', t_w * (_a - 1));
        }
        else {
            _a = -1;
            info_hold.html('');
        }
        _list.mouseenter(function() {
            if (_a != -1) _list.eq(_a - 1).removeClass('active');
            $(this).addClass('active');
            if (_list.index(this) == 0) info_hold.html('<strong>1</strong> star');
            else info_hold.html('<strong>' + (_list.index(this) + 1) + '</strong> stars');
            info_hold.css('marginLeft', t_w * _list.index(this));
        }).mouseleave(function() {
            $(this).removeClass('active');
            if (_a != -1) {
                _list.eq(_a - 1).addClass('active');
                if (_a == 1) info_hold.html('<strong>1</strong> star');
                else info_hold.html('<strong>' + _a + '</strong> stars');
                info_hold.css('marginLeft', t_w * _list.index(this));
            }
            else {
                info_hold.html('');
            }
        }).click(function() {
            _a = _list.index(this) + 1;
            _info.val(_a);
            return false;
        });
    });
}

function scrollToSuccessPanel()
{
    var _speed = 500; //in ms
    var _sbox = $('div.add-review-success[rel]');
    if(_sbox.length)
    {
        $('body').scrollTo($('#reviews').offset().top, _speed);
    }
}

/*--- review info ---*/
function reviewInfo(){
	$('.review-info').each(function(){
		var _hold = $(this);
		var btn_yes = _hold.find('a.yes');
		var btn_no = _hold.find('a.no');
		var box_yes = _hold.find('.yes-info');
		var box_no = _hold.find('.no-info');
		/*$.ajax({
			type: 'GET',
			data: '&yes=0&no=0',
			url: btn_yes.attr('href'),
			success: function(_text){
				box_yes.text(parseInt(_text.split('yes=')[1]));
				box_no.text(parseInt(_text.split('no=')[1]));
			}
		});*/
		btn_yes.click(function(){
		    if($(this).hasClass('disabled'))
		        return false;
		        
		    box_yes.text((parseInt(box_yes.text())+1));			
			$.ajax({
				type: 'GET',
				data: '&yes=1&no=0',
				url: this.href,
				success: function(_text){					
					btn_yes.addClass('disabled');
					btn_no.addClass('disabled');
				}
			});
			return false;
		});
		btn_no.click(function(){
		    if($(this).hasClass('disabled'))
		        return false;
		      
			box_no.text((parseInt(box_no.text())+1));
			$.ajax({
				type: 'GET',
				data: '&yes=0&no=1',
				url: this.href,
				success: function(_text){
					
					btn_yes.addClass('disabled');
					btn_no.addClass('disabled');
				}
			});
			return false;
		});
	});
}
/*---- popups ---*/
function initPopups() {
    if ($('#fader').length == 0) $('body').append('<div id="fader"></div>');
    var _fader = $('#fader');
    _fader.hide();
    var popup_f = false;
    var _popup = $('<div class="popup"><a href="#" class="btn-close"><span>close</span></a><div class="c"><iframe align="left" frameborder="0" id="popupIframe" name="popupIframe">no iframe</iframe></div><div class="b">&nbsp;</div></div>');
    var popup_iframe = _popup.find('iframe');
    _popup.hide().appendTo('body');

    $('a.with-popup').live('click', function() {
        genPopup(this.href);
        if ($.browser.msie) $('div.product-pop-up').hide();
        else $('div.product-pop-up').animate({ opacity: 0 }, 200, function() { $(this).hide(); })
        return false;
    });
    _popup.find('a.btn-close').click(function() {
        closePopup();
        return false;
    });
    _fader.click(function() {
        closePopup();
        return false;
    });
    $(document).keydown(function(e) {
        if (!e) evt = window.event;
        if (e.keyCode == 27) closePopup();
    });

    /*--- generate popup ---*/
    function genPopup(_href) {
        var _url = '';
        var _width = 0;
        var _height = 0;
        var _class = '';
        if (_href.indexOf('?') != -1) _url = _href.split('?')[0];
        if (_href.indexOf('width=') != -1) _width = parseInt(_href.split('width=')[1].substr(0, (_href.split('width=')[1].indexOf('&') != -1) ? (_href.split('width=')[1].indexOf('&')) : (_href.split('width=')[1].length)));
        if (isNaN(_width)) _width = 777;
        if (_href.indexOf('height=') != -1) _height = parseInt(_href.split('height=')[1].substr(0, (_href.split('height=')[1].indexOf('&') != -1) ? (_href.split('height=')[1].indexOf('&')) : (_href.split('height=')[1].length)));
        if (isNaN(_height)) _height = 486;
        if (_href.indexOf('class=') != -1) _class = _href.split('class=')[1].substr(0, (_href.split('class=')[1].indexOf('&') != -1) ? (_href.split('class=')[1].indexOf('&')) : (_href.split('class=')[1].length));

        if (_url != '' && _width != 0 && _height != 0) {
            _popup.removeAttr('class').addClass('popup')
            if (_class != '') _popup.addClass(_class);
            popup_iframe.attr('width', _width).attr('height', _height).get(0).contentWindow.location.replace(_href);
            popup_f = true;
            showPopup();
        }
    }
    /*--- close popup ---*/
    function closePopup() {
        if (popup_f) {
            if ($.browser.msie) {
                _popup.hide();
                _fader.fadeOut(200);
                popup_f = false;
            }
            else {
                _popup.fadeOut(400, function() {
                    _fader.fadeOut(200);
                    popup_f = false;
                });
            }
            popup_iframe.get(0).contentWindow.location = '/cmsglobalfiles/asics/iframe/blank.html';
        }
    }
    /*--- show popup ---*/
    function showPopup() {
        _fader.css({
            opacity: 0,
            height: initH(),
            display: 'block'
        });
        _popup.css({ top: $(window).scrollTop() });
        _fader.fadeTo(400, 0.5, function() {
            if (popup_f) {
                if ($.browser.msie) _popup.show();
                else _popup.fadeIn(200, function() {
                    if (initH() < _popup.outerHeight() + _popup.offset().top) _fader.height(_popup.outerHeight() + _popup.offset().top);
                });
                _popup.css({ top: $(window).scrollTop() + ($(window).height() - _popup.outerHeight()) / 2 });
                if (initH() < _popup.outerHeight() + _popup.offset().top) _fader.height(_popup.outerHeight() + _popup.offset().top);
            }
        });
    }
    /*--- get height ---*/
    function initH() {
        var _h = $('#wrapper').outerHeight();
        if (_h < $(window).height()) _h = $(window).height();
        if (_h < $('body').height()) _h = $('body').height();
        return _h;
    }
}

var kitFunctions = {
    gall_f: false,
    _speed: 500,
    _hold: null,
    gall_hold: null,
    gall_info: null,
    list_hold: null,
    _list: null,
    btn_prev: null,
    btn_next: null,
    _info: null,
    _step: null,
    _a: 0,
    _last: false,
    num_inf: null,
    init: function() {
        var _this = this;
        _this._hold = $('div.top-box div.kit:eq(0)');
        if (_this._hold.length) {
            _this.list_hold = _this._hold.find('div.kit-gallery ul');
            _this.btn_prev = _this._hold.find('div.kit-gallery a.link-prev');
            _this.btn_next = _this._hold.find('div.kit-gallery a.link-next');
            _this._info = _this._hold.find('.gall-info');
            _this.num_inf = _this._hold.find('div.kit-items strong');

            if (_this.list_hold.length) {
                _this._hold.parent().removeClass('no-items');
                _this._list = _this.list_hold.children();
                _this._step = _this._list.outerWidth();
                _this._a = (_this._list.index(_this._list.filter('.active-el:eq(0)')) != -1) ? (_this._list.index(_this._list.filter('.active-el:eq(0)'))) : (0);

                _this._list.removeClass('active-el').eq(_this._a).addClass('active-el');
                if (_this._list.length == 1) _this._info.html('<strong>1</strong> item');
                else _this._info.html('<strong>' + (_this._a + 1) + '</strong> of <strong>' + (_this._list.length) + '</strong> of items');
                if (_this._a == 0) _this.btn_prev.hide();
                else _this.btn_prev.show();
                if (_this._a == _this._list.length - 1) _this.btn_next.hide();
                else _this.btn_next.show();

                if (_this._list.length < 10) _this.num_inf.text('0' + _this._list.length);
                else _this.num_inf.text(_this._list.length);
            }
            else {
                _this.num_inf.text('00');
                _this._hold.parent().addClass('no-items');
            }
            _this._hold.find('div.kit-items').click(function() {
                if (_this._hold.hasClass('active')) {
                    _this._hold.removeClass('active');
                }
                else {
                    _this._hold.addClass('active');
                    if (_this.list_hold.length) _this.gall();
                }
                return false;
            });
            $('body').bind('mousedown', function(e) {
                if (_this._hold.hasClass('active')) {
                    e = e || event;
                    var t = e.target || e.srcElement;
                    t = $(t);
                    if (t.parents('div.kit').length == 0 && (t.hasClass('btn-add-item') || t.parents('a.btn-add-item').length == 0)) {
                        _this._hold.removeClass('active');
                    }
                }
            });
            $('a.btn-add-item').click(function() {
                var _lnk = $(this);
                $.ajax({
                    url: this.href,
                    dataType: 'html',
                    success: function(_html) {
                        _this.update(_html);
                        _lnk.attr('href', _lnk.attr('rel')).unbind('click').find('span').text('VIEW IN MYKIT');
                    }
                });
                return false;
            });
            $('a.btn-add-item-flash').click(function() {
                var _lnk = $(this);
                $.ajax({
                    url: this.href,
                    dataType: 'html',
                    success: function(_html) {
                        _this.update(_html);
                    }
                });
                return false;
            });
        }
    },
    gall: function() {
        var _this = this;
        if (!_this.gall_f && _this._hold.find('div.kit-gallery').is(':visible')) {
            _this.gall_f = true;
            _this._step = _this._list.outerWidth();
            if (_this._last) {
                _this._a = (_this._list.length - 1);
                _this._list.removeClass('active-el').eq(_this._a).addClass('active-el');
                _this.list_hold.animate({ left: -_this._a * _this._step }, { queue: false, duration: _this._speed });
            }
           
            if (_this._list.length == 1) _this._info.html('<strong>1</strong> item');
            else _this._info.html('<strong>' + (_this._a + 1) + '</strong> of <strong>' + (_this._list.length) + '</strong> of items');
            if (_this._a == 0) _this.btn_prev.hide();
            else _this.btn_prev.show();
            if (_this._a == _this._list.length - 1) _this.btn_next.hide();
            else _this.btn_next.show();

            _this.btn_prev.click(function() {
                if (_this._a > 0) moveList(_this._a - 1);
                return false;
            });
            _this.btn_next.click(function() {
                if (_this._a < _this._list.length - 1) moveList(_this._a + 1);
                return false;
            });
            function moveList(_ind) {
                if (_ind != _this._a) {
                    if (_ind == 0) _this.btn_prev.hide();
                    else _this.btn_prev.show();
                    if (_ind == _this._list.length - 1) _this.btn_next.hide();
                    else _this.btn_next.show();
                    _this._info.html('<strong>' + (_ind + 1) + '</strong> of <strong>' + (_this._list.length) + '</strong> of items');
                    _this._list.eq(_this._a).removeClass('active-el');
                    _this._list.eq(_ind).addClass('active-el');
                    _this.list_hold.animate({ left: -_ind * _this._step }, { queue: false, duration: _this._speed });
                    _this._a = _ind;
                }
            }
        }
        else if (_this.gall_f && _this._hold.find('div.kit-gallery').is(':visible')) {
            _this._list = _this.list_hold.children();
            _this._info.html('<strong>' + (_this._a + 1) + '</strong> of <strong>' + (_this._list.length) + '</strong> of items');
            if (_this._a == 0) _this.btn_prev.hide();
            else _this.btn_prev.show();
            if (_this._a == _this._list.length - 1) _this.btn_next.hide();
            else _this.btn_next.show();
        }
    },
    update: function(_html) {
        var _this = this;
        if (_html.length > 0) {
            if (_this.list_hold.length == 0) {
                _this.list_hold = $('<ul></ul>');
                _this._hold.find('div.kit-gallery div.box').append(_this.list_hold);
                _this._hold.parent().removeClass('no-items');
                _this._a = 0;
            }
            _this.list_hold.append('<li>' + _html + '</li>');
            _this._list = _this.list_hold.children();
            if (_this._list.length < 10) _this.num_inf.text('0' + _this._list.length);
            else _this.num_inf.text(_this._list.length);
            _this._hold.addClass('active');
            _this._last = true;
            _this.gall();
        }
    },
    removeItem: function(_id) {
        var _this = this;
        $(".item-" + _id).remove();
        _this.list_hold = _this._hold.find('div.kit-gallery ul');
        _this.num_inf = _this._hold.find('div.kit-items strong');
        _this._list = _this.list_hold.children();
        _this.num_inf.text(_this._list.length);
        _this.list_hold.attr("style", "");
        _this.gall_f = false;
        _this._a = 0;
        if (_this._list.length == 0) {
            _this.num_inf.text("00");
            _this.list_hold.remove();
        }
    }
}

function initKitItems() {
    $('div.kit-item-list').each(function() {
        var _hold = $(this);
        var _num = _hold.find('.kit-numb');
        var _list = _hold.find('>div>ul > li');
        if (_list.length < 10) _num.text('0' + _list.length);
        else _num.text(_list.length);
        _list.each(function() {
            var _el = $(this);
            var btn_remove = _el.find('a.btn-remove');
            btn_remove.click(function() {
                var _href = this.href;
                $.ajax({
                    url: this.href,
                    success: function() {
                        _el.animate({ height: 0, opacity: 0 }, 300, function() {
                            $(this).remove();
                            _list = _hold.find('>div>ul > li');
                            if (_list.length < 10) _num.text('0' + _list.length);
                            else _num.text(_list.length);
                            if (_list.length == 0) _hold.find('>ul').remove();
                        });
                        var _id;
                        if (_href.indexOf('?') != -1) _url = _href.split('?')[0];
                        if (_href.indexOf('id=') != -1) _id = parseInt(_href.split('id=')[1].substr(0, (_href.split('id=')[1].indexOf('&') != -1) ? (_href.split('id=')[1].indexOf('&')) : (_href.split('id=')[1].length)));
                        //$(".item-" + _id).remove();
                        kitFunctions.removeItem(_id);
                        //kitFunctions.init.call(window);
                    }
                });
                return false;
            });
        });
    });
}


function prodPhoto() {
    var change_speed = 500; //in ms
    var img_hold = $('#product-photo');
    var _btn = $('#prog-photo-list a.btn-prod-photo');
    var _hero = $('#product-photo img.active');
    if (_btn.length && img_hold.length) {
        var _a = -1;
        if (_hero.length) {
            _btn.eq(0).data('img', $('<img src="' + _btn.eq(0).attr('href') + '" alt="' + _btn.eq(0).attr('rel') + '"/>'));
            _btn.eq(0).data('img').css('opacity', 0).appendTo(img_hold);
        }
        else {
            _a = (_btn.index(_btn.filter('.active:eq(0)')) != -1) ? (_btn.index(_btn.filter('.active:eq(0)'))) : (0);
            _btn.removeClass('active').eq(_a).addClass('active');
            _btn.eq(_a).data('img', $('<img src="' + _btn.eq(_a).attr('href') + '" alt="' + _btn.eq(_a).attr('rel') + '"/>'));
            _btn.eq(_a).data('img').addClass('active').css('opacity', 1);
            _btn.eq(_a).data('img').appendTo(img_hold);
            $('#product-photo img:eq(0)').remove();
        }
        _btn.click(function() {
            changeEl(_btn.index(this));
            return false;
        });
        $('a.btn-prod-photo img.inactive').parent().unbind('click').bind('click', function(event) {
            event.preventDefault();
        });
        $('a.btn-prod-photo img.inactive').remove();
        
        var fade_f = true;
        function changeEl(_ind) {
            if (fade_f && _ind != _a) {
                if (_a == -1) {
                    $('#product-photo img:eq(0)').removeClass('active').animate({ opacity: 0 }, { queue: false, duration: change_speed }).remove();
                    _a = 0;
                }
                fade_f = false;
                img_hold.stop().height(img_hold.height());
                _btn.eq(_a).removeClass('active');
                _btn.eq(_ind).addClass('active');
                _btn.eq(_a).data('img').removeClass('active').animate({ opacity: 0 }, { queue: false, duration: change_speed });
                if (_btn.eq(_ind).data('img')) {
                    _btn.eq(_ind).data('img').addClass('active').animate({ opacity: 1 }, { queue: false, duration: change_speed });
                    img_hold.animate({ height: _btn.eq(_ind).data('img').height() }, change_speed / 2, function() { $(this).height('auto'); });
                    _a = _ind;
                    fade_f = true;
                }
                else {
                    _btn.eq(_ind).data('img', $('<img />'));
                    _btn.eq(_ind).data('img').load(function() {
                        _btn.eq(_ind).data('img').addClass('active').animate({ opacity: 1 }, { queue: false, duration: change_speed });
                        img_hold.animate({ height: _btn.eq(_ind).data('img').height() }, change_speed / 2, function() { $(this).height('auto'); });
                        _a = _ind;
                        fade_f = true;
                    });
                    _btn.eq(_ind).data('img').css('opacity', 0);
                    img_hold.append(_btn.eq(_ind).data('img'));
                    _btn.eq(_ind).data('img').attr('src', _btn.eq(_ind).attr('href')).attr('alt', _btn.eq(_ind).attr('rel'));
                }
            }
        }
    }
}




function initProductRefine() {
    initTooltips();
    var _hold = $('li.opened');
    var _box = _hold.children('div.refine-popup');
    var _speed = 300;
    if (_box.length) {
        if (_hold.hasClass('opened')) {
            _hold.removeClass('opened');
            _box.stop().animate({ height: 0 }, _speed, function() { $(this).css({ display: 'none', height: 'auto' }); });
        }
        else {
            _hold.addClass('opened');
            if (_box.is(':hidden')) {
                _box.show();
                _h = _box.height();
                _box.height(0);
                $('div.refine-el').customSlider2();
            }
            _box.stop().animate({ height: _h }, _speed, function() { $(this).height('auto'); });
        }
    }
    $('.img-ajax-loader').addClass('hidden');
}

jQuery.fn.customSelect2 = function(_options) {
    var _options = jQuery.extend({
        selectStructure: '<div class="selectArea"><div class="left"></div><div class="center"></div><a href="#" class="selectButton">&nbsp;</a><div class="disabled"></div></div>',
        selectText: '.center',
        selectBtn: '.selectButton',
        selectDisabled: '.disabled',
        optStructure: '<div class="selectOptions"><div class="select-top"></div><div class="select-list"><ul></ul></div><div class="select-bottom"></div></div>',
        optList: 'ul',
        maxHeight: 99999
    }, _options);
    return this.each(function() {
        var select = jQuery(this);
        if (!select.hasClass('outtaHere')) {
            if (select.is(':visible')) {
                var replaced = jQuery(_options.selectStructure);
                var selectText = replaced.find(_options.selectText);
                var selectBtn = replaced.find(_options.selectBtn);
                var selectDisabled = replaced.find(_options.selectDisabled).hide();
                var optHolder = jQuery(_options.optStructure);
                var optList = optHolder.find(_options.optList);
                var maxH = _options.maxHeight;
                if (select.attr('disabled')) selectDisabled.show();
                select.find('option').each(function() {
                    var selOpt = $(this);
                    var _opt = jQuery('<li><a href="#"><span>' + selOpt.html() + '</span></a></li>');
                    if (selOpt.attr('selected')) {
                        selectText.html(selOpt.html());
                        _opt.addClass('selected');
                    }
                    _opt.children('a').click(function() {
                        optList.find('li').removeClass('selected');
                        select.find('option').removeAttr('selected');
                        $(this).parent().addClass('selected');
                        selOpt.attr('selected', 'selected');
                        selectText.html(selOpt.html());
                        select.change();
                        optHolder.hide();
                        replaced.removeClass('selectAreaActive');
                        if (typeof (select.change) == 'function') select.change();
                        if (typeof (select.get(0).onchange) == 'function') select.get(0).onchange();
                        return false;
                    });
                    optList.append(_opt);
                });
                replaced.width(_options.width);
                replaced.insertBefore(select);
                optHolder.css({
                    width: _options.width,
                    display: 'none',
                    position: 'absolute'
                });
                if (select.attr('class') && select.attr('class') != '')
                    optHolder.addClass('drop-' + select.attr('class').split(' ')[0]);
                jQuery(document.body).append(optHolder);

                var optTimer;
                replaced.hover(function() {
                    if (optTimer) clearTimeout(optTimer);
                }, function() {
                    optTimer = setTimeout(function() {
                        optHolder.hide();
                        replaced.removeClass('selectAreaActive');
                    }, 200);
                });
                optHolder.hover(function() {
                    if (optTimer) clearTimeout(optTimer);
                }, function() {
                    optTimer = setTimeout(function() {
                        optHolder.hide();
                        replaced.removeClass('selectAreaActive');
                    }, 200);
                });
                selectBtn.click(function() {
                    if (optHolder.is(':visible')) {
                        optHolder.hide();
                        replaced.removeClass('selectAreaActive');
                    }
                    else {
                        replaced.addClass('selectAreaActive');
                        optHolder.css({
                            top: replaced.offset().top + replaced.outerHeight(),
                            left: replaced.offset().left,
                            display: 'block'
                        });
                        if (optHolder.children('div.select-list').height() > maxH) optHolder.children('div.select-list').css({ height: maxH, overflow: 'auto' }).customScrollV();
                    }
                    return false;
                });
                select.addClass('outtaHere');
            }
        }
    });
}

function initKidsShoeRefine() {
    initTooltips();
    $('.img-ajax-loader').addClass('hidden');
}

function initProductReview() {
    $('.img-ajax-loader').addClass('hidden');
}

$(document).ready(function () {
    initGlobalAdPopup();

    $('.zoom a, .shoe360 a').click(function (event) {
        event.preventDefault();
        return false;
    });
    $('.read-reviews').click(function (event) {
        $('#tab1').css({ display: 'block' });
        $('#tab2').css({ display: 'none' });
        $('#tab3').css({ display: 'none' });
        $('.info-tabs li:eq(0)').addClass('active').children().addClass('active');
        $('.info-tabs li:eq(1)').removeClass('active').children().removeClass('active');
        $('.info-tabs li:eq(2)').removeClass('active').children().removeClass('active');
        $('body').scrollTo($('.tabset').offset().top, 400);
        event.preventDefault();
    });
    if ($('.btn-refine').length) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_pageLoaded(initProductRefine);
        $('.btn-refine').click(function (event) {
            $('.img-ajax-loader').removeClass('hidden');
        });
    }
    if ($('.refineKidsSport').length) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_pageLoaded(initKidsShoeRefine);
        var _select = $('.refineKidsSport');
        _select.customSelect2({ width: 220, maxHeight: 197 });
        _select.change(function () {
            $('.img-ajax-loader').removeClass('hidden');
        });
    }
    $('a.btn-view-comments').click(function () {
        $('body').scrollTo($('.comment-box').offset().top - 10, 400);
        return false;
    });
    if ($('.btnNewsletterSubmit').length) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_pageLoaded(initNewsletterSignup);
        $('.btnNewsletterSubmit').click(function (event) {
            $('.img-ajax-loader').removeClass('hidden');
        });
    }
    initGoogleTracking();
    initSendToPhone();
    prodPhoto();
});

function initSendToPhone()
{
    if(location.href.indexOf('sendtophone=1') > 0)
    {
        $('.options-btns .phone a.with-popup').click();
    }
}

function promoGallery() {
    var _speed = 500; //in ms
    var stay_time = 5000; //in ms or 'false' if not needed
    $('div.promo-gallery, div.shoes-promo').each(function() {
        var _hold = $(this);
        var list_hold = _hold.find('div ul').not('ul.events-promo-date');
        var _list = list_hold.children();
        var btn_prev = _hold.find('a.link-prev').empty();
        var prev_img = $('<span></span>').appendTo(btn_prev);
        var btn_next = _hold.find('a.link-next').empty();
        var next_img = $('<span></span>').appendTo(btn_next);
        var list_w = list_hold.children().length * list_hold.children().outerWidth();
        var hold_w = list_hold.parent().width();
        var _step = list_hold.children().outerWidth();
        var _a = (_list.index(_list.filter('.active:eq(0)')) != -1) ? (_list.index(_list.filter('.active:eq(0)'))) : (0);
        var _t, _f = true;

        var text_hold = _hold.find('ul.events-promo-date');
        if (text_hold.length) {
            var text_f = true;
            var text_list = text_hold.children();
            if ($.browser.msie) text_list.hide().removeClass('active').eq(_a).show().addClass('active');
            else text_list.css({ opacity: 0, display: 'none' }).removeClass('active').eq(_a).css({ opacity: 1, display: 'block' }).addClass('active');
        }

        list_hold.css('left', _a * _step);
        if (_list.length <= 1) {
            btn_prev.hide();
            btn_next.hide();
            stay_time = false;
        }
        else if (_a == 0) {
            temp_img = _list.eq(_list.length - 1).find('img').clone();
            temp_img.attr('src', temp_img.attr('src') + '?height=55&width=auto');
            prev_img.html(temp_img.attr('style', ''));
            temp_img = _list.eq(_a + 1).find('img').clone();
            temp_img.attr('src', temp_img.attr('src') + '?height=55&width=auto');
            next_img.html(temp_img.attr('style', ''));
        }
        else if (_a == _list.length - 1) {
            temp_img = _list.eq(0).find('img').clone();
            temp_img.attr('src', temp_img.attr('src') + '?height=55&width=auto');
            next_img.html(temp_img.attr('style', ''));
            temp_img = _list.eq(_a - 1).find('img').clone();
            temp_img.attr('src', temp_img.attr('src') + '?height=55&width=auto');
            prev_img.html(temp_img.attr('style', ''));
        }
        else {
            temp_img = _list.eq(_a - 1).find('img').clone();
            temp_img.attr('src', temp_img.attr('src') + '?height=55&width=auto');
            prev_img.html(temp_img.attr('style', ''));
            temp_img = _list.eq(_a + 1).find('img').clone();
            temp_img.attr('src', temp_img.attr('src') + '?height=55&width=auto');
            next_img.html(temp_img.attr('style', ''));
        }
        btn_prev.click(function() {
            if (_a > 0) moveList(_a - 1);
            else moveList(_list.length - 1);
            return false;
        });
        btn_next.click(function() {
            if (_a < _list.length - 1) moveList(_a + 1);
            else moveList(0);
            return false;
        });
        _hold.mouseenter(function() {
            _f = false;
            if (_t) clearTimeout(_t);
        }).mouseleave(function() {
            _f = true;
            if (_t) clearTimeout(_t);
            if (_f && stay_time) {
                _t = setTimeout(function() {
                    if (_a < _list.length - 1) moveList(_a + 1);
                    else moveList(0);
                }, stay_time);
            }
        });
        if (_f && stay_time) {
            _t = setTimeout(function() {
                if (_a < _list.length - 1) moveList(_a + 1);
                else moveList(0);
            }, stay_time);
        }
        function moveList(_ind) {
            if (_t) clearTimeout(_t);
            if (_ind != _a) {
                _list.eq(_a).removeClass('active');
                _list.eq(_ind).addClass('active');
                if (_ind == 0) {
                    temp_img = _list.eq(_list.length - 1).find('img').clone();
                }
                else {
                    temp_img = _list.eq(_ind - 1).find('img').clone();
                }
                temp_img.attr('src', temp_img.attr('src') + '?height=55&width=auto');
                prev_img.html(temp_img.attr('style', ''));
                if (_ind == _list.length - 1) {
                    temp_img = _list.eq(0).find('img').clone();
                }
                else {
                    temp_img = _list.eq(_ind + 1).find('img').clone();
                }
                temp_img.attr('src', temp_img.attr('src') + '?height=55&width=auto');
                next_img.html(temp_img.attr('style', ''));
                if (text_f) {
                    if ($.browser.msie) {
                        text_list.eq(_a).removeClass('active').hide();
                        text_list.eq(_ind).addClass('active').show();
                    }
                    else {
                        text_hold.stop().width(text_hold.width());
                        text_list.eq(_a).stop().removeClass('active').animate({ opacity: 0 }, _speed, function() { $(this).hide(); });
                        text_list.eq(_ind).stop().addClass('active').show().animate({ opacity: 1 }, _speed);
                        text_hold.animate({ width: text_list.eq(_ind).outerWidth() }, _speed, function() { $(this).width('auto'); });
                    }
                }
                list_hold.animate({ left: -_ind * _step }, { queue: false, duration: _speed });
                _a = _ind;
            }
            if (_f && stay_time) {
                _t = setTimeout(function() {
                    if (_a < _list.length - 1) moveList(_a + 1);
                    else moveList(0);
                }, stay_time + _speed);
            }
        }
    });
}

/*--- tabs ---*/
function initTabs(h_hold) {
    $(h_hold).each(function(_ind, _el) {
        var btn_h = $(_el);
        var _btn = $(_el).find('a.tab');
        var _a = 0;
        _btn.each(function(_ind, _el) {
            this._box = $('#' + _el.href.substr(_el.href.indexOf("#") + 1));
            if ($(_el).hasClass('active')) {
                this._box.css({ display: 'block' });
                _a = _ind;
            }
            else {
                this._box.css({ display: 'none' });
            }
            _el.onclick = function() {
                if (!$(this).hasClass('active')) {
                    _btn.get(_a)._box.css({ display: 'none' });
                    _btn.eq(_a).removeClass('active');
                    this._box.css({ display: 'block' });
                    $(this).addClass('active');
                    _a = _ind;
                    this._box.find(".hold").customScrollV();
                }
                return false;
            }
        });
    });
}

function clearInputs() {
    if ($('.newsletter-signup').length == 0) {
        $('input:text, input:password, textarea').each(function() {
            var _el = $(this);
            _el.data('val', _el.val());

            _el.bind('focus', function() {
                if (_el.val() == _el.data('val') && _el.attr('id') != 'ctl00_plcMain_cmscompletesearchdialog_srchDialog_txtSearchFor') _el.val('');
            }).bind('blur', function() {
                if (_el.val() == '') _el.val(_el.data('val'));
            });
        });
    }
}



function showSortAjaxLoader() {
    $('.img-ajax-loader2').removeClass('hidden');
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded(hideSortAjaxLoader);
}

function hideSortAjaxLoader() {
    $('.img-ajax-loader2').addClass('hidden');
}

function sendToPhone(param) {
    $('#sendToPhone').attr('href', param);
    $('#sendToPhone').click();
}

function addToMykit(param) {
    $('.btn-add-item-flash').attr('href', param);
    $('.btn-add-item-flash').click();
    $('body').scrollTo($('#wrapper').offset().top, 400)
}

function callGlobalAdPopup() {
    $('.global-ad-campaign').click();
}

function initGlobalAdPopup() {
    $('.global-ad-campaign').attr('href','/popup-pages/asics-global-advertising-campaign.aspx?width=680&height=486&class=global-ad');
}

function initGoogleTracking() {
    $('.kit-options .print').click(TrackPrintMyKit);
    $('.print .btn-print').click(TrackPrintGeneral);
    $('.btn-print-product').click(TrackPrintProduct);
    $('.add-comment .submit').click(TrackNewComment);   
    if (document.location.href.indexOf('pid') > 0) {
        $('.btnStoreSearch').click(TrackStoreFinderByProduct);
    }
    else {
        $('.btnStoreSearch').click(TrackStoreFinderGeneral);
    }
    if (document.location.href.indexOf('s=') > 0)
        $('.btnStoreSearch').click();
}

function TrackPrintMyKit() {
    _gaq.push(['_trackPageview', '/print/mykit']);
}

function TrackAddProductReview() {
    //alert('track');
    _gaq.push(['_trackPageview', '/product/review/new']);
}

function TrackStoreFinderByProduct() {
    _gaq.push(['_trackPageview', '/store-finder/product']);
}

function TrackStoreFinderGeneral() {
    _gaq.push(['_trackPageview', '/store-finder/general']);
}

function TrackPrintProduct() {
    _gaq.push(['_trackPageview', '/print/product']);
    window.print(false);
    return false;
}

function TrackPrintGeneral() {
    _gaq.push(['_trackPageview', '/print' + document.location.pathname]);
}

function TrackNewComment() {
    _gaq.push(['_trackPageview', '/comment/new']);
}

function TrackNewsletterSignup() {
    _gaq.push(['_trackPageview', '/newsletter-signup/success']);
    //alert('success');   
}

