// General javascript Routines
//page global variable
// Version 2 
var usesize, defsize = "medium";
var c_imgsize = "imgsize";
var now = new Date(); 
var monthnumber = now.getMonth();
var timerform
var speed=1000

// jquery functions
function nopanel() {
    $("#centralblock").css('margin-right','30px');
    $("#hidepanel").hide();    
	$("#right").attr("class","nopanel");
	$("#right").css('width','30px');
	$("#backdropR").css('background','url("./images/rightsmall.gif") top right repeat-y');
	$("#buttonpanel").css('display','none');  
    return false;
};	
function hidepanel() {
    $("#centralblock").css('margin-right','30px');
    $("#hidepanel").hide();
	$("#right").attr("class","closed");
	$("#right").css('width','30px');
	$("#backdropR").css('background','url("./images/rightsmall.gif") top right repeat-y');
    $("#buttonpanel img").attr('src','/images/panel1.gif');	
	$("#buttonpanel img").attr('alt','Open Side Panel');	
	$("#buttonpanel img").attr('title','Open Side Panel');
	$("#buttonpanel").css('display','block');  
    return false;
};

function showpanel() {
   	$("#centralblock").css('margin-right','190px');
    $("#hidepanel").show();
	$("#right").attr("class","open");
	$("#right").css('width','180px');
	$("#backdropR").css('background','url("./images/right.gif") top right repeat-y');
  	$("#buttonpanel img").attr('src','/images/panel2.gif');
	$("#buttonpanel img").attr('alt','Close Side Panel');	
	$("#buttonpanel img").attr('title','Close Side Panel');
	$("#buttonpanel").css('display','block');  
   	return false;
}



$(document).ready(function(){
    pageloaded = false;   
    initialiseimgcookie();
	$("#imgsizeformtoggle").css('display','block');
	$("#imgsizeformtoggle").toggle(
	  function(){
	    //show showbutton
	    $("#imgdim").hide();
	    },
	  function () {
	    //show hidebutton
	    $("#imgdim").show(); 
   	  } );
   
//   $("#image img").attr("class", "scaleable"); //change gallery image set class = scaleable      
   $("img.scaleable").click( function() {
      bigclick($(this).attr('id'));
   	  return false; 
    } );
    
    $("#emale").click(function () {
	  emaltxt=('mail' + 'to:' + 'info@' + 'schooner-landing.com');
	  win = window.open(emaltxt,'emailWindow');
	  if (win && win.open &&!win.closed) win.close();
	} );
	
	$(".pics").css('height','323px');
	$(".hidden").css('display','block');
	$('#slides').cycle({ 
 	  fx:      'fade', 
  	  speed:    800, 
  	  timeout:  4000,
//  	  random: 1,
  	  next: '#slides',
  	  pause: 1 
	});
	$('#slides1').cycle({ 
 	  fx:      'fade', 
  	  speed:    800, 
  	  timeout:  4000,
  	  random: 1,
  //	  next: '#slides',
  	  pause: 1 
	});
	$('#slides2').cycle({ 
 	  fx:      'fade', 
  	  speed:    800, 
  	  timeout:  4000,
  	  random: 1,
  //	  next: '#slides',
  	  pause: 1 
	});
   //on panelbutton press toggle panel off/on and set session cookie
   $("img.panelbutton").toggle(
     function() { 
       hidepanel();
       panelcookie(document.title,"hide");
     },
     function() {
       showpanel();
       panelcookie(document.title,"show");
     });
     
   $("a[@rel='external']").attr('target','_blank'); //make all external docs open in new page (strict hack)

  //read cookie for this page and then in the next function see if cookie value is same as class value
  //if not change class value to cookie value and show or hide
   c_val = panelcookie(document.title,"undefined");
   if (c_val == "hide") $("#right").attr("class","closed");
   else if (c_val == "show") $("#right").attr("class","open");
   
   $("#right.closed").each(function() {hidepanel()});
   $("#right.open").each(function() {showpanel()});
   $("#right.nopanel").each(function() {nopanel()});
   
   // show ski report if month  is jan (0),feb,mar,apr,nov or dec
   if ((monthnumber < 2) || (monthnumber > 10)) {
      $("#skiFrame").show();
   }  
   pageloaded = true;
 }); //ready 


// original 'other' functions

