// JavaScript Document
/*********************************
Popup Windows
*********************************/

$(function() {
	Popup.init();
	
	$("#mask").click(function() {
		Popup.windowOff();
	})
	
	$('#closeDiv a, #photoCloseDiv a').live("click", function() {
		Popup.windowOff();					  
	})
	
	$(document).keypress(function(e) {	
		if (e.keyCode==27 && Popup.modalStatus == 1){
			Popup.windowOff(); 
		}
	})
	
	$(window).scroll(function() {
		Popup.centerPopup();
	})
	
	$(window).resize(function() {
		Popup.centerPopup();							
	})
})

var PopupForms = {
	login: "common/loginform.html",
	addLink: "../common/addLinkForm.html",
	editPhotos:"../common/edit_image_window.php",
	reviews:"common/reviews.php"
}

var Popup = function(){
	var obj = this;
	var modalStatus = 0;
		
	var init = function () {
			$('<div></div>').attr('id', 'mask').appendTo('body');
			$('<div></div>').attr('id', 'popup').appendTo('body');
	}
	
	var windowOn = function () {
		 if (this.modalStatus == 0) {
			$('#mask').css({"opacity": "0.7"})
			$('#mask').fadeIn("normal");
			$('#popup').fadeIn("normal");
			this.modalStatus = 1;
		}
	}
	
	var windowOff = function () {
		if (this.modalStatus == 1) {
			$('#mask').fadeOut("normal");
			$("#popup").fadeOut("normal");
			this.modalStatus = 0;
		}
	}
	
	var setContent = function (content) {
		obj.content = content;	
		$("#popup").load(content,'', centerPopup)
	}
	
	var centerPopup = function () {
		var windowWidth = document.body.clientWidth || document.documentElement.clientWidth || window.innerWidth;
		var windowHeight = document.body.clientHeight || document.documentElement.clientHeight || window.innerHeight;
		var scrollHeight = document.body.scrollHeight || document.documentElement.scrollHeight || window.scrollHeight;
		var scrollWidth = document.body.scrollWidth || document.documentElement.scrollWidth || window.scrollWidth;
		var scrollTop = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset;
		var popupHeight = $("#popup").height();
		var popupWidth = $("#popup").width();
		$("#popup").css({
			"position": "absolute",
			"top": scrollTop + 25 +"px",
			"left": (windowWidth-popupWidth)/2 +"px"
		})
		$("#mask").css ({
			"height": scrollHeight + "px",
			"width": scrollWidth + "px"
		})
	}
		
	return {
		init: init,
		windowOn: windowOn,
		windowOff: windowOff,
		modalStatus: modalStatus,
		setContent: setContent,
		centerPopup:centerPopup
	}

}();
