// ==== IMAGE ROTATOR ====
//global variables (yeah, I know) TIMERS -image rotator and reusable ====
var gv_img_rotate_speed = 5000;
var gv_image_rotate_timer = 0;
var gv_temp_timer;
var gv_timer_on = 0;
var gv_root_url = "http://jerseykitesurfing.co.uk/";

function image_rotator() {
	//variables - if image count changes be sure to update css #image_rotator width to be img_width*no_of_images px 
	var img_width = 900;
	var no_of_images = $("#image_rotator img").length;
	var portal_width = img_width*no_of_images;
	$("#image_rotator").css( {width: portal_width + "px"} );
	
	//calculations 
	var total_width = img_width * no_of_images;
	var max_right = total_width - img_width;
	//get current right amount - it will be a string eg. "900px"
	var current_right = $("#image_rotator").css("right");
	//strip off px at the end and convert to number
	current_right = current_right.substring(0,current_right.indexOf("px")) * 1;
	//work out if we should move right again or, if at the end, go to the start instead
	var next_right = current_right+img_width;
	if(next_right>max_right) next_right=0;
	//move to next position
	$("#image_rotator").animate({right: next_right+"px"},300);
	//run next one after an interval
    gv_image_rotate_timer = setTimeout('image_rotator()',gv_img_rotate_speed); 
}
function start_image_rotator() {
  //we are currently assuming that all images are 900px by 200px
  //add in other images to the DOM now rather than in html so that the inital load is faster and those without JS don't load them for no reason.
  $("#image_rotator").append("<img src='images/page_photos/photo_home2.jpg'>");
  $("#image_rotator").append("<img src='images/page_photos/photo_home4.jpg'>");
  $("#image_rotator").append("<img src='images/page_photos/photo_home3.jpg'>");

	//start rotator once first image is loaded
  $("#image_rotator img:first").load(function () {
  	$("#image_rotator").fadeIn(1300);
  	gv_image_rotate_timer = setTimeout('image_rotator()',1500); 
  });
}
function stop_image_rotator() {
  $("#image_rotator").html("").css( "right","0px" );
  clearTimeout(gv_image_rotate_timer);	
}
function stop_generic_timer() {
  if (gv_timer_on == 1) {
    clearTimeout(gv_temp_timer);
  }
}


// ==== AJAX PAGE LOADER ====
function load_page(this_page, callback) {
  //if there was no calling page given then this must be either the first hit or we have got back via history
  if ( this_page == null ) { 
	if( $("#menu a[nav=home]").hasClass("selected") ) {
	//if the home button is already selected then we must have got here from a link and the home page
	//will already be there. abort the function otherwise it would load twice
	  return 0; 
    } else {
	 //otherwise set to blank variable to load the home page
	 this_page = "home";
	}
  }

  //stop any image rotator and generic one
  stop_image_rotator();
  stop_generic_timer();

  //remove current nav highlight, then highlight this one
  $("#menu .selected").removeClass("selected");  
  //find menu link for this page and highlight
  $("#menu a[nav="+this_page+"]").addClass("selected").blur();
  //animate removing current page, load the next one then animate opening
  $("#load_area").fadeOut(300, function() {
    $("#ajax_throbber").show();  //ajax monitoring not working at the mo so doing it manually
    //load the page assuming that it ends with .php
    $("#load_area").load(this_page+".php", function() {
      $("#ajax_throbber").hide();
	   update_internal_links(); 
      //now animate showing the page
  	  $("#load_area").fadeIn(300);
	  //if there is a callback call it
	  if (callback) { eval(callback); }
    });
  }); 
}

// ==== TERMS AND CONDITIONS ====
function show_terms() {
  //get Terms and conditions via AJAX - css and rest of javascript is in loaded file
  $.get("terms_conditions.html", function(data){
    $("#main").append(data);  //add loaded data to the page
  });	
  return false;  //prevent original href firing
}

