/*
 * module_mediapop.js
 * 
 */

/**
 * @author pk
 */


/* ======================== open thumbnail in overlay ======================== */

function mediapop_openOverlay(me) {
	var itemIndex = $('.module_mediapop',$(me).parent()).index(me);
	
	if (itemIndex < 0) {
		me = me[0];
		itemIndex = $('.module_mediapop',$(me).parent()).index(me);
	}
	
	if ($('.overlay_shade').length == 0) {
		var overlayHTML = '<div class="overlay"><div class="overlay_top"></div><div class="overlay_middle"><a href="#" class="overlay_ctrl"></a><div class="overlay_image"></div><div class="overlay_content"></div></div><div class="overlay_bottom"></div></div>';
		if(!$.browser.mozilla || ($.browser.mozilla && $.browser.version.substr(0,3) > 1.5))
			$('body').prepend('<div class="overlay_shade"></div>'); // add overlay background shade
		$('body').prepend(overlayHTML); // add overlay box
		$('.overlay_shade, .overlay_ctrl').click(function(){
			mediapop_hideOverlay();
			return false;
		});
		$(window).resize( function() { // maintain lightbox positioning on resize
			posSemiFixed($('.overlay')[0]);
		});

	} else {
		$('.overlay_shade, .overlay').show();
	}
	
	if ($(me).hasClass('mediapopType_video')) { // if content is youtube
		$('.overlay_image').removeClass('overlay_imageHide');
		$('.overlay .overlay_image').css('width','428px');	
		$('.overlay .overlay_image').css('height','344px');	
		$('.overlay .overlay_image').html('<object data="'+$('.mediapop_url',me).attr('href').replace(/&/,'&amp;')+'" id="object_video" width="425" height="344" type="application/x-shockwave-flash" quality="high"><param name="pluginurl" value="http://www.adobe.com/go/getflashplayer" /> <param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="movie" value="' + $('.mediapop_url',me).attr('href').replace(/&/,'&amp;') + '"></param><div><h5 style="text-align:center;">Get Adobe Flash player</h5><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div></object>');
		$('.overlay .overlay_content').html($('.mediapop_data',me).html());
			posSemiFixed($('.overlay')[0]);
		$('.overlay .overlay_image').css('height','auto');
		
	} else if ($(me).hasClass('mediapopType_audio')) { // if content is audiobook
		$('.overlay_image').removeClass('overlay_imageHide');
		$('.overlay .overlay_image').css('width','290px');		
		$('.overlay .overlay_image').css('height','300px');	
		$('.overlay .overlay_image').html('<iframe id="lightviewContent" scrolling="auto" frameborder="0" hspace="0" src="' + $('.mediapop_url',me).attr('href') + '" name="lightviewContent" style="border: 0pt none ; margin: 0pt; padding: 0pt; width: 290px; height: 300px;"/>');
		$('.overlay .overlay_content').html($('.mediapop_data',me).html());
		$('.overlay .overlay_image iframe').load(function(){
			 posSemiFixed($('.overlay')[0]); // re-position lightbox after image has loaded
		});
		posSemiFixed($('.overlay')[0]);
		$('.overlay .overlay_image').css('height','auto');		
	}
}

function mediapop_hideOverlay() {
	$('.overlay_shade, .overlay').remove();
}


/* ======================== Page DOM Loaded ======================== */


$(document).ready(function(){
	$('.module_mediapop').click(function(){
			mediapop_openOverlay(this);
			return false;
	});
});
