var active_one  = 1; 
var the_one; 
var api;
var caseispaused=false;
var cur_slide=0;
var slide_auto_go=true;

$(window).load(function(){
	
	$("a#case_continue").live('click',function(e){
		e.preventDefault();
		
		SheckIfStopped ();
		t2=setTimeout("SheckIfStopped()", 4000);
		
	});
	
	
	$("a#playercontinue").live('click',function(e){
		e.preventDefault();
		
		api.play();
		
		
	});
	
	$(".MyScrollable-container").mouseleave(function(){
		
		api.play();
		

	});
	
	
	$("img.colored").css('display','none');
	$("img.colored:first").css('display','block');
	
 	$("#slides_group").show();
	dif  = ($(window).width() - 980)  / 2 ; 
	start_point = Math.ceil(1479 - 1000 - dif) ; 
	$(".MyScrollable").css("left",  -1 * start_point)	; 
	the_one =  $(".MyScrollable").scrollable({speed:1300,circular: true, 
		onBeforeSeek: function() {
			
			var sThis2=this;
			var willbe=sThis2.getItems().eq(this.getIndex()).find(".colored").parent().index();
			
			$("img.colored").fadeOut(500);
			$("#slider-control li").removeClass("active");
			
			if (slide_auto_go){
				if (willbe==4)
				willbe=0;
				$("#slider-control li:eq(" + willbe + ")").addClass("active");
				
				
			}
			else
			{
				
				$("#slider-control li:eq(" + cur_slide + ")").addClass("active");
				slide_auto_go=true;
				
			}
			
		},
		  onSeek: function() {
			var sThis = this;
			
			sThis.getItems().eq(this.getIndex()).find(".colored").fadeIn("300",function() {
				//the_one.fadeIn(400);
				//jQuery(this).css({zIndex: 2});
				
				
				//$("#slider-control li:eq(" + ($(this).parent().index()-1) + ")").addClass("active");
				/*jQuery('.cloned').find('.grayscale').css("zIndex", 2);
				jQuery('.cloned').find('.colored').css("zIndex", 1);*/
			});
		}
	}).autoscroll({ autoplay:true , interval:4000});
	
	
	window.api = the_one.data("scrollable");
	
/*	$("#slider-control li a").each(function(i){
		$(this).click(function(e){
			api.pause();
			
			$("#slider-control li").removeClass("active"); 
			$(this).parent().addClass("active");
			the_index = $(this).parent().index();
			var tme=Math.abs(api.getIndex()-the_index)*700;
			if (tme>2500)
			tme=2500;
			slide_auto_go=false;
			cur_slide=the_index;
			the_one.data("scrollable").seekTo(the_index, tme);
			e.preventDefault();
		});
	}); */
	/* ======================================================================= */
	/* ==================== Case Study ======================================= */
	/* ======================================================================= */
	if ($(".thumbs_slider").length > 0 ) {
		$("#image_view a").remove();
		new_anc = Array(); 
		$(".items a").each (function(i){
				the_index = i ; 
				my_img = $(this).find("img.colored"); 
				my_img.addClass("mine_"+i); 
				new_src = my_img.attr("src").replace (/&h=\d+/ig, "").replace (/&w=\d+/ig, "").replace (/lib\/timthumb.php\?src=/ig, ""); 
				new_anc[i] =$("<a class='mine_"+i+"' style='display:block; position:absolute; margin-top:20px; margin-left:20px; z-index:10' href='"+new_src+"' rel='overlay' title='"+my_img.attr("alt")+"'>+</a>") ;

				$(this).click(function(e){ e.preventDefault();
				caseispaused=true;
				
				the_index  = $(this).index(); ThumbsSlide(the_index); busy=1; 
			
				 });
		}); 
		
		//$(".items").mouseleave(function(){}); 
		
		$("#image_view a").each (function(i){
			$(this).css("zIndex",  $(this).css("zIndex")-5  ) ; 
		}) ; 
		$(".items").width(($(".items a").length + 2)* ($(".items a:eq(0)").width() + 14));
		$(".thumbs_slider .items a:eq(0) img.gray").css("zIndex", 1).fadeOut(1); 
		for (n = new_anc.length-1; n >=0 ; n-- ){
			$("#image_view").prepend ( new_anc[n] );   
		}
		$("#image_view a").hide(1);
		$("a[rel='overlay']").colorbox();
		$("#image_view a:eq(0)").show(1);
		i1=setInterval("ThumbsSlide()", 4000) ; 
	}
	
	
});
	var NextThumb = "";  
	cur_act_one = 0 ; 
	var busy = 0; 
	function SheckIfStopped (){
		if (busy  == 1) {
			busy  = 0; 
		}
	}
	function ThumbsSlide(NextOne){
			if (!NextOne) if (busy==1) return  0 ; 
			if ($(".thumbs_slider .items a").length < 1) return 0; 
			if (!NextOne) { NextOne = 1; freq = "slow"; }
			else {freq = NextOne*500 }
			NextThumb = $(".thumbs_slider .items a:eq("  + NextOne + ")");
			cur_act_one = cur_act_one + NextOne; 
			
			if (cur_act_one >= $("#image_view a").length )	 {
				cur_act_one = cur_act_one - $("#image_view a").length   ;
				
			}
			n=0; 
			the_length = $(".thumbs_slider .items a").length; 
			CurrentObj  = $(".thumbs_slider .items a:lt("+(NextOne)+") "); 
			CurrentObj.find ("img.gray").css("zIndex", 2); 
			NewThum = CurrentObj.clone(); 
			NewThum.insertAfter($(".thumbs_slider .items a:last-child"));	
			CurrentObj.animate({'marginLeft':-182}, freq, function(){CurrentObj.remove(); }); 
			CurrentObj.next().find("img.gray").fadeOut(600);  
			NewThum.bind("click", function (e){e.preventDefault(); the_index  = $(this).index(); ThumbsSlide(the_index); busy=1; 
			if(caseispaused==false){
			t2=setTimeout("SheckIfStopped()", 4000)}
			 ; }); 
			NewThum.find("img.gray").fadeIn(100);
			the_img = NextThumb.find("img.colored"); 
			img_src = the_img.attr("src") ;
			title  	= the_img.attr("alt") ;
			new_src = img_src.replace (/&h=\d+/ig, "").replace (/&w=\d+/ig, "").replace (/lib\/timthumb.php\?src=/ig, ""); 
			$("#image_view").find("img").css("zIndex", 2).css("position", "absolute");  
			new_image  = $("<img src='" + new_src+ "' style='z-index:1; position:absolute' rel='overlay' />"); 
			$("#image_view").append (new_image);

			$("#image_view").find("img:eq(0)").fadeOut(600, function(){ 
					$(this).next().css("zIndex", 2).attr ("rel", "");  
					$(this).remove();
					$("#image_view a").hide(1);
					$("#image_view").find("a:eq(" +  cur_act_one + ")").css("zIndex", 10).show();
				}
			) 
	}
