/* General application function on document ready event - written by netfiesta.nl */
$(document).ready(function(){
  function fixScreen() {
   // Header and footer are always 200px height
   min_content_height = $(window).height()-212;
   // Fill content area to bottom of browser viewport
   if( ($("#content").height()+212) < $(document).height() ) {
    $("#content").css("min-height",min_content_height+"px");
   }
   // When content area is less then the height of the submenu+100 so that the submenu will always flow over the content area.
   if( $("#submenu").height()+100 > $("#content").height() ) {
    $("#content").css("height",$("#submenu").height()+100+"px")
   }
   if( $(document).height()-6 > $(window).height() && $("#page_container").height() < $(document).height() ) {
    //$("#page_container").css("padding-bottom","127px"); oude code
    $("#page_container").css("height",$(document).height())
   }
  }
  $("#submenu").dropShadow({left: -2, top: 0, opacity: 0.45, blur: 4});
  $("div.block:not(.active)").hover( function(){$(this).css("background","#c5be91")} ,  function(){$(this).css("background","none");} )
  $("#submenu:not(.accommodation) div.block:not(.normallinks)").bind("click", function() {return click_submenu($(this))} );
  function click_submenu(elem) {
    window.location.href="http://"+window.location.hostname+"/"+elem.find("a").attr("href");
    return false;
  }
  //Disable Firefox imagedrag in polaroids area
  $("#polaroids").bind("mousedown", function(event){if(event.preventDefault) {event.preventDefault()}});

  window.onresize=fixScreen;
  fixScreen();
  if ($("#polaroids").length == 1) { createPolaroids(); }

  //Lightbox configuration
  $(function(){ $.Lightbox.construct({ show_linkback: false, show_info: true, show_extended_info: true, download_link: false, auto_scroll: "disabled", text: {close: 'Close', about: {text: 'Jarno', link: 'test'} } }); });
});

function fadeHeader(header_name) {
 $(document).ready(function(){
  if(header_name=="") {header_name="breakfast"}
  $("#header_fadein").css("background","url('/images/header_"+header_name+".jpg')");
  $.preload("header_"+header_name, {base:'/images/',ext:'.jpg', onFinish:function(){ setTimeout('$("#header_fadein").fadeIn(2500);',1700) } });
 });
}
function createPolaroids() {
  show_pol = Math.ceil(($(document).height()-400)/100);
  if(show_pol>18) {show_pol=18} else if(show_pol<5) {show_pol=5}
  pol_id = create_unique_random_array(show_pol,1,24);
  for(i=0;i<=pol_id.length-1;i++) { pol_id[i] = "sepia_photo"+pol_id[i] }
  $.preload(pol_id, {base:'/images/',ext:'.png', onFinish:showPolaroids });
}
function showPolaroids(data) {
 for(i=0;i<=pol_id.length-1;i++) {
   //if(pol_id[i]=="sepia_photo15") {
   //  new_map = $("<map>").attr({id:pol_id[i],name:pol_id[i]});
   //  $("body").prepend(new_map)
   //  new_area = $("<area />").attr({shape:"poly", coords:"0,65,130,0,198,136,67,199"});
   //  new_area.bind("mouseover", function() { $("#polaroids img").css("z-index",$(this).css("z-index")-1); $("#sepia_photo15 img").css("z-index","92"); alert( $("#sepia_photo15").css("z-index") ); } );
   //  new_map.append(new_area);
   //}
   new_img = $("<img />").attr({id:pol_id[i],src:"/images/"+pol_id[i]+".png"}) //Add: ,usemap:"#"+pol_id[i]
   $("#polaroids").append(new_img);
   if(i==2) {top_pos = 1;} else if(i==4) {top_pos = $(document).height()-300-new_img.height();} else {top_pos = create_unique_random_array(1,0, ($(document).height()-300-new_img.height()) );}
   left_pos = create_unique_random_array(1,5, (225-(new_img.width())) );
   //index = create_unique_random_array(1,1,90); //Enable this setting to set random z-index for each image
   new_img.css({"left":left_pos+"px","top":top_pos+"px"});
   new_img.bind("mouseover", function() { moveForward($(this)); } );
   //alert("id: "+i+"; "+pol_id[i]+".png; width: "+new_img.width()+"; height: "+new_img.height()+"; left-pos: "+left_pos+"; top-pos: "+top_pos); //Debug information
 }
}
function moveForward(elem) {
 if(isNaN(elem.css("z-index"))==false) { $("#polaroids img").css("z-index", elem.css("z-index")-1 ); }
 elem.css("z-index","91");
}
function CreateRandomNumber(min,max) {
 return (Math.round((max-min) * Math.random() + min));
}
function create_unique_random_array(num_elements,min,max) {
 var temp, nums = new Array;
 for (var element=0; element<num_elements; element++) {
   while((temp=NumberFound(CreateRandomNumber(min,max),nums))==-1);
   nums[element] = temp;
 }
 return (nums);
}
function NumberFound(CreateRandomNumber,number_array) {
 for (var element=0; element<number_array.length; element++) {
  if (CreateRandomNumber==number_array[element]) {
    return (-1);
  }
 }
 return (CreateRandomNumber);
}