(function($){

	$.fn.fgxSlider = function(options){

		
		var opts = $.fn.extend({}, $.fn.fgxSlider.defaults, options);
		var count = 1;
		var scrollTo = "";
		var movies = new Array();
		if(location.hash){
			
			scrollTo = location.hash.substring(1,location.hash.length) - 1;
		}
		
		return this.each(function(){
		
			$this = $(this);
		
			
						Cufon.replace('h3');
					/*	$("div.scrollable").scrollable({
							size: 1,
							keyboard: false,
							onBeforeSeek: function(event,index){
							
								if(index > 1){
									return false;
								}
								
						}
						});*/
						$("div.scrollable").scrollable({size:7,speed:800});
						
						var totWidth=0;
						var positions = new Array();
						//$(opts.menu).html(json.value);
						
						$(opts.slides + " " + opts.slide).each(function(i){
							/* Traverse through all the slides and store their accumulative widths in totWidth */
							var slideID = $(this).attr("id");
							var slideIDArr = slideID.split("_");
							positions[i]= totWidth;
							var mID = parseInt(slideIDArr[1]);
							movies[mID] = i;
							totWidth += $(this).width();
							/* The positions array contains each slide's commulutative offset from the left part of the container */
							if(!$(this).width())
							{
								alert("Please, fill in width & height for all your images!");
								return false;
							}
						});
						$(opts.slides).width(totWidth);
						/* Change the container div's width to the exact width of all the slides combined */
						
						
						$(opts.menu  + " a").each(function(){
						
							$(this).click(function(e){
								var $link = $(this); 
							
								/* On a thumbnail click */
								//alert("click");
								var id = $(this).attr("id");
								var movieID = id.substring(id.indexOf("_")+1,id.length);
								var paramArr = movieID.split("_");

								if( $("#slide_" + paramArr[0]).text() == "" || $("#slide_" + paramArr[0]).text() == null ){
									 $.getJSON("php_include/getMovieDetails.php",{movieID: movieID, type: opts.type},function(json){

										 // alert("#slide_" + json.movieID);
										 $("#slide_" + json.movieID).append(json.value);

										 Cufon.replace('h3');
										 	$('div.menuItem').removeClass('act').addClass('inact');
										 	        $link.parent().addClass('act');
										 	
										 	        var pos = $link.parent().prevAll('.menuItem').length;
										 	         	
										 			$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
										 	        /* Start the sliding animation */
										 			
										 	 		//e.preventDefault();
										 	        /* Prevent the default action of the link */
													 
										 			//}//if
										 			
										 			 /*
										 	 		 * Add read more functionality										 	 		 * 
										 	 		 */
										 	 		
										 	 		$("#slide_" + paramArr[0] + " a.readMoreLink").click(function(){
										 	 			$("#slide_" + paramArr[0] + " .origDetails").fadeOut(500,function(){
										 	 				$("#slide_" + paramArr[0] + " .moreText").fadeIn(500);
										 	 				
										 	 			});
										 	 			
										 	 		});
										 	 		
										 	 		$("#slide_" + paramArr[0] + " a.movieReadless").click(function(){
										 	 			$("#slide_" + paramArr[0] + " .moreText").fadeOut(500,function(){
										 	 				$("#slide_" + paramArr[0] + " .origDetails").fadeIn(500);
										 	 				
										 	 			});
										 	 			
													});
										 	 		
										 			
										 			
										 	 		$("#slide_" + paramArr[0] + " .timeRow:eq(0)").show();
										 	 		$("#slide_" + paramArr[0] + " #timeSelect").change(function(){
										 	 			//alert("changing times if: " + paramArr[0]);
										 	 			$("#slide_" + paramArr[0] + " .timeRow").hide();
										 	 			//alert("#slide_" + paramArr[0] + " #times_" + $(this).val());
										 	 			$("#slide_" + paramArr[0] + " #times_" + $(this).val()).show();
										 	 			
										 	 		});
										 			
												
									 });
								 		
									
								}//if not set yet
								else{
								
									 	Cufon.replace('h3');
									 	$('div.menuItem').removeClass('act').addClass('inact');
									 	        $link.parent().addClass('act');
									 	
									 	        var pos = $link.parent().prevAll('.menuItem').length;
									 	         	
									 			$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
									 	        /* Start the sliding animation */
									 	
									 	 		e.preventDefault();
									 	        /* Prevent the default action of the link */
									 	 
									 	 		 /*
									 	 		 * Add read more functionality										 	 		 * 
									 	 		 */
									 	 		
									 	 		$("#slide_" + paramArr[0] + " a.readMoreLink").click(function(){
									 	 			$("#slide_" + paramArr[0] + " .origDetails").fadeOut(500,function(){
									 	 				$("#slide_" + paramArr[0] + " .moreText").fadeIn(500);
									 	 				
									 	 			});
									 	 			
									 	 		});
									 	 		
									 	 		$("#slide_" + paramArr[0] + " a.movieReadless").click(function(){
									 	 			$("#slide_" + paramArr[0] + " .moreText").fadeOut(500,function(){
									 	 				$("#slide_" + paramArr[0] + " .origDetails").fadeIn(500);
									 	 				
									 	 			});
									 	 			
												});
									 	 		
									 	 		$("#slide_" + paramArr[0] + " .timeRow:eq(0)").show();
									 	 		$("#slide_" + paramArr[0] + " #timeSelect").change(function(){
									 	 			$("#slide_" + paramArr[0] + " .timeRow").hide();
									 	 			$("#slide_" + paramArr[0] + " #times_" + $(this).val()).show();
									 	 			
									 	 		});
									 	 			
								}
								
								
							});
							$( opts.menu + ' div.menuItem:first').addClass('act').siblings().addClass('inact');
						});
						
						/* On page load, mark the first thumbnail as active */
						if(scrollTo != ""){
							
							//$('#slides').stop().animate({marginLeft:-positions[2]+'px'},450);
							 //$link.parent().addClass('act');
							
								var id = $('.menuItem:eq('+scrollTo+') a').attr("id");
								var movieID = id.substring(id.indexOf("_")+1,id.length);
							
								 $.getJSON("php_include/getMovieDetails.php",{movieID: movieID, type: opts.type},function(json){
										
									 	// alert("#slide_" + json.movieID);
									 	 
									 	 $("#slide_" + json.movieID).html(json.value);
									 	Cufon.replace('h3');
									 	$('div.menuItem').removeClass('act').addClass('inact');
									 	        //alert(movies.toSource());
									 	        
									 	        var pos = movies[parseInt(movieID)];
									 	        
									 	       $('.menuItem:eq('+scrollTo+')').addClass("act");
									 	        
									 	        //alert(pos);
									 	         
									 			$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
									 	
									 	        /* Start the sliding animation */
									 	
									 	 		//alert("#slide_" + json.movieID);
									 			$("#slide_" + json.movieID + " .timeRow:eq(0)").show();
									 	 		$("#slide_" + json.movieID + " #timeSelect").change(function(){
									 	 			//alert("changing times if: " + paramArr[0]);
									 	 			$("#slide_" + json.movieID + " .timeRow").hide();
									 	 			//alert("#slide_" + paramArr[0] + " #times_" + $(this).val());
									 	 			$("#slide_" + json.movieID + " #times_" + $(this).val()).show();
									 	 			
									 	 		});
												 
									 //}//if
								 });
									
					 		
					 	        /* Start the sliding animation */
					
						}
						
						 /*
			 	 		 * Add read more functionality										 	 		 * 
			 	 		 */
			 	 		
			 	 		$(".slide:eq(0) a.readMoreLink").click(function(){
			 	 			
			 	 			$(".slide:eq(0) .origDetails").fadeOut(500,function(){
			 	 				$(".slide:eq(0) .moreText").fadeIn(500);
			 	 				
			 	 			});
			 	 			
			 	 		});
			 	 		
			 	 		$(".slide:eq(0) a.movieReadless").click(function(){
			 	 			$(".slide:eq(0) .moreText").fadeOut(500,function(){
			 	 				$(".slide:eq(0) .origDetails").fadeIn(500);
			 	 				
			 	 			});
			 	 			
						});
			 	 		
			 	 		$(".timeRow:eq(0)").show();
			 	 		$("#timeSelect").change(function(){
			 	
			 	 			$(".timeRow").hide();
			 	 			$("#times_" + $(this).val()).show();
			 	 			
			 	 		});
			 	 		
			 	 		
			 	 		
		});
	
	};//fgxSlider
	

  $.fn.fgxSlider.defaults = {
		slides: "#slides",
		slide: ".slide",
		menu: "#menu",
		type: "current"	
		
  };//defaults

	
})(jQuery);




