/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



 
if(typeof jQuery != 'undefined') {
   jQuery(function($j) {
		$j.fn.extend({
            looper: function(options) {
                var settings = $j.extend({}, $j.fn.looper.defaults, options);
                var pagin;

                return this.each(
                    function() {
                        if($j.fn.jquery < '1.3.2') {

                            return;
                        }
                        var $t = $j(this);
                        var o = $j.metadata ? $j.extend({}, settings, $t.metadata()) : settings;
                        var position = 0;
                        var slides = $j(o.slides,$t).children().size();
                        var aktiv = false;
                        var mover = 1;
                        distanse = 0;

                        if(slides===1) {
                            $j(o.slides,$t).children().css({
                                position:"absolute",
                                left:position,
                                display:"block"
                            });
                            return;
                        }
                       
                        $j(o.slides,$t).css({
                            width:slides*o.width
                        });
                        
                        $j(o.slides,$t).children().each(function() {
                            $j(this).css({
                                position:"absolute",
                                left:position,
                                display:"block"
                            });
                            position = position+o.width;
                        })
                       
                        if(o.autoStart) {
                            startInterval();
                        }

                        if(o.pagina && slides > 1) {
                            var antall = slides;
                            var counter = 0;
                            $j($t).append("<ul class=" + o.paginaClass + ">");
                            $j(o.slides,$t).children().each(function() {
                                if(counter<antall) {
                                    $j("."+o.paginaClass, $t).append("<li><a rel="+(counter+1)+" href=\"#\">"+(counter+1)+"</a></li>");
                                    counter++;
                                }else {
                                    counter = 0;
                                }
                            });
                            $j("." + o.paginaClass + " li a:eq(0)",$t).parent().addClass("aktiv");
                            pagin = $j("." + o.paginaClass + " li a",$t);
                        }

                        if(o.overPause) {
                            $j(o.slides,$t).mouseover(function() {
                                stopInterval();
                            });
                            $j(o.slides,$t).mouseout(function() {
                                animateShow("neste");
                                startInterval();
                            });
                        }

                        $j(pagin, $t).click(function() {
                            stopInterval();
                            if($j(this).parent().hasClass("aktiv")) {
                                return false;
                            }else {
                                var rel = $j(this).attr("rel");
                                mover = (rel-1);
                                animateShow("neste");
                                startInterval();
                            }
                            return false;
                        });

                        function startInterval() {
                            sliderIntervalID = setInterval(function(){
                                if(aktiv===false) {
                                    animateShow("neste");
                                }
                            },o.sliderPause);
                        }

                        function stopInterval() {
                            clearInterval(sliderIntervalID);
                        }

                        $j(".next",$t).click(function(){
                            if(aktiv===false) {
                                animateShow("neste");
                            }
                            return false;
                        });

                        function animateShow(dir) {
                            aktiv = true;
                            switch(dir) {
                                case "neste":
                                    selected(mover);
                                    distanse = -mover*o.width;
                                    //alert("distanse: " + distanse + " mover:" + mover);
                                    moveforward();
                                    if(mover === 1) {
                                        //alert("mover");
                                        $j(o.slides,$t).fadeOut((o.sliderSpeed/2), function() {});
                                        $j(o.slides,$t).css({
                                            left:0
                                        });
                                        $j(o.slides,$t).fadeIn((o.sliderSpeed/2), function() {
                                            aktiv = false;
                                        });
                                    }else {
                                        $j(o.slides,$t).animate({
                                            left:distanse
                                        }, o.sliderSpeed, function() {
                                            aktiv = false;
                                        });
                                    }
                                    break;
                                case "forrige":
                                    alert("prew");
                                    break;
                                case "steng":
                                    alert("Steng!")
                                    break;
                                default:
                                    break;
                            }
                        }

                        function moveforward() {
                            mover++;
                            if(mover===slides) {
                                mover = 0;
                            }
                            
                        }
                        function selected(mover) {
                            $j(pagin,$t).parent().siblings().removeClass("aktiv");
                            $j("." + o.paginaClass + " li a:eq(" + mover + ")",$t).parent().addClass("aktiv");
                        }
                    });
            }
        });
        $j.fn.looper.defaults = {
            msg: "Test Meg",
            slides: "#slides",
            pagina: false,
            paginaClass: "myPagination",
            xmlUrl: "test.xml",
            width: 590,
            autoStart: true,
            sliderSpeed: 400,
            sliderPause: 3500,
            overPause: true
        };
    });
}


