 
jQuery(document).ready(function() {
 
 /*
 *
 * video expander
 *
 */
 
 	$("a.expanding-video").click( function() {
 	
		var isie = $.browser.msie;
 		var isFireFoxMac=false;

		if (window.navigator.userAgent.indexOf("Macintosh") !=-1 && window.navigator.userAgent.indexOf("Firefox") !=-1) {
			isFireFoxMac=true;
		}
		
		var dimensions_meta = $(this).parent().find("p.video-dimension span").text();
		var xml_meta = $(this).parent().find("p.video-xml span").text();
		var swf_meta = $(this).parent().find("p.video-swf span").text();

 		// options
		var exp_holder_width = dimensions_meta ? dimensions_meta.split(",")[0] : 260;
		var exp_holder_height = dimensions_meta ? dimensions_meta.split(",")[1] : 410;
		var exp_padding = 5;
		var exp_y_offset = parseInt($("img", this).css("margin-left"));
		if (isie) {
			exp_y_offset=0;
		}
		
		// a few initial values
		var exp_orig_pos = findPos(this);
		var exp_start_height = $("img", this).height();
		var exp_start_width = $("img", this).width();
		var exp_animating = false;
		var page_height = document.body.scrollHeight;
		
		var exp_width;
		var exp_height;
		
		
		// browser detect to get around issue with quirksmode
		
		if(isie) {
			exp_width =  exp_holder_width;
			exp_height =  exp_holder_height;
		} else {
			exp_width =  exp_holder_width - (exp_padding*2);
			exp_height =  exp_holder_height - (exp_padding*2);
		}
		
		// create the holder
		var exp_create_holder = $("<div id='expand-holder'></div>").css({position: "absolute", left: (exp_y_offset - exp_padding), top: exp_padding, height: (exp_orig_pos[1]+exp_start_height)});
		var exp_clone = $("img", this).clone();	
		
		// create the overlay
		var exp_overlay=$('<div id="overlay-fader"></div>').css({display: "block", height: page_height });
		$("body").append(exp_overlay);
		
		// create video expander
		$("body").append(exp_create_holder);
		$(exp_create_holder).append(exp_clone);
		$(exp_clone).wrap('<div class="expanding-video"></div>').css({position: "relative"});
		$(exp_clone).css({position: "absolute", bottom : 0}); 	
		$(exp_clone).parent().css({position: "absolute", left: exp_orig_pos[0], bottom: 0, margin: 0, height: exp_start_height, width : exp_start_width, borderWidth:  exp_padding});
		
		// show overlay
		$(exp_overlay).css({height : page_height, display : "block" });
						
			$("a", exp_create_holder).click(function() {
				return false;
			});
			
			setTimeout(function() {
				
				// animate video expander in
				$(exp_clone).parent().animate({height: exp_height, width: exp_width}, 800, function() {
				
					var exp_thumb = $("img", this);
					
					$("img", this).wrap('<div class="expand-video" id="flash-video-container"></div>');
					var exp_video_wrapper=$(this).find(".expand-video");
					$(exp_video_wrapper).css({width : $(exp_thumb).width(), height : $(exp_thumb).height(), position: "absolute", bottom : 0 });
					
					$(exp_thumb).animate({opacity: "0"}, 300, function () {
						
						$(exp_video_wrapper).animate({width : (exp_holder_width - (exp_padding*2)), height : (exp_holder_height - (exp_padding*2)) }, 500, function() {
							
							// The following prevents the popup closing when a users 
							// clicks play on the flash movie
							$(this).click(function() {
								return false;
							});

							// Load flash movie
							var so = new SWFObject(swf_meta, "mediaplayer",(exp_holder_width - (exp_padding*2)), (exp_holder_height - (exp_padding*2)), "8", "#000000");
							so.addVariable("settingsPath", xml_meta);
							so.addParam("wmode", "transparent");
							so.addParam("allowScriptAccess", "sameDomain");
							so.write("flash-video-container"); 
							
							$(this).before('<a href="#" class="expand-close">Close</a>');

						});
					
						$(this).remove();
					});
					
					// add click functionality to the container
					$(this).click( function() {
						
						$(exp_create_holder).find("div#flash-video-container").empty();
						// animate video expander out
						$(this).animate({height: exp_start_height, width: exp_start_width}, 500, function() {
							// remove expander
							$(exp_create_holder).remove();
							$(exp_overlay).remove();
						});
						
						return false;
						
					});
					
					// add click functionality to the overlay
					$(exp_overlay).click( function() {
					
						$(".expand-close").remove();
						
						// animate video expander out
						$(exp_create_holder).find("div#flash-video-container").empty();	
						$(exp_create_holder).find("div#flash-video-container").parent().animate({height: exp_start_height, width: exp_start_width},500, function() {
								// remove expander
							$(exp_create_holder).remove();
								// animate overlay out
							$(exp_overlay).remove();
										
						});
							
					return false;
						
					});
					
					// add click functionality to the close button
					$(".expand-close").click( function() {
						
						$(this).css("display", "none");
						$(this).remove();
						
						// animate video expander out
						$(exp_create_holder).find("div#flash-video-container").empty();	
						$(exp_create_holder).find("div#flash-video-container").parent().animate({height: exp_start_height, width: exp_start_width},500, function() {
							// remove expander
							$(exp_create_holder).remove();
							// animate overlay out
							$(exp_overlay).remove();
								
						});
							
					return false;
						
					});
				});
	
			}, 200);

		
		return false;
		
	
	});
	
	// assign an onclick to the sibling text link
	$("a.expanding-video-link").click(function() {
 		$(this).parent().parent().find("a.expanding-video").click();
 		return false;
 	});
	

	
});