
function getWidthDocument(){ return $(document).width(); }
function getHeightDocument(){ return $(document).height(); }

function setMask(){
	//Get the screen height and width
	var maskHeight = getHeightDocument();
	var maskWidth = getWidthDocument();
	
	//Set heigth and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});

	//transition effect	
	//$('#mask').fadeIn(300);	
	$('#mask').fadeTo("slow",0.3);
	
}

function bindOpen(idBind,idWindow){
	$(idBind).bind(
		'click',
		function() {
	
			setMask();
			
			if($(idWindow).css('display') == 'none') {
	
				//Get the window height and width
				
				var winH = $(idWindow).height();
				var winW = $(idWindow).width();
				
				var windowH = $(window).height();
				
				var versionIE = $.browser.version.substr(0,1);
				
				//Set the popup window to center
				if($.browser.msie && versionIE < 7){
					$(idWindow).css('position', 'absolute');
				}else{
					$(idWindow).css('top',  parseInt((windowH - winH)/2 - 5) + 'px');
					$(idWindow).css('position', 'fixed');
				}
				
				$(idWindow).css('left', $(document).width()/2 - winW/2);
				$(idWindow).css('z-index', '1000');
				
				//transition effect
				$(idWindow).fadeIn(500);
			}
	
			this.blur();
			return false;
		}
	);
}

function bindClose(idWindowClose,idWindow){
	$(idWindowClose).bind(
		'click',
		function()
		{
			$(idWindow).fadeOut(100);
			
			//transition effect
			$('#mask').fadeOut(300);
		}
	);
}

function bindMin(idWindowMin,idWindow,idWindowContent,idWindowBottom,idWindowBottomContent,idWindowResize,idWindowMax){
	$(idWindowMin).bind(
		'click',
		function()
		{
			$(idWindowContent).SlideToggleUp(300);
			$(idWindowBottom +', '+ idWindowBottomContent).animate({height: 10}, 300);
			$(idWindow).animate({height:40},300).get(0).isMinimized = true;
			$(this).hide();
			$(idWindowResize).hide();
			$(idWindowMax).show();
		}
	);
}
function bindMax(idWindowMax,idWindow,idWindowContent,idWindowBottom,idWindowBottomContent,idWindowResize,idWindowMin,windowContent){
	$(idWindowMax).bind(
		'click',
		function()
		{
			var windowSize = $.iUtil.getSize(document.getElementById(windowContent));
			$(idWindowContent).SlideToggleUp(300);
			$(idWindowBottom +', '+ idWindowBottomContent).animate({height: windowSize.hb + 13}, 300);
			$(idWindow).animate({height:windowSize.hb+43}, 300).get(0).isMinimized = false;
			$(this).hide();
			$(idWindowMin +', '+idWindowResize).show();
		}
	);
}

function bindResizable(idWindow,idWindowTop,idWindowResize,idWindowBottom,idWindowBottomContent,idWindowContent,window){
	$(idWindow).Resizable(
		{
			minWidth: 200,
			minHeight: 60,
			maxWidth: 700,
			maxHeight: 400,
			dragHandle: idWindowTop,
			handlers: {
				se: idWindowResize
			},
			onResize : function(size, position) {
				$(idWindowBottom +', '+ idWindowBottomContent).css('height', size.height-33 + 'px');
				var windowContentEl = $(idWindowContent).css('width', size.width - 25 + 'px');
				if (!document.getElementById(window).isMinimized) {
					windowContentEl.css('height', size.height - 48 + 'px');
				}
			}
		}
	);
}

$(document).ready(
	function()
	{
		bindOpen('#windowOpen','#window');
		bindClose('#windowClose','#window');
		//bindMin('#windowMin','#window','#windowContent','#windowBottom','#windowBottomContent','#windowResize','#windowMax');
		//bindMax('#windowMax','#window','#windowContent','#windowBottom','#windowBottomContent','#windowResize','#windowMin','windowContent');
		bindResizable('#window','#windowTop','#windowResize','#windowBottom','#windowBottomContent','#windowContent','window');
	}
);

$(document).ready(
	function()
	{
		bindOpen('#windowOpen2','#window2');
		bindClose('#windowClose2','#window2');
		//bindMin('#windowMin2','#window2','#windowContent2','#windowBottom2','#windowBottomContent2','#windowResize2','#windowMax2');
		//bindMax('#windowMax2','#window2','#windowContent2','#windowBottom2','#windowBottomContent2','#windowResize2','#windowMin2','windowContent2');
		bindResizable('#window2','#windowTop2','#windowResize2','#windowBottom2','#windowBottomContent2','#windowContent2','window2');
	}
);

$(document).ready(
	function()
	{
		bindOpen('#windowOpen3','#window3');
		bindClose('#windowClose3','#window3');
		//bindMin('#windowMin3','#window3','#windowContent3','#windowBottom3','#windowBottomContent3','#windowResize3','#windowMax3');
		//bindMax('#windowMax3','#window3','#windowContent3','#windowBottom3','#windowBottomContent3','#windowResize3','#windowMin3','windowContent3');
		bindResizable('#window3','#windowTop3','#windowResize3','#windowBottom3','#windowBottomContent3','#windowContent3','window3');
	}
);


$(document).ready(
	function()
	{
		bindOpen('#windowOpen4','#window4');
		bindClose('#windowClose4','#window4');
		//bindMin('#windowMin4','#window4','#windowContent4','#windowBottom4','#windowBottomContent4','#windowResize4','#windowMax4');
		//bindMax('#windowMax4','#window4','#windowContent4','#windowBottom4','#windowBottomContent4','#windowResize4','#windowMin4','windowContent4');
		bindResizable('#window4','#windowTop4','#windowResize4','#windowBottom4','#windowBottomContent4','#windowContent4','window4');
	}
);