/***************************/
//@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;
var popupStatus2 = 0;
var popupStatus3 = 0;

				function getInternetExplorerVersion()
				// Returns the version of Internet Explorer or a -1
				// (indicating the use of another browser).
				{
				  var rv = -1; // Return value assumes failure.
				  if (navigator.appName == "Microsoft Internet Explorer")
				  {
					var ua = navigator.userAgent;
					var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
					if (re.exec(ua) != null)
					  rv = parseFloat( RegExp.$1 );
				  }
				  return rv;
				}

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#bg_popUp").css({
			"opacity": "0.7"
		});
		$("#bg_popUp").fadeIn("slow");
		$("#popUpWrap").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#bg_popUp").fadeOut("slow");
		$("#popUpWrap").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popUpWrap").height();
	var popupWidth = $("#popUpWrap").width();
	//centering
	$("#popUpWrap").css({
		"position": "absolute",
		//"top": windowHeight/2-popupHeight/2,
		//"top": "730px",
		//"top": "100px",
		//"top": (document.body.clientHeight - 520) +"px",
		"top": (document.body.clientHeight/2 - popupHeight/2) +"px",
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

//"height": windowHeight
	
if ( (getInternetExplorerVersion() < 7) && (getInternetExplorerVersion() > 0) ) {
	$("#bg_popUp").css({
		"height": document.body.clientHeight
	});
}
	
}

//-----------------------

//loading popup with jQuery magic!
function loadPopup2(){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		$("#bg_popUp").css({
			"opacity": "0.7"
		});
		$("#bg_popUp").fadeIn("slow");
		$("#privacyPop").fadeIn("slow");
		popupStatus2 = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup2(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		$("#bg_popUp").fadeOut("slow");
		$("#privacyPop").fadeOut("slow");
		popupStatus2 = 0;
	}
}

//centering popup
function centerPopup2(){

	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#privacyPop").height();
	var popupWidth = $("#privacyPop").width();
	//centering
	$("#privacyPop").css({
		"position": "absolute",
		//"top": windowHeight/2-popupHeight/2,
		//"top": "730px",
		"top": (document.body.clientHeight - 500) +"px",
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
if ( (getInternetExplorerVersion() < 7) && (getInternetExplorerVersion() > 0) ) {
	$("#bg_popUp").css({
		"height": document.body.clientHeight
	});
}
	
}

//-----------------------

//loading popup with jQuery magic!
function loadPopup3(){
	//loads popup only if it is disabled
	if(popupStatus3==0){
		$("#bg_popUp").css({
			"opacity": "0.7"
		});
		$("#bg_popUp").fadeIn("slow");
		$("#creditPop").fadeIn("slow");
		popupStatus3 = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup3(){
	//disables popup only if it is enabled
	if(popupStatus3==1){
		$("#bg_popUp").fadeOut("slow");
		$("#creditPop").fadeOut("slow");
		popupStatus3 = 0;
	}
}

//centering popup
function centerPopup3(){

	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#creditPop").height();
	var popupWidth = $("#creditPop").width();
	//centering
	$("#creditPop").css({
		"position": "absolute",
		//"top": windowHeight/2-popupHeight/2,
		//"top": "730px",
		"top": (document.body.clientHeight - 500) +"px",
			"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

if ( (getInternetExplorerVersion() < 7) && (getInternetExplorerVersion() > 0) ) {
	$("#bg_popUp").css({
		"height": document.body.clientHeight
	});
}
	
}

// -----------------------------------


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#btn_preview").click(function(){
	
		var text_own_obj = document.getElementById('text_own');
		if ( text_own_obj ) {

			//document.getElementById('textBody').innerHTML = text_own_obj.value;
			
			var editor_data = CKEDITOR.instances.text_own.getData();
			//alert(editor_data);
			document.getElementById('textBody').innerHTML = editor_data;
			
			var userImage = document.getElementById('userImage');
			if ( userImage ) {
				var previewImage = document.getElementById('previewImage');
				previewImage.src = userImage.src;
			}
		
		} else {
		
			var textTo = document.getElementById('text_to').value;
			var textFrom = document.getElementById('text_from').value;
			
			if ( 'To' == textTo ) {
				textTo = '';
			}
			if ( 'From' == textFrom ) {
				textFrom = '';
			}
		
			document.getElementById('textTo').innerHTML = textTo;
			document.getElementById('textBody').innerHTML = document.getElementById('text_body').value;
			document.getElementById('textFrom').innerHTML = textFrom;
			
			var selectedImage = document.getElementById('selectedImage');
			var previewImage = document.getElementById('previewImage');
			previewImage.src = selectedImage.src;
			
		}
	
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		return false;
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popUpWrapClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#bg_popUp").click(function(){
		disablePopup();
		disablePopup2();
		disablePopup3();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
		if(e.keyCode==27 && popupStatus2==1){
			disablePopup2();
		}
		if(e.keyCode==27 && popupStatus3==1){
			disablePopup3();
		}
	});
	

	//-------------------------------

	//LOADING POPUP
	//Click the button event!
	$("#btn_privacy").click(function(){
	
		//centering with css
		centerPopup2();
		//load popup
		loadPopup2();
		return false;
	});

	//-------------------------------

	//LOADING POPUP
	//Click the button event!
	$("#btn_credits").click(function(){
	
		//centering with css
		centerPopup3();
		//load popup
		loadPopup3();
		return false;
	});


});