﻿// JavaScript Document

(function( $ ){ 
  
  
  var defaults = {
	 
	 width: 1400,
	 
	 height: 400,
	 
	 speed: 4000, 
	 
	 dotYoffset : 20,
	 
	 dotXoffset : 5,
	 
	 showTitles : false,
	 
	 text: '',
	 
	 sliderBkgdUrl: '',
	 
	 sliderWidth: 230,
	 
	 sliderHeight: 110,
	 
	 sliderListWidth: 135,
	 
	 sliderListHeight: 80,
	 
	 linkLabel : 'Read More'
	  
  }


  var methods = {
	  
	  
     init : function( options ) { 
		return this.each(function(){  
			var pluginkey = Math.round(Math.random()*11000);					  
			options = $.extend(true, defaults, options); 							  
       		var $this = $(this),
            	data = $this.data('easySlideShow ');  
		 
		 
		 
         if ( ! data ) { 
		 
		 //SETUP 
		 
         	var images  = $('img', $this);  
			
			//WRAPPER
			
			var preloader = $('<div class="preLoader"></div>');
			$this.wrap('<div class="Impress-Wrapper'+pluginkey+'"/>').append(preloader);
			
			var impressWrapper = $('.Impress-Wrapper'+pluginkey).css({
									  //width:'100%',
									  //height:'100%',
									  overflow:'hidden', 
									  height: '100%',
									  position:'relative',
									  width: options.width,
									  margin: '0 auto'
									  });
 
 	
			//if header Box
 			if(options.showTitles){
				
				var textBlocks 	  = [],
				titleContainerCss = 'position:absolute; right:150px; top:140px; width:'+options.sliderWidth+'px; height:'+options.sliderHeight+'px; z-index:99; background:url('+options.sliderBkgdUrl+') center no-repeat;';
				titleContainer    = $('<div class="TitleContainer" style="'+titleContainerCss+'"></div>'),
				label 			  = '<a href="'+'#0000'+'" style="position:absolute; left:84px; bottom:3px;color:#d4d4d4;">'+options.linkLabel+'</a>'; 
				titleList	      = $('<ul style="position:absolute; height:'+options.sliderListHeight+'px; width:'+options.sliderListWidth+'px; top:3px; margin:0;left:28px; overflow:hidden;"></ul>'),
				contolCss 		  = 'height:100%;width:26px;position:absolute;top:0;cursor:pointer;',
				rightControl      = $('<div class="RightControl" style="'+contolCss+'right:0;background:right center url('+options.sliderBkgdUrl+') no-repeat;"></div>'), 
				leftControl       = $('<div class="LeftControl" style="'+contolCss+'left:0;background:0 center url('+options.sliderBkgdUrl+') no-repeat;"></div>');
				
				$([leftControl, rightControl]).each(function(t, e){ 
															 switch(t) {
																 case 0:
																 	 var link = function(){core.fadeToPrev(true);}; 
																 break;
																 case 1:
																 	 var link = function(){core.fadeToNext(true);}; 
																 break;
																 
															 }
															 var i = $(this).click(link);
 															 i.css({opacity:0}).hover(function(){ 
																								i.css({opacity:0.9});																	  
																							
																								}, function(){
																										  
																								i.css({opacity:0})
																								
																								});
															 
															
															
															
															});
				
				
				titleContainer.append(label,rightControl,titleList,leftControl); 
				
				$this.prepend(titleContainer); 
				
				var t=0;
				$.each(options.text,function(i, e ){ 
											 t++;  
											 //var left = ''; 
											 var obj 		 = $('<li style="list-style: none;text-align:center;position:absolute;left:'+options.sliderListWidth*2*t+'px;padding:0 5%; width:90%; height:100%;"></li>');
											 obj.left  		 = options.sliderListWidth * 2 * t;
											 obj.title 		 = $('<h3 style="display:block; width:100%; position:absolute; top:0; margin:0;">'+i+'</h3>');
											 obj.description = $('<div style="display:block; width:100%; position:absolute; top:'+30+'px;">'+e+'</div>'); 
											 obj.append(obj.title, obj.description);
											 titleList.append(obj);
											 textBlocks.push(obj); 
											 }); 
				
				}
			 
			
			//SET UP 
			
			//DONE SETTING UP NOW LETS LOAD SOME PHOTOS			
			
			var loadedimages = 0;
			var currentimage = 0;
			var coreTimer 	 = null;
			var animating 	 = false;
			var paused 		 = false;
			var thumbs = new Array();
			
			images.each(function(i, e){  
								 image = $(e);
								 var url = image.attr('src'); 
								 image.remove(); 
								 var thisimg = new Image(); 
								 thisimg.src = url; 
								 thisimg.height = options.height;
								 thisimg.width  = options.width;
								 var css = 'position:absolute; bottom:0; display:block; width:'+options.width+';px height:'+options.height+'px; left:50%; margin-left:-'+options.width/2+'px;' ; 
								 thisimg.style.cssText = css;
								 $(thisimg).css({ opacity:0 });
								 images[i] = thisimg; 
						 		 $(thisimg).load(function(){
												loadedimages++;						  
												if( images.length == loadedimages ){intro(); }		  
														  
														  });
								 
			// create controls
								var circle = document.createElement( 'div' ); 
								var myX    = (images.length * (options.dotXoffset * 2 + 10) + 20)-(options.dotXoffset * 2 + 10) * (i+1);
								var myY    = options.dotYoffset;
								circle.style.cssText = 'position:absolute; bottom:'+myY+'px; right:' +myX+'px; background:blue; height:10px; width:10px;';
								impressWrapper.append(circle); 
								thumbs.push(circle);
								
			});//  images each
			
			
			//DEFINE CORE FUNCTIONS  
			
			var core = { 
			
						fadeToImage : function (image, i){ 
								var current = $(images[currentimage]);
								var type = typeof(image);
								
								switch ( type ) {
									case 'string':
										var next = $('.'+image);
									break; 
									case 'object':
										var next = image; 
									break; 
									default:
										 return;
									break; 
									} 
									animating = true;
									clearTimeout(coreTimer);
									$(thumbs[currentimage]).css({background:'blue'});
									currentimage = i; 									
									$(thumbs[currentimage]).css({background:'white'});
									current.animate({opacity:0}, 'slow', function(){animating=false;});  
									next.animate({opacity:1}, 'slow', function(){core.openPanels(i);}); 
						
							
						},
				
				
						fadeToNext : function (repeat){ 
									if(animating)return;
									clearTimeout(coreTimer);
 									var current = $(images[currentimage]);
									var next = Number(currentimage) + 1;
									if( next == images.length ) { next = 0; } 
									animating = true;  
									
									var callback = function(){animating=false; };
									if(repeat){callback = function(){animating=false; coreTimer = setTimeout(thefunk, options.speed );}}
									
									current.animate({opacity:0}, 'slow');
									$(images[next]).animate({opacity:1}, 'slow', callback);
									
								if(options.showTitles){		 
									$(textBlocks[currentimage].description).animate({left:-options.sliderListWidth*2}, 'slow', function(){$(this).css({left:0});}); 
									$(textBlocks[currentimage]).delay(50).animate({left:-options.sliderListWidth*2}, 'slow', function(){ $(this).css({left:(textBlocks[currentimage].left)}) }); 
									
									var title  = textBlocks[currentimage].title; 
									var des    = textBlocks[currentimage].description; 
									var margin = (options.sliderListHeight - (title.outerHeight()+des.outerHeight())) / 2;
									if(margin < 0) {margin = 0;}
									textBlocks[currentimage].title.css({top:margin})
									
									$(textBlocks[currentimage].description).css({left:options.sliderListWidth,  top: (title.outerHeight()+ margin) }).delay(200).animate({left:0}, 'slow'); 
									$(textBlocks[currentimage]).animate({left:0}, 'slow');
								}//if
									
									$(thumbs[currentimage]).css({background:'blue'});
									currentimage = next; 								
									$(thumbs[currentimage]).css({background:'white'});



							},
							
						fadeToPrev : function (restart){
									if(animating)return;
									clearTimeout(coreTimer);
 									var current = $(images[currentimage]);
									var prev = Number(currentimage) - 1;
									if( prev < 0 ) { prev = images.length - 1; }  
									animating = true;  
									
									var callback = function(){animating=false; };
									if(restart){callback = function(){animating=false; coreTimer = setTimeout(thefunk, options.speed );}}
									
									current.animate({opacity:0}, 'slow');
									$(images[prev]).animate({opacity:1}, 'slow', callback);
									
									
								if(options.showTitles){	
									$(textBlocks[currentimage].description).animate({left:-options.sliderListWidth*2}, 'slow', function(){$(this).css({left:0});}); 
									$(textBlocks[currentimage]).delay(50).animate({left:-options.sliderListWidth*2}, 'slow', function(){ $(this).css({left:(textBlocks[currentimage].left)}) }); 
									
									var title  = textBlocks[prev].title; 
									var des    = textBlocks[prev].description; 
									var margin = (options.sliderListHeight - (title.outerHeight()+des.outerHeight())) / 2;
									if(margin < 0) {margin = 0;}
									textBlocks[prev].title.css({top:margin}); 
									$(textBlocks[prev].description).css({left:options.sliderListWidth,  top: (title.outerHeight()+ margin) }).delay(200).animate({left:0}, 'slow'); 
									$(textBlocks[prev]).animate({left:0}, 'slow'); 
								}
									$(thumbs[prev]).css({background:'white'});
									$(thumbs[currentimage]).css({background:'blue'});
									currentimage = prev; 								
									
									
									
									
							
							
							} 
					
			}// core functions
			
			 
							
			var thefunk = function(){core.fadeToNext(true);}; 
			
			//intro function
 			function intro() { 

				preloader.fadeOut();
				$this.append(images);   
				$(thumbs[currentimage]).css({background:'white'});
				$(images[currentimage]).animate({opacity:1}, function(){var coreTimer = setTimeout(thefunk, options.speed ); });  
				
				if(options.showTitles){
					var title  = textBlocks[currentimage].title; 
					var des    = textBlocks[currentimage].description; 
					var margin = (options.sliderListHeight - (title.outerHeight()+des.outerHeight())) / 2;
					if(margin < 0) {margin = 0;}
					textBlocks[currentimage].title.css({top:margin});
					$(textBlocks[currentimage].description).css({left:options.sliderListWidth,  top: (title.outerHeight()+ margin) }).delay(200).animate({left:0}, 'slow'); 
					$(textBlocks[currentimage]).animate({left:0}, 'slow');
				}
				
			}//intro  
			
			
			//end
           $(this).data('easySlideShow ', {
               target : $this,
               easySlideShow  : 'easySlideShow '
           });//data

         }
       });
     },//init
	 
	 
     destroy : function( ) {

       return this.each(function(){

         var $this = $(this),
             data = $this.data('easySlideShow ');

         // Namespacing FTW
        
         $this.removeData('easySlideShow ');

       })

     }
	 
	 
	 
  };

  $.fn.easySlideShow  = function( method ) { 
	if ( methods[method] ) {
      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.easySlideShow ' );
    }    
  
  };

})( jQuery );
