$(function() {

	// Bookit Tracking Link
	$("a[href*='bookings.nzbookit.com']").click(function() {
		pageTracker._link(this.href);
		return false;
	});

	$(".script-only").show();
	SetFooter();
	Format();
	InitExpandingList();
	SetSearchEffects();
	SetFlightSearchEffects();
	InitExpandingGallery();

	if (window.location.href.indexOf("Search") != -1) {
		// bind window resize
		var resizeTimer = null;
		$(window).bind('resize', function() {
			if (resizeTimer) clearTimeout(resizeTimer);
			resizeTimer = setTimeout(SetFooter, 50);
		});
	};

});

/*setTimeout ( "EQContentPodbar()", 100 ); // see comment in EQContentPodbar() function*/


// GLOBAL FUNCTIONS ===============================================================
function SetFooter(){
	var removeHeight = $("#header").height() + $("#navigation").height() + 2 + $("#footer").height() + 2 +9;
	var newPageHeight = $(window).height() - removeHeight;
	//if( newPageHeight > $("#content").height() ){
	if( newPageHeight > $("#page").height() ){
		$("#page").height( $(window).height() - removeHeight );
	}
	// set something on carbon-zero so it snaps back to correct location in IE6
	//$("#carbon-zero").css( {"display":"block"} );
}

function Format(){
	/* general ops to format various items */
	
	/* h3 in multi-col pods */
	$(".multi-pod h2").each( function(){ if($(this).height() > 20) $(this).css({"padding":"9px 11px 8px 11px"}); } );
	
	/* -- listings -- */
	/* p's following images have padding */
	$(".listing li").find(".thumbnail ~ p").css( {"padding":"0 0 0 85px"} );
	/* set bigger click */
	$(".listing li:not(:has([rel=external]))").biggerlink();
	$(".listing li:has([rel=external])").biggerlink({ follow: false });

	/* for event page [/TravellersAndServices/TravelNewZealand/Events/] */
	$(".event-list li").biggerlink({ follow: false });
	
	/* form-containers immediately following H2's have top border */
	$("h2").next(".form-container").each( function(){ $(this).addClass("thick-border") } );
	
	/* h1s above a p should have an underline */
	$("p, .article-image, .article-panorama").prev("h1").each( function(){
		$(this).css( {"border-bottom":"1px solid #cacaca"} );
	});
	
	/* travel-nz detail pages */
	if ($('.book-it-list .hot-deal').length > 0 && $('.multi-pod').length == 0) {
		var jumplink = '<a class="book-it-jump" href="#bookit">BookIt</a>';
		var heading = $('#content h1:first');
		heading.css('position', 'relative').css('zoom','1');
		
		var TitleHtml = heading.html();
		heading.html(TitleHtml + jumplink);
		
		$('.book-it-jump').css('position', 'absolute').css('bottom', '0').css('right', '0');
		$('.book-it-list').attr('id', 'bookit');

		var bookingHeading = '<h3 class="book-list-heading">Latest and Greatest Deals!</h3>';
		$('.book-it-list').parent().prepend(bookingHeading);
		$('.book-list-heading').css('border-bottom', '5px solid #37507D').css('padding-bottom', '0.5em');
	}
}

function EQContentPodbar(){
	// called after a slight delay to allow any pods within the page that are being EQ'd to fire, allowing this EQ to report the pages height correctly
	if( window.location.href.indexOf("ArrivalsAndDepartures") != -1 ){
		// don't EQ on this page
	}else{
		Equalise($("#content, #podbar"));
	}
	
	// carbon-zero needs to be adjusted by 1px in ie6 depending on whether a #content's height is odd or even
	if( $("#content").height()%2 == 1 ){
		$("#ie6 #carbon-zero").css( {"bottom":"-5px"} );
	}
	// but switch for homepage template
	if( $("#content").height()%2 == 1 ){
		$("#ie6 .homepage #carbon-zero").css( {"bottom":"-4px"} );
	}else{
		$("#ie6 .homepage #carbon-zero").css( {"bottom":"-5px"} );
	}
	
}

