/* ------------------------------------------------------------------------
	Class: wonderWindow
	Use: Photo Viewer for jQuery
	Author: Jo Greenwood (www.wonderdust.co.uk) - based on prettyPhoto by Stephane Caron (http://www.no-margin-for-errors.com)
	Version: 1
	------------------------------------------------------------------------- */

	$.fn.wonderWindow = function(settings) {
		// global Variables
		var caller = 0;
		var holderHeight = 380;
		var holderWidth = 549;
		var flashHeight=300;
		var flashWidth=400;
		
		settings = jQuery.extend({
			animationSpeed: 'normal', /* fast/slow/normal */
			padding: 16 /* padding for each side of the picture */
		}, settings);
	
		$(this).each(function(){
			$(this).bind('click',function(){
				open(this); return false;
			});
		});
	
		function open(el) {
					
			caller = $(el);
		
			// Find out if the picture is part of a set
			theRel = $(caller).attr('rel');
				
			// Find out the type of content
			contentType = "image";
			if($(caller).attr('href').indexOf('.swf') > 0){ contentType = 'flash'; }
			
			$('div.pictureHolder #fullResImageContainer  embed').remove();
	
			$('div.pictureHolder #fullResImageContainer').hide();
			$('.loaderIcon').show();
			
			//$('div.pictureHolder #fullResImageContainer').fadeOut(settings.animationSpeed)

		
			// Display the correct type of information
			(contentType == 'image') ? _preload() : _writeFlash();
	
		};
	
	
		showimage = function(width,height,containerWidth,containerHeight,contentHeight,contentWidth){
			$('.loaderIcon').hide();
	
			$('div.pictureHolder .content').animate({'height':contentHeight,'width':containerWidth},settings.animationSpeed);
	
			projectedTop = ((holderHeight/2) - (containerHeight/2));
			if(projectedTop < 0) projectedTop = 0;

			// Resize the holder
			$('div.pictureHolder').animate({
				'top': projectedTop,
				'left': ((holderWidth/2) - (containerWidth/2)),
				'width': containerWidth
			},settings.animationSpeed,function(){
				$('#fullResImage').attr({
					'width':width,
					'height':height
				});

				$('div.pictureHolder').width(containerWidth);

				// Fade the new image
				$('div.pictureHolder #fullResImageContainer').fadeIn(settings.animationSpeed);
				
			});
		};
		
	
		function _fitToHolder(width,height){
		
			// Get the container size, to resize the holder to the right dimensions
			contentHeight = height;
			contentWidth = width;
			containerHeight = height + parseFloat($('div.pictureHolder .top').height()) + parseFloat($('div.pictureHolder .bottom').height());
			containerWidth = width + settings.padding;
		
			imageWidth = width;
			imageHeight = height;

			return {
				width:imageWidth,
				height:imageHeight,
				containerHeight:containerHeight,
				containerWidth:containerWidth,
				contentHeight:contentHeight,
				contentWidth:contentWidth
			};
		};	
	
		function _preload(){

		
			// Set the new image
			imgPreloader = new Image();
	
			//$('div.pictureHolder .content').css('overflow','hidden');
	
			$('div.pictureHolder #fullResImage').attr('src',$(caller).attr('href'));
			$('#fullResImage').show();

			imgPreloader.onload = function(){
				var correctSizes = _fitToHolder(imgPreloader.width,imgPreloader.height);
				imgPreloader.width = correctSizes['width'];
				imgPreloader.height = correctSizes['height'];
			
				// Need that small delay for the anim to be nice
				setTimeout('showimage(imgPreloader.width,imgPreloader.height,'+correctSizes["containerWidth"]+','+correctSizes["containerHeight"]+','+correctSizes["contentHeight"]+','+correctSizes["contentWidth"]+')',0);
				
					
			};
	
			imgPreloader.src = $(caller).attr('href');
			
		};
	
	
	
		function _writeFlash(){
			//flashParams = $(caller).attr('rel').split(';');
			//$(flashParams).each(function(i){
				// Define the width and height
			//	if(flashParams[i].indexOf('width') >= 0) flashWidth = flashParams[i].substring(flashParams[i].indexOf('width') + 6, flashParams[i].length);
			//	if(flashParams[i].indexOf('height') >= 0) flashHeight = flashParams[i].substring(flashParams[i].indexOf('height') + 7, flashParams[i].length);
			//	if(flashParams[i].indexOf('flashvars') >= 0) flashVars = flashParams[i].substring(flashParams[i].indexOf('flashvars') + 10, flashParams[i].length);
			//});	
		
			$('.pictureHolder #fullResImageContainer').append('<embed width="'+flashWidth+'" height="'+flashHeight+'" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" wmode="opaque" name="prettyFlash" allowscriptaccess="always" bgcolor="#FFFFFF" quality="high" src="'+$(caller).attr('href')+'"/>');
			$('#fullResImage').hide();
	
			contentHeight = parseFloat(flashHeight);
			contentWidth = parseFloat(flashWidth);
			containerHeight = contentHeight + parseFloat($('div.pictureHolder .top').height()) + parseFloat($('div.pictureHolder .bottom').height());
			containerWidth = parseFloat(flashWidth) + parseFloat($('div.pictureHolder .content').css("padding-left")) + parseFloat($('div.pictureHolder .content').css("padding-right")) + settings.padding;
		
			setTimeout('showimage('+flashWidth+','+flashHeight+','+containerWidth+','+containerHeight+','+contentHeight+','+contentWidth+')',500);
		};
	
	
	};
	