
var currNr=-1;
(function($) {
	
	$.fn.kenBurns = function(options) {

		var nrChildren = 0;
		var imageWidth=900;
		var imageHeight=415;
		var totalWidth=900;
		var totalHeight=415;
		
		var realImageWidth=0;
		var realImageHeight=0;
		
		var thumbWidth = 20;
          var thumbHeight = 25;
		  var thumbSpace = 5;
		  
		var navigationType="custom"
		var navigationDivId="navCustom"
		
		var widthArray = [];
		var heightArray = [];
		var descriptionArray= [];
		var descriptionOpacityArray = [];
		
		var initialPosition="";
		var finalPosition="";
		
		var images;
		
		
		
		
		var tempNr=0;
		
		var lastIndex=99;
		
		var slideshowTime=0;
		
		var fadeTime=800;
		
		var easingType="linear"
		
		var curInter;
		
		
		var cacheObject;
		
		var intervalID;
		var tempCount=0;
		var paused=false;
		var transitionTime = 3000;
		var basePath="";
		
		var totalItems=0;
		var hideOnCue="off"
		var buttonsAlign="";
		var defaults = {
			imageWidth:900,
			imageHeight:415,
			thumbWidth : 20,
            thumbHeight : 25,
		    thumbSpace : 5,
			navigationType:"custom",
			navigationDivId:"navCustom",
			hideOnCue:"off",
			buttonsAlign:"right",
			basePath:""
		},

		options = $.extend(defaults, options);

		this.each(function() {
		
		var thisId=$(this)[0].getAttribute('id');
		var thisDiv = $(this);
		
		totalWidth=options.imageWidth;
		totalHeight=options.imageHeight;
		navigationType=options.navigationType;
		navigationDivId=options.navigationDivId;
		hideOnCue=options.hideOnCue;
		buttonsAlign=options.buttonsAlign;
		thumbWidth=options.thumbWidth;
		thumbHeight=options.thumbHeight;
		thumbSpace=options.thumbSpace;
		
		basePath=options.basePath;
		
		nrChildren = $(this).children().length;
		
		jQuery(this).css({'position' : 'relative', 'width' : totalWidth, 'height' : totalHeight, 'overflow': 'hidden'})
			
		
		$(this).append("<div id='" + thisId + "-imgCon'></div>")
		$(this).append("<div id='" + thisId + "-descCon'></div>")
		$(this).append("<div id='" + thisId + "-navCon'></div>")
		
		var imgCon=jQuery(this).find('#' + thisId + '-imgCon');
		var navCon=jQuery(this).find('#' + thisId + '-navCon');
		var descCon=jQuery(this).find('#' + thisId + '-descCon');
		
		
		descCon.css({
			'position' : 'relative',
			'z-index' : 4
		})
		imgCon.css({'position' : 'relative', 'z-index' : '3'})
		
		for(i=0;i<nrChildren;i++){
			imgCon.append($(this).children().eq(i).clone())
			
		}
		images=imgCon.children();
		
		
		
		var descriptionBgDiv="";
		var descriptionTextDiv="";
		
		
		for (i = 0; i < nrChildren; i++) {
			descriptionBgDiv="<div class='descriptionBg' id='descriptionBg-" + i + "'></div>";
			descriptionTextDiv="<div class='descriptionText' id='descriptionText-" + i + "'>";
			descCon.append("<div class='descriptionMain' style='position:absolute; top:0px; left:0px;'></div>")
			
			
			descriptionArray[i] = descCon.children().eq(i)
			descriptionArray[i].css('visibility', 'hidden')
			
			if(images.eq(i).attr("title")!=undefined && images.eq(i).attr("title")!='')
			{

				descriptionTextDiv+=images.eq(i).attr("title") + "</div>";
				
				//console.log(images.eq(i).attr("data-descriptionOpacity"))
				descriptionArray[i].append(descriptionBgDiv)
				descriptionArray[i].append(descriptionTextDiv)
				
				if(jQuery.browser.msie){
				descriptionArray[i].find('.descriptionBg').css('margin-top', (descriptionArray[i].find('.descriptionBg').css('top')));
				descriptionArray[i].find('.descriptionBg').css('margin-left', (descriptionArray[i].find('.descriptionBg').css('left')));
				}
				else
					descriptionArray[i].find('.descriptionBg').css('position', 'absolute')
				descriptionOpacityArray[i]=descriptionArray[i].find('.descriptionBg').css('opacity');
				//alert(descriptionOpacityArray[i])
				//descriptionOpacityArray[i]=images.eq(i).attr("data-descriptionOpacity");
				
			}
			
		}
		//*************************************************
		//WATCH OUT - THIS FUNCTION KILLS THE DIVS NOT HERE
		for (i = 0; i < nrChildren; i++) {
			jQuery(this).children().remove(':not(#' + thisId + '-imgCon, #' + thisId + '-navCon, #' + thisId + '-descCon)')
		}
		//*************************************************
		
		
		
		//*************NAVIGATION SETUP********************
		
		if(navigationType=="arrows" || navigationType=="custom"){
			if(navigationType=="arrows")	
			{
				navCon.append("<img src='" + basePath + "img/arrow.png' id='" + thisId + "-arrowleft' style='cursor:pointer; position:absolute; top: " + (totalHeight-30) + "px; left: " + (totalWidth-90)+ "px;'/>")
				navCon.append("<img src='" + basePath + "img/pause.png' id='" + thisId + "-pausebtn' style='cursor:pointer; position:absolute; top: " + (totalHeight-30) + "px; left: " + (totalWidth-60)+ "px;'/>")
				navCon.append("<img src='" + basePath + "img/play.png' id='" + thisId + "-playbtn' style='cursor:pointer; position:absolute; top: " + (totalHeight-30) + "px; left: " + (totalWidth-60)+ "px;'/>")
		
				if(jQuery.browser.msie || jQuery.browser.opera)
				navCon.append("<img src='" + basePath + "img/arrowie.png' id='" + thisId + "-arrowright' style='cursor:pointer; position:absolute; margin:auto; top: " + (totalHeight-30) + "px; left: " + (totalWidth-30)+ "px;'/>")
				else
				navCon.append("<img src='" + basePath + "img/arrow.png' class='flipped' id='" + thisId + "-arrowright' style='cursor:pointer; position:absolute; top: " + (totalHeight-30) + "px; left: " + (totalWidth-30)+ "px;'/>")
				
				
				
			}
			else
			{
				navCon.append(jQuery(("#" + navigationDivId)))
			}	
			
			
			$(this).find('#' + thisId + '-arrowleft').css('opacity' , '0.5')
			$(this).find('#' + thisId + '-pausebtn').css('opacity' , '0.5')
			$(this).find('#' + thisId + '-playbtn').css('opacity' , '0.5')
			$(this).find('#' + thisId + '-playbtn').css('visibility' , 'hidden')
			
			$(this).find('#' + thisId + '-arrowright').css('opacity' , '0.5')
			$(this).find('#' + thisId + '-arrowleft').mouseover(function() {
			  	navCon.find('#' + thisId + '-arrowleft').css({
					'opacity' : '0.9'
				})
				});
				
			$(this).find('#' + thisId + '-arrowleft').mouseout(function() {
			  	navCon.find('#' + thisId + '-arrowleft').css({
					'opacity' : '0.5'
				})
				});
			$(this).find('#' + thisId + '-pausebtn').mouseover(function() {
			  	navCon.find('#' + thisId + '-pausebtn').css({
					'opacity' : '0.9'
				})
				});
				
			$(this).find('#' + thisId + '-pausebtn').mouseout(function() {
			  	navCon.find('#' + thisId + '-pausebtn').css({
					'opacity' : '0.5'
				})
				});
			$(this).find('#' + thisId + '-playbtn').mouseover(function() {
			  	navCon.find('#' + thisId + '-playbtn').css({
					'opacity' : '0.9'
				})
				});
				
			$(this).find('#' + thisId + '-playbtn').mouseout(function() {
			  	navCon.find('#' + thisId + '-playbtn').css({
					'opacity' : '0.5'
				})
				});
			$(this).find('#' + thisId + '-arrowright').mouseover(function() {
					
					
			  	navCon.find('#' + thisId + '-arrowright').css({
					'opacity' : '0.9'
				})
				});
				
			$(this).find('#' + thisId + '-arrowright').mouseout(function() {
			  	navCon.find('#' + thisId + '-arrowright').css({
					'opacity' : '0.5'
				})
				});
			
			
			$(this).find('#' + thisId + '-arrowleft').click(function() 
			{
				if(currNr==0)
				gotoImage(nrChildren-1)
				else
				gotoImage(currNr-1)
			});
			$(this).find('#' + thisId + '-pausebtn').click(function(){
				if(paused==true)
				paused=false;
				else
				paused=true;
				
				
				navCon.find('#' + thisId + '-pausebtn').css('visibility' , 'hidden')
				navCon.find('#' + thisId + '-playbtn').css('visibility' , 'visible')
			});
			$(this).find('#' + thisId + '-playbtn').click(function(){
				
				paused=false;
				gotoNext();
				
				navCon.find('#' + thisId + '-pausebtn').css('visibility' , 'visible')
				navCon.find('#' + thisId + '-playbtn').css('visibility' , 'hidden')
				
			});
			$(this).find('#' + thisId + '-arrowright').click(function() 
			{
				gotoNext();
			});
			
			if(!jQuery.browser.msie)
			{
				$(this).mouseover(function() 
				{
					navCon.stop();
					navCon.animate({
						opacity:1
					},1000)
				});
				$(this).mouseout(function() 
				{
					navCon.stop();
					navCon.animate({
						opacity:0
					},1000)
				});
			}
		
		}
		var buttonPos=0;
		if(buttonsAlign=="right")
		buttonPos=totalWidth - (thumbSpace + thumbWidth) * (nrChildren) - 5;
		
		if(buttonsAlign=="center")
		buttonPos=totalWidth/2 - ((thumbSpace + thumbWidth) * (nrChildren))*0.5;
		
		
		if(navigationType=="numbers"){
			for(i=0;i<nrChildren;i++)
			{
				navCon.append("<div class='rounded' style='position:absolute; cursor:pointer; font-size:11px; background-color: #000000; padding:4px; padding-left:8px; padding-right:8px; left:" + buttonPos + "px; top:" + (totalHeight-thumbHeight-5) + "px;'>" + (i+1) + "</div>")
				if(i!=0)
				navCon.children().eq(i).css('opacity', '0.7')
				buttonPos+=thumbWidth + thumbSpace;
				
				navCon.children().eq(i).click(function(){
					gotoImage(navCon.children().index(jQuery(this)))
				})
				navCon.children().eq(i).mouseout(function(){
					if(navCon.children().index(jQuery(this))!=currNr)
					jQuery(this).css('opacity', '0.7')
				})
				navCon.children().eq(i).mouseover(function(){
					if(navCon.children().index(jQuery(this))!=currNr)
					jQuery(this).css('opacity', '0.9')
				})
			}
		}
		
		if(hideOnCue=="on") for(i=0;i<imgCon.children().length;i++)
		imgCon.children().eq(i).css('opacity', '0')
		
		navCon.css({'position' : 'absolute', 'margin' : 'auto', 'top' : '0px', 'z-index' : '90'})
		images.css({'position' : 'absolute', 'margin' : 'auto'})
		
		var nrLoaded = 0;
		totalImages=images.length;
		var started=false;
		for (i = 0; i < nrChildren; i++) {
			images.eq(i).load(checkLoaded)
			if(images.eq(i)[0].nodeName!='IMG')
			totalImages--;
			
		}
		function checkLoaded()
		{
			nrLoaded++;
			if(nrLoaded>=totalImages){
			startScript();
			}
		}
		
		
		setTimeout(failSafe,5000)
		function failSafe()
		{
			//function to ensure that the slideshow has started
			if(started==false)
			startScript();
		}
		
		function startScript()
		{
			
			started=true;
			jQuery('#preloader').hide();
			
			if(!jQuery.browser.msie)
			thisDiv.css('opacity', 1)
			
			for (i = 0; i < nrChildren; i++) {
				widthArray[i] = images.eq(i).width();
				heightArray[i] = images.eq(i).height();
			}
			//console.log(widthArray)
			gotoImage(0,'on')
		}
		
		
		
		function hidePrev(){
			images.eq(tempNr).stop();
		}
		function gotoImage(arg,pfirstTime)
		{
			if(arg==currNr || started==false)
			return;
			
			if(currNr>=0)
			{
				tempNr=currNr;
				setTimeout(hidePrev,fadeTime)
				images.eq(currNr).css('z-index',lastIndex)	
			}
			
			
			for(i=0;i<nrChildren;i++)
			if(i==currNr || i==arg)
			images.eq(i).css('visibility','visible')
			else
			images.eq(i).css('visibility','hidden')
			
			
			images.eq(arg).css('z-index', lastIndex + 1)
			lastIndex++;
			
			if(images.eq(arg).attr("data-slideshowTime")===0 || images.eq(arg).attr("data-slideshowTime")==undefined)
			slideshowTime=5;
			else
			slideshowTime=parseFloat(images.eq(arg).attr("data-slideshowTime"));
		
			
			
			
			if(images.eq(arg).attr("data-transitionTime")=='' || images.eq(arg).attr("data-transitionTime")==undefined)
			transitionTime=5;
			else
			transitionTime=parseFloat(images.eq(arg).attr("data-transitionTime"))*1000;
			
			initialPosition=images.eq(arg).attr("data-initialPosition");
			finalPosition=images.eq(arg).attr("data-finalPosition");
			
			realImageWidth=widthArray[arg];
			realImageHeight=heightArray[arg];
			
			if(realImageWidth===0)
			{
				realImageWidth=totalWidth;
				realImageHeight=totalHeight;
			}
			
			cacheObject=images.eq(arg);
			if(cacheObject.attr("data-href")!=undefined && cacheObject.attr("data-href")!='')	{
				cacheObject.css('cursor','pointer')
				
				if(cacheObject.attr("data-target")=="_self"){
				cacheObject.click(function(){
					location.href=cacheObject.attr("data-href");
				})
				}
				else
				cacheObject.click(function(){
					window.open(cacheObject.attr("data-href"));
				})
				
			}
			
			
			var auxcenter = -(realImageWidth * parseFloat(images.eq(arg).attr("data-initialZoom")) - totalWidth) * 0.5;
			var auxright = auxcenter*2;
			
			var auxcenter2 = -(realImageHeight * parseFloat(images.eq(arg).attr("data-initialZoom")) - totalHeight) * 0.5;
			var auxright2 = auxcenter2*2;
			
			
			//****************INITIAL POSITION***********************
			if(initialPosition=="topLeft")
			{
				images.eq(arg).animate({
				left:0,
				top:0,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			
			if(initialPosition=="topCenter")
			{
				images.eq(arg).animate({
				left:auxcenter,
				top:0,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			if(initialPosition=="topRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:0,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			
			if(initialPosition=="middleLeft")
			{
				images.eq(arg).animate({
				left:0,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			
			if(initialPosition=="middleCenter")
			{
				
				images.eq(arg).animate({
				left:auxcenter,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			
			
			if(initialPosition=="middleRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			if(initialPosition=="bottomLeft")
			{
				images.eq(arg).animate({
				left:0,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			if(initialPosition=="bottomCenter")
			{
				images.eq(arg).animate({
				left:auxcenter,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			if(initialPosition=="bottomRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-initialZoom"))*realImageWidth
				},0)
			}
			
			
			auxcenter = -(realImageWidth * parseFloat(images.eq(arg).attr("data-finalZoom")) - totalWidth) * 0.5;
			auxright = auxcenter*2;
			
			auxcenter2 = -(realImageHeight * parseFloat(images.eq(arg).attr("data-finalZoom")) - totalHeight) * 0.5;
			auxright2 = auxcenter2*2;
			
			
			
			
			
			//****************FINAL POSITION***********************
			if(finalPosition=="topLeft")
			{
				images.eq(arg).animate({
				left:0,
				top:0,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			if(finalPosition=="topCenter")
			{
				images.eq(arg).animate({
				left:auxcenter,
				top:0,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			if(finalPosition=="topRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:0,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			if(finalPosition=="middleLeft")
			{
				images.eq(arg).animate({
				left:0,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			if(finalPosition=="middleCenter")
			{
				images.eq(arg).animate({
				left:auxcenter,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			if(finalPosition=="middleRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			if(finalPosition=="bottomLeft")
			{
				images.eq(arg).animate({
				left:0,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			if(finalPosition=="bottomCenter")
			{
				images.eq(arg).animate({
				left:auxcenter,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			if(finalPosition=="bottomRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},{	duration: transitionTime,	queue: false},easingType)
			}
			
			//END FINAL TRANSITION
			
			
			//HANDLE TRANSITIONS
			for(i=0;i<images.length;i++){
				if(i==currNr||i==arg)
				descriptionArray[i].css('visibility', 'visible')
				else
				descriptionArray[i].css('visibility', 'hidden')
			}
			
			
			descriptionArray[arg].animate({
				top:-15,
				opacity:0
			},0).animate({
				top:0,
				opacity:1
			},{queue:false, duration:fadeTime})
			
			//descriptionArray[arg].children().eq(1).css({opacity: 1},{queue:false, duration:fadeTime});
			/*if(jQuery.browser.msie==2){
				descriptionArray[arg].children().eq(0).css({opacity:0})
				descriptionArray[arg].children().eq(0).css({opacity: descriptionOpacityArray[arg]},{queue:false, duration:fadeTime});
				descriptionArray[arg].children().eq(1).css({opacity:0})
				descriptionArray[arg].children().eq(1).css({opacity: 1},{queue:false, duration:fadeTime});
			}*/
			
			if(currNr>=0)
			{
				
				descriptionArray[currNr].animate({
					opacity:0
				},{queue:false, duration:fadeTime})
				
				/*if(jQuery.browser.msie==2){
					descriptionArray[currNr].children().eq(0).animate({opacity: 0},{queue:false, duration:fadeTime});
					descriptionArray[currNr].children().eq(1).animate({opacity: 0},{queue:false, duration:fadeTime});
				}*/
				
				
				if(navigationType=="numbers")
				{
					navCon.children().eq(currNr).css('opacity','0.7')
					navCon.children().eq(arg).css('opacity','1')
					
				}
				if (navigationType == "bullets") {
					jQuery('#bullets').children().eq(currNr).css('opacity','0.5')
					jQuery('#bullets').children().eq(arg).css('opacity','1')
				}
				
				
			}
			if(pfirstTime!='on')
			images.eq(arg).css({opacity:0})
				
			
			
			images.eq(arg).animate({
			opacity:1
			},{queue:false, duration:fadeTime})
			
			currNr=arg;
			tempCount=0;
			
			setTimeout(setIndex,2000)
			
		}
		function setIndex(){
			//alert('ceva')
			//images.eq(currNr + 1).css('z-index', lastIndex + 1)
			//lastIndex++;
		}
		function checkTimer()
		{
			if(paused==false)
			tempCount++;
			
			
			if(tempCount>=slideshowTime)
			{
				gotoNext();
				
			}
		}
		function gotoNext()
		{
			
			if(currNr==nrChildren-1)
			gotoImage(0)
			else
			gotoImage(currNr+1)
		}
		
		
		intervalID = setInterval(checkTimer,1000)
		
		$.fn.kenBurns.gotoImage = function(arg){
			gotoImage(arg)
		}
			
        return this;

		}); // end each

	}
	
	

})(jQuery);

