// JavaScript Document
/*               GLOBAL VARIABLES              */
var scroll_speed 	= 200;
var scrolling 		= false;

var navBoxes 	= "komodo-nav";


var kmTabs	 	= "komodo-about-tab";


var allNavTabs 	= new Array(kmTabs);

var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
var isDyn = (isDOM || isIE4 || isNS4);

var scroll_speed = 200;

/*            END GLOBAL VARIABLES         */

function setTextSize(size) {
	if(document.getElementsByTagName) {
		if(size == null || size == "undefined" || size == ""){
			size = getCookie("textsize");
			if(size == null || size == "undefined" || size == ""){
				size = "medium";
			}		
		}
		var bodyTag = document.getElementsByTagName("body")[0];
		switch(size) {
			case "small": 
				bodyTag.style.fontSize = "85%";					
				break;
			case "medium": 
				bodyTag.style.fontSize = "100%";
				break;
			case "large": 
				bodyTag.style.fontSize = "140%";
				break;
		}
		// Set code to add this setting to a cookie; Invalidate the cookie after session?
		setCookie("textsize", size, null, "/", "puertadelsolblog.com", null);		
	}
}

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function getRef(id){
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}
function getStyle(id){
	return (isNS4 ? getRef(id) : getRef(id).style);
}
function scrollUp(){
	var aryULs = this.parentNode.getElementsByTagName('ul');
	for(scrollArea = 0; scrollArea < aryULs.length;++scrollArea){
		if(aryULs[scrollArea].style.display == 'block'){
			var elemToScroll = aryULs[scrollArea];
			var currentTop = (elemToScroll.style.top.toString() != "")?parseInt(elemToScroll.style.top,10):0;
			currentTop = (currentTop + scroll_speed >= 0)?0:currentTop + scroll_speed;
			elemToScroll.style.top = currentTop.toString() + 'px';
			break;
		}
	}
}
function scrollDown(){
	var aryULs = this.parentNode.getElementsByTagName('ul');
	for(var scrollArea = 0; scrollArea < aryULs.length;++scrollArea){
		if(aryULs[scrollArea].style.display == 'block'){
			var elemToScroll = aryULs[scrollArea];
			var currentTop = (elemToScroll.style.top.toString() != "")?parseInt(elemToScroll.style.top,10):0;
			var scrollContentHeight = (elemToScroll.clientHeight > 0)?elemToScroll.clientHeight:elemToScroll.offsetHeight;
			if(scrollContentHeight > 200){
				currentTop = ((currentTop - scroll_speed) <= (200 - scrollContentHeight))?(200 - scrollContentHeight):currentTop - scroll_speed;
				elemToScroll.style.top = currentTop.toString() + 'px';
			}
			break;
		}
	}
	
}
function stopScrolling(){
	scrolling = false;
}
function clickNavTab(cookie){
	//alert(document.getElementById(this.id.split('-')[1]).tagName);
	var aryOtherTabs = this.otherTabs.split(',');
	for(tabNum = 0;tabNum < aryOtherTabs.length;++tabNum){
		//set the depth for the tab item that is a sibling to this one
		if(aryOtherTabs[0].toUpperCase() == this.id.toUpperCase()){
			document.getElementById(aryOtherTabs[tabNum]).style.zIndex = aryOtherTabs.length - tabNum;
		}else{
			document.getElementById(aryOtherTabs[tabNum]).style.zIndex = tabNum;
		}
			
		//set the visibility for the sibling tab items content to invisible
		document.getElementById(aryOtherTabs[tabNum].split('-')[1]).style.display = 'none';
		
		//make sure that this tab, since it was clicked, is the highest depth
		if(aryOtherTabs[tabNum].toUpperCase() == this.id.toUpperCase()){
			this.style.zIndex = 3000;
			
			if(cookie != false){
				// cookie which tab was currently focused, remove the cookie at the end of the session
				setCookie(this.id.split('-')[0], this.id, null, "/", "puertadelsolblog.com", null);
			}
			
			//also, make sure that this items content is visible
			document.getElementById(this.id.split('-')[1]).style.display = 'block';
		}
	}
}
function setupNavTabs(){
	//loop through all of the nav tabs and setup functions, vars, and css values
	for(tabGroupNum = 0;tabGroupNum < allNavTabs.length;++tabGroupNum){
		var aryNavTabs = allNavTabs[tabGroupNum].split(',');
		for(tabNum = 0;tabNum < aryNavTabs.length;++tabNum){
			document.getElementById(aryNavTabs[tabNum]).onclick = clickNavTab;
			document.getElementById(aryNavTabs[tabNum]).otherTabs = allNavTabs[tabGroupNum];
			
			if(tabNum == 0){
				document.getElementById(aryNavTabs[tabNum].split('-')[1]).style.display = 'block';
			}else{
				document.getElementById(aryNavTabs[tabNum].split('-')[1]).style.display = 'none';
			}
		}
	}
	//find the cookied focused tabs
	for(tabGroupNum = 0;tabGroupNum < allNavTabs.length;++tabGroupNum){
		var aryNavTabs = allNavTabs[tabGroupNum].split(',');
		for(tabNum = 0;tabNum < aryNavTabs.length;++tabNum){
			
			var focusedTab = getCookie(aryNavTabs[tabNum].split('-')[0]);

			//if a cookie has been set to save which tab was focused previously, focus that tab
			if(focusedTab == aryNavTabs[tabNum]){
				//focus the tab
				document.getElementById(aryNavTabs[tabNum]).onclick(false);
			}
		}
	}	
		
}
function setupNavScrollers(){
	var aryNavBoxes = navBoxes.split(',');
	for(navBoxNum = 0; navBoxNum < aryNavBoxes.length; ++navBoxNum){
		//alert(document.getElementById(aryNavBoxes[navBoxNum]).id);
		var navBoxLinks = document.getElementById(aryNavBoxes[navBoxNum]).getElementsByTagName('a');
		for(aNum = 0;aNum < navBoxLinks.length;++aNum){
			if(navBoxLinks[aNum].className == "navbox-scroll-up"){
				navBoxLinks[aNum].onclick = scrollUp;
			}else if(navBoxLinks[aNum].className == "navbox-scroll-down"){
				navBoxLinks[aNum].onclick = scrollDown;
			}
		}
		
	}
}	
function hoverMainTab(){
	this.style.zIndex = 2002;
	this.style.backgroundPosition = '-70px 0px';
}
function setupHoverStates(){
	document.getElementById('main-nav-blog').onMouseover = hoverMainTab;
}
function setupRememberMe(){
	//create the remember me html for the check box and the label
	if(document.getElementById('remember-me')){
		var rememberDiv = document.getElementById('remember-me');
		var checked = '';
		if(getCookie('remember') == 'true'){
			checked = 'checked';
		}
		rememberDiv.innerHTML = "<label for='remember-me-button' style='margin-left: 15px;'>Remember Me?</label><input type='checkbox' name='remember-me' id='remember-me-button' value='remember' onChange='changeRemember(this)' " + checked + "/>";
	}
	
	//fill in the the fields from the cookies if they exist

}
function changeRemember(checkObj){
	if(checkObj.checked == false){
		deleteCookie('user', '/', 'puertadelsolblog.com');
		deleteCookie('email', '/', 'puertadelsolblog.com');
		deleteCookie('url', '/', 'puertadelsolblog.com');
		deleteCookie('remember', '/', 'puertadelsolblog.com');
	}else{
		var author  = document.getElementById('author').value;
		setCookie("user",  author , ( new Date("December 31, 3000 23:59:59") ), "/", 'puertadelsolblog.com', null);

		var email	= document.getElementById('email').value;
		setCookie("email",  email , ( new Date("December 31, 3000 23:59:59") ), "/", 'puertadelsolblog.com', null);
		
		var url		= document.getElementById('url').value;
		setCookie("url",  url , ( new Date("December 31, 3000 23:59:59") ), "/", 'puertadelsolblog.com', null);	
		
		setCookie("remember",  'true' , ( new Date("December 31, 3000 23:59:59") ), "/", 'puertadelsolblog.com', null);	
	}
}

function setupDynamics(){
	//alert('setting up dynamics');
	//setupHoverStates();
	
	setupNavTabs();
	
	setupNavScrollers();
	
	setupRememberMe();
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
addLoadEvent(setupDynamics);
