$(document).ready(function() {


   var width = $(".ps-pic").width();
   if (!width) return;

//    var psPre = $(".ps-pre span");
//    var psPreLength = $(".ps-pre span").length;
    var allWidth = $(".ps-pre span").eq($(".ps-pre span").length-1).position().left + $(".ps-pre span").eq($(".ps-pre span").length-1).width();
//    var diffWidth = (width - 10 - allWidth)/psPreLength;
//    if (diffWidth > 0) { diffWidth = 0 };
	var psPreHeight = 48*((width-($(".ps-pre span").length*7))/allWidth);
	if (psPreHeight > 48) { psPreHeight = 48; }
    for (i=0; i<$(".ps-pre span").length; i++) {
//		var psPreWidth = psPre.eq(i).width()+diffWidth;
//      $(".ps-pre img").not(".advanced-ps-pre img").eq(i).css({width: psPreWidth + "px"});
//		psPre.not(".advanced-ps-pre span").eq(i).css({width: psPreWidth + 2 + "px"});
        $(".ps-pre img").not(".advanced-ps-pre img").eq(i).css({width: "auto", height: psPreHeight + "px"});
		$(".ps-pre span").not(".advanced-ps-pre span").eq(i).css({width: "auto"});

    }

    makeCurrent();

    $(".ps-screen").draggable({
        axis: "x",
        start: function()
        {
           leftStart = ($(".ps-screen").position().left);
        },
        stop: function()
        {
            leftStop = ($(".ps-screen").position().left);
            var psPicLenght = $(".ps-pic").length;
            if (leftStart > leftStop) {
                $(".next-ps-control").unbind("click");
                $(".ps-screen").draggable("disable");
                if (Math.round(leftStart) != -((psPicLenght - 1)*width)) {
                    $(this).animate({left: leftStart-width+"px"}, "easeInQuint", function() {
                        makeCurrent();
                    });
                } else {
                    $(".ps-screen").draggable("disable");
                    $(this).animate({left: leftStart+"px"}, "easeInQuint", function() {
                        makeCurrent();
                    });
                }
            } else {
                $(".prev-ps-control").unbind("click");
                if (leftStart != 0) {
                    $(".ps-screen").draggable("disable");
                    $(this).animate({left: leftStart+width+"px"}, "easeInQuint", function() {
                        makeCurrent();
                    });
                } else {
                    $(".ps-screen").draggable("disable");
                    $(this).animate({left: leftStart+"px"}, "easeInQuint", function() {
                        makeCurrent();
                    });
                }
            }
        }
 });

    $(".ps-clipper").hover(
            function() {
                $(this).children(".ps-control").fadeIn("fast");
            },
            function() {
                $(this).children(".ps-control").fadeOut("fast");
            }
    );

    $(".prev-ps-control").not("disabled").click(function(){movePrev();});

    function movePrev() {
    $(".ps-screen").draggable("disable");
    $(".prev-ps-control").unbind("click");
        var psScreen = $(".ps-screen");
        if (psScreen.css("left") != "0px") {
            psScreen.animate({left: "+="+width+"px"}, "easeInQuint", function() {
                makeCurrent();
            });
        }
    }

    $(".next-ps-control").not("disabled").click(function(){moveNext();});

    function moveNext() {
    $(".ps-screen").draggable("disable");
    $(".next-ps-control").unbind("click");
        var psScreen = $(".ps-screen");
        var psPicLenght = $(".ps-pic").length;
        if (psScreen.css("left") != -((psPicLenght - 1)*width)+"px") {
            psScreen.animate({left: "-="+width+"px"}, "easeInQuint", function() {
                makeCurrent();
            });
        }
    }

    $(".ps-pre span").click(function(){
        if (!$(this).hasClass("current")) {
        $(".ps-pre span").not(this).removeClass("current");
        $(this).addClass("current");
        $(".ps-screen").animate({left: -($(this).prevAll().length)*width+"px"}, "easeInQuint", function(){
            makeCurrent();
        });
        }
    });

    function makeCurrent() {
        $(".ps-screen").draggable("enable");
        var psScreenLeft = $(".ps-screen").position().left;
        var psPicLenght = $(".ps-pic").length;
        var psPicNumber = -(Math.round(psScreenLeft)/width)+1;
        $(".ps-pre span.current").add(".ups-links a.current").removeClass("current");
        $(".ps-pre span").eq(psPicNumber - 1).addClass("current");
		$(".ups-links a").eq(psPicNumber - 1).addClass("current");
        if (psPicNumber == 1) {
            $(".prev-ps-control").addClass("disabled");
        } else {
            $(".prev-ps-control")
                    .removeClass("disabled")
                    .bind("click", movePrev);
        }
        if (psPicNumber == psPicLenght) {
            $(".next-ps-control").addClass("disabled");
        } else {
            $(".next-ps-control")
                    .removeClass("disabled")
                    .bind("click", moveNext);
        }
    }


});

