/*
* 	Easy Slider - jQuery plugin
*	written by Alen Grakalic	
*	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
*
*	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
*	Dual licensed under the MIT (MIT-LICENSE.txt)
*	and GPL (GPL-LICENSE.txt) licenses.
*
*	Built for jQuery library
*	http://jquery.com
*
*/

(function($) {

    var snurrare = 2;
    var intervalTime;

    $.fn.easySliderPuff = function(options) {

        // default configuration properties
        var defaults = {
            speed: 1000,
            intervalSpeed: 6500
        };
        var options = $.extend(defaults, options);

        return this.each(function() {
            var obj = $(this);
            var s = $("li", obj).length;
            var w = obj.width();
            var ts = s - 1;
            var t = 0;
            $("ul", obj).css('width', s * w);

            intervalTime = setInterval(function() { animateThis($("#slideshow"), snurrare, w, s, options.speed); }, options.intervalSpeed);

            $("#prevtick").click(function() {
                clearInterval(intervalTime);

                snurrare = snurrare - 2;
                if (snurrare == 0) {
                    snurrare = 1;
                }
                animateThis(obj, snurrare, w, s, options.speed);

                intervalTime = setInterval(function() { animateThis($("#slideshow"), snurrare, w, s, options.speed); }, options.intervalSpeed);
                return false;
            });
            $("#nexttick").click(function() {
                clearInterval(intervalTime);
                var nextId = snurrare;
                if (snurrare > s) {
                    snurrare = 1;
                }
                animateThis(obj, snurrare, w, s, options.speed);
                intervalTime = setInterval(function() { animateThis($("#slideshow"), snurrare, w, s, options.speed); }, options.intervalSpeed);
                return false;
            });

            // PUFF HANDLER START
            $("a", ".puffNav").click(function() {
                var id = $(this).attr("alt");
                animate(obj, id, w, s, options.speed);

                changeImgNav(id);
                clearInterval(intervalTime);

                var nextId = parseInt(id, 10) + 1;
                if (nextId > s) {
                    nextId = 1;
                }

                intervalTime = setInterval(function() { animateThis($("#slideshow"), nextId, w, s, options.speed); }, options.intervalSpeed);

            });
            // PUFF HANDLER END
        });

    };

    function animate(obj, t, w, s, speed) {
        id = parseInt(t, 10) - 1;
        if (id == s) {
            id = 0;
        } else if (id == -1) {
            id = 0;
        }
        p = (id * w * -1);
        $("ul", obj).animate(
            { marginLeft: p },
			speed
		);

        var data = $("#puff" + (t - 1) + " span").attr("title");
        dataRes = data.split("|");
        if (dataRes[1] != "") {
            link = dataRes[1];
        } else {
            link = "#";
        }
        alt = dataRes[0];
        $("#tickcnt").html("<p>" + t + " av " + s + "</p>");
        $("#tickerlink").html('<a class="gotocal" href="' + link + '">L&auml;s mer</a>');
        $("#tickerheading").html('<img class="nr" src="/Images/TextImage.aspx?text=' + alt + '&color=8d8983&bgcolor=F4F0EB&fontsize=13&style=bold&t=.gif" alt="' + alt + '"/>')
    };

    function animateThis(obj, t, w, s, speed) {
        if (snurrare == s) {
            snurrare = 1;
        } else {
            snurrare = parseInt(t, 10) + 1;
        }
        //changeImgNav(t);
        animate(obj, t, w, s, speed);
    }

    function changeImgNav(id) {
        //        $(".puffNav img").each(function() {
        //            var oldURL = $(this).attr("src");
        //            var newURL = oldURL.replace("gfx/puffNavSelected.gif", "gfx/puffNav.gif");
        //            $(this).attr("src", newURL);
        //        });

        //        $("#puffNav" + id).attr("src", "gfx/puffNavSelected.gif");

    }
})(jQuery);
