function initTabs(){
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++){
		if (sets[i].className.indexOf("tabs") != -1){
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++){
				if (links[j].className.indexOf("tab") != -1){
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

	//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function (){
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c){
	//reset all tabs before change
							for (var i = 0; i < this.tabs.length; i++){
								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}

function initHideValue(){
	var inputs = document.getElementsByTagName('input');
	var textareas = document.getElementsByTagName('textarea');
	if(inputs){
		for (i = 0; i < inputs.length; i++){
			if(inputs[i].type == 'text'){
				inputs[i].defValue = inputs[i].value;
				inputs[i].onfocus = function(){
					if (this.value == this.defValue){
						this.value = '';
					}
				}
				inputs[i].onblur = function(){
					if (this.value == ''){
						this.value = this.defValue;
					}
				}
			}
		}
	}
	if(textareas){
		for (i = 0; i < textareas.length; i++){
			textareas[i].defValue = textareas[i].value;
			textareas[i].onfocus = function(){
				if (this.value == this.defValue){
					this.value = '';
				}
			}
			textareas[i].onblur = function(){
				if (this.value == ''){
					this.value = this.defValue;
				}
			}
		}
	}
}

function buttonHover(){
	var inputs = document.getElementsByTagName('input');
	if(inputs){
		for(i = 0; i < inputs.length; i++){
			if(inputs[i].className.indexOf('not-hover') != -1){
				inputs[i].onmouseover = function(){
					this.className += ' hover';
				}
				inputs[i].onmouseout = function(){
					this.className = this.className.replace(' hover', '');
				}
			}
		}
	}
}

function tableHover(){
	var tableList = document.getElementsByTagName('table');
	if(tableList){
		for (i = 0; i < tableList.length; i++){
			if(tableList[i].className.indexOf('ie-hover') != -1){
				var cells = tableList[i].getElementsByTagName('td');
				for (j = 0; j < cells.length; j++){
					cells[j].onmouseover = function(){
						this.parentNode.className += ' hover';
					}
					cells[j].onmouseout = function(){
						this.parentNode.className = this.parentNode.className.replace('hover', '');
					}
				}
			}
		}
	}
}
function CookieHandler() {
  this.setCookie = function (name, value, days_around) {
    if (typeof(seconds) != 'undefined') {
      this.date = new Date();
      date.setTime(date.getDate() + days_around);
      this.expires = "; expires=" + date.toGMTString();
    }
    else {
      this.expires = "";
    }
    document.cookie = name + "=" + value + this.expires + "; path=/";
  }
  this.getCookie = function (name) {
    name += "=";
    var carray = document.cookie.split(';');
    for(var i=0;i < carray.length;i++) {
      var c = carray[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return null;
  }
  this.deleteCookie = function (name) {
    this.setCookie(name, "", -1);
  }
}

function Cookier(name) {
  var self = this;
  this.cookie_name = name + "=";
  
  this.make_cookie = function (value, days_around) {
    days_around = ( days_around ? days_around : 10 )
    var date = new Date();
    date.setTime(date.getDate() + days_around);
    var expires = "; expires=" + date.toGMTString();
    document.cookie = self.cookie_name + value + expires + "; path=/";
  }
  this.eat_cookie = function () {
    var cookie_jar = document.cookie;
    var cookie_start = cookie_jar.indexOf(self.cookie_name) + self.cookie_name.length;
    var cookie_end = cookie_jar.slice(cookie_start).indexOf(';') + cookie_start;
    return cookie_jar.slice(cookie_start, cookie_end);
  }
  this.toss_cookie = function () {
    self.make_cookie(self.cookie_name, "", -1);
  }
}

function fontResize(){
	var link = document.getElementById('font-resize');
	if(link) {
	  var cookier = new CookieHandler();
	  var size = Number(cookier.getCookie('font-size'));
	  if (!size) {
	    size = 100
    } else {
      document.getElementById('main').style.fontSize = size + '%';
    }
		link.onclick = function(){
      if (size == 150){
      	size = 100;
      	cookier.setCookie('font-size', size, 10);
      	document.getElementById('main').style.fontSize = size + '%';
      } else {
      	size += 10;
      	cookier.setCookie('font-size', size, 10);
      	document.getElementById('main').style.fontSize = size + '%';
      }
			return false;
		}
	}
}
if (typeof(console) == "undefined") { 
  var console = { 
    log: function() {}
  }
} else {
  var logger = function(message) {
    console.log(message);
  }
};
function Video_switch(element) {
  var self = this;
  this.root = element;
  
  this.size_fixer = function(mov) {
    var tt = setTimeout( function() {
      if (typeof(mov.GetRectangle()) == "string") {
        var size = mov.GetRectangle();
        var sa= size.split(',');
        var new_size = [0,0];
        var nw = sa[2]-sa[0];
        var nh = sa[3]-sa[1];
        if (nw > 460) {
          nh = nh * ( 460 / nw );
          nw = 460;
        }
        new_size[2] = nw
        new_size[3] = nh;
        mov.SetRectangle(new_size.toString());
        $(mov).parent().andSelf().attr('width', new_size[2]).attr('height', new_size[3]+16);
        mov.SetControllerVisible(true);
      } else {
        self.size_fixer(mov);
      }
    }, 1000);
  }
  
  this.start_up = function() {
    self.size_fixer(document.movie1);
  }
  
  return this;
}

if (window.addEventListener){
	window.addEventListener("load", initTabs, false);
	window.addEventListener("load", initHideValue, false);
	window.addEventListener("load", fontResize, false);
} else
if (window.attachEvent){
	window.attachEvent("onload", initTabs);
	window.attachEvent("onload", initHideValue);
	window.attachEvent("onload", buttonHover);
	window.attachEvent("onload", tableHover);
	window.attachEvent("onload", fontResize);
}


/*
Complete rewrite of included javascript, using more efficient and readable jquery-based code.
*/

$(function(){
	// load script file and trigger callback when the script is loaded 
	var js = function(j, callback){
		$.getScript("/js/" + j + ".js", callback);
	}
	
	// unobtrusive ajax form-submits and validation
	js('jquery.form-min', function(){
		$().bind('cbox_complete', function(){
			// I think this doesn't work in colorbox
			// $('#colorbox input.date').datepicker();
			$('#colorbox form').ajaxForm({
				dataType:  'json', 
				success:   function (data){        		
					if (data.message){
						alert(data.message);
						$.fn.colorbox.close();
					}else{
						var info ='';
						$('#colorbox form *').removeClass('error');
						for (i in data.error_info){
							info += data.error_info[i] + "\n";
							$('#'+i,'#colorbox form').addClass('error');
						}
						alert(data.error + "\n\n" + info);
					}
				}
			});
		});
	});
	
	// unobtrusive ajax lightbox
	js('jquery.colorbox-min', function(){
		$('a[href*=/contact.php?id=], a[href=/share-page.php]').colorbox();
	});
	
	// active trail stuff
	var url = window.location.href;
	var rel = window.location.pathname;
	$('a[href=' + url + '], a[href=' + rel + ']' ).addClass('active');
	$('li:has(a[href=' + url + '], a[href=' + rel + '])').addClass('active');
	
	
	
	// setup date entry fields
	// this is a stripped-down jqueryui, if you need more widgets, etc make a new custom build
	js('jquery-ui-1.7.2.custom.min', function(){
		$('input.date').datepicker();
	});
	
	// fancy auto-submit for category search
	$('#cat_search_form select').change(function(){
		$('#cat_search_form').submit();
	});
	$('#cat_search_form input:submit').hide();
	
	if( $('#movie1').length > 0) {
	  new Video_switch($('#movie1')).start_up();
  }
	
});