/* Expanding list */
function InitExpandingList(){

	$(".section-content").hide();
	$(".expanding-list .item").addClass("closed").parent(".last").addClass("closed");
	
	$(".expanding-list .item").hover(
		function(){
			$(this).addClass("hover").parent(".last").addClass("hover");
		},
		function(){
			$(this).removeClass("hover").parent(".last").removeClass("hover");
		}
	);
	
	// pre expand items selected via the #
	if(location.hash)
    {
        $(location.hash).next(".section-content").slideDown("fast");
        $(location.hash).removeClass("hover").parent(".last").removeClass("hover"); ;
        $(location.hash).removeClass("closed").parent(".last").removeClass("closed");
        $(location.hash).addClass("open").parent(".last").addClass("open");
    }

	var doOnce = false;

	$(".expanding-list .item").click(
		function() {
			if (!doOnce) {
				var theHeight = $("#content").height();
				$("#content").attr("style", "min-height:" + theHeight + "px");
				$("#ie6 #content").attr("style", "height:" + theHeight + "px");
				doOnce = true;
			}

			if ($(this).hasClass("open")) {
				$(this).next(".section-content").slideUp("fast");
				$(this).removeClass("open").parent(".last").removeClass("open"); ;
				$(this).addClass("closed").parent(".last").addClass("closed"); ;

				if ($(".expanding-list .open").length == 0) {
					$("#expand-collapse").removeClass("collapse");
					$("#expand-collapse").html("Expand All");
				}
			}

			else {
				$(this).next(".section-content").slideDown("fast");
				$(this).removeClass("hover").parent(".last").removeClass("hover"); ;
				$(this).removeClass("closed").parent(".last").removeClass("closed");
				$(this).addClass("open").parent(".last").addClass("open");

				if ($(".expanding-list .open").length == $(".expanding-list h2").length) {
					$("#expand-collapse").html("Collapse");
					$("#expand-collapse").addClass("collapse");
				}
			}
		}
	);
	
	
	$("#expand-collapse").click(
	    function(){
	        if($(this).hasClass("collapse")){
	            $(this).removeClass("collapse");
	            $(".section-content").hide();
	            $(".expanding-list .item").removeClass("open");
	            $(this).html("Expand All");	        
	        }
	        else {
	            $(".section-content").show();
	            $(".expanding-list .item").addClass("open");
	            $(this).addClass("collapse");
	            $(this).html("Collapse");
	        }	        
	    }
	);	
}

/* Expanding list */
function InitExpandingGallery(){
	if($(".gallery li").length > 6){
		$(".gallery-wrapper").prepend("<a id=\"gallery-expand-collapse\" href=\"javascript:;;\">Expand All</a>");
		$(".gallery").css("padding-top", "10px");
		$("#gallery-expand-collapse").click(
			function(){
				if($(this).hasClass("collapse")){
					$(this).html("Expand All");
					$(this).removeClass("collapse");
					$(".gallery").removeClass("expand-gallery");
				}
				else{
					$(".gallery").addClass("expand-gallery");
					$(this).addClass("collapse");
					$(this).html("Collapse");
				}
			}
		);
	}
	else{
		$(".gallery").css("height", "auto");
	}
}

// ARRIVALS / DEPATURES ===============================================================
function InitArrivalsDepatures(){
	//$(".arrivals-depatures thead").removeClass("killIt");
	var htmlString = "<div class='faux-thead self-clear'>";
	var trackWidth = 0;
	$(".arrivals-depatures th").each(function(){
		htmlString += "<p style='width:"+$(this).width()+"px' " + (( $(this).hasClass("status") ) ? "class='status'":"") + ">" + $(this).text() + "</p>";
		//htmlString += "<p style='width:"+$(this).width()+"px' " + ">" + $(this).text() + "</p>";
		trackWidth += $(this).width();
	});
	
	htmlString += "</div>";
	$(".faux-thead").remove();
	$(".results").before(htmlString);
	//$(".faux-thead .status").css( {"width":"auto"} );
	$(".arrivals-depatures thead").addClass("killIt");
	
	$(".arrivals-depatures .loading").height( $(".arrivals-depatures .results").height() );

}
function HideFilter() {	$("#filter").hide(); }
function ShowFilter() {	$("#filter").show(); }

// ROUTE MAP ===============================================================
function InitRouteMap() {
	var so = new SWFObject("/Flash/RouteMaps.swf", "RouteMaps", "510", "460", "8", "#ffffff");
	so.addVariable("theData", BuildRouteXML());
	//so.addParam("wmode", "transparent");
	so.write("route-map-flash");
}

function BuildRouteXML() {
	var theXML = "<routes>";
	$(".route-map").each( function(){
		theXML += "<route title='"+$(this).children("h2").text()+"'>";
		$(this).children("table").children("tbody").children("tr").each( function(){
			theXML += "<location title='"+$(this).children("td:first").text()+"'>";
				$(this).children("td:last").children().each( function(){
					var url = $(this).attr("href");
					var img = $(this).children().attr("src");
					theXML += "<airline url='"+url+"' img='"+img+"'/>";
				});
			theXML += "</location>";
		});
		theXML += "</route>";
	});
	theXML += "</routes>";
	
	return theXML;
}


function SetSearchEffects(){
    if($(".search input[type=text]").val() == "")
        $(".search input[type=text]").addClass('google-brand');

	$(".search input[type=text]").focus(function(){
	    $(".search input[type=text]").removeClass('google-brand');
	});

	$(".search input[type=text]").blur(function(){
	    if(this.value == "")
	        $(this).addClass('google-brand');
	});
		
	// usage: add the class 'submit-on-enter' to any element
	$(".submit-on-enter input[type=text], .submit-on-enter textarea").keydown(function(ev){
        //$(".submit-on-enter").append(ev.keyCode);
        if(ev.keyCode == 13)
        {
            ev.preventDefault();
            ev.stopPropagation();
             //$(".submit-on-enter").append("==submitting");    
             $(this).parents(".submit-on-enter").children("input[type=image], input[type=button]").click();
             return false;
        }
    });
}

function SetFlightSearchEffects(){
    $(".arrivals-depatures input[type=text]").focus(function(){
		if($(this).val() == "Enter a Flight Number")
			$(this).val("");
	});
	
	$(".arrivals-depatures input[type=text]").blur(function(){
	    if($(this).val() == "")
	        $(this).val("Enter a Flight Number");
	});
}

