/* Gallery (slide-on-click, auto-slide-left) */
jQuery.fn.gallAjaxSlide = function(_options){
    // defaults options	
    var _options = jQuery.extend({
        duration: 700,
        autoSlide: 5000
    },_options);

    return this.each(function(){
        var _hold = $(this);
        var _speed = _options.duration;
        var _timer = _options.autoSlide;
        var box = _hold.find('div.gallery-block');
        var _wrap = $('<ul></ul>');
        box.append(_wrap);
        var _swicher = _hold.find('ul.switcher > li > a');
        var _next = _hold.find('a.link-next');
        var _prev = _hold.find('a.link-prev');
        var _count = _swicher.index(_swicher.filter(':last'));
        var _active = _swicher.index(_swicher.filter('.active:eq(0)'));
        if (_active < 0) _active = 0;
        var last = _active;
        var flag = true;		
        $.ajax({
            type: "GET",
            global: false,
            dataType: "html",
            url: _swicher.get(_active).href,
            success: function(msg){
                var temp = $('<li rel='+_active+'>'+msg+'</li>');
                box.find(".loading").fadeOut("slow", function(){
                    _wrap.hide().append(temp);
                    _wrap.fadeIn("slow");
                });				
            },
            error:function(){
                alert('Ajax error!');
            }
        });
		
        var _wrapHolderW = 1;
        var _t;
		
        function scrollEl(){
            if (_wrap.find('li[rel='+_active+']').length){
                var temp = _wrap.find('li[rel='+_active+']');
                _wrap.animate({
                    marginLeft: -(temp.position().left) + "px"
                }, {
                    queue:false, 
                    duration: _speed
                });
            }
            else{
                $.ajax({
                    type: "GET",
                    global: false,
                    dataType: "html",
                    url: _swicher.get(_active).href,
                    success: function(msg){
                        var temp = $('<li rel='+_active+'>'+msg+'</li>');
                        flag = false;
                        for (var i = _active; i > -1; i--){
                            if (_wrap.find('li[rel='+i+']').length){
                                _wrap.find('li[rel='+i+']').after(temp);
                                i = -1;
                                flag = true;
                            }
                        };
                        if (!flag) _wrap.prepend(temp);
                        if (temp.next().length && last != _count){
                            _wrap.css({
                                marginLeft: -(temp.next().position().left) + "px"
                                });
                        }
                        if (last == _count){
                            _wrap.css({
                                marginLeft: -(_wrap.find('li[rel='+_count+']').position().left) + "px"
                                });
                        }
                        _wrap.animate({
                            marginLeft: -(temp.position().left) + "px"
                        }, {
                            queue:false, 
                            duration: _speed
                        });
                        last = _active;
                    },
                    error:function(){
                        alert('Ajax error!');
                    }
                });
            }
            _swicher.removeClass('active').eq(_active).addClass('active');
			
        }
        function runTimer(){
            _t = setInterval(function(){
                _active++;
                if (_active > (_count - _wrapHolderW + 1)) _active = 0;
                scrollEl();
            }, _timer);
        }
        runTimer();
        _next.click(function(){
            _active++;
            if(_t) clearTimeout(_t);
            if (_active > (_count - _wrapHolderW + 1)) _active = 0;
            scrollEl();
            runTimer();
            return false;
        });
        _prev.click(function(){
            _active--;
            if(_t) clearTimeout(_t);
            if (_active < 0) _active = _count - _wrapHolderW + 1;
            scrollEl();
            runTimer();
            return false;
        });
        _swicher.click(function(){
            _active = _swicher.index($(this));
            if(_t) clearTimeout(_t);
            scrollEl();
            runTimer();
            return false;
        });
    });
}
$(document).ready(function(){
    $('div.gallery-ajax').gallAjaxSlide({
        duration: 700,
        autoSlide: 10000
    });

    $(".jsdisplay").each(function(){
        $(this).fadeIn("fast");
    });

    $(".toggle-you").bind("change", function(){
        $("." + $(this).attr("ref")).slideToggle();
    });

    if($(".toggle-you").is(":checked")){            
        $('.toggle-you').each(function(){
            var l = $(this).attr("ref");
            $("." + l + "").show();
        });
    }
    $(".search-result").each(function(){
        var imgH = ($(this).find(".left").find("img").height() + 10);
        if($(this).height() < imgH){
            $(this).css("height", imgH);
        }
    });
    if($(".category_search").length > 0){
        $("#content").append('<div id="cloneHolder"></div>');
    }
    $(".category_search").bind({
        mouseover: function(){           
            var set = $(this).offset();
            if(!$(this).hasClass("active")){
                $(this).addClass("active");
                $("#cloneHolder").empty();
                $("#cloneHolder").show();
                $("#cloneHolder").css("position", "absolute");
                $("#cloneHolder").css("top", set.top + "px");
                $("#cloneHolder").css("left", set.left + "px");
                $("#cloneHolder").append($(this).html());
                $("#cloneHolder").find(".sale_details_trunc").hide();
                $("#cloneHolder").find(".sale_details_full").show();
               
            }
        },
        mouseleave: function(){
            $(this).css("position", "static");
            $(this).removeClass("active");
        /*$("#cloneHolder").hide();*/
        }
    });
    $("#cloneHolder").bind({
        mouseleave: function(){
            $(this).hide();
        } 
    });
    /* Toggle Tutorial List */
    $(".toggle-tutorial-wrap a").click(function(){
        $("#tutorial-nav-wrap").toggle();
        if($("#content").height() < $("#tutorial-nav-wrap").height()){
            $(".content-relative").height(($("#tutorial-nav-wrap").height()+ 30));
        }
        console.log($("#content").height());
        console.log($("#tutorial-nav-wrap").height());
        return false
    });
    $(".js-show").show();
}); // END DOM READY

