/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

// Loading popup with jQuery magic!
function loadPopup(video_id) {
	StopAllVideos();

	// Hide all Videos
	$('#popupContact div.popupVideo, #popupContact object, #popupContact embed').hide();
	
	// Load active video inside popup, but don't show the whole popup yet.
	$active_video = $('#popupContact div.popupVideo[id=popup' + video_id + ']').show();
	$active_video.find('object, embed').css('visibility', 'hidden').show();
	
	// Show the Next/Prev Buttons were applicable
	if ($active_video.next('.popupVideo').length > 0) {
		$('#popup_next_btn').show();
	} else {
		$('#popup_next_btn').hide();
	}
	
	if ($active_video.prev('.popupVideo').length > 0) {
		$('#popup_prev_btn').show();
	} else {
		$('#popup_prev_btn').hide();
	}
	
	// Loads popup only if it is disabled
	if (true) {
		$('#backgroundPopup').css('opacity', '0.7').fadeIn('slow');

		// Prepare Popup Window
		centerPopup();

		$('#popupContact').fadeIn('slow', function() {
			// Finally show the video.
			$active_video.find('object, embed').css('visibility', 'visible');
		});
		popupStatus = 1;
	}
}

// Switch to Next Video
function nextVideo() {
	var $next_vid = $('#popupContact div.popupVideo:visible').next('.popupVideo');
	
	if ($next_vid.length == 1) {
		// Get Video ID
		var vid_id = parseInt($next_vid.attr('id').replace('popup', ''), 10);
		
		// Load New Video
		loadPopup(vid_id);
	}
}

// Switch to Previous Video
function prevVideo() {
	var $prev_vid = $('#popupContact div.popupVideo:visible').prev('.popupVideo');
	
	if ($prev_vid.length == 1) {
		// Get Video ID
		var vid_id = parseInt($prev_vid.attr('id').replace('popup', ''), 10);
		
		// Load New Video
		loadPopup(vid_id);
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if (popupStatus == 1) {
		StopAllVideos();
		$('#popupContact div.popupVideo:visible').find('object, embed').css('visibility', 'hidden');
		$('#backgroundPopup').fadeOut('slow');
		$('#popupContact').fadeOut('slow');
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var popupHeight = $('#popupContact').height(); // 530px is the avergage height of the YouTube video.
	var popupWidth = $('#popupContact').width();
	
	//centering
	$("#popupContact").css({
		'position': 'fixed',
		"top": ((windowHeight / 2) - (popupHeight / 2)),
		"left": ((windowWidth / 2) - (popupWidth / 2))
	});
	//only need force for IE6

	$("#backgroundPopup").css({
		"height": $('body').height()
	});
}

// Stops any active video players on the page.
function StopAllVideos() {
	if (swfobject != undefined) {
		// Loop through all videos and stop them
		$('object').each(function() {
			var player = swfobject.getObjectById(this.id);
			
			if (player != undefined && typeof player.stopVideo == 'function') {
				// Player is active. Stop Video.
				player.stopVideo();
			}
		});
	}
}

//CONTROLLING EVENTS IN jQuery
$(function(){
	// SWITCHING VIDEO THUMBS
	$('ul#mediaBoxLinks a').click(function(){
		StopAllVideos();
		
		// Hide all Inner Videos
		$('object[id^=innerVideo], object[id^=innerVideo] embed').hide();
		
		$("div#videoThumbContainer > a, div#videoThumbContainer div").stop().hide();
		
		$("div#videoThumbContainer object").css('visibility', 'visible');
		
		// Get the VideoThumb ID
		var active_thumb = $(this).attr('href');
		var pos = active_thumb.search(/videoThumb([0-9]+)/i);
		active_thumb = active_thumb.substr(pos);
		
		$("div#videoThumbContainer a#" + active_thumb + ", div#videoThumbContainer div#" + active_thumb).show();
		
		$("div#videoThumbContainer div#" + active_thumb + " object:first").show();
		
		$("ul#mediaBoxLinks a").removeClass('active');
		
		$(this).addClass('active');
		
		return false;
	});

	//LOADING POPUP
	//Click the button event!
	$("#videoThumbContainer a[id^=videoThumb]").click(function(){
		// Get Video ID
		var vid_id = parseInt($(this).attr('id').replace('videoThumb', ''), 10);

		//load popup
		loadPopup(vid_id);

		return false;
	});

	//CLOSING POPUP
	//Click the x event or Click out event!
	$('#popupContactClose, #backgroundPopup').click(function(){
		disablePopup();
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	// Next Button
	$('#popup_next_btn').click(function(){
		nextVideo();
	});
	
	// Previous Button
	$('#popup_prev_btn').click(function(){
		prevVideo();
	});
	
	//$('#popupContact div.popupVideo').css('z-index', '100');

});