window.master_file_path = "http://www.castatetests.com/";
window.modules_path = "modules";
window.engine_path = "engine";
var current_page = "index";
var page_character = new Array();

window.ceduPrice = 232;

page_character["index"] = new Image(215, 194);
page_character["index"].src = "images/standin_guy.png";
page_character["continuing_ed"] = new Image(326, 235);
page_character["continuing_ed"].src = "images/standin_group.png";
page_character["electrical_cert"] = new Image(174, 351);
page_character["electrical_cert"].src = "images/climbing_guy.png";
page_character["information"] = new Image(296, 281);
page_character["information"].src = "images/carryin_guy.png";
page_character["tell_a_friend"] = new Image(313, 248);
page_character["tell_a_friend"].src = "images/telling_guy.png";
page_character["contact"] = new Image(284, 219);
page_character["contact"].src = "images/sitting_guy.png";
page_character["enroll"] = new Image(300, 250);
page_character["enroll"].src = "images/enroll_guys.png";

$(document).ready( function()
{
	
	if (!is_IE(6))
	{
	
		initiate_ajax_links();
		initiate_links();
		initiate_forms();
		initiate_url_listener();
		
		initiate_iframe();
			
		$(window).resize( function()
		{
			
			initiate_iframe();
			
		});
		
	}
	
});

function initiate_iframe()
{
	
	$(".external_content").html("<iframe name='ec_iframe' id='ec_iframe' style='width: 100%; height: 100%;'></iframe>");
	
	var viewportwidth;
	var viewportheight;
	 
	if (typeof window.innerWidth != 'undefined')
	{
		
		viewportwidth = window.innerWidth;
		viewportheight = window.innerHeight;
		
	}
	
	else if (typeof document.documentElement != 'undefined' &&
			 typeof document.documentElement.clientWidth != 'undefined' &&
			 document.documentElement.clientWidth != 0)
	{
		
		viewportwidth = document.documentElement.clientWidth;
		viewportheight = document.documentElement.clientHeight;
		
	}
	
	else
	{
		
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
		
	}
	
	$(".external_content").css("height", (viewportheight - 112) + "px");
	//$(".ec_iframe").css("height", (viewportheight - 112) + "px");
	//$(".external_content").attr("width", viewportwidth + "px");

}

function initiate_links()
{
	$("a:not(.ajax_link)").each( function()
	{
		var target_url = $(this).attr("href");
		
		$(this).attr("href", function()
		{
			if ($(this).hasClass("pdf"))
			{
				return "javascript: navigate_to(\"" + $(this).attr("href") + "\", \"pdf\");";
				
			} else {
				
				return "javascript: navigate_to(\"" + $(this).attr("href") + "\", \"link\");";	
			}
		});	
	});	
}

function initiate_forms()
{
	$("#contact_form").submit( function()
	{
		var email_data = "name=" + $("#name").val();
		email_data += "&company=" + $("#company").val();
		email_data += "&phone=" + $("#phone").val();
		email_data += "&email=" + $("#email").val();
		email_data += "&message=" + escape($("#message").val());
		
		$.ajax(
		{
			type: "POST",
			url: window.master_file_path + window.modules_path + "/contact_content/functions/contact.php",
			data: email_data,
			success: function(data)
			{
				$("#contact_form").prepend(data);	
			}
		});
		
		return false;	
	});
	
	$("#tell_a_friend_form").submit( function()
	{
		var email_data = "email_to=" + $("#email_to").val();
		email_data += "&personalize=" + escape($("#personalize").val());
		
		$.ajax(
		{
			type: "POST",
			url: window.master_file_path + window.modules_path + "/tell_a_friend_content/functions/tell_a_friend.php",
			data: email_data,
			success: function(data)
			{
				$("#tell_a_friend_form").prepend(data);	
			}
		});
		
		return false;	
	});	
}

function initiate_url_listener()
{
	var url_listener = setInterval("get_url_hash()", 0);	
}

