var sliderImagesCount = 0;

// easing
jQuery.extend(jQuery.easing, {
	swap1: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	swap2: function (x, t, b, c, d) {
		alert('');
		return c*(t/=d)*t + b;
	}
});

function createSlider(flag) {
    if (!flag && sliderImagesCount < $(".scroll-pane TABLE TD.pic").length) {
        return;
    }

    $(".jspPane").css({ "margin-left": 0 });
    $('.scroll-pane').jScrollPane();
    var interval = setInterval(function() {
        if (parseInt($(".jspPane").css("margin-left")) != 0) {
            $(".jspPane").css({ "margin-left": 0 });
            $('.scroll-pane').jScrollPane();
        }
        else {
            clearInterval(interval);

            if ($(".scroll-pane TABLE TD.cur").length) {
                var $link = $(".scroll-pane TABLE TD.cur > SPAN");
                var $drag = $(".scroll-pane .jspDrag");

                for (i = 0; i <= 25; i++) {
                    $('.scroll-pane').data("jsp").scrollByX((
                        $link.offset().left + $link.width() / 2 + 45 -
                        $drag.offset().left - $drag.width() / 2
                    ) / 2);
                }
            }
        }
    }, 50);
}

$(document).ready(function(){

	// text inputs hints
	$(".text_hint[value!=]")
		.each(function(){this.hint=this.value})
		.bind("focus",function(){if(this.hint==this.value){$(this).attr("value","").removeClass("text_hint")}})
		.bind("blur",function(){if(this.value==""){	$(this).attr("value",this.hint).addClass("text_hint")}})

	// promo
    $(".promo__i").css({ "left": $("body").width() / 2 - 1488 });

	$(".promo__i").draggable({
        axis:'x',
        cancel:'A',
        cursor:'move',
        containment: [$("body").width() - $(".promo__i").width(), 0, 0, $(".promo__i").height()]
    });

	if ($.fn.jScrollPane) {
        $(".scroll-pane TABLE TD.pic IMG").bind("load", function() { sliderImagesCount++; createSlider(); });
        createSlider(true);
        setTimeout(function() { createSlider(true) }, 500);
        setTimeout(function() { createSlider(true) }, 1000);
	}

	// tabs
    $('.tabs DT').click(function(){
		return true;
		$(this).siblings().removeClass('cur').end().next('DD').andSelf().addClass('cur');
		return false
	});

	// catalog
    $('.catalog H2 A').click(function(){
		var cat = $(".catalog")
		if (cat.hasClass("catalog__opened")) {cat.removeClass("catalog__opened")}
		else {cat.addClass("catalog__opened")}
		return false
	});

	// loginform
	$(".tint").height($(document).height());

    // tablesorter
    $("TABLE.sortable").each(function(i, table) {
        if ($(table).find("THEAD").length == 0) return;
        if ($(table).find("TBODY").length == 0) return;

        var opts = { headers: {} };
        $(table).find("TH").each(function(i, th) {
            if ($(th).hasClass("no-sort")) {
                opts.headers[i] = { sorter: false };
            }
        });

        if ($(table).find("TR.no-sort").length) {
            $(table).find("TR.no-sort").each(function(i, tr) {
                $(tr).attr({ id: "subsort-" + i });
                $(tr).prev().attr({ id: "sort-" + i });
            });

            $(table).bind("sortStart", function() {
                //$(this).find("TR.tr-section").hide();
            });

            $(table).bind("sortEnd", function() {
                $(table).find("TR.no-sort").each(function(i, tr) {
                    $(tr).insertAfter($("#" + tr.id.substring(3)));//.show();
                });
            });
        }

        $(table).tablesorter(opts);
    });

})
