/*
There's quirky problems with mousenter/mouseleave in different browsers so there's a fair bit of hacking in here.
*/

// Projects
$(function(){
	// Default
	target_id = "";

	// Only perform this request on relevant pages.
	if(document.getElementById("collection_thumbnails")){		
		// Loop through each hotspot and set it up.
		$(".thumbnail_hotspot").each(function(i){
			// On mouseover
			$(this).mouseenter(function(){
				// Remove the animation from any existing clips.
				if(target_id != ""){
					// Remove the clip.
					$("#"+target_id).html("");
					
					// Set the target ID to nothing so that clicking on the page later doesn't pop up the video.
					target_id = "";
				}
				
				// Parse out the target ID and set it to be used by the click function above.
				this_id = this.id;
				target_id_parts = this_id.split("_");
				target_id = target_id_parts[1];
				
				// Get the swf file.
				clip_to_load = document.getElementById("swf_"+target_id).value;
				preloader = "/wp-content/plugins/collections/public/preloader/preloader";
				
				// Only swap out the clip if it exists.
				if(clip_to_load != ""){	
					// Get the full embed code required.
					AC_FL_RunContent(
						'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'
						,'width','190'
						,'height','80'
						,'src',preloader
						,'quality','high'
						,'pluginspage','http://www.macromedia.com/go/getflashplayer'
						,'align','middle'
						,'play','true'
						,'loop','true'
						,'scale','showall'
						,'wmode','transparent'
						,'devicefont','false'
						,'id',"swf_preloader"
						,'bgcolor','#000000'
						,'name','Video'
						,'menu','true'
						,'allowFullScreen','false'
						,'allowScriptAccess','sameDomain'
						,'movie',preloader
						,'salign', ''
						,'FlashVars','clip_to_load='+clip_to_load
					);
					
					// Write the embed code to the current div.
					$("#"+target_id).html(document.getElementById("embed_code").value);				
				}else{
					// If there is no video clip then disable the popup.
					target_id = "";
				}
			});
			
			// On mouseout
			$(this).mouseleave(function(){
				if(target_id != ""){
					// Remove the clip.
					$("#"+target_id).html("");
					
					// Set the target ID to nothing so that clicking on the page later doesn't pop up the video.
					target_id = "";
				}
			});
		});
				
		// Open a project on a mouseclick.
		$(document).mousedown(function(){
			if(target_id != ""){
				// Remove the clip.
				$("#"+target_id).html("");
				
				// Popup the video
				fancybox_options = {
					'hideOnContentClick':	false
					,'overlayOpacity':		0.8
				}
				$("#link_"+target_id).fancybox(fancybox_options).trigger('click');
				
				// Set the target ID to blank so it doesn't keep popping up.
				target_id = "";			
			}
		});
				
		// Remove the animation from any existing clips.
		$("#menu,#header,#player_wrapper,#audio_directors_list,.jcarousel-next,.jcarousel-prev,#director_list").mouseenter(function(){
			if(target_id != ""){
				// Remove the clip.
				$("#"+target_id).html("");
				
				// Set the target ID to nothing so that clicking on the page later doesn't pop up the video.
				target_id = "";
			}
		});
	}
});

// Featured Project
$(function(){
	// Only perform this request on relevant pages.
	if(document.getElementById("featured_project")){
		$(".featured_project_hotspot").each(function(i){
			// On mouseover
			$(this).mouseenter(function(){
				// Remove the animation from any existing clips.
				if(target_id != ""){
					// Remove the clip.
					$("#"+target_id).html("");
					
					// Set the target ID to nothing so that clicking on the page later doesn't pop up the video.
					target_id = "";
				}
				
				// Parse out the target ID and set it to be used by the click function above.
				this_id = this.id;
				target_id_parts = this_id.split("_");
				target_id = target_id_parts[1];
				
				// Get the swf file.
				clip_to_load = document.getElementById("swf_"+target_id).value;
				preloader = "/wp-content/plugins/collections/public/preloader/preloader_large";
				
				// Only swap out the clip if it exists.
				if(clip_to_load != ""){				
					// Get the full embed code required.
					AC_FL_RunContent(
						'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'
						,'width','456'
						,'height','194'
						,'src',preloader
						,'quality','high'
						,'pluginspage','http://www.macromedia.com/go/getflashplayer'
						,'align','middle'
						,'play','true'
						,'loop','true'
						,'scale','showall'
						,'wmode','transparent'
						,'devicefont','false'
						,'id',preloader
						,'bgcolor','#000000'
						,'name','Video'
						,'menu','true'
						,'allowFullScreen','false'
						,'allowScriptAccess','sameDomain'
						,'movie',preloader
						,'salign', ''
						,'FlashVars','clip_to_load='+clip_to_load
					);
					
					// Write the embed code to the current div.
					$("#"+target_id).html(document.getElementById("embed_code").value);				
				}
			});
			
			// On mouseout
			$(this).mouseleave(function(){
				if(target_id != ""){
					// Remove the clip.
					$("#"+target_id).html("");
					
					// Set the target ID to nothing so that clicking on the page later doesn't pop up the video.
					target_id = "";
				}
			});
		});
		
		// Remove the animation from any existing clips.
		$("#menu,#header,#player_wrapper,#audio_directors_list,.jcarousel-next,.jcarousel-prev,#director_list").mouseenter(function(){
			if(target_id != ""){
				// Remove the clip.
				$("#"+target_id).html("");
				
				// Set the target ID to nothing so that clicking on the page later doesn't pop up the video.
				target_id = "";
			}
		});
	}
});

$(function(){
	// Only perform this request on relevant pages.
	if(document.getElementById("fancybox_gallery")){
		fancybox_options = {
			'hideOnContentClick':	false
			,'overlayOpacity':		0.8
		}
		$(".fancybox").fancybox(fancybox_options);
	}
});