﻿$(document).ready(function() {
    slideTabs();
    slideGall();
    ieHover('.gallery .box-h ul .box, .sign-in,.entry ul li');

});

/*--- IE6 hover ---*/
function ieHover(h_list) {
    if ($.browser.msie && $.browser.version < 7) {
        $(h_list).hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });
    }
}



/*---- slide tabs ---*/
function slideTabs() {
    var _holder = $('#carousel');
    if (_holder.length) {
        var _btn = _holder.find('.nav > li');
        var _list = _holder.find('.carousel > li');
        var _a = _btn.index(_btn.filter('.active:eq(0)'));
        if (_a == -1) _a = _list.length - 1;
        _list.removeClass('active').hide().eq(_a).addClass('active').show();
        var _step = _list.parent().width();
        _btn.each(function(_i) {
            var _el = $(this);
            var _i = _i;
            var _t;
            _el.mouseenter(function() {
                if (_t) clearTimeout(_t);
                _t = setTimeout(function() {
                    if (_i != _a) {
                        _btn.removeClass('active');
                        _el.addClass('active');
                        if (_i > _a || _a == _list.length - 1) {
                            _list.eq(_a).stop().animate({ left: -_step }, 700, function() {
                                $(this).css({ display: 'none', left: '0' });
                            });
                            _list.eq(_i).stop().css({ display: 'block', left: _step }).animate({ left: 0 }, 700);
                        }
                        else {
                            _list.eq(_a).stop().animate({ left: _step }, 700, function() {
                                $(this).css({ display: 'none', left: '0' });
                            });
                            _list.eq(_i).stop().css({ display: 'block', left: -_step }).animate({ left: 0 }, 700);
                        }
                        _a = _i;
                    }
                }, 500);
            }).mouseleave(function() {
                if (_t) clearTimeout(_t);
            });

        });
    }
}
/*--- slide gallery ---*/
function slideGall() {
    $('.gallery').each(function() {
        var list_h = $(this).find('.box-h .hold > ul');
        var btn_next = $(this).find('.box-h .link-next');
        var btn_prev = $(this).find('.box-h .link-previous');
        var _step = list_h.parent().width();
        var _btn = $('<ul class="gallery-nav"></ul>');
        var _a = 0;
        for (var i = 1; i <= list_h.children().length; i++) {
            _btn.append('<li><a href="javascript:;">' + (i) + '</a></li>');
        }
        $(this).children('.frame').append(_btn);
        _btn = _btn.find('a');
        _btn.eq(_a).addClass('active');
        _btn.click(function() {
            changeEl(_btn.index(this));
            return false;
        });
        if (list_h.children().length > 1) list_h.append(list_h.children().eq(0).clone()).prepend(list_h.children().eq(list_h.children().length - 2).clone());
        list_h.css('margin-left', -_step);
        btn_prev.click(function() {
            changeEl(-2);
            return false;
        });
        btn_next.click(function() {
            changeEl(-1);
            return false;
        });
        var _f = true;
        function changeEl(_ind) {
            if (_f) {
                _f = false;
                if (_ind == -2) {
                    _btn.eq(_a).removeClass('active');
                    if (_a == 0) {
                        _btn.eq(_btn.length - 1).addClass('active');
                        list_h.animate({ marginLeft: -_step * (_a) }, 600, function() {
                            list_h.css('margin-left', -_step * _btn.length);
                            _a = _btn.length - 1;
                            _f = true;
                        });
                    }
                    else {
                        _btn.eq(_a - 1).addClass('active');
                        list_h.animate({ marginLeft: -_step * (_a) }, 600, function() {
                            _a--;
                            _f = true;
                        });
                    }
                }
                else if (_ind == -1) {
                    _btn.eq(_a).removeClass('active');
                    if (_a == _btn.length - 1) {
                        _btn.eq(0).addClass('active');
                        list_h.animate({ marginLeft: -_step * (_a + 2) }, 600, function() {
                            list_h.css('margin-left', -_step);
                            _a = 0;
                            _f = true;
                        });
                    }
                    else {
                        _btn.eq(_a + 1).addClass('active');
                        list_h.animate({ marginLeft: -_step * (_a + 2) }, 600, function() {
                            _a++;
                            _f = true;
                        });
                    }
                }
                else {
                    if (_ind != _a) {
                        _btn.eq(_a).removeClass('active');
                        _btn.eq(_ind).addClass('active');
                        list_h.animate({ marginLeft: -_step * (_ind + 1) }, 600, function() {
                            _a = _ind;
                            _f = true;
                        });
                    }
                    else {
                        _f = true;
                    }
                }
            }
        }
    });
}