/* ====================== THE WISE MONKEY ======================== */
function get_wisemonkey() {
	$("#compact_wind").fadeOut(500,function() {
	  $.ajax({
	    url: "get_wisemonkey_json.php",
	    dataType: "json",
	    cache: false,
	    success: function(json){
			$("#wind_speed_knots").text(json.wind_speed_knots);
		    $("#wind_direction_compass").text(json.wind_direction_compass );
		    $("#wind_direction_compass_plus").text(json.wind_direction_compass_plus );
		 	var arrow="http://www.thewisemonkey.net/images/arrow" + json.wind_direction_degrees + "day.gif"
		    $("#compact_wind #arrow").attr("src", arrow);    
	  		$("#compact_wind").fadeIn(800);
		    //run it agin in a minute
		    setTimeout('get_wisemonkey()',60000);
	    }, //of success
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$("#ajax_throbber").hide();
			//alert("Eeek:"+textStatus+ ": error:" +errorThrown );
		}
	  }); //of ajax
	}); //of fadeout
}

function launch_wisemonkey() { 
  var winName = 'compact' ;
  var  destUrl="http://www.thewisemonkey.net/live.php";
  var windowprops = 'directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=yes';
  window.open(destUrl, winName, windowprops); 
}

// =======================================================================================================================
// ==== GENERAL AND ON PAGE LOAD ====
//prevent AJAX caching (GOLIVE:just for the mo while developing)
$.ajaxSetup({
  cache: false
});

//jQuery address change
function addressChangeHandler(evt) {
  var page = $.address.parameter("page");  
 //gallery and beaches pages are handled differently as there are subpages with other paramaters in the url
	
 //============  Gallery   ============
  if(page=="gallery") {
	var folder = $.address.parameter("folder");
	var gallery_page_already_open = $("#gallery_wrapper").length;
	  //if no folder specified then we have either come from a folder gallery page or we have landed from another page
	  //check this by looking to see if the main gallery page element exists or not
	if(gallery_page_already_open){
		if (folder) {
	      //if a folder is specified in the url then run the function in the gallery page to load it	
		  load_gallery_triggered(folder);
	    } else {
		  load_gallery_list();  //it is there so we will run the slide back animation
		}
	} else {
		if (folder) {
	      //if a folder is specified in the url then run the function in the gallery page to load it	
		  load_page(page, "load_gallery_triggered('"+folder+"');" );
	    } else {
          load_page(page);  //it does not exist so load the whole gallery page
		}
    }	
	
 //============= Beaches ============
  } else if(page=="beaches") {
	var site = $.address.parameter("site");
	var beach_page_already_open = $("#map_portal").length;
	if(beach_page_already_open){		
		if (site) {
			 zoom_in(site);
		} else {
	         zoom_out();  //site not specified so zoom out
		}
	} else {
		if (site) {
	      //if a site is specified in the url then run the function in the beaches page to zoom into it	
		  load_page(page, "zoom_in('"+site+"');" );
	    } else {
          load_page(page);  //it does not exist so load the whole beach page
		}
    }
	
  //============  Normal basic pages  ============	
  }	else {
    load_page(page);
  }
}

function update_internal_links() {
  //change internal links to trigger the address handler rather than jumping to the href
  $("a.internal").click(function(event){
     event.preventDefault();
	//well grab the page name before the .php. We ar assuming that there are no url parameters!
	var page = $(this).attr("href");
	page = page.substring(0,page.indexOf(".php"));
	$.address.value("?page="+page); //trigger address change
  });	
}

$(document).ready(function() {
	// Setup jQuery Address Listener
	$.address.change(addressChangeHandler);
		
	//menu nav - goto page by looking at the nav attribute
	$("#menu a").click(function(event) {
	    event.preventDefault();
		var page=$(this).attr('nav');
		$.address.value("?page="+page); //trigger address change
	});
	
	//ajax monitoring - throbber - #ajax_monitor
//	$("body").bind("ajaxSend", function(){
//		document.body.style.cursor = 'wait';
//	    $("#ajax_throbber").show();
//	}).bind("ajaxSuccess", function(){
//		document.body.style.cursor = 'default';
//	    $("#ajax_throbber").hide();
//	})
		

  //wise monkey. open chromeless. get after 3 secs.
  $("#compact_wind").click(function(event) {
    event.preventDefault();
    launch_wisemonkey();
  });
  setTimeout('get_wisemonkey()',3000); //delay of 3 secs so it doesn't impact any image rotator loads

});