﻿/* ------------------------------------------------------------------------
s3Slider
	
Developped By: Boban Karišik -> http://www.serie3.info/
CSS Help: Mészáros Róbert -> http://www.perspectived.com/
Version: 1.0
	
Copyright: Feel free to redistribute the script/modify it, as
long as you leave my infos at the top.
------------------------------------------------------------------------- */


(function($) {

    $.fn.s3Slider = function(vars) {

        var element = this;
        var timeOut = (vars.timeOut != undefined) ? vars.timeOut : 7000;
        var current = null;
        var timeOutFn = null;
        var faderStat = true;
        //var mOver = false;
        var items = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsHeadings = $("#" + element[0].id + "Content ." + element[0].id + "Image .h");
        var itemsSpan = $("#" + element[0].id + "Content ." + element[0].id + "Image .t");

        //for (var i = 0; i < items.length; i++) {
        //    $(items[i]).mouseover(function() {
        //        mOver = true;
        //    });

        //    $(items[i]).mouseout(function() {
        //        mOver = false;
        //        fadeElement(true);
        //   });
        //}

        var fadeElement = function(isMouseOut) {
            var thisTimeOut = timeOut;
            thisTimeOut = (faderStat) ? 10 : thisTimeOut;
            if (items.length > 0) {
                if (items.length > 2) {
                    timeOutFn = setTimeout(makeSlider, thisTimeOut);
                }
            } else {
                console.log("Poof..");
            }
        }

        var makeSlider = function() {
            
	    current = (current != null) ? current : items[(items.length-1)];
            var currNo = jQuery.inArray(current, items) + 1
            currNo = (currNo == items.length) ? 0 : (currNo - 1);
		
            var newMargin = $(element).width() * currNo;

            if (faderStat == true) {
                //if (!mOver) {
                    $(items[currNo]).css('z-index', 1);
                    $(items[currNo]).show();


                    // $('#pagnation').hide();
                    // $(itemsHeadings[currNo]).hide();
                    // $(itemsSpan[currNo]).hide();

                    $('#pagnation').fadeIn(300);
                    $(itemsHeadings[currNo]).fadeIn(300);
                    $(itemsSpan[currNo]).fadeIn((300), function() {
                        faderStat = false;
                        $('.pagnationItem').removeClass("pagnationCurrent");
                        $('#pagnation #row' + currNo).addClass("pagnationCurrent");
                        current = items[currNo];
                        //if (!mOver) {
                            fadeElement(false);
                        //}
                    });
                //}
            } else {
                //if (!mOver) {
                    $('#pagnation').fadeOut(300);
                    $(itemsHeadings[currNo]).fadeOut(300);
                    $(itemsSpan[currNo]).fadeOut((300), function() {
                        $(items[currNo]).css('z-index', 2);
                        $(items[currNo]).slideUp(500, 'easeInOutQuint', function() {

                        });
                        faderStat = true;
                        current = items[(currNo + 1)];
                        //if (!mOver) {
                            fadeElement(false);
                        //}
                    });

                //}
            }
        }

        var makePagnation = function() {
            for (i = 0; i < items.length-1; i++) {
		if (i==0){
			$('#pagnation').append('<div id="row' + i + '" class="pagnationItem pagnationCurrent">&nbsp;</div>');
		}
		else{
			$('#pagnation').append('<div id="row' + i + '" class="pagnationItem">&nbsp;</div>');
		}	
            }

        }

        makeSlider();
        if (items.length > 2) {
            makePagnation();
        }
    };
})(jQuery);  