/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
//
//@implementing and optimizing for Talent Banque: Alex Baskov, 2009
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusNews = 0;


//loading popup with jQuery magic!
function loadPopupNews()
{
	//loads popup only if it is disabled
	if(popupStatusNews == 0)
	{
		$("#popupNewsBackground").css({
			"opacity": "0.5" // set to zero, if you don't want to have a background
		});
		$("#popupNewsBackground").fadeIn("slow");
		$("#popupNews").fadeIn("slow");
		popupStatusNews = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopupNews()
{
	//disables popup only if it is enabled
	if(popupStatusNews == 1)
	{
		$("#popupNewsBackground").fadeOut("slow");
		$("#popupNews").fadeOut("slow");
		popupStatusNews = 0;
	}
}

//centering popup
function centerPopupNews()
{
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupNews").height();
	var popupWidth = $("#popupNews").width();

	//
	// we need here to emulate fixed position... so we use offsetHeight and offsetWidth instead
	// (c) Alex Baskov
	//
	var topScrollerPosition = 0;
	
	// we may just scroll to top and see nice popup
	//scroll(0,0);

	// center vertically in the visible area...
	if ($.browser.msie) {
		topScrollerPosition = document.body.scrollTop;
	}
	else {
		topScrollerPosition = window.pageYOffset;
	}

	//centering
	$("#popupNews").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		//"top": parseInt((windowHeight-popupHeight)/2) + topScrollerPosition,
		//"top": topScrollerPosition + 60,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#popupNewsBackground").css({
		"height": windowHeight
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

	//LOADING POPUP
	//Click the button event!
	
	$(".linkNews").live('click', function() {
	
		$("#popup_news_image").parent().show();
		
		$("#popupNews .title").html($(this).parent().parent().find(".title").html());
		$("#popupNews .news_desc").html($(this).parent().parent().find(".full_desc").text());
		$("#popupNewsGoToNews").attr("href", $(this).parent().find(".allNews").attr("href"));
		
		var imgSrc = $(this).attr("img");
		if (imgSrc != "") {
			$("#popup_news_image").attr("src", imgSrc);
			var height = $(this).attr("img-height");
			$("#popup_news_image").attr("height", height);
			$("#popup_news_image").attr("width", $(this).attr("img-width"));
	
			$("#popup_news_image").parent().css("height", height + "px");
		}
		else {
			$("#popup_news_image").parent().hide();
		}
		
		//centering with css
		centerPopupNews();
		//load popup
		loadPopupNews();
	});
				
	//CLOSING POPUP
	$("#popupNewsClose").click(function() {
		disablePopupNews();
	});
	
	//Click out event!
	$("#popupNewsBackground").click(function() {
		disablePopupNews();
	});
	
	//Press Escape event!
	$(document).keypress(function(e) {
		if(e.keyCode == 27 && popupStatusNews == 1) {
			disablePopupNews();
		}
	});

});


