(function($) {

	$.fn.kenBurns = function(options) {

		var nrChildren = 0;
		var imageWidth=710;
		var imageHeight=420;
		
		var realImageWidth=0;
		var realImageHeight=0;
		
		var thumbWidth = 32;
          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 currNr=-1;
		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 defaults = {
			imageWidth:700,
			imageHeight:420,
			navigationType:"custom",
			navigationDivId:"navCustom"
		},

		options = $.extend(defaults, options);

		this.each(function() {
		
		var thisId=$(this)[0].getAttribute('id');
		
		imageWidth=options.imageWidth;
		imageHeight=options.imageHeight;
		navigationType=options.navigationType;
		navigationDivId=options.navigationDivId;
		
		nrChildren = $(this).children().length;
		
		jQuery(this).css('position', 'relative')
		
		$(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');
		
		
		
		//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 id='" + thisId + "-descriptionText'";
		
		
		for (i = 0; i < nrChildren; i++) {
			descriptionBgDiv="<div id='" + thisId + "-descriptionBg' class='rounded' ";
			descriptionTextDiv="<div id='" + thisId + "-descriptionText' ";
			descCon.append("<div id='" + thisId + "-description' style='position:absolute; top:0px; left:0px; z-index:5;'></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:20;'>" + 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='img/arrow.png' id='" + thisId + "-arrowleft' style='cursor:pointer; position:absolute; top: " + (imageHeight-30) + "px; left: " + (imageWidth-90)+ "px;'/>")
				navCon.append("<img src='img/pause.png' id='" + thisId + "-pausebtn' style='cursor:pointer; position:absolute; top: " + (imageHeight-30) + "px; left: " + (imageWidth-60)+ "px;'/>")
				navCon.append("<img src='img/play.png' id='" + thisId + "-playbtn' style='cursor:pointer; position:absolute; top: " + (imageHeight-30) + "px; left: " + (imageWidth-60)+ "px;'/>")
		
				if(jQuery.browser.msie || jQuery.browser.opera)
				navCon.append("<img src='img/arrowie.png' id='" + thisId + "-arrowright' style='cursor:pointer; position:absolute; margin:auto; top: " + (imageHeight-30) + "px; left: " + (imageWidth-30)+ "px;'/>")
				else
				navCon.append("<img src='img/arrow.png' class='flipped' id='" + thisId + "-arrowright' style='cursor:pointer; position:absolute; top: " + (imageHeight-30) + "px; left: " + (imageWidth-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;
				
				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)
				});
			}
		
		}
		else
		{
			var buttonPos=imageWidth - (thumbSpace + thumbWidth) * (nrChildren) - 850;
			for(i=0;i<nrChildren;i++)
			{
				navCon.append("<div id='rounded2' style='position:absolute; cursor:pointer; font-size:20px; behavior: url(js/PIE.htc); left:" + buttonPos + "px; top:" + (imageHeight-thumbHeight-32) + "px;'>" + (i+1) + "</div>")
				navCon.children().eq(i).css('')
				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({'background-position' : '0 0'})
				})
				navCon.children().eq(i).mouseover(function(){
					if(navCon.children().index(jQuery(this))!=currNr)
					jQuery(this).css({'background-position' : '0 -32px'})
				})
			}
		}
		
		
		
		
		navCon.css({'position' : 'absolute', 'margin' : 'auto', 'top' : '0px', 'z-index' : '99'})
		images.css({'position' : 'absolute', 'margin' : 'auto'})
		
		
		for (i = 0; i < nrChildren; i++) {
			widthArray[i]=images.eq(i).width();
			heightArray[i]=images.eq(i).height();
		}
		
		
		
		jQuery(this).css({'position' : 'relative', 'width' : imageWidth, 'height' : imageHeight, 'overflow': 'hidden'})
		imgCon.css({'position' : 'relative', 'z-index' : '3'})
		gotoImage(0)
		
		function hidePrev()
		{
			
			images.eq(tempNr).css('visibility', 'hidden')
		}
		function gotoImage(arg)
		{
			if(arg==currNr)
			return;
			
			if(currNr>=0)
			{
				tempNr=currNr;
				images.eq(currNr).animate({
				opacity:0
				},{queue:false, duration:fadeTime})
				
				setTimeout(hidePrev,fadeTime)
				
				images.eq(currNr).css('z-index',lastIndex+1)
				
				
			}
			
			images.eq(arg).stop();
			images.eq(arg).animate({
				opacity:1
				},0)
			
			
			for(i=0;i<nrChildren;i++)
			if(i==currNr || i==arg)
			images.eq(i).css({'visibility' : 'visible'})
			else
			images.eq(i).css({'visibility' : 'hidden', 'overflow' : 'hidden', 'opacity' : '0'})
			
			images.eq(arg).css({'z-index': lastIndex})
			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=imageWidth;
				realImageHeight=imageHeight;
			}
			
			
			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")) - imageWidth) * 0.5;
			var auxright = auxcenter*2;
			
			var auxcenter2 = -(realImageHeight * parseFloat(images.eq(arg).attr("data-initialZoom")) - imageHeight) * 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")) - imageWidth) * 0.5;
			auxright = auxcenter*2;
			
			auxcenter2 = -(realImageHeight * parseFloat(images.eq(arg).attr("data-finalZoom")) - imageHeight) * 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
				},transitionTime,easingType)
			}
			if(finalPosition=="topCenter")
			{
				images.eq(arg).animate({
				left:auxcenter,
				top:0,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},transitionTime,easingType)
			}
			if(finalPosition=="topRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:0,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},transitionTime,easingType)
			}
			if(finalPosition=="middleLeft")
			{
				images.eq(arg).animate({
				left:0,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},transitionTime,easingType)
			}
			if(finalPosition=="middleCenter")
			{
				images.eq(arg).animate({
				left:auxcenter,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},transitionTime,easingType)
			}
			if(finalPosition=="middleRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:auxcenter2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},transitionTime,easingType)
			}
			if(finalPosition=="bottomLeft")
			{
				images.eq(arg).animate({
				left:0,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},transitionTime,easingType)
			}
			if(finalPosition=="bottomCenter")
			{
				images.eq(arg).animate({
				left:auxcenter,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},transitionTime,easingType)
			}
			if(finalPosition=="bottomRight")
			{
				images.eq(arg).animate({
				left:auxright,
				top:auxright2,
				width: parseFloat(images.eq(arg).attr("data-finalZoom"))*realImageWidth
				},transitionTime,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({'background-position' : '0 0'})
					navCon.children().eq(arg).css({'background-position' : '0 -32px'})
					
				}
			}
			
			
			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)
		

        return this;

		}); // end each

	}

})(jQuery);

