﻿(function($) {
    function Gallery(el, options) {
        this._hold = $(el);
        this.initOptions(options);
        this._timer = options.autoRotation;
        this._t;
        this.initialize();
    }
    $.fn.gallery = function(options) {
        return new Gallery(this.get(0), options);
    };
    Gallery.prototype = {
        initOptions: function(_obj) {
            this.options = {
                duration: 700,
                slideElement: 1,
                autoRotation: false,
                effect: false,
                listOfSlides: 'ul > li',
                switcher: false,
                disableBtn: false,
                nextBtn: 'a.link-next, a.btn-next, a.next',
                prevBtn: 'a.link-prev, a.btn-prev, a.prev',
                circle: true,
                direction: false,
                directionBox: false,
                IE: false,
                textBox: false
            };
            for (key in _obj) this.options[key] = _obj[key];
        },
        initialize: function() {
            var _this = this;
            var _hold = _this._hold;
            var _speed = _this.options.duration;
            var _timer = _this.options.autoRotation;
            var _el = _hold.find(_this.options.listOfSlides);
            if (_this.options.effect) var _list = _el;
            else var _list = _el.parent();
            var _textBox = _hold.find(_this.options.textBox);
            var _switcher = _hold.find(_this.options.switcher);
            var _next = _hold.find(_this.options.nextBtn);
            var _prev = _hold.find(_this.options.prevBtn);
            var _count = _el.index(_el.filter(':last'));
            var _w = _el.outerWidth(true);
            var _h = _el.outerHeight(true);
            if (_this.options.switcher) var _active = _switcher.index(_switcher.filter('.active:eq(0)'));
            else var _active = _el.index(_el.filter('.active:eq(0)'));
            if (_active < 0) _active = 0;
            var _last = _active;
            var flag = true;
            var der = _this.options.directionBox;
            // Installation directions
            if (!_this.options.direction) {
                var _wrapHolderW = Math.ceil(_list.parent().width() / _w);
                if (((_wrapHolderW - 1) * _w + _w / 2) > _list.parent().width()) _wrapHolderW--;
            }
            else {
                var _wrapHolderW = Math.ceil(_list.parent().height() / _h);
                if (((_wrapHolderW - 1) * _h + _h / 2) > _list.parent().height()) _wrapHolderW--;
            }
            if (_textBox) {
                var typ = _textBox.outerHeight(true);
                if (!der) _textBox.css({ bottom: -typ });
                else _textBox.css({ top: -typ });
            }
            // Setting "fade" or "slide" effect
            if (!_this.options.effect) var rew = _count - _wrapHolderW + 1;
            else var rew = _count;
            if (!_this.options.effect) {
                if (!_this.options.direction) _list.css({ marginLeft: -(_w * _active) })
                else _list.css({ marginTop: -(_h * _active) })
            }
            else {
                _list.css({
                    opacity: 0
                }).removeClass('active').eq(_active).addClass('active').css({
                    opacity: 1
                }).css('opacity', 'auto');
                _switcher.removeClass('active').eq(_active).addClass('active');
                _list.parent().css({ height: _list.eq(_active).outerHeight(true) });
                if (!der) _textBox.eq(_active).css({ bottom: 0 });
                else _textBox.eq(_active).css({ top: 0 });
            }
            // Disable or enable buttons "prev next"
            if (_this.options.disableBtn) {
                if (_count < _wrapHolderW) _next.addClass(_this.options.disableBtn);
                _prev.addClass(_this.options.disableBtn);
            }
            // Function to "fade"
            function fadeElement() {
                if ($.browser.msie && _this.options.IE) {
                    _list.eq(_last).css({ opacity: 0 });
                    _list.removeClass('active').eq(_active).addClass('active').css({ opacity: 'auto' });
                }
                else {
                    _list.eq(_last).animate({ opacity: 0 }, _speed);
                    _list.removeClass('active').eq(_active).addClass('active').animate({
                        opacity: 1
                    }, _speed, function() {
                        if (_this.options.textBox) setTimeout(function() {
                            if (!der)
                                _textBox.eq(_active).animate({ bottom: 0 }, _speed, function() {
                                    _textBox.eq(_last).css({ bottom: -typ });
                                    _last = _active;
                                    flag = true;
                                });
                            else
                                _textBox.eq(_active).animate({ top: 0 }, _speed, function() {
                                    _textBox.eq(_last).css({ top: -typ });
                                    _last = _active;
                                    flag = true;
                                });
                        }, 500);
                        $(this).css('opacity', 'auto');
                    });
                }
                _list.parent().animate({ height: _list.eq(_active).outerHeight(true) }, _speed);
                if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
                if (!_this.options.textBox) _last = _active;
            }
            // Function for "slide"
            function scrollEl() {
                if (!_this.options.direction) _list.animate({ marginLeft: -(_w * _active) }, { queue: false, duration: _speed })
                else _list.animate({ marginTop: -(_h * _active) }, { queue: false, duration: _speed })
                if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
            }
            function toPrepare() {
                if ((_active == rew) && _this.options.circle) _active = -_this.options.slideElement;
                for (var i = 0; i < _this.options.slideElement; i++) {
                    _active++;
                    if (_active > rew) {
                        _active--;
                        if (_this.options.disableBtn && (_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
                    }
                };
                if (_active == rew) if (_this.options.disableBtn && (_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
                if (!_this.options.effect) scrollEl();
                else fadeElement();
            }
            _this._hold.bind('runTimer', function() {
                if (_this._t) clearTimeout(_this._t);
                _this._t = setInterval(function() {
                    if (flag) {
                        flag = false;
                        if (!_this.options.effect) flag = true;
                        if (!_this.options.textBox) flag = true;
                        toPrepare();
                    }
                }, _this._timer);
            });
            _next.click(function() {
                if (flag) {
                    flag = false;
                    if (!_this.options.effect) flag = true;
                    if (!_this.options.textBox) flag = true;
                    if (_this._t) clearTimeout(_this._t);
                    if (_this.options.disableBtn && (_count > _wrapHolderW)) _prev.removeClass(_this.options.disableBtn);
                    toPrepare();
                    if (_this._timer) _this._hold.trigger('runTimer');
                }
                return false;
            });
            _prev.click(function() {
                if (flag) {
                    flag = false;
                    if (!_this.options.effect) flag = true;
                    if (!_this.options.textBox) flag = true;
                    if (_this._t) clearTimeout(_this._t);
                    if (_this.options.disableBtn && (_count > _wrapHolderW)) _next.removeClass(_this.options.disableBtn);
                    if ((_active == 0) && _this.options.circle) _active = rew + _this.options.slideElement;
                    for (var i = 0; i < _this.options.slideElement; i++) {
                        _active--;
                        if (_active < 0) {
                            _active++;
                            if (_this.options.disableBtn && (_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
                        }
                    };
                    if (_active == 0) if (_this.options.disableBtn && (_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
                    if (!_this.options.effect) scrollEl();
                    else fadeElement();
                    if (_this._timer) _this._hold.trigger('runTimer');
                }
                return false;
            });
            if (_this.options.switcher) _switcher.click(function() {
                if (flag) {
                    flag = false;
                    if (!_this.options.effect) flag = true;
                    if (!_this.options.textBox) flag = true;
                    _active = _switcher.index($(this));
                    if (_this._t) clearTimeout(_this._t);
                    if (!_this.options.effect) scrollEl();
                    else fadeElement();
                    if (_this._timer) _this._hold.trigger('runTimer');
                }
                return false;
            });
            if (_this._timer) _this._hold.trigger('runTimer');
        },
        stop: function() {
            var _this = this;
            if (_this._t) clearTimeout(_this._t);
        },
        play: function() {
            var _this = this;
            if (_this._t) clearTimeout(_this._t);
            if (_this._timer) _this._hold.trigger('runTimer');
        }
    }
} (jQuery));
jQuery.fn.animationNav = function(_options) {
    // defaults options	
    var _options = jQuery.extend({
        duration: 500,
        addBG: '<li class="bg"><a href="#"><span>&nbsp;</span></a></li>',
        bgClass: 'bg',
        animEffect: 'swing'
    }, _options);
    return this.each(function() {
        var _obg = jQuery(this);
        var _liWidth = [];
        var _aWidth = [];
        var _lis = _obg.find('> li');
        var _links = _obg.find('> li > a');
        var _linkIndex = _links.index(_links.filter('.active'));
        var _timer = false;
        var _pLeft = parseInt(jQuery(_obg).css('paddingLeft'));
        var _visible = false;
        jQuery(_lis).filter(':last-child').css('background', 'none');
        _lis.each(function(i, el) {
            _liWidth[i] = jQuery(el).outerWidth(true);
            _aWidth[i] = jQuery(el).width();
        });
        jQuery(_obg).append(_options.addBG);
        jQuery.fn.animationNav.animateThis = function(_activeIndex, _obg) {
            var _activeLiWidth = _liWidth[_activeIndex];
            var _activeAWidth = _aWidth[_activeIndex];
            var _left = _pLeft;
            for (var j = _activeIndex - 1; j >= 0; j--) {
                _left += _liWidth[j];
            }
            $('li.bg', _obg).stop().animate({ width: _activeAWidth + 'px' }, { queue: false, duration: 150, easing: 'linear' }).animate({
                left: _left + 'px'
            }, { queue: false, duration: _options.duration, easing: _options.animEffect });
            _left = _pLeft;
        }
        jQuery.fn.animationNav.showThis = function(_activeIndex, _obg) {
            var _activeLiWidth = _liWidth[_activeIndex];
            var _activeAWidth = _aWidth[_activeIndex];
            var _left = _pLeft;
            for (var j = _activeIndex - 1; j >= 0; j--) {
                _left += _liWidth[j];
            }
            $('li.bg', _obg).css({
                left: (_left + (_activeAWidth / 2)) + 'px',
                width: 0,
                opacity: 0,
                display: 'block'
            }).animate({
                width: _activeAWidth + 'px',
                opacity: 1,
                left: _left + 'px'
            }, { queue: false, duration: _options.duration, easing: _options.animEffect });
            _left = _pLeft;
        }
        jQuery.fn.animationNav.hideThis = function(_activeIndex, _obg) {
            var _activeAWidth = _aWidth[_activeIndex];
            var _left = parseInt($('li.bg', _obg).css('left'));
            $('li.bg', _obg).fadeOut(200);
        }
        if (_lis.filter('.active').length) {
            var _lisIndex = _lis.index(_lis.filter('.active'));
            jQuery.fn.animationNav.animateThis(_lisIndex, _obg);
        } else {
            jQuery(_obg).children('.' + _options.bgClass).css('display', 'none');
        }
        _links.parent().hover(function() {
            _linkIndex = _links.parent().index(jQuery(this));
            if (_timer) clearTimeout(_timer);
            if (_lis.filter('.active').length || _visible) {
                jQuery.fn.animationNav.animateThis(_linkIndex, _obg);
            } else {
                jQuery.fn.animationNav.showThis(_linkIndex, _obg);
                _visible = true;
            }
        }, function() {
            var _this = this;
            _timer = setTimeout(function() {
                if (_lis.filter('.active').length) {
                    var _lisIndex = _lis.index(_lis.filter('.active'));
                    jQuery.fn.animationNav.animateThis(_lisIndex, _obg);
                } else {
                    var _linksIndex = _links.index($(_this));
                    jQuery.fn.animationNav.hideThis(_linksIndex, _obg);
                    _visible = false;
                }
            }, 200);
        });
    });
}
function initDrop() {
    $('div.header-wrapper').each(function() {
        var hold = $(this);
        var link = hold.find('#nav > ul > li:has("ul")');
        var ul = hold.find('#nav > ul');
        var ul2 = hold.find('#nav > ul > li > ul');
        var box = hold.find('> div.drop-bg');
        if (ul.length) var ulLeft = ul.offset().left;
        ul2.each(function(_i) {
            var k = link.eq(_i).offset().left - ulLeft;
            $(this).css({ left: -k - 373 });
            //yen
            $(this).children('li').children('a').css({ 'color': '#d7d3c7' });
            $(this).children('li').children('a').hover(function() { $(this).css('color', '#fff'); }, function() { $(this).css('color', '#d7d3c7'); });

            
        });
        if (link.hasClass('active')) {
            box.show();
        }
        hold.find('#nav > ul > li').mouseenter(function() {
            box.hide();
        }).mouseleave(function() {
            if (link.hasClass('active')) {
                box.show();
                
            }
        });
        link.hover(function() {
            box.show();
        }, function() {
            if (!link.hasClass('active')) {
                box.hide();
            }
        });
    });
}
function ieHover(h_list, h_class) {
    if ($.browser.msie && $.browser.version < 7) {
        if (!h_class) var h_class = 'hover';
        $(h_list).hover(function() {
            $(this).addClass(h_class);
        }, function() {
            $(this).removeClass(h_class);
        });
    }
}
// Open-Close plugin
jQuery.fn.OpenClose = function(_options) {
    // default options
    var _options = jQuery.extend({
        activeClass: 'active',
        opener: '.opener',
        slider: '.slide',
        slideSpeed: 400,
        animStart: false,
        animEnd: false,
        event: 'click'
    }, _options);
    return this.each(function() {
        // options
        var _holder = jQuery(this);
        var _slideSpeed = _options.slideSpeed;
        var _activeClass = _options.activeClass;
        var _opener = jQuery('>' + _options.opener, _holder);
        var _slider = jQuery('>' + _options.slider, _holder);
        var _animStart = _options.animStart;
        var _animEnd = _options.animEnd;
        var _event = _options.event;
        if (_slider.length) {
            if (_holder.hasClass(_activeClass)) _slider.show();
            else _slider.hide();
            _opener.bind(_event, function() {
                if (!_slider.is(':animated')) {
                    if (typeof _animStart === 'function') _animStart();
                    if (_holder.hasClass(_activeClass)) {
                        _holder.removeClass(_activeClass).find(_options.slider).slideUp(_slideSpeed, function() {
                            if (typeof _animEnd === 'function') _animEnd();
                        });
                    }
                    else {
                        _holder.addClass(_activeClass).find(_options.slider).slideDown(_slideSpeed, function() {
                            if (typeof _animEnd === 'function') _animEnd();
                        });
                    }
                }
                return false;
            });
        }
    });
}
function InitLightBox() {
    var _speed = 300;
    var _ieVersion = 10;
    var _opener = $('a.popup-opener');
    var _lay = $('<div class="overlay"></div>').appendTo($('body'));
    if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {
        _lay.css({ opacity: 0.7 }).hide();
    } else { _lay.css({ opacity: 0 }).hide(); }
    _opener.click(function() {
        var _light = $($(this).attr('href')).appendTo($('body'));
        var _body = $('body').height();
        Position();
        if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {
            _light.show();
            _lay.show();
        } else {
            _light.css({ opacity: 0 }).show().animate({ opacity: 1 }, { queue: false, duration: _speed });
            _lay.show().animate({ opacity: 0.7 }, { queue: false, duration: _speed });
        }
        _lay.click(function() { Close(); return false; });
        _light.find('a.btn-close').click(function() { Close(); return false; });
        function Close() {
            if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {
                _light.hide();
                _lay.hide();
            } else {
                _light.animate({ opacity: 0 }, { queue: false, duration: _speed, complete: function() { $(this).hide(); } });
                _lay.animate({ opacity: 0 }, { queue: false, duration: _speed, complete: function() { $(this).hide(); } });
            }
        }
        $(window).resize(function() { Position(); });
        $(window).scroll(function() { Position(); });
        function Position() {
            var _w = _light.outerWidth(true);
            var _h = _light.outerHeight(true);
            if (window.innerHeight) { var _wx = window.innerWidth; var _wy = window.innerHeight; }
            else { _wx = document.documentElement.clientWidth; _wy = document.documentElement.clientHeight; };
            if (_body < _wy) { _lay.css({ height: _wy }) }
            else { _lay.css({ height: _body }); };
            var _top = (_wy - _h) / 2 + $(document).scrollTop();
            if (_top + _h > _body) _top = _body - _h - 8;
            _light.css({ left: (_wx - _w) / 2, top: _top });
        };
        return false;
    });
};
$(document).ready(function() {
    ieHover('#nav > ul > li, .favourites .signup .btn-hold, .sign-popup .btn-hold', 'hover');
    initDrop();
    $('#nav > ul').animationNav();
    $('div.gallery-vertikal').gallery({
        duration: 500,
        listOfSlides: 'div.holder > ul > li',
        slideElement: 3,
        nextBtn: 'div.btn-next > a',
        prevBtn: 'div.btn-prev > a',
        circle: false,
        direction: 'vertikal'
    });
    gal3 = $('div.gallery-fade').gallery({
        duration: 500,
        listOfSlides: 'ul.big-img > li',
        nextBtn: 'div.btn-next > a',
        prevBtn: 'div.btn-prev > a',
        autoRotation: 10000,
        effect: 'fade'
    });
    gal3.stop();
    $('div.heading > a.launch').click(function() {
        gal3.play();
        return false;
    });
    /*gal1 = $('div.gallery-fade2').gallery({		duration: 600,		listOfSlides: 'ul.fade-img > li',		nextBtn: 'div.btn-next > a',		prevBtn: 'div.btn-prev > a',		autoRotation: 10000,		effect: 'fade',		switcher: 'div.control > div > ul > li'	});*/
    gal2 = $('div.gallery-fade2').gallery({
        duration: 600,
        listOfSlides: 'ul.hold-text > li',
        nextBtn: 'div.btn-next > a',
        prevBtn: 'div.btn-prev > a',
        autoRotation: 10000,
        effect: 'fade',
        switcher: 'div.control > div > ul > li'
    });
    //gal1.stop();
    gal2.stop();
    $('div.gallery-fade2 > a.launch').click(function() {
        gal1.play();
        gal2.play();
        return false;
    });
    $('div.gallery-fade > a.launch').click(function() {
        gal4.play();
        return false;
    });
    gal4 = $('div.gallery-fade').gallery({
        duration: 600,
        listOfSlides: 'ul.big-img > li',
        nextBtn: 'div.btn-next > a',
        prevBtn: 'div.btn-prev > a',
        autoRotation: 10000,
        effect: 'fade'
    });
    gal4.stop();
    $('div.big-fade').gallery({
        duration: 600,
        listOfSlides: 'ul.big-fade-hold > li',
        nextBtn: 'div.btn-next > a',
        prevBtn: 'div.btn-prev > a',
        autoRotation: 10000,
        effect: 'fade',
        switcher: 'div.control > div > ul > li',
        textBox: 'ul.big-fade-hold > li > div.text'
    });
    $('div.smoll-fade').gallery({
        duration: 600,
        listOfSlides: 'ul.list-wrap > li',
        nextBtn: 'div.btn-next > a',
        prevBtn: 'div.btn-prev > a',
        autoRotation: 10000,
        effect: 'fade',
        switcher: 'div.control > div > ul > li',
        textBox: 'ul.list-wrap > li > div.text',
        directionBox: 'top'
    });
    // Open-Close init
    $('.archive ul li').OpenClose({
        activeClass: 'active',
        opener: 'a',
        slider: 'div.block',
        slideSpeed: 400
    });
    InitLightBox();
});	
