$(document).ready(function() {	
 	InitializeModalDialog();
});

function InitializeModalDialog() {
	//select all the a tag with name equal to modal
	$("div.enquiryButton").click(function(e) {
		console.log("hello");
		InitializeModalDialogFadeIn(e, $(this));
	});
	
	
	$('a[name=modal]').click(function(e) {
		InitializeModalDialogFadeIn(e, $(this));
	});
	
	$(".officeInfoBox img").click(function(e){
		InitializeModalDialogFadeIn(e, $(this));
	});
	ModifyPointerEffect($(".officeInfoBox img"));
	
	$(".officeInfoBox2 img").click(function(e){
		InitializeModalDialogFadeIn(e, $(this));
	});
	ModifyPointerEffect($(".officeInfoBox2 img"));
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});	
}

function InitializeModalDialogFadeIn(e, triggerEle) {
	//Cancel the link behavior
	if (e) { e.preventDefault(); }
	
	//Get the A tag
	var id = $(triggerEle).attr('href');
	id = id.substring(id.indexOf("#"));

	/*
	switch (id) {
		case "#dialog": {
			if (!$(triggerEle).attr("oid")) {
				return;
			}
			
			//Modify the src of iframe in the dialog window
			var originalSrc = $(id).find("iframe").attr("src");
			if (originalSrc.indexOf("?") >= 0) {
				originalSrc = originalSrc.substring(0,originalSrc.indexOf("?"));
			}
			$(id).find("iframe").attr("src",originalSrc+"?view=officeDetail&oid="+$(triggerEle).attr("oid"));

			break;
		}
		default: {
			return;
			break;
		}
	}*/
	
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set heigth and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	//transition effect		
	$('#mask').fadeIn(500);	
	$('#mask').fadeTo("slow",0.8);	

	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
	
	//alert("winH:"+ (winH/2) + "scrollH:" + ($(window).scrollTop()) + "height: " + ($(id).height()/2));
		  
	//Set the popup window to center of the current window position
	$(id).css('top',  winH/2+$(window).scrollTop() -$(id).height()/2);
	$(id).css('left', winW/2-$(id).width()/2);

	//transition effect
	$(id).fadeIn(500); 
}

function ModifyPointerEffect(targetEle) {
	targetEle.mouseover(function() {
		$(this).css("cursor","pointer");
	}).mouseout(function() {
		$(this).css("cursor","default");
	});
}
