/*-------------------------------------------------
Title:		IPOX JavaScript functions
Author:		John Reed, john@studiobonito.com
Updated:	November 24 2008
------------------------------------------------- */





// add load event function
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}





// popup function
function popup(url,w,h) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var popupProps = 'width='+w+',height='+h+',left='+winl+',top='+wint;
	
	window.open(url,'popup',popupProps);
}





// prepares links based on XML "rel" attribute
function prepareLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		
		// prepare external links
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}

		// prepare close window links
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "close") {
			anchor.onclick = function() {
				window.close();
			}
		}
	}

	if (!document.getElementById) return;
	
	var privacy = document.getElementById('privacy');
	
	if(privacy) {
		privacy.onclick = function() {
			popup(privacy.href,700,540);
			return false;
		}
	}	
}





// prepare forms
function prepForms() {
	if (!document.getElementById) return;
	
	var q = document.getElementById('q');
	var s = document.getElementById('s');

	var ccoptin = document.getElementById('ccoptin');
	var ea = document.getElementById('ea');
	
	// opt-in logic
	if(ccoptin) {
		var w = 700; // popup window width
		var h = 540; // popup window height
	
		var winl = (screen.width - w) / 2;
		var wint = (screen.height - h) / 2;
		var popupProps = 'width='+w+',height='+h+',left='+winl+',top='+wint;

		ccoptin.onsubmit = function() {
			var popup = window.open('about:blank','popup',popupProps);
			ccoptin.target = 'popup';
			return true;
		}

		ea.onfocus = function() {
			if(ea.value == 'your email address') {
				ea.value = '';
				ea.className = 'active';
			}
		}
	
		ea.onblur = function() {
			if(ea.value == '') {
				ea.value = 'your email address';
				ea.className = '';
			}
		}
	}
	
	if(q) {
		// search query input logic
		q.onfocus = function() {
			if(q.value == 'Search for...') {
				q.value = '';
				q.className = 'active';
			}
		}
	
		q.onblur = function() {
			if(q.value == '') {
				q.value = 'Search for...';
				q.className = '';
			}
		}
	}

	if(s) {
		// search query input logic
		s.onfocus = function() {
			if(s.value == 'Search for...') {
				s.value = '';
				s.className = 'active';
			}
		}
	
		s.onblur = function() {
			if(s.value == '') {
				s.value = 'Search for...';
				s.className = '';
			}
		}
	}
}





// calculate times
function calcage(secs, num1, num2) {
	s = ((Math.floor(secs/num1))%num2).toString();
	if (s.length < 2)
		s = "0" + s;
	return s;
}





// countdown function
function countdown() {
	if(!document.getElementById) return false;
			
	// initialize variables
	var now = new Date();
	var theEvent = new Date("Feb 3 2009 09:30");
	
		// get the local time
		var localTime = now.getTime();
		
		// obtain local UTC offset and convert to msec
		var localOffset = now.getTimezoneOffset() * 60000;
		
		// obtain UTC time in msec
		var utc = localTime + localOffset;
		
		// obtain and add destination's UTC time offset
		var offset = -5;   
		var nyTime = utc + (3600000*offset);
		
		var timeDiff = nyTime - localTime;
	
	// make the time adjustment
	theEvent = theEvent - timeDiff;
	
	
	var ms = (theEvent - now);
	
	if(ms>0) {
		var secs = ms / 1000;
		var frac = secs / 10;
		frac = frac.toString();
		frac = frac.substring(frac.indexOf(".") + 2);
		frac = frac.substring(0,2);
		
		document.getElementById('days').innerHTML = calcage(secs,86400,100000);
		document.getElementById('hrs').innerHTML = calcage(secs,3600,24);
		document.getElementById('mins').innerHTML = calcage(secs,60,60);
		document.getElementById('secs').innerHTML = calcage(secs,1,60);
		document.getElementById('frac').innerHTML = frac;
	} else {
		document.getElementById('h4').style.display = 'none';
		document.getElementById('countdown').style.display = 'none';
	}
}


// load events
addLoadEvent(prepareLinks);
addLoadEvent(prepForms);




