$.fn.makeAbsolute = function(rebase) {
    return this.each(function() {

        var el = $(this);
        var pos = el.position();

        el.css({ position: "absolute",
            marginLeft: 0, marginTop: 0,
            top: pos.top, left: pos.left });

        if (rebase){
            el.remove().appendTo("body");
        }
    });
}
var PRODUCT_WIDTH = 267;
var PRODUCT_LIST_LEFT = 68;
var PRODUCT_IX = 0;
var PAGE_SLIDESHOW_IX;
var PAGE_FIRST_IMAGE;
var PAGE_IMAGES_LENGTH;

$(document).ready(function(){
    hideShowButtons();
    startPageSlideShow();
    PAGE_FIRST_IMAGE = $(".pageimages li").eq(0);
    PAGE_IMAGES_LENGTH = $(".pageimages li").length;
    $("#products li.relative").hover(function(){
        $(this).find(".product-background").stop(true, true).animate({opacity:1});
        $(this).find("a.name").css({color:"maroon"});
    }, function(){
        $(this).find(".product-background").stop(true, true).animate({opacity:0.75});
        $(this).find("a.name").css({color:"black"});
    });
    $("#products li.relative").click(function(){
        var p = $(this);
        p.siblings().find(".absolute").andSelf().animate({width:PRODUCT_WIDTH});
        p.find(".absolute").andSelf().animate({width:PRODUCT_WIDTH * 2});
        var p_ix = $.inArray(p[0], $("#products li.relative"));
        var l = $(window).width() / 2 - ((p_ix + 1) * PRODUCT_WIDTH);
        PRODUCT_IX = p_ix;
        hideShowButtons();
        $("#products ul").animate({left: l});
    });
    $("#arrow-left").click(function(){
        var l = parseInt($("#products ul").css("left").replace("px", ""), 10);
        l = l + PRODUCT_WIDTH;
        $("#products li.relative").find(".absolute").andSelf().animate({width:PRODUCT_WIDTH});
        $("#products ul").animate({left:l});
        $("#arrow-right").fadeIn();
        PRODUCT_IX = PRODUCT_IX - 1;
        hideShowButtons();
    });
    $("#arrow-right").click(function(){
        var l = parseInt($("#products ul").css("left").replace("px", ""), 10);
        l = l - PRODUCT_WIDTH;
        $("#products li.relative").animate({width:PRODUCT_WIDTH});
        $("#products ul").animate({left:l});
        $("#arrow-left").fadeIn();
        PRODUCT_IX = PRODUCT_IX + 1;
        hideShowButtons();
    });
    $("#products a").click(function(evt){ evt.preventDefault(); });

    $("#pageimages-arrow-right").click(function(){
        if(PAGE_SLIDESHOW_IX){
            window.clearInterval(PAGE_SLIDESHOW_IX);
        }
        $this = $(this);
        $(".pageimages li.current").fadeOut().queue(function(){
            if ($this.index() != PAGE_IMAGES_LENGTH-1){
                $this.next().fadeIn();
                $this.next().toggleClass("current");
            }
            else{
                PAGE_FIRST_IMAGE.fadeIn();
                PAGE_FIRST_IMAGE.toggleClass("current");
            }
            $this.removeAttr("class");
            $this.dequeue();
        });
        startPageSlideShow()
    });
    $("#pageimages-arrow-left").click(function(){
        if(PAGE_SLIDESHOW_IX){
            window.clearInterval(PAGE_SLIDESHOW_IX);
        }
        $this = $(this);
        $(".pageimages li.current").fadeOut().queue(function(){
            if ($this.index() != 0){
                $this.prev().fadeIn();
                $this.prev().toggleClass("current");
            }
            else{
                $(".pageimages li:last").fadeIn();
                $(".pageimages li:last").toggleClass("current");
            }
            $this.removeAttr("class");
            $this.dequeue();
        });
        startPageSlideShow()
    });
});
function hideShowButtons(){
    if(PRODUCT_IX <= 0){
        $("#arrow-left").fadeOut();
    } else {
        $("#arrow-left").fadeIn();
    }
    if(PRODUCT_IX >= $("#products li.relative").length - 2){
        $("#arrow-right").fadeOut();
    } else {
        $("#arrow-right").fadeIn();
    }
}
function startPageSlideShow(){
    PAGE_SLIDESHOW_IX = window.setInterval(function(){
        $(".pageimages li.current").fadeOut().queue(function(){
            $this = $(this);
            if ($this.index() != PAGE_IMAGES_LENGTH - 1){
                $this.next().fadeIn();
                $this.next().toggleClass("current");
            }
            else{
                PAGE_FIRST_IMAGE.fadeIn();
                PAGE_FIRST_IMAGE.toggleClass("current");
            }
            $this.removeAttr("class");
            $this.dequeue();
        });
    }, 4000);
}
function debugWrite(s){
    var $d = $("#debug");
    if(!$d.length){
        $d = $("<div>");
        $d.attr({"id":"debug"});
        $("body").append($d);

    }
    $d.html(s + "<br />" + $d.html());
}





