/**
 * The system wide onload function 
 */
$(function() {
  
  /** Search on enter */	
  $("#searchbox").focus(function(){
	if($("#searchbox").val() == "Search tours") {
	  $("#searchbox").val("");
	}
  });
  
  
  $("#myemail").focus(function(){
		if($("#myemail").val() == "Enter a valid email address") {
		  $("#myemail").val("");
		}
  });
  
  $("#searchbox").keyup(function(event){
	if(event.keyCode == '13'){
		$("#cse-search-box").submit();
//		document.location = 'http://www.google.com.au/#hl=en&q=site%3Awww.fiftydegreesnorth.com/tours+'+$("#searchbox").val();
	}  
  });
	
  /** Sliding function for the navigation */
  $(".nav-item").click(function (e) {
	  
    $(".nav-item").removeClass('nav-item-active');

	if(open_panel != '' && open_panel != $(this).attr('panel')) {
	  /** Closing an already open panel and opening up a new one */
	  //$("#extension").hide();
	  $("#panel_" + open_panel).toggle("slide", { direction: "up" }, 100, registerStatus(this));
	  $("#panel_" + open_panel).wait();
	  $(this).addClass('nav-item-active');
	} else if (open_panel == $(this).attr('panel')) {
	  /** Closing the open tab */	
	} else {
	  /** Opening a new tab */
	  $(this).addClass('nav-item-active');
	}
	
    $("#panel_" + $(this).attr('panel')).toggle("slide", { direction: "up" }, 400, registerStatus(this));
    //$("#extension").toggle("slide",{direction: "down"},400,extendHighlight(this));
    
  });

  /** A wait function to pause between animations */
  $.fn.wait = function(time, type) {
	    time = time || 300;
	    type = type || "fx";
	    return this.queue(type, function() {
	        var self = this;
	        setTimeout(function() {
	            $(self).dequeue();
	        }, time);
	    });
	};
  
  
});


function extendHighlight(e){
	  /** Extend the highlight */
	  $("#extension").css({
	    left: 0, /** Resetting the position */
	    width: function() { return $(e).outerWidth(); }
	  }); 
	  
	  /** Position the extension */
	  $("#extension").position({
	    my: "top left",
	    at: "top left",
	    of: e,
	    offset: "0 -93",
	    collision: "fit"
	  });
	
}

var open_panel = '';
function registerStatus(e){
	open_panel = '';
	if($("#panel_" + $(e).attr('panel')).css('display') == 'none') {
	  open_panel = $(e).attr('panel'); /** Register the open panel */
	} 
}

function addOverlay(url){
	
	/* Pop up the overlay window */
	$('body').prepend('<div id="overlay" class="ui-widget-overlay"></div>');
	/* Set the overlay to cover the whole of the webapge, not just the visible screen */
	$('#overlay').css('height',$(document).height());
	
	/* Add a layer for the content */
	$('body').prepend('<div id="overlay_wrapper" class="ui-corner-all"></div>');
	$('#overlay_wrapper').append('<div id="close" class="clickable" onclick="closeOverlay()">Close Overlay [X]</div>');
	$('#overlay_wrapper').append('<div id="overlay_content"></div>');
	
	/* Position the content layer and resize it */
	$('#overlay_wrapper').css({
		'top'	: (( $(window).height() / 4 ) - 60) + "px",
		'left'	: (( $(window).width() / 4 ) - 60) + "px",
		'min-height' : $(window).height() / 2,
		'width' : $(window).width() / 2
	});
	
	$.get(url,function(data){ $("#overlay_content").html(data); });
	
}

function closeOverlay(){
  $("#overlay_wrapper").remove();
  $("#overlay").remove();
}

function subscribe(){
  $.get('/newsletters/add/'+$("#myemail").val(),function(data){ $("#subscribe_container").html(data); });	
}

function simpleSearch(country,tour){
	if(tour!=0)
		window.location.href = '/tours/' + tour;
	else if(country!=0)
		window.location.href = '/places/' + country;
	
}

function setToursSelectForm(country_id){
	$.get('/tours/get_tour_list_by_country/'+country_id,function(data){ $("#tours_select_form").html(data); });
	
}

function setLocationSelectForm(place_id,type){
	
	if(type == "region") {
		$.get('/places/get_country_list_by_region/'+place_id,function(data){ $("#country_list").html(data); });
		$.get('/places/get_place_list_by_region/'+place_id,function(data){ $("#place_list").html(data); });
	} else if(type == "country"){
		$.get('/places/get_place_list_by_country/'+place_id,function(data){ $("#place_list").html(data); });
	}
	
}

function setPrintLayout(){
	$('#print_layout').attr('value','true');
	$('#searchform').submit();
}


function sortSearchResultsBy(type){
	$('#searchform').submit();
	
}












