$(document).ready(function(){
	
	// launchimage2 the plugin with the class to be attached to the links.
	// and the zoom image2_time.
	$.lightboximage2('.rw-lightbox-image2', 400);

});

jQuery.lightboximage2 = function(image2_link_class, image2_time) {

	var image2_link_class = image2_link_class || 'lightbox-image2';
	var image2_time = image2_time || 400;
	
	jQuery.lightboximage2.image2_link_class = image2_link_class;
	jQuery.lightboximage2.open = false;
	jQuery.lightboximage2.image2items = setupImage2Links();
	setupImage2Lightbox();
	setupImage2Thumbnails();
	
	$(window).scroll(scrollImage2Lightbox);
	
	function launchimage2(eventObj) {
		
		$lightboximage2.show();
		var item = getCurrentImage2Item();
		var $image4 = $lightboximage2.find('img:first');
		
		$lightboximage2.originalWidth = $image4.width();
		$lightboximage2.originalHeight = $image4.height();
		
		$image4.css({
			'width' : $image4anchor.width(),
			'height' : $image4anchor.height()
		});
		
		$lightboximage2.css({
			'margin-top' : 0,
			'margin-left' : 0,
			'top' : (item.top / jQuery(window).height() * 100) + '%',
			'left' : (item.left / jQuery(document).width() * 100) + '%',
			'opacity' : 0.1,
			'width': $image4anchor.width(),
			'height': $image4anchor.height()
		});
		
		$("#close-image2").attr('item', $image4anchor.attr('item')).fadeIn(image2_time);
		$image4.animate({
			'width' : $lightboximage2.originalWidth,
			'height' : $lightboximage2.originalHeight
		}, image2_time);
			
		$lightboximage2.animate({
			'marginTop' : - ($lightboximage2.originalHeight / 2) + $(window).scrollTop(),
			'marginLeft' : - $lightboximage2.originalWidth / 2,
			'top' : '50%',
			'left' : '50%',
			'opacity' : 1,
			'width' : $lightboximage2.originalWidth,
			'height' : $lightboximage2.originalHeight
		}, image2_time, function(){

			setImageLightbox2Status(true);
			fadeInImage2Content();
			
		});
		
		return false;
		
	}
	
	function close(eventObj) {
	
		fadeOutImage2Content(function(){	
			
			setImageLightbox2Status(false);
			
			id = $('#close-image2').attr('item');
	
			$image4anchor = $('a' + jQuery.lightboximage2.image2_link_class + '[item=' + id + ']');
			
			var item = jQuery.lightboximage2.image2items[$image4anchor.attr('item')];
			
			var $image4 = $lightboximage2.find('img:first');
			
			$lightboximage2.originalWidth = $image4.width();
			$lightboximage2.originalHeight = $image4.height();
			
			$image4.animate({
				'width' : 0,
				'height' : 0
			}, image2_time, function(){
			
				$(this).css({
					'width' : $lightboximage2.originalWidth,
					'height' : $lightboximage2.originalHeight	
				});
				
			});
				
			$lightboximage2.animate({
				'marginTop' : 0,
				'marginLeft' : 0,
				'top' : (item.top / jQuery(window).height() * 100) + '%',
				'left' : (item.left / jQuery(window).width() * 100) + '%',
				'opacity' : 0,
				'width': 0,
				'height': 0
			}, image2_time, function(){

				$(this).hide();
				
			});
			
			$("#close-image2").fadeIn(800);
			
		});
		
		return false;	
		
	}	
	
	function nextImage2(item_id) {
		var current_image2_item = getCurrentImage2Item();
		
		if(current_image2_item.id != item_id) {
			$image4anchor = $('a' + jQuery.lightboximage2.image2_link_class + '[item=' + item_id + ']');
			fadeOutImage2Content(function(){
				fadeInImage2Content(image2_time/2);
			}, image2_time/2);
		}
	}
	
	function fadeInImage2Content(fade_image2_time) {
		
		var fade_image2_time = fade_image2_time || 100;
		var $image4content = $('<div id="lightbox-image2-content">');		

		var item = getCurrentImage2Item();
		
		if(item.filetype == 'movie') {
			$img = $('<img>').attr({
			
				'width': item.movie.width,
				'height': item.movie.height,
				'src': item.movie.src
				
			}).unbind('click').click(function(){
				$('#lightbox-image2-content').quicktime(item.movie);
				$(this).hide();
			});
			
		} else {
			$img = $('<img>').attr('src', item.image.src);
			item.description.append($image4thumbnails);
		}

		$image4content.appendTo('#lightbox-image2').append($img).append(item.description).css({"opacity": 0}).animate({
			"opacity": 1
		}, fade_image2_time, function(){
			if($('#lightbox-image2-content').css('filter')) $('#lightbox-image2-content').css('filter', '');
		});
		setCurrentImage2Thumbnail(item.id);
		addLinksToImage2Thumbnails();
	}
	
	function fadeOutImage2Content(callback, fade_image2_time) {
		var fade_image2_time = fade_image2_time || 50;
		var $image4content = $('#lightbox-image2-content');
		
		$('#movie-image2').remove()
		$('#lightbox-image2').find('img:first').show(function(){
			
			$image4content.fadeOut(fade_image2_time, function(){
			
				$(this).remove();
				callback();
				
			});	
		});
	}
	
	function setupImage2Lightbox() {
		
		$('<div id="lightbox-image2"></div><div id="lightbox-image2-loading">Loading...</div>').appendTo('body').hide();
		
		$.lightboximage2.loading = $('#lightbox-image2-loading');
		$lightboximage2 = $('#lightbox-image2');
		
		var loop_image2 = setInterval(function(){
		
			if($lightboximage2.css('background-image') != 'none') {

				$('<img id="lightbox-image2-background">').appendTo($lightboximage2).attr('src', $lightboximage2.css('background-image').replace(/^url\("?|"?\)$/g, ''));
			
				$('<a id="close-image2" href="#close-image2" title="Close Lightbox">Close Lightbox</a>').unbind('click').click(close).hide().appendTo($lightboximage2);

				$lightboximage2.css('background-image', 'none');
				
				clearInterval(loop_image2);
			
			}
				 		
		}, 10);	
	}
	
	function setupImage2Links() {
		
		var image2items = Array();
		
		$(image2_link_class).each(function(i){
		
			var $link = $(this);
			
			var link_array = this.href.split('.');
			var type = fileType(link_array[link_array.length - 1]);
			var position = $link.offset({ border: true, padding: true });
			var $description = $('<div>').append(getImageDescription($(this)));
			
			image2items[i] = {
				'id': i,
				'filetype': type,
				'description': $description,
				'top': position.top, 
				'left': position.left			
			}
						 
			switch(type) {
			
				case 'movie':
					
					var rel = $link.attr('rel').replace(/\[|\]|\s/gi, "").split(',');
					var src = rel[2];
					image2items[i].movie = {
						'url': $link.attr('href'),
						'title': $link.attr('title'),
						'width': parseInt(rel[0]),
						'height': parseInt(rel[1]),
						'src': rel[2]	
					}
					break;
					
				case 'image':
				
					image2items[i].image = {
						'src': $link.attr('href'),
						'title': $link.attr('title')
					}
					var src = $link.attr('href');
			}
					
			$(this).attr('item', i).unbind('click').click(function(){
			
				$image4anchor = $(this);
				
				var item = getCurrentImage2Item();
				
				if(jQuery.lightboximage2.open == false) {
				
					var img = new Image();
					
					$.lightboximage2.loading.show();
					
					jQuery(img).load(function(){
					
						$.lightboximage2.loading.hide();
						launchimage2();
						
					}).attr('src', src);
					
				} else {
					nextImage2($image4anchor.attr('item'));
				}
				
				return false;
				
			});
		});
		
		return image2items;
	
	}
	
	function setupImage2Thumbnails() {
	
		var li_image2_array = Array();
		
		$.each(jQuery.lightboximage2.image2items, function(i){
		
			if(this.filetype == 'image') {
			
				var $li = $('<li><a href="#" thumb-image2-id="' + i + '" title="View ' + this.image.title + '"><img src="' + this.image.src + '" /></a></li>');
				li_image2_array.push($li);
			}
		});
		
		$image4thumbnails = $('<ul id="lightbox-image2-thumbnails">');
		
		$.each(li_image2_array, function(){
			$image4thumbnails.append(this)
		});
	}
	
	function addLinksToImage2Thumbnails() {
		$('#lightbox-image2-thumbnails a').each(function(){
			$(this).unbind('click').click(function(){
				nextImage2($(this).attr('thumb-image2-id'));
				return false;
			});
		});
	}
	
	function setCurrentImage2Thumbnail(id) {
		$('.current-thumb-image2').removeClass('current-thumb-image2');
		$('[thumb-image2-id=' + id + ']').addClass('current-thumb2-image');
	}
	
	function setImageLightbox2Status(status) {
		if(status == true) {
			jQuery.lightboximage2.open = true;
			if($.browser.msie && parseInt($.browser.version) == 7) {
				$('#lightbox-image2').css('filter', '')
				$('#lightbox-image2-background').attr('src', $('#lightbox-image2-background').attr('src').replace(/_ie.([\S])/, '.$1'));
			}
		} else {
			jQuery.lightboximage2.open = false;
			if($.browser.msie && parseInt($.browser.version) == 7) {
				var src = $('#lightbox-image2-background').attr('src').replace(/\.png/, '_ie.png');
				$('#lightbox-image2-background').attr('src', src);
			}
		}
	}
		
	function getCurrentImage2Item() {
		return jQuery.lightboximage2.image2items[$image4anchor.attr('item')];
	}
	
	function getImageDescription($image4anchor) {
		code = $image4anchor.parents('li:first').html().replace( /(>)?([\n\s]+)(<)/ig, '$1$3');
		return $(code);
	}

	function scrollImage2Lightbox() {
		$lightboximage2.css('marginTop', - ($lightboximage2.height() / 2) + $(window).scrollTop());
	}
	
	// Returns the filetype of the string.
	function fileType( ext ) {
				
		if(/^(?:mov|mp4|swf)$/.test(ext)) return 'movie';		
		if(/^(?:png|jp[e]?g|gif|tif[f]?)$/.test(ext)) return 'image';
		return false;
		
	}
}

