/***************************/
//@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(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#vid_placeholder").hide();
		$("#div_vid_player").hide();
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		$("#div_vid_player").show();
		$("#vid_placeholder").show();
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	if (!isLoggedIn && !(active_module == "pages" && module_detail == "tos"))
	{
		// Get the cookie value on whether the user has accepted the terms - Expires after 90 days
		var strAcceptedTerms = $.cookie('BioinvestTerms');
		strAcceptedTerms = 'hasaccepted=1'; //SWITCH TO TURN ON ALWAYS
		if (strAcceptedTerms != 'hasaccepted=1')
		{
			// AJAX Callback
			$.post(virpath+'index.php?hp=1&m=site_terms', {'termslabel':'popupterms'},
				function(response) 
				{
					var response = response.split("\n");
					if ( response.length == 2 )
					{
						if ( response[0] == 'ok' && response[1] )
						{
							$('#termsArea').html(response[1]);
							return true;
						}
					}
				}
			);
			
			//LOADING POPUP
			//centering with css
			centerPopup();
			//load popup
			loadPopup();
						
			//CLOSING POPUP
			$("#btnVisitTerms").click(function(){
				disablePopup();
				window.location = virpath + 'page/tos/';
			});
			
			$("#btnPopupAcceptTerms").click(function(){
				disablePopup();
				$.cookie('BioinvestTerms', 'hasaccepted=1', { expires: 90, path: '/' });
			});
			
		}
	}
	
	$("#btnAcceptTerms").click(function(){
		$.cookie('BioinvestTerms', 'hasaccepted=1', { expires: 90, path: '/' });
		window.location = virpath;
	});

});
