/* ------------------------------------------------------------------------
	Class: zoombox light
	Jonathan Boyer - Stéphane Pérès
	Version: 1.1
------------------------------------------------------------------------- */

$(document).ready(function(){
	zoombox.init();
});


zoombox = {
	/***************************************
	 *
	 *     	CONFIGURATION
	 * 
	 * **********************************/
	Dwidth : 		640, 		// Default width
	Dheight: 		480,		// Default height
	duration:		750,		// Animation duration (ms)
	animation:		"all",		// Do we animate the size of the box ?  (all : animation everytime, img: animation only on link with an img, no : no size animation)
	maskOpacity:	0.0,		// Opacity of the black overlay,
	FLVPlayer:		"/js/zoombox/FLVplayer.swf",   // URL of the FLV Player
	MP3Player:	"/js/zoombox/MP3player.swf",  // URL of the MP3 Player
	allowOverflow:	false,	// Automatically adjust width/height of the box if the content is larger than the window
	bindable:		"a[rel^='zoombox']",  // (jQuery expression) elements which have the zoombox behaviour
	theme:		"default", 			// Define the theme to use
	fixed:		false,			// Do we fix the box ? => when we scroll the box follow the window or not ?
	
	themes:{
		"default" :	 '\
			<div id="zoombox"> \
				<div id="zoombox_aplat" class="zoombox_close"></div>\
				<div id="zoombox_contener">\
					<div id="zoombox_relative">\
						<div id="zoombox_close" class="zoombox_close"></div>\
						<div id="zoombox_content"></div>\
						<div id="zoombox_bg"><div class="zoombox_bg_h"></div><div class="zoombox_bg_b"></div><div class="zoombox_bg_d"></div><div class="zoombox_bg_g"></div><div class="zoombox_bg_bg"></div><div class="zoombox_bg_bd"></div><div class="zoombox_bg_hg"></div><div class="zoombox_bg_hd"></div></div>\
						<div id="zoombox_nav">\
							<table>\
								<tr>\
									<td width="39"><div id="zoombox_prev"></div></td>\
									<td><div id="zoombox_title"><span></span></div></td>\
									<td width="39"><div id="zoombox_next"></div></td>\
								</tr>\
							</table>\
						</div>\
					</div>\
				</div>\
			</div>\
			',
		"lightbox" :	 '\
			<div id="zoombox" class="lightbox"> \
				<div id="zoombox_aplat" class="zoombox_close"></div>\
				<div id="zoombox_contener">\
					<div id="zoombox_relative">\
						<div id="zoombox_content"></div>\
						<div id="zoombox_bg"><div class="zoombox_bg_h"></div><div class="zoombox_bg_b"></div><div class="zoombox_bg_d"></div><div class="zoombox_bg_g"></div><div class="zoombox_bg_bg"></div><div class="zoombox_bg_bd"></div><div class="zoombox_bg_hg"></div><div class="zoombox_bg_hd"></div></div>\
						<div id="zoombox_infos"<div id="zoombox_title"><span></span></div></div>\
						<div id="zoombox_close" class="zoombox_close"></div>\
						<div id="zoombox_next"></div><div id="zoombox_prev"></div>\
					</div>\
				</div>\
			</div>\
			',
	},


	/***************************************
	 *
	 *     	VARIABLES
	 * 
	 * **********************************/
	inCSS:		null,
	
	width:		0,
	height:		0,
	
	url:			null,			// Url loaded in the box
	title:			null,			// Title of the link
	type:		"multimedia",	// Keep the type of media loaded in the box
	animateOpening: false,		// Do we use animation for opening/closing the box ?
	timer:		null,			// Timer to load an img
	loaded:		false,		// Is the image loaded ?
	gallery:		"",			// Gallery of the current element
	position:		0,			// Current position in the gallery
	margesH:		null,			// Horizontal marges of contener
	margesV:		null,			// Vertical marges of contener

	filtreImg:			/(\.jpg)|(\.jpeg)|(\.bmp)|(\.gif)|(\.png)/i,
	filtreMP3:			/(\.mp3)/i,
	filtreFLV:			/(\.flv)/i,
	filtreSWF:			/(\.swf)/i,
	filtreQuicktime:	/(\.mov)|(\.mp4)/i,
	filtreWMV:			/(\.wmv)|(\.avi)/i,
	filtreDailymotion:	/(http:\/\/www.dailymotion)|(http:\/\/dailymotion)/i,
	filtreVimeo:		/(http:\/\/www.vimeo)|(http:\/\/vimeo)/i,
	filtreYoutube:		/(youtube\.)/i,
	filtreKoreus:		/(http:\/\/www\.koreus)|(http:\/\/koreus)/i,
	filtreDeezer:		/(http:\/\/www\.deezer)|(http:\/\/deezer)/i,
	
	galleryRegExp:  /\[(?:.*)\]/,
	
	init : function(){
		zoombox.images = new Array();
		$(zoombox.bindable).each(function(){
			var gallery = zoombox.galleryRegExp.exec($(this).attr("rel"));
			if(!zoombox.images[gallery]){
				zoombox.images[gallery]=new Array();
			}
			zoombox.images[gallery].push($(this));
			
			$(this).bind('click',function(){
				zoombox.click($(this));
				return false;
			});
		});
		$(window).resize(zoombox.resize);
		$(window).scroll(zoombox.resize);
		$(document).keyup(function(event){ zoombox.keyboard(event); });
	},

	click : function(div){
		zoombox.gallery=zoombox.galleryRegExp.exec(div.attr("rel"));
		for (var i = 0; i < zoombox.images[zoombox.gallery].length; i++){
			if($(zoombox.images[zoombox.gallery][i]).attr("href") == div.attr("href")){
				zoombox.position=i;
				break;
			}
		}
		zoombox.animateOpening=false;
		
		if(zoombox.animation=="all"){
			zoombox.animateOpening=true;
			zoombox.inCSS = {
				"top" :	 div.offset().top,
				"left" :	div.offset().left,
				"width":	div.width(),
				"height":   div.height()
			}
		}
		if(div.children("img").length && (zoombox.animation=="all" || zoombox.animation=="img")){
			zoombox.inCSS = {
				"top" :	 div.children("img").offset().top,
				"left" :	div.children("img").offset().left,
				"width":	div.children("img").width(),
				"height":   div.children("img").height()
			}
			zoombox.animateOpening=true;
		}

		zoombox.url=div.attr("href");
		zoombox.title=div.attr("title");
		dimensions =div.attr("rel").split(' ');
		if((dimensions[1])&&(dimensions[2]) && parseInt(dimensions[1])>0  && parseInt(dimensions[2])>0 ){ zoombox.width = parseInt(dimensions[1]); zoombox.height =  parseInt(dimensions[2]);}
		else{zoombox.width=zoombox.Dwidth; zoombox.height=zoombox.Dheight;}
				
		zoombox.open();
		return false;
	},
	
	/**
	 * Open the box, this function could be called with javascript if you want to open a box with a specific action.
	 * @param url The link that you want to open in the box
	 * @param width (optional) Width of the box
	 * @param height (optional) Height of the box
	 * */
	open : function(url,width,height){
		if(url!=undefined){
			zoombox.url=url;
			if(width==undefined){	zoombox.width=zoombox.Dwidth;	}	else{	zoombox.width=width;	}
			if(height==undefined){	zoombox.height=zoombox.Dheight;	}	else{	zoombox.height=height;	}	
			zoombox.animateOpening=false;
			zoombox.title = null;
		}
		if(zoombox.filtreImg.test(zoombox.url) && zoombox.loaded==false){
		    img=new Image();
			img.src=zoombox.url;
			zoombox.type="img";
			$("body").append('<div id="zoombox_loader"></div>');
			$("#zoombox_loader").css("marginTop",zoombox.scrollY());
			zoombox.timer = window.setInterval("zoombox.loadImg(img)",100);
			return false;
		}
		zoombox.loaded=false;

		if(!$("#zoombox").length){
			$("body").append(zoombox.themes[zoombox.theme]);

			$("#zoombox_aplat").css({
				"height":$("body").height(),
			});
			if(zoombox.margesH==null){ zoombox.margesH = parseInt($("#zoombox_contener").css("paddingLeft")) + parseInt($("#zoombox_contener").css("paddingRight")) + parseInt($("#zoombox_contener").css("marginLeft")) + parseInt($("#zoombox_contener").css("marginRight")); }
			if(zoombox.margesV==null){ zoombox.margesV = parseInt($("#zoombox_contener").css("paddingTop")) + parseInt($("#zoombox_contener").css("paddingBottom")) + parseInt($("#zoombox_contener").css("marginTop")) + parseInt($("#zoombox_contener").css("marginBottom")); }
			
			$("#zoombox_next").hide();
			$("#zoombox_prev").hide();
			$("#zoombox_title").hide();
			$(".zoombox_close").click(zoombox.close);
			$("#zoombox_next").click(zoombox.next);	
			$("#zoombox_prev").click(zoombox.prev);	
			$("embed").css("visibility","hidden"); 	
			$("object").css("visibility","hidden");
			if(zoombox.gallery){
				if(zoombox.position<zoombox.images[zoombox.gallery].length-1){
					$("#zoombox_next").show();
				}
				if(zoombox.position>0){
					$("#zoombox_prev").show();
				}
			}
			$('#zoombox_aplat').css({'opacity': 0});
			$('#zoombox_aplat').fadeTo(zoombox.duration,zoombox.maskOpacity);
			if(zoombox.inCSS!=null){
				zoombox.inCSS.left-=parseInt($("#zoombox_contener").css("paddingLeft"))+parseInt($("#zoombox_contener").css("marginLeft"));
				zoombox.inCSS.top-=parseInt($("#zoombox_contener").css("paddingTop"))+parseInt($("#zoombox_contener").css("marginTop"));
			}
			var boxBeginCSS = zoombox.inCSS;
		}
		else{
			$("#zoombox_title span").empty();
			$("#zoombox_content").empty();				

			var boxBeginCSS = {
				"width":$("#zoombox_contener").css("width"),
				"height":$("#zoombox_contener").css("height"),
				"top":$("#zoombox_contener").css("top"),
				"left":$("#zoombox_contener").css("left")
			}
			var transition = true;
		}
		
		var content = zoombox.getContent(zoombox.url);	
		if(zoombox.type=="img"){ $("#zoombox_content").append(content); }
		if(transition){ $("#zoombox_content img").hide(); }	
		if(zoombox.title && zoombox.title!="" && zoombox.title!=null){ $("#zoombox_title span").append(zoombox.title); $("#zoombox_title").show();   }
		else{ $("#zoombox_title").hide();	$("#zoombox_titlel").hide();	$("#zoombox_title").hide(); }
		if(zoombox.allowOverflow==false && (zoombox.height+120)>zoombox.windowH()){
			zoombox.width=zoombox.width*((zoombox.windowH()-120)/zoombox.height);
			zoombox.height=(zoombox.windowH()-120);
			var content = zoombox.getContent(zoombox.url);
		}

		var topPos = (zoombox.windowH() - zoombox.height - zoombox.margesV)/2 + zoombox.scrollY();
		var leftPos = (zoombox.windowW() - zoombox.width - zoombox.margesH)/2;

		var cssProp = {
			"width":zoombox.width,
			"height":zoombox.height,
			"top":topPos,
			"left":leftPos
		}

		if(zoombox.animateOpening==false && transition !=true){
			$("#zoombox_contener").css(cssProp);
			$('#zoombox_contener').hide();
			$('#zoombox_contener').show();
			$("#zoombox_content").empty().append(content);
		}
		else{
			$("#zoombox_contener").css(boxBeginCSS);
			$("#zoombox_contener").animate(cssProp,zoombox.duration,function(){
				if(zoombox.type=="img"){
						$("#zoombox_content img").fadeIn(zoombox.duration/2);
				}
				else{
					$("#zoombox_content").empty().append(content);
				}
			});
		}
	},
	
	close : function(){
		window.clearInterval(zoombox.timer);
		if(zoombox.type!="img"){	$("#zoombox_content").empty();	}	
		if(zoombox.animateOpening==true){
			zoombox.inCSS.opacity = 0; 
			$("#zoombox_contener").animate(zoombox.inCSS,zoombox.duration);
		}
		else{
			$("#zoombox_contener").remove();
		}
		$("#zoombox_aplat").animate({"opacity":0},zoombox.duration,zoombox.remove);
	},

	remove : function(){
		$("#zoombox").remove();
		$("#zoombox_loader").remove();
		$("embed").css("visibility","visible");
		$("object").css("visibility","visible");
	},
	
	resize : function(){
		var topPos = (zoombox.windowH() - zoombox.height - zoombox.margesV)/2 + zoombox.scrollY();
		var leftPos = (zoombox.windowW() - zoombox.width - zoombox.margesH)/2;
		$("#zoombox_contener").css("left",leftPos);
		if(zoombox.fixed){	$("#zoombox_contener").css("top",topPos);	}
	},
	
	keyboard : function(event){
      		if(event.keyCode==39){
      			zoombox.next();
      		}
      		else if(event.keyCode==37){
      			zoombox.prev()
      		}
      		else if(event.keyCode==27){
      			zoombox.close();
		}
	},

	next : function(){
		if(zoombox.gallery!=null){
			zoombox.position++;
			if(zoombox.position==zoombox.images[zoombox.gallery].length-1){
				$("#zoombox_next").fadeOut();
			}
			if($("#zoombox_prev").is(":hidden")){
				$("#zoombox_prev").fadeIn();
			}
			zoombox.click(zoombox.images[zoombox.gallery][zoombox.position]);
		}
	},

	prev : function(){
		if(zoombox.gallery!=null){
			zoombox.position--;
			if(zoombox.position==0){
				$("#zoombox_prev").fadeOut();
			}
			if($("#zoombox_next").is(":hidden")){
				$("#zoombox_next").fadeIn();
			}
			zoombox.click(zoombox.images[zoombox.gallery][zoombox.position]);
		}
	},

	loadImg : function(img){
			if(img.complete){
				window.clearInterval(zoombox.timer);
				zoombox.loaded=true;
				zoombox.width=img.width;
				zoombox.height=img.height;
				$('#zoombox_loader').remove();
				if($('#zoombox_content img').length){
					$('#zoombox_content img').fadeOut(zoombox.duration/2,function(){$('#zoombox_content').empty(); zoombox.open();})
				}
				else{
					$('#zoombox_content').empty();
					zoombox.open();
				}
			}	
			if(typeof(j)=='undefined'){j=0;}
 			$('#zoombox_loader').css({'background-position': "0px "+j+"px"});
			j=j-40;
			if(j<(-440)){j=0;}
	},
	
	getContent : function(url){
			var content
			
			zoombox.type="multimedia";
	
			if(zoombox.filtreImg.test(url)){
				content='<img src="'+img.src+'" width="100%" height="100%"/>';
				zoombox.type="img";
			}
			else if(zoombox.filtreMP3.test(url)){
				zoombox.width=300;
				zoombox.height=40;
				content ='<object type="application/x-shockwave-flash" data="'+zoombox.MP3Player+'?son='+url+'" width="'+zoombox.width+'" height="'+zoombox.height+'">';
				content+='<param name="movie" value="'+zoombox.MP3Player+'?son='+url+'" /></object>';
			}		
			
			else if(zoombox.filtreFLV.test(url)){
				content='<embed src="'+zoombox.FLVplayer+'" width="'+zoombox.width+'" height="'+zoombox.height+'" allowscriptaccess="always" allowfullscreen="true" flashvars="file='+url+'&width='+zoombox.width+'&height='+zoombox.height+'&autostart=true" wmode="transparent" />';			
			}
			
			else if(zoombox.filtreSWF.test(url)){
				content='<object width="'+zoombox.width+'" height="'+zoombox.height+'"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="'+url+'" /><embed src="'+url+'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+zoombox.width+'" height="'+zoombox.height+'" wmode="transparent"></embed></object>';		
			}
			
			else if(zoombox.filtreQuicktime.test(url)){
				zoombox.height = zoombox.height+20;
				content='<embed src="'+url+'" width="'+zoombox.width+'" height="'+zoombox.height+'" controller="true" cache="true" autoplay="true"/>';
			}
			
			else if(zoombox.filtreWMV.test(url)){
				content='<embed src="'+url+'" width="'+zoombox.width+'" height="'+zoombox.height+'" controller="true" cache="true" autoplay="true" wmode="transparent" />';
			}

			else if(zoombox.filtreDailymotion.test(url)){
				id=url.split('_');
				id=id[0].split('/');
				id=id[id.length-1];
				content='<object width="'+zoombox.width+'" height="'+zoombox.height+'"><param name="movie" value="http://www.dailymotion.com/swf/'+id+'&colors=background:000000;glow:000000;foreground:FFFFFF;special:000000;&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.dailymotion.com/swf/'+id+'&colors=background:000000;glow:000000;foreground:FFFFFF;special:000000;&related=0" type="application/x-shockwave-flash" width="'+zoombox.width+'" height="'+zoombox.height+'" allowFullScreen="true" allowScriptAccess="always" wmode="transparent" ></embed></object>';
			}
			
			
			else if(zoombox.filtreVimeo.test(url)){
				id=url.split('/');
				id=id[3];
				content='<object width="'+zoombox.width+'" height="'+zoombox.height+'"><param name="allowfullscreen" value="true" />	<param name="allowscriptaccess" value="always" />	<param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id='+id+'&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00AAEB&amp;fullscreen=1" />	<embed src="http://www.vimeo.com/moogaloop.swf?clip_id='+id+'&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00AAEB&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+zoombox.width+'" height="'+zoombox.height+'" wmode="transparent" ></embed></object>';
			}
			
			
			else if(zoombox.filtreYoutube.test(url)){
				id=url.split('watch?v=');
				id=id[1].split('&');
				id=id[0];
				content='<object width="'+zoombox.width+'" height="'+zoombox.height+'"><param name="movie" value="http://www.youtube.com/v/'+id+'&hl=fr&rel=0&color1=0xFFFFFF&color2=0xFFFFFF&hd=1"></param><embed src="http://www.youtube.com/v/'+id+'&hl=fr&rel=0&color1=0xFFFFFF&color2=0xFFFFFF&hd=1" type="application/x-shockwave-flash" width="'+zoombox.width+'" height="'+zoombox.height+'" wmode="transparent"></embed></object>';
			}
			
			
			else if(zoombox.filtreKoreus.test(url)){
				url=url.split('.html');
				url=url[0];
				content='<object type="application/x-shockwave-flash" data="'+url+'" width="'+zoombox.width+'" height="'+zoombox.height+'"><param name="movie" value="'+url+'"><embed src="'+url+'" type="application/x-shockwave-flash" width="'+zoombox.width+'" height="'+zoombox.height+'"  wmode="transparent"></embed></object>';
			}
			
			
			else if(zoombox.filtreDeezer.test(url)){
				zoombox.width=220;
				zoombox.height=55;
				id=url.split('/');
				id=id[id.length-1];
				content='<object width="220" height="55"><param name="movie" value="http://www.deezer.com/embedded/small-widget-v2.swf?idSong='+id+'&colorBackground=0x000000&textColor1=0xFFFFFF&colorVolume=0xFF6600&autoplay=0"></param><embed src="http://www.deezer.com/embedded/small-widget-v2.swf?idSong='+id+'&colorBackground=0x000000&textColor1=0xFFFFFF&colorVolume=0xFF6600&autoplay=0" type="application/x-shockwave-flash" width="220" height="55" wmode="transparent"></embed></object>';
			}
			
			else{
				if(zoombox.width==zoombox.Dwidth && zoombox.height==zoombox.Dheight){
					zoombox.width=zoombox.windowW()-100;
					zoombox.height=zoombox.windowH()-100;
				}
				content='<iframe src="'+url+'" width="'+zoombox.width+'" height="'+zoombox.height+'" border="0"></iframe>';
			}
			return content;
	},
	
	windowH : function(){
		if (window.innerHeight) return window.innerHeight  ;
		else{return $(window).height();}
	},
	
	windowW : function(){
		if (window.innerWidth) return window.innerWidth  ;
		else{return $(window).width();}
	},
	
	scrollY : function() {
		scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scrOfY = window.pageYOffset;
		} else if( document.body && ( document.body.scrollTop ) ) {
			scrOfY = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollTop ) ) {
			scrOfY = document.documentElement.scrollTop;
		}
		return scrOfY;
	},

	scrollX : function() {
		scrOfX = 0;
		if( typeof( window.pageXOffset ) == 'number' ) {
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft ) ) {
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft ) ) {
			scrOfX = document.documentElement.scrollLeft;
		}
		 return scrOfX;
	}
}