/**
 * @author Educational Networks
 * source: http://www.workshop.rs/projects/jqfancytransitions
 * @param {Object} carouselID
 * @param {Object} options
 **/
EnImageTransitions = function(carouselID, options){
	var settings = {
		width: 600,
		height: 360,
		strips: 10,
		delay: 10000,
		stripDelay: 50,
		position: 'alternate', // top, bottom, alternate, curtain
		direction: 'fountainAlternate', // left, right, alternate, random, fountain, fountainAlternate
		effect: 'wave'
	};
	
	if (options) 
		$.extend(settings, options);
	this.settings = settings;
	this.carouselID = carouselID;
	this.order = new Array();
	this.inc = 0;
	this.stripTimer;
	this.nextImage = "";
	this.stopTrans = false;
	
	this.init = function(){
		params = this.settings;
        if (params.effect == 'zipper') {
            params.direction = 'alternate';
            params.position = 'alternate';
        }
        if (params.effect == 'wave') {
            params.direction = 'alternate';
            params.position = 'top';
        }
        if (params.effect == 'curtain') {
            params.direction = 'alternate';
            params.position = 'curtain';
        }
        stripWidth = parseInt(params.width / params.strips);
        gap = params.width - stripWidth * params.strips;
        stripLeft = 0;
        odd = 1;
		$('#' + this.carouselID + "-infowrap").append("<div class='enImgTransContainer' id = '" + this.carouselID + "-enImgTransContainer'></div>");
		$('#' + this.carouselID + "-enImgTransContainer").append("<div class='enImgTransWrap' id = '" + this.carouselID + "-enImgTransWrap'></div>");
        for (j = 1; j < params.strips + 1; j++) {
            if (gap > 0) {
                tstripWidth = stripWidth + 1;
                gap--;
            }
            else {
                tstripWidth = stripWidth;
            }
            $('#' + this.carouselID + "-enImgTransWrap").append("<div class='ft-" + this.carouselID + " enImgTrans " + ((j == 1) ? " leftEnImgTrans":"") + ((j == params.strips) ? " rightEnImgTrans":"") + "' id='ft-" + this.carouselID + j + "' style='width:" + tstripWidth + "px; height:" + params.height + "px; position: absolute;'></div>");
            $("#ft-" + this.carouselID + j).css({
                'background-position': -stripLeft + 'px top',
                'left': stripLeft
            });
            stripLeft += tstripWidth;
            if (params.position == 'bottom') 
                $("#ft-" + this.carouselID + j).css('bottom', 0);
            if (j % 2 == 0 && params.position == 'alternate') 
                $("#ft-" + this.carouselID + j).css('bottom', 0);
            if (params.direction == 'fountain' || params.direction == 'fountainAlternate') {
                this.order[j - 1] = parseInt(params.strips / 2) - (parseInt(j / 2) * odd);
                this.order[params.strips - 1] = params.strips;
                odd *= -1;
            }
            else {
                this.order[j - 1] = j;
            }
        }
        $('#' + this.carouselID + "-enImgTransWrap").css({
            'width': params.width,
            'height': params.height
        });
	}

   	this.transition = function(nextImage, link, callbackFun){
		if(link) {
			$('#' + this.carouselID + ' .enImgTrans').unbind('click');
			$('#' + this.carouselID + ' .enImgTrans').click(function(){window.open(link);});
			$('#' + this.carouselID + ' .enImgTrans').css('cursor', 'pointer');
		} else {
			$('#' + this.carouselID + ' .enImgTrans').unbind('click');
			$('#' + this.carouselID + ' .enImgTrans').css('cursor', 'auto');
		}
		$('#' + this.carouselID + ' .enImgTrans').toggle(true);
		this.nextImage = nextImage;
        if (this.settings.direction == 'random') 
            $.fisherYates(this.order);
        if ((this.settings.direction == 'right' && this.order[0] == 1) || this.settings.direction == 'alternate' || this.settings.direction == 'fountainAlternate') 
            this.order.reverse();
        enImgTransPointer = this;
        this.stripTimer = setInterval(function(){
        	enImgTransPointer.strips(enImgTransPointer.order[enImgTransPointer.inc], callbackFun)
        }, this.settings.stripDelay);
        this.inc = 0;
    };

    this.strips = function(itemId, callbackFun){
    	var self = this;
        temp = this.settings.strips;
        if (this.inc == temp) {
            clearInterval(this.stripTimer);
            return;
        }
		if(this.inc == temp-1) {
			var animated = function() {
				if(!self.stopTrans) $('#'+self.carouselID + "-infowrap").css({'background-image' : 'url(' + self.nextImage +')'});
				if(callbackFun && typeof(callbackFun) == "function")
					callbackFun();
			}
		}
        if (this.settings.position == 'curtain') {
        	//if(window.console) console.log(this.carouselID + " [enImageTransition]");
            currWidth = $('#ft-' + this.carouselID + itemId).width();
            $('#ft-' + this.carouselID + itemId).css({
                width: 0,
                opacity: 0,
                'background-image': 'url(' + this.nextImage + ')'
            });
            $('#ft-' + this.carouselID + itemId).animate({
                width: currWidth,
                opacity: 1
            }, 1000, animated);
        }
        else {
        	//if(window.console) console.log(this.carouselID + " [enImageTransition]");
            $('#ft-' + this.carouselID + itemId).css({
                height: 0,
                opacity: 0,
                'background-image': 'url(' + this.nextImage + ')'
            });
            $('#ft-' + this.carouselID + itemId).animate({
                height: this.settings.height,
                opacity: 1
            }, 1000, animated);
        }
        this.inc++;
    };
	
	this.stopTransition = function() {
		this.stopTrans = true;
		clearInterval(this.stripTimer);
		$('#'+this.carouselID + "-infowrap").css({'background-image' : 'url(' + this.nextImage +')'});
	};
	
	this.startTransition = function() {
		this.stopTrans = false;
	}
	
	fisherYates = function(arr){
        var i = arr.length;
        if (i == 0) 
            return false;
        while (--i) {
            var j = Math.floor(Math.random() * (i + 1));
            var tempi = arr[i];
            var tempj = arr[j];
            arr[i] = tempj;
            arr[j] = tempi;
        }
    };
	
	this.log = function(str){
		if (window.console) 
			console.log(str);
	};
	
	this.init();
	
	return this;
}
