var isHome = true;
var minHeight = 840;
var showRegister = false;
var headerWidth = 250;//$("div#header").width();	
var bgAnim = true;
var currLeftBg = 0;
var currRightBg = 0;
var intro = true;

var loadDone = false;

var images = ["img/footer_bg.png",
		"img/header_bg.png",
		"img/register_tab.png",
		"img/content_box_bg.png",
		"img/logo.gif",
		"img/nav_sub_bg.jpg",
		"img/bg/6acre.jpg",
		"img/bg/blueprint.jpg",
		"img/bg/ex_area.jpg",
		"img/bg/freehold.jpg",
		"img/bg/gardens.jpg",
		"img/bg/guarded.jpg",
		"img/bg/lakeview.jpg",
		"img/bg/others.jpg",
		"img/bg/cyberjaya.jpg",
		"img/bg/location.jpg",
		"img/6acre_pic.jpg",
		"img/architect.jpg",
		"img/area_loc.jpg",
		"img/freehold_pic.jpg",
		"img/garden_pic.jpg",
		"img/lakeview_pic.jpg",
		"img/others_pic.jpg",
		"img/location_map.jpg",
		"img/cyberjaya_map_small.jpg",
		"img/suntrack_large.gif",
		"img/downtown.jpg",
		"img/education.jpg",
		"img/working.jpg",
		"img/future.jpg",
		"img/aerial_pic.jpg",
		"img/satellite_pic.jpg",
		"img/map_pic.jpg"
	];
	
var totalImages = images.length;
var currLoaded = 0;


$(document).ready(
	function()
	{	
		var options = { 
			beforeSubmit:  showRequest,  // pre-submit callback 
			success:       showResponse  // post-submit callback 
		}; 
				 
		// bind form using 'ajaxForm' 
		$('#frmRegister').ajaxForm(options); 
	
		//Add class parent to first link of li.parent
		$("li.parent").each(
			function()
			{
				$(this).children("a").eq(0)
					.addClass("parent")
					.click(
						//Accordion	
						function()
						{
							var par = $(this).parent();
							
							if(!$(par).hasClass("open"))
							{
								$("li.parent").removeClass("open");
								$(par).addClass("open");
							
								$("li.parent ul").slideUp();
								
								$(this).parent().children("ul").slideDown();
							}
						}
					);
			}
		);
		
		//Window resize event
		$(window).resize(
			function()
			{
				alignSite();
			}
		);
		
		$("div#register_tab").click(
			function()
			{
				toggleRegister();
			}
		);
		
		$("div#register a#btn_close").click(
			function()
			{
				toggleRegister();
			}
		);
		
		alignSite();
		startBgAnim();
		
		loadImage(0);		
	}
);

function loadImage(index)
{
	var src = images[index];		
	var i = new Image();
	
	$(i).load(
		function()
		{
			currLoaded++;
			$("span#loading_status").html(currLoaded + "/" + totalImages + " elements loaded");
			
			if(currLoaded == totalImages)
			{
				$("div.loading").hide();	
			}
			else
			{
				loadImage(currLoaded);
			}
		});
		
	i.src = src;
	
	$("div#preloader").append(i);
}

function alignSite()
{
	var width = $(window).width();		
	var height = $(window).height();
	
	if(height < minHeight) height = minHeight;
				
	var mainWidth = width-headerWidth+10;		
	
	//Contents
	$("div#main div.content").height(height);
	$("div#main div.content").width(mainWidth);
	
	$("div#header").height(height);	

	//Footer
	var footerMargin = height - 45;//$("div#footer").height();
	$("div#footer").css({"marginTop":footerMargin+"px","width":width+"px"});
			
	//Register form/tab
	var regMarginTop = footerMargin - 25;		
	if(showRegister)
	{
		regMarginTop = (height - 450)/2 - 40;
	}
	
	if(intro)
	{
		var flashMarginTop = (height - 615)/2;
		$("div#intro").height(height).width(width);
		$("#flash_intro").css("marginTop",flashMarginTop+"px");
	}
	
	if(!loadDone)
	{
		$("div#loading_modal").height(height).width(width);
		
		var loadHeight = $("#loading_text").height();
		
		var lmTop = (height - loadHeight)/2;
		lmTop - 60;
		var lmLeft = (width - 280)/2;
		
		$("#loading_text").css({"marginTop":lmTop+"px", "marginLeft":lmLeft+"px"});
	}
	
	var regMarginLeft = (width - 662)/2;
	$("div#register").css({"marginTop":regMarginTop+"px", "marginLeft":regMarginLeft+"px"});
	
	if(isHome)
	{				
		var bgWidth = (width/2) - (headerWidth/2);					
		var margin = (width - headerWidth)/2;
		
		var bgMarginLeft = (width/2 - 950)/2;
		var bgMarginTop = (height - 1070)/2;
					
		$("div.home_bg img").css({"marginLeft":bgMarginLeft,"marginTop":bgMarginTop});
		$("div.home_bg").height(height).width(bgWidth);
		
		$("div#container").width(width);
		
		$("div#header").css("marginLeft",margin+"px");	
		$("div#right_bg").css("marginLeft",(bgWidth+headerWidth)+"px");
	}
	else
	{
		var cbMarginTop = (height - $("div.content_box").height())/2 - 30 + "px";
		$("div.content_box").css("marginTop",cbMarginTop);
	}	
}

