var cal_month = 0;
var cal_year = 0;
var avail_item_id = 0;
var form_name = "";
var form_mode = "check";

function load_avail_checker( mode ){
  form_mode = mode;
  $("#availability_holder").load( "cal.php" , { "mode" : mode , "year" : cal_year , "month" : cal_month, "item_id" : avail_item_id } );
}

function bind_calender_clicks(){

  $(".jq_avail_close_button").bind("click" , function(){
	$("#availability_holder").hide();													   
  });

  $(".jq_day_select").bind("click" , function(){
	$("#availability_holder").hide();
	//
	$date_uk = $(this).html() + " / " + cal_month + " / " + cal_year;
    $( "#drprop_" + avail_item_id ).attr( "value" , $date_uk );
    $( ".drprop_" + avail_item_id ).attr( "value" , $date_uk );

	$("#" + form_name ).submit();
  });

  $(".jq_month_left").bind("click" , function(){
	cal_month = cal_month - 1;
	if( cal_month == 0  ){
		cal_month = 12;
		cal_year = cal_year - 1;
	}
	load_avail_checker( form_mode );
  });

  $(".jq_month_right").bind("click" , function(){
	cal_month = cal_month + 1;
	if( cal_month == 13  ){
		cal_month = 1;
		cal_year = cal_year + 1;
	}
	load_avail_checker( form_mode );
  });

}

var main_images = new Array();
var super_images = new Array();

function animate_image(){

  if( main_images.length != 0 ){

	  current_image = $(".main_image").attr("src");
	
	  // get next image
	  
	  for (var i=0; i<main_images.length; i++) {
		if( current_image == main_images[i] ){
			next_image_index = i + 1;
			
			if( next_image_index == main_images.length ){
				next_image_index = 0;
			}
			break;
		}
	  }
	  
	  if( super_images[next_image_index] == "" ){
		$('.main_image_anchor').css("display","none");
	  } else {
		$('.main_image_anchor').css("display","block");
        img_super = super_images[next_image_index];
		$('.main_image_anchor').attr( "href" , img_super );
	  };
	
	 $(".main_image").fadeOut("fast", function(){
		$(".main_image").attr("src",main_images[next_image_index]);
		$(".main_image").fadeIn("fast");
	  });
	
	  setTimeout('animate_image()', 4000);

  }

}

$(document).ready(function() {

  $(".fancybox").fancybox(); 

  // 4 sced ticker
  //setTimeout('animate_image()', 2000);

  //init / get thumbs / get super
  index = 0;
  $('.jq_prod_details_thumb').each( function(){
	img_tiny = $(this).attr("src");
	img_big = img_tiny.replace("tiny","big");
	main_images[index] = img_big;
	index++;
  });

  index = 0;
  $('.jq_prod_details_thumb').each( function(){
	super_images[index] = $(this).attr("super");
	index++;
  });

  // === Thumb Hover ( Product Details page )
  $(".jq_prod_details_thumb").hover(
    function () {
      img_tiny = $(this).attr('src');
	  img_big = img_tiny.replace("tiny","big");
      $('.main_image').attr( "src" , img_big );
	  img_super = $(this).attr( "super" );
      $('.main_image_anchor').attr( "href" , img_super );
	  if( img_super == "" ){
	    $('.main_image_anchor').css("display","none");
	  } else {
	    $('.main_image_anchor').css("display","block");
	  };

    },
    function () {
    }
  );

//  $(".jq_add_booking").bind("click", function(){
	//return confirmBuy(document.form_1, 'cart');
//  });
  
  $(".jq_check_availability").bind("click" , function(){
	avail_item_id = $(this).attr("item_id");
	var d = new Date();
	cal_year = d.getYear();
	if( cal_year < 1900 ){
	  cal_year = cal_year + 1900;
	}
	
	cal_month = d.getMonth() + 1;
	pos = $(this).offset();
	
	x_offset = -200;
	scrolltop = $(window).scrollTop();
	
	if( (pos.top - scrolltop ) > 240 ){
		y_offset = -150; // display above
	} else {
		y_offset = -16; // display below
	}
	$("#availability_holder").css( { "left": ( pos.left + x_offset ) + "px", "top":( pos.top + y_offset ) + "px" } );
	$("#availability_holder").show();
	load_avail_checker( "check" );
	form_name = $(this).attr("form_name");	
  });

  $(".jq_add_booking").bind("click" , function(){	
	avail_item_id = $(this).attr("item_id");
	var d = new Date();
	cal_year = d.getYear();
	if( cal_year < 1900 ){
	  cal_year = cal_year + 1900;
	}
	cal_month = d.getMonth() + 1;
	pos = $(this).offset();
	
	x_offset = -200;
	scrolltop = $(window).scrollTop();
	
	if( (pos.top - scrolltop ) > 240 ){
		y_offset = -150; // display above
	} else {
		y_offset = -16; // display below
	}
	$("#availability_holder").css( { "left": ( pos.left + x_offset ) + "px", "top":( pos.top + y_offset ) + "px" } );
	$("#availability_holder").fadeIn("fast");
	load_avail_checker( "book" );
	form_name = $(this).attr("form_name");
	
  });
  
  $('.jq_post_code_button').bind("click" , function(){

	var post_code = $(".post_code_field_1").attr("value") + " " + $(".post_code_field_2").attr("value");

	$('#post_code_results').hide();

	//post_code = post_code.split(' ').join('');

    // validate post code
	
	if( post_code ){
		  		
		  //valid ?
		  if( post_code.length < 5 ){
			  alert("The post code appears too short");
		  } else if ( post_code.length > 8 ){
			  alert("The post code appears too long");			  
		  } else {
			$('#post_code_results').fadeIn("slow");
		  	$('#resultstext').load("distance_check.php" , { "postcode" : post_code } );
		  }
	}

  });
  
  
});