//cookie handling
function createcookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else {
    var expires = "";
  }
  document.cookie = name+"="+value+expires+"; path=/";
}

function readcookie(name){
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return "undefined";
}

function erasecookie(name){
  createCookie(name, "", -1);
}

function initialiseimgcookie() {    
   var size= readcookie(c_imgsize);
   if ((size != "large") && (size != "medium") && (size != "small")){	
      createcookie(c_imgsize,defsize,1);
   	  /*retest to see if cookie created*/
   	  size = readcookie(c_imgsize);  
   	  if ((size != "large") && (size != "medium") && (size != "small")) {  
        usesize = defsize; /*cookies not set*/
	    return; 
      }      	
   }
   usesize = size;	
   /* if main page with imgsize form then set radio buttons */
   if (document.forms.imgdim) {
     setCheckedValue(document.forms.imgdim.imgsize, usesize);};
}

function panelcookie(titlestr,state) {  
   //get title 
   if (titlestr.indexOf("Gallery") == 0)
     titlestr = "GalleryPstate"; 
   else
     titlestr = titlestr + "Pstate";
   var pstate = readcookie(titlestr);
   if (state == "undefined") return(pstate);
   if (pstate != state) {	
      createcookie(titlestr,state,1);
	  return; 
   }      	
}  

//radio box handling
/* function setCheckedValue(radioObj, newValue) {
   var undefined;
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function radioClick (e) {
    if (pageloaded == false) return;
    if (! e)
        e = event;
    obj = e.target || e.srcElement;
    usesize = obj.value;
    createcookie (c_imgsize,usesize,1);
} */



//image display in popup
function bigclick(imagename) {
  var horiz = 1040; 			//large
  var vert = 768;
  /* if (usesize == "small") {
    horiz = 640; 
    vert = 480;}
  else if (usesize == "large") {
    horiz = 1040; 
    vert = 768;} */
     
  if (pageloaded == false) return;
  var imgn = "./images/" + "large" + "/" + imagename + ".jpg";
  if(document.images) {
    win = window.open(imgn,'NextWin','scrollbars=yes,width=' + horiz + ',height=' + vert + ',resizable=yes');
    //if (win && win.open &&!win.closed) win.close();
  }     
}

// timer for webcams
function dotimer(){

    today=new Date()
    slutsec=today.getSeconds()
    slutmin=today.getMinutes()
    sluttim=today.getHours()
    sluta=(slutsec) + 60 * (slutmin) + 3600 * (sluttim)
    diff=sluta - starta
    tim=Math.floor(diff / 3600)
    min=Math.floor((diff / 3600 - tim) * 60)
    sek=Math.round((((diff / 3600 - tim) * 60) - min) * 60)
    document.timerform.timer.value=tim + ':'
    if(min<10)document.timerform.timer.value+='0'
    document.timerform.timer.value+=min + ':'
    if(sek<10)document.timerform.timer.value+='0'
    document.timerform.timer.value+=sek
    window.setTimeout("dotimer()",speed)
}

function Timer(){

    today=new Date()
    startsek=today.getSeconds()
    startmin=today.getMinutes()
    starttim=today.getHours()
    starta=(startsek) + 60 * (startmin) + 3600 * (starttim)
    document.write('<form name=timerform><input name=timer size=7')
    document.write('></form>')
    dotimer()

}
/*object workaround for IE etc*/

function createObject(objectDefinition)
{
    document.write(objectDefinition);
}



/* Webcam scripts not used*/

function ImageRefresh() {
	document.CameraImage.src = "http://192.168.1.253:50000/SnapshotJPEG?Resolution=320x240&Quality=Standard&Count=" + Count;
	Count++;
}

/* Deprecated - done in jquery
function show( id ) {
  if (document.getElementById)
    document.getElementById(id).style.display = 'block';
  else if (document.all) 
    document.all.id.style.display = 'block';      
}

function hide( id ) {
  if (document.getElementById)
    document.getElementById(id).style.display = 'none';
  else if (document.all) 
    document.all.id.style.display = 'none';   
}

 function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
} 

function testcookieval (size) {
	if ((size == "large")||(size == "medium")||(size == "small")) {
   	return true;
	}
   return false;  
}

   id = ($("body").attr("id"));
   if ((id == "contact") || (id == "gallery")){hidepanel()};
*/
