

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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 expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet("standard");


sfHover = function() {
	var sfEls = document.getElementById("navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


function fsize(size,unit,id){
  var vfontsize = document.getElementById(id);
  if(vfontsize){
   vfontsize.style.fontSize = size + unit;
  }
}
//ext link
$(document).ready(function() {
	
	//stripe table 
	$(".stripe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
    $(".stripe tr:even").addClass("even");
	
	$('#slideshow').cycle({  
    		timeout: 4000 
	});
	
	/*$('#fav').jFav();*/
		
        $('a').filter(function() {
         return this.hostname && this.hostname !== location.hostname;
        }).addClass('externalLink');
		
		// addSizes was written by Natalie Downe 
	// http://natbat.net/2008/Aug/27/addSizes/
	
	// Copyright (c) 2008, Natalie Downe under the BSD license
	// http://www.opensource.org/licenses/bsd-license.php
	
	$('a[href$=".pdf"], a[href$=".doc"], a[href$=".zip"]').each(function(){
		var link = $(this);
		var bits = this.href.split('.');
		var type = bits[bits.length -1];
		
		var url= "http://json-head.appspot.com/?url="+encodeURIComponent (this.href)+"&callback=?";
		//console.log(url);
	
		// then call the json thing and insert the size back into the link text
		 $.getJSON(url, function(json){
			if(json.ok && json.headers['Content-Length']) {
				var length = parseInt(json.headers['Content-Length'], 10);
				
				// divide the length into its largest unit
				var units = [
					[1024 * 1024 * 1024, 'GB'],
					[1024 * 1024, 'MB'],
					[1024, 'KB'],
					[1, 'bytes']
				];
				
				for(var i = 0; i < units.length; i++){
					
					var unitSize = units[i][0];
					var unitText = units[i][1];
					
					if (length >= unitSize) {
						length = length / unitSize;
						// 1 decimal place
						length = Math.ceil(length * 10) / 10;
						var lengthUnits = unitText;
						break;
					}
				}
				
				// insert the text directly after the link and add a class to the link
				// note: if you want to insert the size into the link rather than after it change the following 'after' to 'append'
				link.after(' <span class="quiet small">(' + type + ' ' + length + ' ' + lengthUnits + ')</span>');
				link.addClass(type);
			}
		});
	});
	
	//setup cookies
	var COOKIE_NAME = 'show1';
    var ADDITIONAL_COOKIE_NAME = 'show2';
    var options = { path: '/', expires: 1 };
	$.cookie(COOKIE_NAME, 'false', options);
	$.cookie(ADDITIONAL_COOKIE_NAME, 'false', options);
    
	//Hide divs
	$("#showIt1").css("display","none");
	$("#showIt2").css("display","none");
	
	//check cookie status and set initial visibility of divs
	if ($("#contact2").is(":checked")&&($.cookie(COOKIE_NAME))){
		$("#showIt1").show("fast");
	} else {     
		$("#showIt1").hide("fast");
	}
	if ($("#contact3").is(":checked")&&($.cookie(ADDITIONAL_COOKIE_NAME))){
		$("#showIt2").show("fast");
	} else {     
		$("#showIt2").hide("fast");
	}

	// Add onclick handler to checkboxs
	$("#contact2").click(function(){       
		if ($("#contact2").is(":checked")){
		 	$.cookie(COOKIE_NAME, 'true', options);
			$("#showIt1").show("fast");
			$("#tel").focus();
		} else {     
       		$.cookie(COOKIE_NAME, 'false', options);
			$("#showIt1").hide("fast");
		}
	});
	$("#contact3").click(function(){       
		if ($("#contact3").is(":checked")){
			$.cookie(ADDITIONAL_COOKIE_NAME, 'true', options);
			$("#showIt2").show("fast");
			$("#address").focus();
		} else {     
       		$.cookie(ADDITIONAL_COOKIE_NAME, 'false', options);
			$("#showIt2").hide("fast");
		}
	});
		
});