$(document).ready(function() {
	$('input[@type=text]').focus(function(){
		var currentVal = $(this).val();										// Read initial field value
		$(this).val('');														// Clear initial value

		$(this).blur(function(){
			var getNewVal = $(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				$(this).val(currentVal);										// Switch to initial value
			}
		});
	});

	// fix png for IE
	$('img[@src$=.png], div').ifixpng();

	// Safari CSS
	if ( $.browser.safari ) {
		$("head").append('<link href="css/safari.css" media="screen" rel="stylesheet" type="text/css" />');
	}

	// remove any borders on last LI element
	$("#navigator> ul, #content .inner ul, #page-title ul, #fullside ul, ul.columns3").each(function(){
		 $(this).children("li:last").css({borderRight:"0",borderBottom:"0",background:"none",marginRight:"0",paddingRight:"0"});
	});

	// submenu
	//$("#navigator ul ul, #navigator ul ul ul").hide();
	$("#navigator ul li").hover(
		function(){ menu($(this).attr("id")+"ul","show"); },
		function(){ menu($(this).attr("id")+"ul","hide"); }
	);
	
	$("ul").prev("p").css({paddingBottom:0,marginBottom:0});	
	$("#left-large ul").css("margin-bottom","16px");

	// accordion
//	if ( $.browser.version!="6.0" ) 
{
		$('#acc1> div').not(".opened").hide();
		$('#acc1> h3').click(function() {
			var $nextDiv = $(this).next();
			var $visibleSiblings = $nextDiv.siblings('div:visible');
			if ($visibleSiblings.length ) {
				$visibleSiblings.slideUp('fast', function() {
					$nextDiv.slideToggle('fast');
				});
			} else {
			   $nextDiv.slideToggle('fast');
			}

			return false;
		});
	}
	
	// toggle optional fields
	$("select").change(function(){
		if ( $(this).val() == "Other" && $(this).siblings("input.optional") ){
			$(this).siblings("input.optional").css("visibility","visible");
		} else if ( $(this).siblings("input.optional") ) {
			$(this).siblings("input.optional").css("visibility","hidden");
		}
	});

	// validate forms
	if ( $("form.tovalidate").length ) {
		$("form.tovalidate").validate({
			rules:{
				excessDetails: {
					required: "#policyExcessYes:checked"
				},
				injurySustained: {
					required: "#personalInjuryYes:checked"
				},
				hireChargesDetails: {
					required: "#hireChargesYes:checked"
				},
				earningsLossDetails: {
					required: "#earningsLossYes:checked"
				},
				recoveryChargesDetails: {
					required: "#recoveryChargesYes:checked"
				},
				useLossDetails: {
					required: "#useLossYes:checked"
				},
				otherLossesDetails: {
					required: "#otherLossesYes:checked"
				},
				hear_other:{
					required: function(){
						return $("#hear").val() == "Other";
					}
				}
			}
		});
	}
	
	// toggle descriptions
	$("#hidedesc p").hide();
	$("#hidedesc h3").each(function(){
		$(this).hover(
			function(){ $(this).css("cursor","hand"); },
			function(){ $(this).css("cursor","default"); }
		).click(
			function(){
				$(this).next("p").toggle();
				return false;
			}
		);
	});
	
	// Terms and condition check
	//$("form.tovalidate .submit").attr("disabled","disabled");
	$("#tac").click(function(){
		if ( $("#tac:checked").length == 1 ) {
			$("form.tovalidate .submit").attr("disabled","");
		} else {
			$("form.tovalidate .submit").attr("disabled","disabled");
		}
	});
	
	// open popup window
	$("a.popup-tac").click(function(){
		window.open($(this).attr("href"),'TAC','menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,width=460,height=640,top=60,left=60');
		return false;
	});
});

if ( $("form.tovalidate").length ) {
	$("form.tovalidate").validate();
}

var detect = navigator.userAgent.toLowerCase();
var os_ = navigator.platform.toLowerCase();
var browser,thestring;

if (checkIt('safari')) browser = "Safari"
else browser = "not Safari"

if ( os_ == 'macppc' || os_ == 'macintel' ) os = "Mac"
else os = "not Mac"

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function menu(id,status) {
	switch (status) {
		case 'show':
			$("#"+id).show();
			break;
		case 'hide':
			$("#"+id).hide();
			break;
 		}
	}