// initialise
$(document).ready(function() {
	$("ul.sf-menu").supersubs({
		minWidth: 12,   // minimum width of sub-menus in em units 
		maxWidth: 30,   // maximum width of sub-menus in em units 
		extraWidth: 1     // extra width can ensure lines don't sometimes turn over 
		// due to slight rounding differences and font-family 
	}).superfish({
		speed: 300

	})
	.find("ul").bgIframe({ opacity: false });

	// call supersubs first, then superfish, so that subs are 
	// not display:none when measuring. Call before initialising 
	// containing tabs for same reason. 

	//search field
	var DEF_VAL = "Search"; // Default Value
	var isSafari = ((parseInt(navigator.productSub) >= 20020000) &&     // detecting WebCore
	               (navigator.vendor.indexOf("Apple Computer") != -1));

	//for safari, use type="search" input to get mac-like search field
	if (isSafari) {
		$("#publicsearch").addClass("issafari");

		//change using setAttribute since jQuery attr function prohibits changing type due to IE issues.
		//we're only changing for Safari, so we wont have those issues.
		if ($("#publicsearch > input[name='search']").length) $("#publicsearch > input[name='search']")[0].setAttribute('type', 'search');

		$("#publicsearch > input[name='search']").attr({
			//type: "search",
			autosave: "at.migrationexpert.search",
			results: "5",
			placeholder: DEF_VAL

		});
	} else {
		$("#search").bind({
			focus: function() {
				if ($(this).val() == DEF_VAL) {
					$(this).val('');
					$(this).addClass("focus");
				}
			},
			blur: function() {
				if ($(this).val() == '') {
					$(this).val(DEF_VAL);
					$(this).removeClass("focus");
				}
			}
		});
		if ($("#search").val() == '') $("#search").val(DEF_VAL);
	}

	//button states for IE
	function attachButtonEvents(myClass) {
		$("." + myClass).mouseover(function() {
			$(this).addClass("buttonHover");
		}).mouseout(function() {
			$(this).removeClass("buttonHover");
			$(this).removeClass("buttonActive");
			//cant get ie7 to move background on mousedown via class change.
			//do it manually via js
			//	if($(this).hasClass("p-button")){
			//		$(this).css( {backgroundPosition: "right -114px"} );
			//		$(this).children().css( {backgroundPosition: "0 0"} );
			//	}
		}).mousedown(function() {
			$(this).addClass("buttonActive");
			$(this).removeClass("buttonHover");
			//cant get ie7 to move background on mousedown via class change.
			//do it manually via js
			//	if($(this).hasClass("p-button")){
			//		$(this).css( {backgroundPosition: "right -190px"} );
			//		$(this).children().css( {backgroundPosition: "0 -76px"} );
			//	}
		}).mouseup(function() {
			$(this).removeClass("buttonActive");
			$(this).addClass("buttonHover");
			//cant get ie7 to move background on mousedown via class change.
			//do it manually via js
			//	if($(this).hasClass("p-button")){
			//		$(this).css( {backgroundPosition: "right -152px"} );
			//		$(this).children().css( {backgroundPosition: "0 -38px"} );
			//	}
		});
	}

	attachButtonEvents("p-button");
	attachButtonEvents("s-button");
	attachButtonEvents("a-button");

	//select country drop down
	$("li.change-country").click(function(event) {
		if ($("#countryselect:hidden").length) {
			//animate show
			$("#countryselect").fadeIn("normal");
		} else {
			//animate hide
			$("#countryselect").fadeOut("normal");
		}
	});
	
	//close country select on mouse out after delay
	$("#countryselect").mouseleave(function(event) {
		$("li.change-country").oneTime(800, function() {
			$(this).trigger("click");
		});
	});

	//billboard rollovers
	$("#billboardmenu > li").mouseenter(function(event, from) {
		//remove active styling from active menu item
		$(this).parent().children().children(".current").removeClass("current");

		$(this).children("a").addClass("current");

		//hide all bilboard images and text
		$("#billboard-two-col > a").addClass("hidden");

		//show current tabs billboard image and text
		var selector_string;

		if ($(this).hasClass("travel")) selector_string = "travel"
		else if ($(this).hasClass("work")) selector_string = "work"
		else if ($(this).hasClass("working")) selector_string = "working"
		else if ($(this).hasClass("student")) selector_string = "student"
		else if ($(this).hasClass("family")) selector_string = "family"		
		else if ($(this).hasClass("business")) selector_string = "business"

		$("#billboard-two-col > a." + selector_string).removeClass("hidden");

		if (from != "auto") $("#billboardmenu").stopTime('controlled');
	});

	// auto rotate through billboards if user hasn't moused over tab
	$("#billboardmenu").everyTime(8000, "controlled", function() {
		if ($(this).find("li").find(".current").parent().next().length > 0) {
			$(this).find("li").find(".current").parent().next().trigger("mouseenter", ["auto"]);
		} else {
			$(this).find(".default").trigger("mouseenter", ["auto"]);
		}
	});




	//round corners by adding a div above and below content div, and setting background images.
	//for browsers below FF3, Safari 3/Chrome

	var ua = navigator.userAgent;
	var moz = $.browser.mozilla && /gecko/i.test(ua);
	var webkit = $.browser.webkit && /Safari\/[5-9]/.test(ua);

	if (!(webkit || moz)) {
		$(".round").each(function() {
			var current_id = $(this).attr("id")
			var width = $(this).width();
			var padding_top = parseInt($(this).css("padding-top").replace("px", ""));
			var padding_bottom = parseInt($(this).css("padding-bottom").replace("px", ""));
			var padding_left = $(this).css("padding-left");
			var padding_right = $(this).css("padding-right");

			$(this).css({
				"border": "0",
				"width": (width + 2) + "px",
				"background": "transparent url(/images/" + current_id + "-bg.png) repeat-y top left",
				"overflow": "hidden"
			});
			$(this).prepend('<div id="' + current_id + '-top"></div>');
			$(this).append('<div id="' + current_id + '-bottom"></div>');
			$("#" + current_id + "-top").css({
				"position": "absolute",
				"top": "0",
				"left": "0",
				"width": (width + 2) + "px",
				"height": "5px",
				"margin": "0",
				"padding": "0 " + padding_right + " 0 " + padding_left,
				"background": "transparent url(/images/" + current_id + "-top-bg.png) no-repeat top left"
			});
			$("#" + current_id + "-bottom").css({
				"position": "relative",
				"bottom": "-" + padding_bottom,
				"left": "-" + padding_left,
				"width": (width + 2) + "px",
				"height": "5px",
				"margin": "0",
				"padding": "0 " + padding_right + " 0 " + padding_left,
				"background": "transparent url(/images/" + current_id + "-bottom-bg.png) no-repeat top left"
			});
			// fix bottom margin ie6
			if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
				$("#" + current_id + "-bottom").css("margin-bottom", "-10");
			}
		});


	}


	//make visa type li fill all horizontal space
	var total_padding = 0;
	var total_borders = -1; //left li has no border
	var panel_width = $("#visatypes").width();
	var num_cols = $("#visatypes > ul > li").length;
	$("#visatypes > ul > li").each(function() {
		total_padding += parseInt($(this).css("padding-left").replace("px", ""));
		total_padding += parseInt($(this).css("padding-right").replace("px", ""));
		total_borders += 1;
	});
	$("#visatypes > ul > li").width((panel_width - (total_padding + total_borders)) / num_cols);


	//make visa type li same height
	var visatype_height = 0;

	$("#visatypes > ul > li").each(function() {
		if ($(this).height() > visatype_height) visatype_height = $(this).height();
	}).height(visatype_height);


	//remove left margins on left panels and make adjacent panels the same height
	$("dl.other-visas > dd > ul > li:even").addClass("first").each(function() {
		if ($(this).height() > $(this).next().height()) {
			$(this).next().height($(this).height());
		} else {
			$(this).height($(this).next().height());
		}
	});

	//add margins to right panels in case content has been incorrectly classed
	$("dl.other-visas > dd > ul > li:odd").removeClass("first")

	//set height of left banners div
	//mske sure this happens last so right col height doesn't change afterwards

//TODO: get this working on client area.  leftbanner round botom not repositioning after resize in ie.

//	if ($("#leftbanners").length && $("#leftnav").length) {
//		var left_col_height = $("#leftbanners").outerHeight(true) + $("#leftnav").outerHeight(true);
//		var right_col_height = $("#right-col").height();
//
//		if (left_col_height < right_col_height) {
//			$("#leftbanners").height(right_col_height - ($("#leftnav").outerHeight(true) + ($("#leftbanners").outerHeight(true) - $("#leftbanners").height())));
//		}
//	}

});

