var tmrWaitLoad;
var tmrDelayNextImage;
var img_width;
var img_height;
var img_index = -1;
var images;
var unique_index = 1;
var last_unique_index;
var slideShow = true;
var resizeIndex = 1;

$(document).ready(function(){

	unique_index = 1;
	set_wrapper_size();
	set_text_overlay_positions();

  load_next_image();

});

function set_wrapper_size(){
	updateWidthHeight();
  $("#wrapper").css("width"  , viewWid + "px" );
  $("#wrapper").css("height" , viewHei + "px" );
}

function set_text_overlay_positions(){
	updateWidthHeight();
  $(".text_overlay").css("top", (viewHei - 280) + "px");
	$("#footer").css("top", (viewHei - 40) + "px");
	$("#contact_details	").css("top", (viewHei - 290) + "px");
}

function on_resize(){
  set_wrapper_size();
  set_text_overlay_positions();
  set_main_image_size();
}

function resize_image(this_index){
	img_width = $(".main_" + this_index).width();
	img_height = $(".main_" + this_index).height();

	ratio_width = (viewWid / img_width);
	ratio_height = (viewHei / img_height);

	if( ratio_height > ratio_width ){
		$(".main_" + this_index).height(img_height * ratio_height);
		$(".main_" + this_index).width(img_width * ratio_height);
		left_offset = parseInt(((viewWid - (img_width * ratio_height)) / 2));
		$(".main_" + this_index).css("left" , left_offset + "px");
		$(".main_" + this_index).css("top" , "0px");
	} else {
		$(".main_" + this_index).width(img_width * ratio_width);
		$(".main_" + this_index).height(img_height * ratio_width);
		top_offset = parseInt(((viewHei - (img_height * ratio_width)) / 2));
		$(".main_" + this_index).css("left" , "0px");
		$(".main_" + this_index).css("top" , top_offset + "px");
	}	
}

function set_main_image_size(){
	updateWidthHeight();
	resize_image(unique_index);
	resize_image(last_unique_index);
}

function new_image_loaded(){

  img_width = $(".loading_image").width();
  img_height = $(".loading_image").height();

  loaded_img_src  = $(".loading_image").attr("src");

  $('#place_image_before_this').before("<img class='main_image main_" + unique_index + "' style='display: none;' src='" + loaded_img_src + "' />");

  set_main_image_size();

  // trigger fade image in
  $(".main_" + unique_index).fadeIn( 1000 , function(){
    $(".main_" + last_unique_index ).remove();
    tmrWaitLoad = setTimeout("load_next_image()",3000);
  });
  
  // set link text
  
  str = picsArray[img_index]['text'];
  
  $('.text_overlay').html( str.toUpperCase() + "<br>GALLERY" );
  $('.text_overlay').attr("href" , picsArray[img_index]['href'] );

  // colour scheme?
  if( picsArray[img_index]['home_colour_scheme'] == 0 ){
    logo_src = "../zimages/logo/logo_00.png";
    text_overlay_colour = "#FFF";
    schemeClass = "scheme_00";
    footer_text_colour = "#FFF";
  } else {
    logo_src = "../zimages/logo/logo_01.png";
    text_overlay_colour = "#000";
    schemeClass = "scheme_01";
    footer_text_colour = "#000";
  }

  $("#footer").css("color" , footer_text_colour );
  $("#main_nav ul").removeClass('scheme_00');
  $("#main_nav ul").removeClass('scheme_01');
  $("#main_nav ul").addClass( schemeClass );
  $(".text_overlay").css("color" , text_overlay_colour );
  $(".logo").css("background-image" , logo_src );

  //tmrWaitLoad = setTimeout("wait_fade_in()",2000);

}

function load_next_image(){

	last_unique_index = unique_index;

  img_index++;
  if(img_index == images) img_index = 0;  

  unique_index++;

  // load the next image
  $("#loading_image").html("<img class='loading_image' src='" + picsArray[img_index]['image'] + "' onload='new_image_loaded()' />"); 
	
}