jQuery.fn.quicktime = function(item) {
	
	return this.each(function(){
		
		$container = $(this);
		
		// build the Quickimage2_time Object
		var qo = new QTObject(item.url, item.title, item.width, item.height);
			
		qo.addParam("showlogo", "false");
		qo.addParam("autoplay", "true");
		
		//qo.addParam("href", item.url);
		qo.addParam("target", "myself");
		qo.addParam("controller", "true");
		qo.addParam("bgcolor", "#ffffff");
		qo.addParam("wmode", "transparent");
		var error = "<p>Quicktime does not appear to be installed please visit Apple's homepage to download it.</p>";
		
		// TODO Add movie error checking.
		var qt = '<div id="movie">'+error+'</div>';
		jQuery(qt).prependTo($container).css({"width": item.width, "height": item.height});
		qo.write('movie');
		
	});
}

/*
 * QTObject Embed
 * http://blog.deconcept.com/2005/01/26/web-standards-compliant-javascript-quickimage2_time-detect-and-embed/
 *
 * by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/)
 *
 * v1.0.2 - 02-16-2005
 */

QTObject=function(mov,id,w,h){this.mov=mov;this.id=id;this.width=w;this.height=h;this.redirect="";this.sq=document.location.search.split("?")[1]||"";this.altTxt="This content requires the Quicktime Plugin. <a href='http://www.apple.com/quicktime/download/'>Download Quicktime Player</a>.";this.bypassTxt="<p>Already have Quicktime Player? <a href='?detectqt=false&"+this.sq+"'>Click here.</a></p>";this.params=new Object();this.doDetect=getQueryParamValue('detectqt');}
QTObject.prototype.addParam=function(name,value){this.params[name]=value;}
QTObject.prototype.getParams=function(){return this.params;}
QTObject.prototype.getParam=function(name){return this.params[name];}
QTObject.prototype.getParamTags=function(){var paramTags="";for(var param in this.getParams()){paramTags+='<param name="'+param+'" value="'+this.getParam(param)+'" />';}
if(paramTags==""){paramTags=null;}
return paramTags;}
QTObject.prototype.getHTML=function(){var qtHTML="";if(navigator.plugins&&navigator.plugins.length){qtHTML+='<embed type="video/quicktime" src="'+this.mov+'" width="'+this.width+'" height="'+this.height+'" id="'+this.id+'"';for(var param in this.getParams()){qtHTML+=' '+param+'="'+this.getParam(param)+'"';}
qtHTML+='></embed>';}
else{qtHTML+='<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="'+this.width+'" height="'+this.height+'" id="'+this.id+'">';this.addParam("src",this.mov);if(this.getParamTags()!=null){qtHTML+=this.getParamTags();}
qtHTML+='</object>';}
return qtHTML;}
QTObject.prototype.getVariablePairs=function(){var variablePairs=new Array();for(var name in this.getVariables()){variablePairs.push(name+"="+escape(this.getVariable(name)));}
if(variablePairs.length>0){return variablePairs.join("&");}
else{return null;}}
QTObject.prototype.write=function(elementId){if(isQTInstalled()||this.doDetect=='false'){if(elementId){document.getElementById(elementId).innerHTML=this.getHTML();}else{document.write(this.getHTML());}}else{}}
function isQTInstalled(){var qtInstalled=false;qtObj=false;if(navigator.plugins&&navigator.plugins.length){for(var i=0;i<navigator.plugins.length;i++){var plugin=navigator.plugins[i];if(plugin.name.indexOf("Quicktime")>-1){qtInstalled=true;}}}else{execScript('on error resume next: qtObj = IsObject(CreateObject("QuicktimeCheckObject.QuicktimeCheck.1"))','VBScript');qtInstalled=qtObj;}
return qtInstalled;}
function getQueryParamValue(param){var q=document.location.search;var detectIndex=q.indexOf(param);var endIndex=(q.indexOf("&",detectIndex)!=-1)?q.indexOf("&",detectIndex):q.length;if(q.length>1&&detectIndex!=-1){return q.substring(q.indexOf("=",detectIndex)+1,endIndex);}else{return"";}}