$(document).ready(function(){
	var current_page; //string, hash ID of the currently selected page
	
	var main_navi = new Array(); //string array, collection of main navigation ID
	main_navi["Home"] = "#HomeNav";
	main_navi["Services"] = "#ServicesNav";
	main_navi["Products"] = "#ProductsNav";
	main_navi["ContactUs"] = "#ContactUsNav";

	//initialize hash navigation; see <http://stilbuero.de/jquery/history/>
	$.ajaxHistory.initialize();
	
	//initialize current_page value
	current_page = window.location.hash.replace(/^.*#/, '').split("/");
	if (current_page[0]=='')
	{
		current_page[0] = "Home";
	}
	
	//determines the active page, loads corresponding content; see <http://stilbuero.de/jquery/history/>
	$("a.a-main").history(function(){
		current_page = "#"+$(this).attr('id');
		$("a.a-main").each(function(){
			if($(this).attr('id')!=current_page)
			{
				$(this).removeClass('active');
			}
		});
		$(current_page).addClass('active');
		LoadContent_FUNC(this.href.replace(/^.*#/, ''));
	})

	//trigger a click event at start
	$(main_navi[current_page[0]]).trigger('click');
	
	//load hit counter
	$("#hits-counter").load("php/HitCounter.php");	
});

/*--- FUNCTIONS ---*/

/*
	Function Name: LoadContent_FUNC
	Description: loads the content of the selected page
	Parameters: hash - id of the content to be loaded
	Returns: <none>
*/
function LoadContent_FUNC(hash)
{	
	var Curr_LVAR;
	
	hash = hash.split("/");
	
	$("#ContentDiv").load("contents.php #" + hash[0],function(){
		
		switch(hash[0])
		{
			case "Home":
				$("#AboutUsDiv").load("aboutus.html");
				LoadFlash_FUNC("feat");
				document.title = "Target Display Co., Inc. - " + hash[0];
				break;
			case "Products":
				LoadFlash_FUNC('dept');
				$("#IntroDiv").load("intro.html #dept");
				$("#DeptStrNav").addClass('submenu-active');
				$("a.product").history(function(){
					Curr_LVAR = "#"+$(this).attr('id');
					$(".product").each(function(){
						if($(this).attr('id')!=Curr_LVAR)
						{
							$(this).removeClass('submenu-active');
						}
					});
					$(Curr_LVAR).addClass('submenu-active');
					hash = this.href.replace(/^.*#/, '');
					hash = hash.split("/");
					LoadFlash_FUNC(hash[1]);
					document.title = "Target Display Co., Inc. - " + hash[0];
				});
				break;
			case "Services":
				LoadFlash_FUNC('design');
				$("#IntroDiv").load("intro.html #design");
				$("#DesignNav").addClass('submenu-active');
				$("a.service").history(function(){
					Curr_LVAR = "#"+$(this).attr('id');
					$(".service").each(function(){
						if($(this).attr('id')!=Curr_LVAR)
						{
							$(this).removeClass('submenu-active');
						}
					});
					$(Curr_LVAR).addClass('submenu-active');
					hash = this.href.replace(/^.*#/, '');
					hash = hash.split("/");
					LoadFlash_FUNC(hash[1]);
					document.title = "Target Display Co., Inc. - " + hash[0];
				});
				
				break;
			case "ContactUs":
				InitContactForm_FUNC();
				document.title = "Target Display Co., Inc. - " + hash[0];
				break;
		}
		document.title = "Target Display Co., Inc. - " + hash[0];
	});
}

/*
	Function Name: LoadFlash_FUNC
	Description:  loads the slide show
	Parameters: FlashID - the id of the selected slideshow
	Returns: <none>
*/
function LoadFlash_FUNC(hash)
{
	var swfGrp_LVAR;
	var FlashLabel_LVAR;
	var ContDiv;
	FlashName = "flash/slideshow.swf";
	$(".flash-container").show();
	switch(hash)
	{
		case "dept":
			swfGrp_LVAR = 0;
			ContDiv = "#products-flash";
			break;
		case "shelvrack":
			swfGrp_LVAR = 1;
			ContDiv = "#products-flash";
			break;
		case "disp":
			swfGrp_LVAR = 2;
			ContDiv = "#products-flash";
			break;
		case "sign":
			swfGrp_LVAR = 3;
			ContDiv = "#products-flash";
			break;
		case "troll":
			$(".flash-container").hide();
			break;
		case "str":
			$(".flash-container").hide();
			break;
		case "scale":
			swfGrp_LVAR = 4;
			ContDiv = "#products-flash";
			break;
		case "design":
			swfGrp_LVAR = 5;
			ContDiv = "#services-flash";
			break;
		case "PM":
			$(".flash-container").hide();
			break;
		case "sourcing":
			$(".flash-container").hide();
			break;
		case "feat":
			swfGrp_LVAR = 6;
			ContDiv = "#featured-flash";
			break;
		default:
			swfGrp_LVAR = 0;
	}
	$("#IntroDiv").load("intro.html #"+hash);
	$(ContDiv).flash(
		{
			width : '490',
			height : '320',
			src : FlashName,
			wmode: 'transparent',
			flashvars : {grp: swfGrp_LVAR}
		},
		{ version: 10 }
	);
}

/*
	Function Name: InitContactForm_FUNC
	Description:  prepares the contact form
	Parameters: <none>
	Returns: <none>
*/
function InitContactForm_FUNC()
{
	var CFopt_MVAR;

	var ValidEmail_LVAR=false;
	var ValidInput_LVAR=false;
	var CaptchaStr_LVAR="";
	
	$("#sending-img").hide();
	
	$("#ContactBtn").attr("disabled", true);
	
	CFopt_MVAR = {
		url: 'php/SubmitContactForm.php',
		type: 'post',
		success: function(responseText){
			alert(responseText);
			$(":input", "#ContactForm").each(function() {
				if (this.type!='submit')
					this.value = "";
			});
			$("#sending-img").hide();
			$("#ContactForm").fadeTo("fast",1);
		}
	};
	
	$("#ContactForm").ajaxForm(CFopt_MVAR);
	
	$(":input", "#ContactForm").each(function() {
		count=0;
		$(this).blur(function(){
			if (this.type!="submit" && this.id!="EmailTxtBx" && this.id!="CaptchaTxtBx")
			{
				if(this.value!="")
				{
					$(this).parent().siblings('.verify').removeClass("invalid");
					$(this).parent().siblings('.verify').addClass("valid");
					count++;
				}
				else
				{
					$(this).parent().siblings('.verify').removeClass("valid");
					$(this).parent().siblings('.verify').addClass("invalid");
				}
			}
			if (count<3)
				ValidInput_LVAR=false;
			else
				ValidInput_LVAR=true;
			IsValid_FUNC();
		});
	});
	
	$("#EmailTxtBx").blur(function(){
		var Pattern_LVAR = 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);
		ValidEmail_LVAR = Pattern_LVAR.test($(this).val());
		if(ValidEmail_LVAR)
		{
			$("#EmailSpan").removeClass("invalid");
			$("#EmailSpan").addClass("valid");
		}
		else
		{
			$("#EmailSpan").removeClass("valid");
			$("#EmailSpan").addClass("invalid");
		}
		IsValid_FUNC();
	});
	
	$("#ContactBtn").click(function(){
		$("#sending-img").show();
		$("#ContactForm").fadeTo("slow",0.25);
	});
	
	function IsValid_FUNC()
	{
		if(ValidInput_LVAR==false || ValidEmail_LVAR==false)
			$("#ContactBtn").attr("disabled", true);
		else
			$("#ContactBtn").attr("disabled", false);
	}
}