function showHome()
{
	if(!isHome)
	{
		isHome = true;
		minHeight = 820;
		
		var width = $(window).width();		
	
						
		var margin = (width - headerWidth)/2;
		
		$("p#intro_text").fadeIn(600);
		$("div#main").fadeOut(500);
		
		$("div#header").animate({"marginLeft":margin+"px"},1000,
			function()
			{
				alignSite();
				startBgAnim();
				$("div.home_bg").fadeIn(1500);
			}
		);

		$("ul#main_nav li").removeClass("active");		
	}
}

function startBgAnim()
{
	bgAnim = true;
	setTimeout("doBgAnim()",6000);
}

function doBgAnim()
{
	var fadeTime = 3000;		
	
	//LEFT SIDE
	var len = $("div#left_bg img").length;		
	var thisBg = currLeftBg++		
	if(currLeftBg > len-1) currLeftBg = 0;		
	$("div#left_bg img:eq("+thisBg+")").fadeOut(fadeTime);
	$("div#left_bg img:eq("+currLeftBg+")").fadeIn(fadeTime);
			
	
	//RIGHT SIDE
	len = $("div#right_bg img").length;		
	thisBg = currRightBg++		
	if(currRightBg > len-1) currRightBg = 0;		
	$("div#right_bg img:eq("+thisBg+")").fadeOut(fadeTime);
	$("div#right_bg img:eq("+currRightBg+")").fadeIn(fadeTime);
	
	
	//Loop it
	if(bgAnim) startBgAnim();
}

function stopBgAnim()
{
	bgAnim = false;
}

//Load a Page
function loadPage(file,link)
{	
	var par = $(link).parent();
	
	if(!$(par).hasClass("active"))
	{
		if(isHome)
		{
			isHome = false;
			minHeight = 700;
			stopBgAnim();
			
			$("div.home_bg").fadeOut(1000);
			
			$("p#intro_text").fadeOut(800,
				function()
				{
					$("div#header").animate({"marginLeft":"-13px"},1000);
					
					loadPageFile(file);
				}
			);			
		}
		else
		{
			loadPageFile(file);
		}
		
		$("ul#main_nav li").removeClass("active");			
		
		if(link != null) $(par).addClass("active");
	}
}

function loadPageFile(file)
{
	//Fade out content first
	$("div#main").fadeOut(500,
		function()
		{
			//load the file
			$("div#main").load(file, 
				function()
				{
					alignSite();
					//Fade in after complete
					$("div#main").fadeIn(1000, 
						function()
						{
							alignSite();
							$("div.content_box").fadeIn(600);
						}
					);
				}
			);

		}
	);		
}

//Show hide Register form
function toggleRegister()
{
	if(showRegister) //hide it
	{
		var marginTop = ($(document).height() - 70);
		
		$("div#register").animate({"marginTop":marginTop+"px"},400,
			function()
			{
				$("div#register_contents").hide();
			}
		);
	}
	else //show it
	{
		$("div#register_contents").show();		
		$("div#register_form").show();
		$("div#register_success").hide();		
		
		
		var marginTop = ($(window).height() - 450)/2 - 40;
		
		$("div#register").animate({"marginTop":marginTop+"px"},500);
	}
	
	showRegister = !showRegister;
}

function showRequest()
{	
	var validate = false;
	
	if(jQuery.trim($("input#name").val()) == "")
	{
		alert("Please enter your name.");
	}
	else if(jQuery.trim($("#mobile").val()) == "")
	{
		alert("Please enter your handphone number.");
	}
	else if(jQuery.trim($("input#email").val()) == "")
	{
		alert("Please enter your e-mail address.");
	}
	else if(!isValidEmailAddress($("input#email").val()))
	{
		alert("Please enter a valid e-mail address.");
	}
	else
	{
		validate = true;
	}
	
	if(validate)
	{		
		$("div#register_form").fadeOut(1000,
			function()
			{
				$("div#register_success").fadeIn(600);
			}
		);
	}
	
	return validate;
	
}

function showResponse(data)
{
	if(data == "1")
	{
		//alert("Thank you for registering with us. We will update you shortly.");
		//toggleRegister();
	}
}	


function isValidEmailAddress(emailAddress) 
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
