/*
 * 	MikadoSlider - jQuery plugin
 *	written by Giorgio Marziani de Paolis
 *
 *	Copyright (c) 2010 Giorgio Marziani de Paolis
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library 1.4
 *	http://jquery.com
 *
 */


(function($) {

    $.fn.mikadoSlider = function(options) {

        // default configuration properties
        var defaults = {
            prevId: 'prevBtn',
            nextId: 'nextBtn',
            navId: '#thumbNav',
            numPerPage: 6
        };

        var options = $.extend(defaults, options);

        this.each(function() {
            var obj = $(this);
            var s = $("li", obj).length;
            var w = $("li", obj).width();
            var h = $("li", obj).height();

            var ss = $("li", $(options.navId)).length;
            var ww = $("li", $(options.navId)).width();
            var hh = $("li", $(options.navId)).height();

            var clickable = true;
            obj.width(w);
            obj.height(h);
            //$(options.navId).width(ww); 
            $(options.navId).height(hh);
            var ts = s - 1;
            var tsts = ss - 1;
            var t = 0;


            var tt = 0;
            for (var i = 0; i < s; i++) {
                $(options.navId + ' li').click(function() {
                    var currObj = $("a.slider_on");
                    currObj.attr("class","slider");
                    animate(obj, $("a", $(this)).attr('rel'), true, '');
                    var nextObj = $("a", $(this));
                    nextObj.attr("class","slider_on");
                });

            };

            $("#" + options.nextId).click(function() {
                animate($(options.navId), "next", true,'next');
                
                var currObj = $("a.slider_on");
                var currRel = currObj.parent().next().children("a").attr("rel");
                
                if ((currRel > 0) && (currRel != null)) {
                    animate($('.anythingSlider'), currRel, true, '');
                    var nextObj = $("a.slider_on").parent().next().children("a");
                    nextObj.attr("class","slider_on");
                    currObj.attr("class","slider");
                }
               
            });
            $("#" + options.prevId).click(function() {
                animate($(options.navId), "prev", true,'prev');
                
                var currObj = $("a.slider_on");
                var currRel = currObj.parent().prev().children("a").attr("rel");
               
                if ((currRel >= 0) && (currRel != null)) {
                    animate($('.anythingSlider'), currRel, true, '');
                    var prevObj = $("a.slider_on").parent().prev().children("a");
                    prevObj.attr("class","slider_on");
                    currObj.attr("class","slider");
                }
            });

           
            function animate(source, dir, clicked, direction) {
                var isNav = 0;

                if (source.attr("id") == "thumbNav"){
                   
                    var current = parseInt($("a.slider_on").attr('rel'));
                    //alert(current + " " + (current % 6) + " " + direction);
                     if ( ((current % 6) == 5) && (direction == 'next') && (current > 0)) { 
                       //alert("avanti");
                       source.children(".wrapper").children("ul").animate(
					        { left: "-=840px" },
					        { queue: false, duration: 'fast', complete: function() { clickable = true; } }
    						    
					    );
					 } else if ( ((current % 6) == 0) && (direction == 'prev') && (current > 0)){ 
					   //alert("indietro");
                        source.children(".wrapper").children("ul").animate(
					        { left: "+=840px" },
					        { queue: false, duration: 'fast', complete: function() { clickable = true; } }
    						    
					    );
					 }
				}
               
                
                if (("#" + source.attr("id")) == options.navId)
                    isNav = 1;
                if (true) {
                    clickable = false;
                    var ot = t;
                    var otot = tt;
                    switch (dir) {
                        case "next":
                            if (isNav == 1)
                                tt = (otot >= tsts) ? tsts : tt + 1;
                            else
                                t = (ot >= ts) ? ts : t + 1;
                            break;
                        case "prev":
                            if (isNav == 1)
                                tt = (tt <= 0) ? 0 : tt - 1;
                            else
                                t = (t <= 0) ? 0 : t - 1;
                            break;
                        default:
                            if (isNav == 1)
                                tt = dir;
                            else
                                t = dir;
                            break;
                    };
                    var p = 0;
                    if (isNav) {

                        p = (tt * ww * -1);

                    }
                    else {
                        p = (t * w * -1);

                    }
                    if (isNav == 1 && tt < options.numPerPage && dir == "next") {
                        return;
                    }
                    if (isNav == 1 && tt > ss - options.numPerPage && dir == "prev") {
                        return;
                    }
                    //alert("muovo" + dir + " " + p);
                     if (source.attr("id") != "thumbNav"){
					     source.children(".wrapper").children("ul").animate(
					        { marginLeft: p },
					        { queue: false, duration: 'fast', complete: function() { clickable = true; } }
    						    
					    );
					}
                };
            };

            /*
            var timeout;
            if(options.auto){;
            timeout = setTimeout(function(){
            animate("next",false);
            },options.pause);
            };		
            */
        });

    };

})(jQuery);
