$(document).ready(function(){
	
	if($.browser.msie && $.browser.version == 6) {
		$(".search-button").pngfix();
		$("a[href $='.pdf']").addClass("application-pdf");
		$('a[href ^="mailto:"]').addClass('application-mail');
	} else {
		//iPhone/iPod Touch detection
		if ( isAppleMobile() ) { 
			var o = document.getElementById('iphone-ipod-notify');
			o.innerHTML = "<h1 style='text-align:center;border: 1px solid #a23e14; -webkit-border-radius: 10px;'> <a href='/iphone'> Tap here to go to our <br /> iPhone/iPod touch web site.</a></h1>";
		}
	}

	if($('#home_spy').length!=0) {
		//Setup the main rotater on the home page
		$('#home_spy').cycle({
			speed:       4000,
			timeout:     4000,
			pager:      '#home_spy_nav',
			pagerEvent: 'click',
			fastOnEvent: true
		});
	}
	
	$('#arrivals-and-departures-switch li a').click(function() { 
		if($(this).attr('href') == '#arrivals-show') {
			$('#arrivals-and-departures-switch').removeClass();
			$('#arrivals-and-departures-switch').addClass('arrivals');
			$('ol#departures').hide();
			$('ol#quickbook').hide();
			$('ol#arrivals').show();
		//	$('form#flightSearch').show();
		//	$('#flightSearch').attr('action','index.html?action=arrivals');
		} else if($(this).attr('href') == '#departures-show') {
			$('#arrivals-and-departures-switch').removeClass();
			$('#arrivals-and-departures-switch').addClass('departures');
			$('ol#departures').show();
			$('ol#arrivals').hide();
			$('ol#quickbook').hide();
		//	$('form#flightSearch').show();
		//	$('form #flightSearch').attr('action','?action=departures');
		} else if($(this).attr('href') == '#booking-show') {
			$('#arrivals-and-departures-switch').removeClass();
			$('#arrivals-and-departures-switch').addClass('quickbook');
			$('ol#departures').hide();
			$('ol#arrivals').hide();
			$('ol#quickbook').show();
			//$('form#flightSearch').hide();
		}
		//return false;
	});

	
	/* mainmenu navigation */	
	var level2 = window.location.pathname.split("/");
	$('#mainmenu a').each(function(){
		for (n in level2) {
			if(this.className == level2[n]) {
				$(this).addClass('active');
			}
			if(level2[n] == 'flight-information'){
				$('ol#quickbook').hide();
				$('ol#departures').hide();
				$('ol#arrivals').show();
			}
		}
	});
	
	/* secondary navigation */
	$('.secondary-navigation a, .featured a').each(function() {
		var current_href = $(this).attr("href").toString().replace(/%2D/g,"-").replace(/%5F/g,"_").replace(/#/g,"");
		var window_location = window.location.toString().replace(/%2D/g,"-").replace(/%5F/g,"_").replace(/#/g,"");
	
		// 1st condition: current URL is identical to link in second/featured menu
		// 2nd condition: current URL is identical to link in second/featured menu plus trailing forward slash
		// 3rd condition: current URL without the querystring is identical to link in second/featured menu
		// 4th condition: current URL without the querystring is identical to link in second/featured menu plus trailing forward slash
		if ((window_location == current_href) || (window_location == (current_href+"/")) || (window_location.split("?")[0] == current_href) || (window_location.split("?")[0] == (current_href+"/"))){
			$(this).addClass("active");			
		} 

		
	});
	
	$.get('/_inc/_temp/weather.xml',{},function(xml){
		var temp_text = $(xml).find('temp_f').text();
		var weather_icon = $(xml).find('icon_url_name').text();
		var last_updated = $(xml).find('observation_time').text();
		var last_updated_array = new Array();
		var last_updated_array = last_updated.split(', ');
		var last_updated = last_updated_array[1];
		var weather = $(xml).find('weather').text();
		$('<li class="temp_f"></li>')
			.html(temp_text + '° F ')
			.appendTo('#NOAAcurrent ul');
		$('<li class="weather"></li>')
			.html(weather)
			.appendTo('#NOAAcurrent ul');
		$('<li class="timestamp"></li>')
			.html(last_updated)
			.appendTo('#NOAAcurrent ul');
		$('<img src="/_img/_weather/'+weather_icon+'" alt="'+weather+' image" />')
			.appendTo('#NOAAcurrent div.imgholder');
	});
	
	$(".trafficReport table tr td").parents("tr").find("td:first-child").css("width", "180px");
	
	var i = document.getElementById('quickbook-hook');
	if (i) {
			startBooking();
	}
	
/* news archive */

	if($.isFunction($.fn.accordion)) { 
		$("#year-accordion").accordion();
		$("#accordion").accordion();
		$("#article-year-list li a").each(function() { 
			var year = getParameterByName("year")
			if (this.text == year) {
				$(this).addClass("active");
			}
		});
	}

	if($.isFunction($.fn.tweet)) {
		$(".tweet").tweet({
			username: "flypensacola",
			join_text: "auto",
			avatar_size: 32,
			count: 3,
			auto_join_text_default: "we said,", 
			auto_join_text_ed: "we",
			auto_join_text_ing: "we were",
			auto_join_text_reply: "we replied to",
			auto_join_text_url: "we were checking out"
		});
	}
	setTimeout("checkTweets()", 5000);
	
		getFlights(true);
	//tab control
	$('ul.qbt-tabs li > a').click(function() {
		$('li').removeClass();
		$(this).parent().addClass('active');
	});
});		

function checkTweets() {
	if($(".tweet_list li").length==0) {
		$(".tweet_list").append("<li>No recent tweets!</li>");
		
	} 
	$(".tweet_list").append("<li><a  href='http://www.twitter.com/#!/flypensacola'>See all tweets</a></li>");
}

function isAppleMobile() {
	return ((navigator.platform.indexOf("iPhone") != -1) || (navigator.userAgent.indexOf('iPod') != -1));
}

function surveyPopup() {
	window.open( "https://www.surveymonkey.com/s.aspx?sm=_2fz25gK2aM0_2fWCA3lI3d6LQ_3d_3d", "myWindow", "status = 1, height = 700, width = 520, resizable = 1, scrollbars = 1", scrollbars=1 );
} 

function custPopup() {
	window.open( "https://www.surveymonkey.com/s/3G9VF35", "myWindow", "status = 1, height = 700, width = 520, resizable = 1, scrollbars = 1", scrollbars=1 );
} 

function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

