


(function($) {
	var opts = new Array;
	var level = new Array;
	var img = new Array;
	var links = new Array;
	var titles = new Array;
	var order = new Array;
	var imgInc = new Array;
	var inc = new Array;
	var colInt = new Array;
	var imgInt = new Array;	
	var directions = new Array;
	var positions = new Array;
	
	
	$.fn.jayImg = $.fn.jayimg = function(options){
		
		init = function(el){
				
			opts[el.id] = $.extend({}, $.fn.jayImg.defaults, options);
			img[el.id] = new Array(); // images array
			links[el.id] = new Array(); // links array
			titles[el.id] = new Array(); // titles array
			order[el.id] = new Array(); // strips order array
			imgInc[el.id] = 0;
			inc[el.id] = 0;
			params = opts[el.id];
			directions =['left', 'right', 'alternate', 'random', 'fountain', 'fountainAlternate'];
			positions =['top', 'bottom', 'alternate', 'curtain'];
			
			// width of cols
			colWidth = parseInt(params.width / params.cols); 
			gap = params.width - (colWidth*params.cols); // number of pixels
			colLeft = 0;
			var Timages =0;
			
			// create images and titles arrays
			$.each($('#'+el.id+' img'), function(i,item){
				img[el.id][i] = $(item).attr('src');
				links[el.id][i] = $(item).next().attr('href');
				titles[el.id][i] = $(item).attr('alt') ? $(item).attr('alt') : '';
				$(item).hide();
				Timages++;
			});			
			
			
			// set panel
			$('#'+el.id).css({
				'background-color':'white',
				'width': params.width,
				'height': params.height,
				'position': 'relative',
				'background-position': 'top left'
				});
			
			// create title bar
			
			$('#'+el.id).append("<div id='ft-title-wrap-"+el.id+"'><div class='ft-title-head alpha30' id='ft-title-head-"+el.id+"'></div> <div class='ft-title' id='ft-title-"+el.id+"'></div></div>");
			$("#ft-title-head-"+el.id).css({"bottom":(opts[el.id].titleHeight+20)});
			
			
			odd = 1;
			for(j=1; j < params.cols+1; j++){
				
				if( gap > 0){
					totalColWidth = colWidth + 1;
					gap--;
				} else {
					totalColWidth = colWidth;
				}
				
				if(params.links)	
					$('#'+el.id).append("<a href='"+links[el.id][0]+"' class='ft-"+el.id+"' id='ft-"+el.id+j+"' style='width:"+totalColWidth+"px; height:"+params.height+"px; float: left; position: absolute;outline:none;'></a>");
				else
					$('#'+el.id).append("<div class='ft-"+el.id+"' id='ft-"+el.id+j+"' style='width:"+totalColWidth+"px; height:"+params.height+"px; float: left; position: absolute;'></div>");
				
				// positioning bars
				$("#ft-"+el.id+j).css({ 
					'background-position': -colLeft +'px top',
					'left' : colLeft 
				});
				
				colLeft += totalColWidth;
				
				if(params.position == 'bottom')
					$("#ft-"+el.id+j).css( 'bottom', 0 );
					
				if (j%2 == 0 && params.position == 'alternate')
					$("#ft-"+el.id+j).css( 'bottom', 0 );
		
				// bars order
					// fountain
					if(params.direction == 'fountain' || params.direction == 'fountainAlternate'){ 
						order[el.id][j-1] = parseInt(params.cols/2) - (parseInt(j/2)*odd);
						order[el.id][params.cols-1] = params.cols; // fix for odd number of bars
						odd *= -1;
					} else {
					// linear
						order[el.id][j-1] = j;
					}
			}
			
				
			$.transition(el,1);//intial
			
			if(Timages!=1){
			clearInterval(imgInt[el.id]);	
			imgInt[el.id] = setInterval(function() { $.transition(el,0);  }, params.delay+params.colDelay*params.cols);
			}
	};// end init
		
	
	
		
		// transition
		$.transition = function(el,start){			
			
			
			if(opts[el.id].randomiser ==true){
				$.fisherYates (directions);
				$.fisherYates (positions);
				opts[el.id].direction = directions[0];
				opts[el.id].position = positions[0];				
			}		
			//debug(opts[el.id].direction)	
			
			
			colInt[el.id] = setInterval(function() { $.cols(order[el.id][inc[el.id]], el);  },opts[el.id].colDelay);
			
			if(start!=1)
				$('#'+el.id).css({ 'background-image': 'url('+img[el.id][imgInc[el.id]]+')' });
			else
				imgInc[el.id]--;
			
			imgInc[el.id]++;
			if  (imgInc[el.id] == img[el.id].length) {
				imgInc[el.id] = 0;
			}
			
			if (imgInc[el.id] == -1){
				imgInc[el.id] = img[el.id].length-1;
			}
			
			
			if(start==1){
				//hide initial bars
				$('#ft-title-'+el.id).css({'height':0,'opacity':0});
				$('#ft-title-head-'+el.id).css({'width':0,'opacity':0});
			}
			
			
			// Titles
			if(titles[el.id][imgInc[el.id]]!=''){
				var titlesArr = titles[el.id][imgInc[el.id]].split("-");
				//debug(titlesArr)
				$('#ft-title-'+el.id).animate({ height: '0px', opacity :0 }, opts[el.id].titleSpeed, function(){					
					$(this).html(titlesArr[1]).animate({ height: opts[el.id].titleHeight, opacity: opts[el.id].titleOpacity }, opts[el.id].titleSpeed);
				});
				
				
				$('#ft-title-head-'+el.id).animate({  width:0,opacity:0}, opts[el.id].titleSpeed, function(){					
					$(this).html("<p>"+titlesArr[0]+"</p>").animate({ width:720,opacity:100}, opts[el.id].titleSpeed);
				});
				
			} else {			
				//$('#ft-title-'+el.id).animate({ opacity: 0}, opts[el.id].titleSpeed);
			}
			
			inc[el.id] = 0;
			
			
			if(opts[el.id].direction == 'random')
				$.fisherYates (order[el.id]);
			
			if((opts[el.id].direction == 'right' && order[el.id][0] == 1) 
				|| opts[el.id].direction == 'alternate'
				|| opts[el.id].direction == 'fountainAlternate')			
					order[el.id].reverse();		
			
		
		};


		// cols animations
		$.cols = function(itemId, el){
			temp = opts[el.id].cols;
			if (inc[el.id] == temp) {
				clearInterval(colInt[el.id]);
				return;
			}
			$('.ft-'+el.id).attr('href',links[el.id][imgInc[el.id]]);
			if(opts[el.id].position == 'curtain'){
				currWidth = $('#ft-'+el.id+itemId).width();
				$('#ft-'+el.id+itemId).css({ width: 0, opacity: 0, 'background-image': 'url('+img[el.id][imgInc[el.id]]+')' });
				$('#ft-'+el.id+itemId).animate({ width: currWidth, opacity: 1 }, 1000);
			} else {
				$('#ft-'+el.id+itemId).css({ height: 0, opacity: 0, 'background-image': 'url('+img[el.id][imgInc[el.id]]+')' });
				$('#ft-'+el.id+itemId).animate({ height: opts[el.id].height, opacity: 1 }, 1000);
			}
			
			inc[el.id]++;
			
		};	
	
		
		this.each (
				function(){ init(this); }
			);	
		
		
		
	}// end fn
	
	// shuffle array function
	$.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;
	   }
	  
	}	
	
	
	// default values
	$.fn.jayImg.defaults = {	
		width: 500, // width of panel
		height: 332, // height of panel
		cols: 50, // number of strips
		delay: 9000, // delay between images in ms
		colDelay: 40, // delay beetwen strips in ms
		titleOpacity: 0.8, // opacity of title
		titleHeight: 20, // opacity of title
		titleSpeed: 1000, // speed of title appereance in ms
		position: 'curtain', // top, bottom, alternate, curtain
		direction: 'left', // left, right, alternate, random, fountain, fountainAlternate
		randomiser:false, // random dir and position
		effect: '', // curtain, zipper, wave
		navigation: false, // prev next and buttons
		links : false // show images as links 		
	};
	
	 function debug($obj) {
		 if (window.console && window.console.log)
		   window.console.log($obj);
	  };
})(jQuery);