function get_url_hash()
{
	current_hash = window.location.hash.replace(/#/g, "");
	
	if (!current_hash)
	{
		current_hash = "index";	
	}
	
	if (current_hash != current_page)
	{
		load_page(current_hash + "_content", current_hash);	
	}	
}

function navigate_to(target_url, is_pdf)
{
	load_page();
	retract_login();

	if (is_IE() && is_pdf == "link")
	{	
		$.ajax(
		{
			type: "POST",
			url: window.master_file_path + "send_http_request.php",
			data: "url=" + target_url,
			success: function(data)
			{
				document.body.scrollTop = 0;
				$(".external_content").html(data).fadeIn();
			}
		});
		
	} else {
		
		document.body.scrollTop = 0;
		initiate_iframe();
		$("#ec_iframe").attr("src", target_url);
		$(".external_content").fadeIn();
	}
}

function retract_login()
{
	$(".login_wrapper").animate(
	{	
		top: -40
		
	}, { duration: 100 });
}

function restore_login()
{
	$(".login_wrapper").animate(
	{	
		top: 0
		
	}, { duration: 100 });
}

function initiate_ajax_links()
{
	$(".ajax_link").each( function()
	{	
		if (undefined === (parent_li = $(this).parent("li").attr("id")))
		{
			parent_li = $(this).attr("id");		
		}

		$(this).attr("href", function()
		{	
			return "javascript: load_page(\"" + parent_li + "_content\", \"" + parent_li + "\");";	
		});
	});
}

function load_page(page_name, url_name)
{
	var target_file = "";
	
	if (page_name)
	{
		target_file = "file_name=" + page_name;
		
		if (!is_IE(6))
		{
		
			if (page_name == "index_content")
			{
			
				$("#logo").removeClass("logo_button");
				$("#logo").html("");
				$(".tab").css("background-image", "url(../images/tab_105x62.png)");
					
			} else {
			
				$(".tab").css("background-image", "url(../images/tab_105x62.png)");
				
				$("#logo").addClass("logo_button");
				$("#logo").html("<a id=\"index\" class=\"ajax_link\" href=\"http://www.castatetests.com\"><img src=\"images/transparent.gif\" width=\"220\" height=\"85\" border=\"none\" /></a>");
				
				if (url_name)
				{
				
					$("." + url_name).css("background-image", "url(../images/tab_ro_105x62.png)");
					
				}
				
				initiate_iframe();
				//$("#ec_iframe").attr("src", "http://www.castatetest.com.php5-3.dfw1-1.websitetestlink.com/manage/orderform.html");
			}
		
		}
		
	}
	
	$.ajax(
	{
		
		type: "POST",
		url: window.master_file_path + "parse_xml_content.php",
		data: target_file,
		success: load_response
		
	});
	
	start_transition();
	
	if (url_name)
	{
		
		current_page = url_name;
		document.location.hash = url_name;
		
	}
	
}

function load_response(data)
{

	if (data != 0)
	{
	
		new_html = "<div class=\"content_padding\">";
		new_html += data;	
		new_html += "</div>";
		
		$("#content").html(new_html);
		initiate_effects();
		initiate_ajax_links();
		initiate_links();
		initiate_forms()
		
		end_transition();
		
	} else {
	
		$("#ecc_bottom_image").css("display", "none");
			
	}
	
}

function start_transition()
{

	distance = $(this).height();
	
	$("#content").animate(
	{
	
		top: -distance
			
	}, { duration: 100 });
	
	$("#content_bg").animate(
	{
	
		bottom: distance+30
			
	}, { duration: 100 });
	
	$("#ecc_bottom_image").animate(
	{
		
		"margin-left": 400
		
	}, { duration: 100 });
	
	$(".external_content").fadeOut();
	restore_login();
	
}

function end_transition()
{
	document.body.scrollTop = 0;
	$("#content").animate(
	{
	
		top: 0
			
	}, { duration: 100 });
	
	$("#content_bg").animate(
	{
	
		bottom: 30
			
	}, { duration: 100 });
	
	$("#ecc_bottom_image").css("display", "block");
	
	$("#ecc_bottom_image").attr("src", page_character[current_page].src).attr("style", "filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + page_character[current_page].src + "', sizingMethod='scale');").animate(
	{
		
		"margin-left": 280
		
	}, { duration: 100 });
		
}

function checkout_order()
{
	var cedu32credits = $('.cedu32credits').val();
	var gecertcredits = $('.gecertcredits').val();
	var discountcode = $('.discountcode').val();
	retract_login();
	document.forms['orderinfo'].submit();
	//$("#ec_iframe").attr("src", "http://www.castatetest.com.php5-3.dfw1-1.websitetestlink.com/manage/orderform.php");
	//var chargetotal = $(".totalprice").html();
	//window.frames['ec_iframe'].document.getElementById('chargetotal').value = chargetotal;
	//setTimeout("window.frames['ec_iframe'].document.forms['orderform'].submit()", 1000);
	$(".external_content").fadeIn();
}

function adj_order(product)
{
	if (product == "cedu")
	{
		$(".certcode").val($(".ceducode").val());
	}
	else
	{
		$(".ceducode").val($(".certcode").val());
	}
	if ($(".discountcode").val().toLowerCase() == "cupertino101" || $(".discountcode").val().toLowerCase() == "return001")
	{
		window.ceduPrice = 200;
	}
	$('.totalprice').html((($('.cedu32credits').val()*window.ceduPrice)+($('.gecertcredits').val()*261.55)).toFixed(2));
}

function complete()
{
	$('.external_content').fadeOut();
	$('#ec_iframe').attr('src', 'http://www.castatetests.com/admin/scripts/post_transaction.php');
}
