//*  jQuery Image Carousel
//   Coded by Michael Wood for Zolv.
//   michael@zolv.com

$(document).ready(function(){

//  Show the elements
	$("#ImageCarousel").show();

//  Define the elements
	var controls='<div class="controls"><div class="prev"><img src="/_Assets/images/jquery-image-carousel-prev.gif" alt="Previous" /></div><div class="next"><img src="/_Assets/images/jquery-image-carousel-next.gif" alt="Next" /></div></div>';
	var scroller='<div class="img-window"><div class="img-strip"></div></div>';
	var mask='<div class="txt-mask"></div>';

//  Set the elements
	$("#ImageCarousel dl").prepend(controls);
	$("#ImageCarousel dl").prepend(scroller);
	$("#ImageCarousel dl").prepend(mask);
	$("#ImageCarousel dl dt").each(function(i){
		$("#ImageCarousel dl dt").eq(i).next("dd").eq(0).addClass("img").next("dd").eq(0).addClass("txt");
		$("#ImageCarousel dl dt").eq(i).next("dd").eq(0).children("a").eq(0).appendTo($("#ImageCarousel .img-window .img-strip"));
		});

//  Set behaviour initial state
	height_of_image=$("#ImageCarousel .img-window .img-strip img").height()
	var current_image = 0 ;
	var fadeFxOn = false ;
	var actAsReel = true ;
	var fadeToValue = 0.33 ;
	var max=$("#ImageCarousel dl dt").length-1
	$("#ImageCarousel dl dt").eq(current_image).addClass("active").next("dd").eq(0).addClass("img-active").next("dd").eq(0).addClass("txt-active");
	if(fadeFxOn){
		$("#ImageCarousel .controls .prev").fadeTo("fast",fadeToValue)
		};
	
//  Create behaviour
	$("#ImageCarousel .controls .prev").click(function(){
		if(actAsReel){
			move_to=(current_image)<=0?max:(current_image-1)
			}
		else{
			move_to=(current_image-1)<=0?0:(current_image-1)
			}
		move_img_strip($("#ImageCarousel dl dt").eq(move_to))
		if(fadeFxOn){
			$("#ImageCarousel .controls .prev").fadeTo("fast",move_to==0?fadeToValue:1)
			$("#ImageCarousel .controls .next").fadeTo("fast",1)
			}
		});
	$("#ImageCarousel .controls .next").click(function(){
		if(actAsReel){
			move_to=(current_image)>=max?0:(current_image+1)
			}
		else{
			move_to=(current_image+1)>=max?max:(current_image+1)
			}
		move_img_strip($("#ImageCarousel dl dt").eq(move_to))
		if(fadeFxOn){
			$("#ImageCarousel .controls .next").fadeTo("fast",move_to==max?fadeToValue:1)
			$("#ImageCarousel .controls .prev").fadeTo("fast",1)
			};
		});
	$("#ImageCarousel dl dt").click(function(){
		move_img_strip($(this))
		if(fadeFxOn){
			$("#ImageCarousel .controls .prev").fadeTo("fast",$("#ImageCarousel dl dt").index($(this))==0?fadeToValue:1)
			$("#ImageCarousel .controls .next").fadeTo("fast",$("#ImageCarousel dl dt").index($(this))==max?fadeToValue:1)
			};
		});

//  Create functions
	function move_img_strip(o){
		clicked=o;
	  	$("#ImageCarousel .txt-mask").fadeIn("fast",function(){
		  	$("#ImageCarousel dl dd.txt-active").hide();
	  		target_image = $("#ImageCarousel dl dt").index(clicked);
	  		target_image_top_distance = 0 - ((height_of_image+11)*target_image);
	  		$("#ImageCarousel dl dt").eq(current_image).removeClass("active").next("dd").eq(0).removeClass("img-active").next("dd").eq(0).removeClass("txt-active");
	  		$("#ImageCarousel .img-window .img-strip").animate( { "top": target_image_top_distance+"px" } );
	  		clicked.next("dd").eq(0).toggleClass("img-active").next("dd").eq(0).fadeIn("slow",function(){
	  			$("#ImageCarousel dl dt").eq(target_image).addClass("active").next("dd").eq(0).addClass("img-active").next("dd").eq(0).addClass("txt-active");
			  	$("#ImageCarousel .txt-mask").fadeOut("fast")
	  			current_image=target_image
	  			});
	  		});
		};

	function hide_controls_on_michael_box(){
		index_michael_box>=(total_in_michael_box-1)?$("#michael_box .int .next").hide():$("#michael_box .int .next").show();
		index_michael_box<=0?$("#michael_box .int .prev").hide():$("#michael_box .int .prev").show();

		}

	function create_michael_box(){
		index_michael_box=$("#ImageCarousel .img-window .img-strip a").index($(this));
		total_in_michael_box=$("#ImageCarousel .img-window .img-strip a").length;
		$("body").append('<div id="michael_box"><div class="int"><div class="controls"><p class="loader">0% loaded.</p><p class="head">&#160;</p><p class="move"><span class="prev">Previous</span> &#160; <strong>'+index_michael_box + ' of ' + total_in_michael_box + '</strong> &#160; <span class="next">Next</span></p><p class="close">Close</p></div></div></div>');
		$("#michael_box .controls .close").click(function(){ close_michael_box(); });
		$("#michael_box .controls .prev").click(function(){ move_michael_box(-1); });
		$("#michael_box .controls .next").click(function(){ move_michael_box(1); });
		$("#ImageCarousel .img-window .img-strip a , #ImageCarousel .viewall a").click(function(){
			if(document.all){
//				$("body").css("overflow-y","hidden");
				}
			if(jQuery.browser.safari || jQuery.browser.opera || jQuery.browser.msie){
				$("#michael_box").css("position","fixed");
				}
			else{
				$("body").css("overflow","hidden");
				$("#michael_box").css("top",$("html").scrollTop());
				}
			$("#michael_box").css("height",$("body").height()).css("display","block");
			index_michael_box=$("#ImageCarousel .img-window .img-strip a").index($(this));
			if(index_michael_box==-1){ index_michael_box=0 }
			hide_controls_on_michael_box();
			$("#michael_box .int strong").html((index_michael_box+1) + " of " + total_in_michael_box)
			img_src=$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).attr("href")!="#"?$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).attr("href"):$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).find("img").attr("src");
			img_alt=$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).attr("title")?$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).attr("title"):$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).find("img").attr("alt");
			$("#michael_box .int .controls .head").html(img_alt);
			if($("#michael_box .int img").length>0){
				$("#michael_box .int img").attr("src",img_src);
				$("#michael_box .int img").attr("alt",img_alt);
				}
			else{
				$("#michael_box .int").append('<img src="'+img_src+'" alt="'+img_alt+'"/>');
				}
			michael_box_img_loader($("#ImageCarousel .img-window .img-strip a"),"place_michael_box()");
			return false;
			});
		}

	function michael_box_img_loader(o,f){
		$("#michael_box .int img").hide();
		michael_box_total_loaded=0;
		michael_box_img_loaded=0
		per_image=100/o.length;
		if($("michael_box_preloader").length==0){
			$("body").append('<div id="michael_box_preloader" style="width:1px;height:1px;overflow:hidden;display:block;"></div><div id="michael_box_preloaded" style="width:1px;height:1px;overflow:hidden;display:block;"></div>')
			for(n=0;n<o.length;n++){
				$("#michael_box_preloader").append('<img src="'+$(o).eq(n).attr("href")+'"/>')
				}
			}
		michael_box_timeout=setTimeout(function(){michael_box_img_loader_works(f)},750);
		}

	function michael_box_img_loader_works(f){
		clearTimeout(michael_box_timeout);
		for(n=$("#michael_box_preloader img").length-1;n>=0;n--){
			if($("#michael_box_preloader img").eq(n).width()!=null){
				if($("#michael_box_preloader img").eq(n).width()>5){
					michael_box_total_loaded+=per_image;
					$("#michael_box_preloader img").eq(n).appendTo($("#michael_box_preloaded"))
					}
				}
			}
		michael_box_total_loaded>99?michael_box_img_loaded=1:$("#michael_box .int .controls .loader").html(Math.floor(michael_box_total_loaded)+"% loaded.");
		michael_box_img_loaded?eval(f):michael_box_timeout=setTimeout(function(){michael_box_img_loader_works(f)},750);
		}

	function place_michael_box(){
		$("#michael_box .int .controls .head").show();
		$("#michael_box .int .controls .loader").hide();
		$("#michael_box .int .controls .close").hide();
		img_h = $("#michael_box .int").find("img").height();
		img_w = $("#michael_box .int").find("img").width();
		img_w<240?img_w=600:null;
		img_h<100?img_h=450:null;
		$("#michael_box .int img").ready(function(){
			$("#michael_box .int").animate({
	  			"marginTop":0-(img_h/2)-30, 
	  			"marginLeft":0-(img_w/2)-20,
	  			"height":img_h,
	  			"width":img_w
	  			},function(){
	  				$("#michael_box .int .controls .close").show();
	  				$("#michael_box .int img").show()
					$("#michael_box .int .move").show();
					$(document).resize(function(){ 
						place_michael_box()
						});
					});
				});
		}

	function close_michael_box(o){
		index_michael_box=0;
		$("html").css("overflow","auto");
		$("#michael_box").css("display","none");
		$("body").css("overflow","visible");
		}

	function move_michael_box(n){
		index_michael_box=index_michael_box+n;
		hide_controls_on_michael_box();
		if(index_michael_box>=0 && index_michael_box<total_in_michael_box){
			$("#michael_box .int strong").html((index_michael_box+1) + " of " + total_in_michael_box)
			$("#michael_box .int img").fadeTo("slow",0.01,function(){
				if($("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).attr("href")!="#"){
					img_src=$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).attr("href");
					}
				else{
					img_src=$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).find("img").attr("src");
					}
				img_alt=$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).attr("title")?$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).attr("title"):$("#ImageCarousel .img-window .img-strip a").eq(index_michael_box).find("img").attr("alt");
				$("#michael_box .int .controls .head").html(img_alt);
				$("#michael_box .int img").remove();
				find_string="img[src*='"+img_src+"']";
				img_h = $("#michael_box_preloaded").find(find_string).height();
				img_w = $("#michael_box_preloaded").find(find_string).width();
				$("#michael_box .int").append('<img src="'+img_src+'" alt="'+img_alt+'" style="opacity:0;filter:alpha(opacity=0);" />').ready(function(){
	  				$("#michael_box .int .controls .close").hide();
					$("#michael_box .int").animate({
			  			"marginTop":0-(img_h/2)-30, 
			  			"marginLeft":0-(img_w/2)-20,
			  			"height":img_h,
			  			"width":img_w
			  			},function(){
			  				$("#michael_box .int img").fadeTo("slow",1);
			  				$("#michael_box .int .controls .close").show();
			  				})
					})
				});
			}
		}
	create_michael_box()
	});
