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

		var nrChildren = 0;
		var imageWidth=700;
		var imageHeight=420;
		var totalWidth=700;
		var totalHeight=420;
		
		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 intervalID;
		var tempCount=0;
		var paused=false;
		var transitionTime = 3000;
		var basePath="";
		
		var hideOnCue="off"
		var buttonsAlign="";
		var defaults = {
			imageWidth:700,
			imageHeight:420,
			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');
		ceva = thisId;
		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'})
		//console.log(descCon)
		
		
		
		
		//console.log(jQuery(this))
		for(i=0;i<nrChildren;i++)
		{
			imgCon.append($(this).children().eq(i).clone())
			
		}
		images=imgCon.children();
		
		
		
		var descriptionBgDiv="<div id='" + thisId + "-descriptionBg' ";
		var descriptionTextDiv="<div class=descriptionText";
		
		
		for (i = 0; i < nrChildren; i++) {
			descriptionBgDiv="<div id='" + thisId + "-descriptionBg' class='rounded' ";
			descriptionTextDiv="<div class='descriptionText'";
			descCon.append("<div id='" + thisId + "-description' style='position:absolute; top:0px; left:0px;'></div>")
			
			
			descriptionArray[i] = descCon.children().eq(i)
			descriptionArray[i].css('visibility', 'hidden')
			
			if(images.eq(i).attr("data-descriptionText")!=undefined)
			{
				
				
				
				
				
				descriptionBgDiv+="style='position: absolute;  left:" + images.eq(i).attr("data-descriptionX") + "px; top: " + (parseInt(images.eq(i).attr("data-descriptionY"))-3) + "px; z-index:98; width:" + images.eq(i).attr("data-descriptionWidth") + "px; height:" + (parseInt(images.eq(i).attr("data-descriptionHeight"))+3) + "px; background-color:" + images.eq(i).attr("data-descriptionBg") + "; opacity:" + images.eq(i).attr("data-descriptionOpacity") + "; filter:alpha(opacity=" + images.eq(i).attr("data-descriptionOpacity")*100 + ");'></div>";
				descriptionTextDiv+="style='position: absolute; text-shadow: 2px 2px 2px #000; left:" + images.eq(i).attr("data-descriptionX") + "px; top: " + images.eq(i).attr("data-descriptionY") + "px; z-index:98; width:" + images.eq(i).attr("data-descriptionWidth") + "px; height:" + images.eq(i).attr("data-descriptionHeight") + "px;  z-index:99; color:" + images.eq(i).attr("data-descriptionTextColor") + "; text-align:" + images.eq(i).attr("data-descriptionTextAlign") + "; font-size:16px;'>" + images.eq(i).attr("data-descriptionText") + "</div>";
				
				
				descriptionArray[i].append(descriptionBgDiv)
				descriptionArray[i].append(descriptionTextDiv)
				
				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 + "images/slideshow/arrow.png' id='" + thisId + "-arrowleft' style='cursor:pointer; position:absolute; top: " + (totalHeight-30) + "px; left: " + (totalWidth-90)+ "px;'/>")
				navCon.append("<img src='" + basePath + "images/slideshow/pause.png' id='" + thisId + "-pausebtn' style='cursor:pointer; position:absolute; top: " + (totalHeight-30) + "px; left: " + (totalWidth-60)+ "px;'/>")
				navCon.append("<img src='" + basePath + "images/slideshow/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 + "images/slideshow/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 + "images/slideshow/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')
				})
			}
		}
		
		
		
		
		navCon.css({'position' : 'absolute', 'margin' : 'auto', 'top' : '0px', 'z-index' : '99'})
		images.css({'position' : 'absolute', 'margin' : 'auto'})
		
		var nrLoaded = 0;
		var started=false;
		for (i = 0; i < nrChildren; i++) {
			images.eq(i).load(checkLoaded)
		}
		function checkLoaded()
		{
			nrLoaded++;

			if(nrLoaded>=images.length)
			startScript();
		}
		
		
		nrLoaded++;
		
		setTimeout(failSafe,5000)
		function failSafe()
		{
			//function to ensure that the slideshow has started
			if(started==false)
			startScript();
		}
		
		function startScript()
		{
			for(i=0;i<imgCon.children().length;i++)
			{
				if(hideOnCue=="on")
				imgCon.children().eq(i).css('opacity', '0')
				
			}
			
			
			started=true;
			thisDiv.css('opacity', 1)
			for (i = 0; i < nrChildren; i++) {
				widthArray[i] = images.eq(i).width();
				heightArray[i] = images.eq(i).height();
			}
			gotoImage(0)
		}
		
		
		
		function hidePrev()
		{
			
			images.eq(tempNr).css('visibility', 'hidden')
		}
		function gotoImage(arg)
		{
			if(arg==currNr)
			return;
			
			if(currNr>=0)
			{
				tempNr=currNr;
				
				
				setTimeout(hidePrev,fadeTime)
				
				images.eq(currNr).css('z-index',lastIndex)
				
				
			}
			
			//images.eq(arg).stop();
			//images.eq(arg).css({
			//	opacity:1
			//	})
			
			
			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")===0 || 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;
			}
			
			
			if(images.eq(arg).attr("data-link")!=undefined)
			{
				images.eq(arg).css('cursor','pointer')
				images.eq(arg).click(function(){
					location.href=images.eq(arg).attr("data-link");
				})
			}
			
			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
			},0).animate({
				top:0
			},1000)
			
			
			if(currNr>=0)
			{
				descriptionArray[arg].children().eq(0).stop();
				descriptionArray[arg].children().eq(1).stop();
				
				descriptionArray[currNr].children().eq(0).animate({
					opacity:0
				},{queue:false, duration:fadeTime})
				
				descriptionArray[currNr].children().eq(1).animate({
					opacity:0
				},{queue:false, duration:fadeTime})
				
				descriptionArray[arg].children().eq(0).animate({
					opacity:0
				},0)
				descriptionArray[arg].children().eq(1).animate({
					opacity:0
				},0)
				
				
				descriptionArray[arg].children().eq(0).animate({
					opacity:descriptionOpacityArray[arg]
				},1000).delay(slideshowTime*1000 - 2000).animate({
					opacity:0
				},1000)
				descriptionArray[arg].children().eq(1).animate({
					opacity:1
				},1000).delay(slideshowTime*1000 - 2000).animate({
					opacity:0
				},1000)
				// END HANDLE TRANSITIONS
				
				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')
				}
			}
			images.eq(arg).animate({
				opacity:0
				},{queue:false, duration:0})
				
				images.eq(arg).animate({
				opacity:1
				},{queue:false, duration:fadeTime})
			
			currNr=arg;
			tempCount=0;
			
			
		}
		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)
		}
			
		//console.log(ceva23==undefined)
        return this;

		}); // end each

	}
	
	

})(jQuery);

