
/*
	script_global.js
	
	Global scripts for Akimbo.biz
	(c)2008 Gordon Hicks

	created:  2008 Mar 8
	modified: 2008 Mar 31
	
*/


	// bind initialization handler to window load
	window.onload = init;
	window.onunload = finish;

	// initialization handler
	function init() {
		initLoginDropdown();
		initSearchDropdown();
		initAd();
		//initCalendar(); -- PHP does all of this for us already
	}

	// finish handler
	function finish() {
		finishAd();
	}

/* Login Dropdown */

	// initialize login dropdown
	function initLoginDropdown() {
		// check browser support...
		if ( !document.getElementById )  return;
		// check page has the need parts...
		if ( !document.getElementById('loginNav') )  return;
		if ( !document.getElementById('dropdownLogin') )  return;
		// ok...
		// bind the handlers...
		document.getElementById('loginNav').onclick = dropdownLoginShow;
		document.getElementById('dropdownLoginNav').onclick = dropdownLoginHide;
		document.getElementById('dropdownLoginClose').onclick = dropdownLoginHide;
	}

	// show login panel
	function dropdownLoginShow() {
		if ( document.getElementById('dropdownSearch') )  dropdownSearchHide();
		document.getElementById('dropdownLogin').style.visibility = 'visible';
		document.getElementById('dropdownLogin').style.zIndex = '3';
		return false;
	}
	
	// hide login panel
	function dropdownLoginHide() {
		document.getElementById('dropdownLogin').style.visibility = 'hidden';
		document.getElementById('dropdownLogin').style.zIndex = '0';
		return false;
	}
	

	
/* Search Dropdown */

	// initialize search dropdown
	function initSearchDropdown() {
		// check browser support...
		if ( !document.getElementById )  return;
		// check page has the need parts...
		if ( !document.getElementById('searchNav') )  return;
		if ( !document.getElementById('dropdownSearch') )  return;
		// ok...
		// bind the handlers...
		document.getElementById('searchNav').onclick = dropdownSearchShow;
		document.getElementById('dropdownSearchNav').onclick = dropdownSearchHide;
		document.getElementById('dropdownSearchClose').onclick = dropdownSearchHide;
	}

	// show search panel
	function dropdownSearchShow() {
		if ( document.getElementById('dropdownLogin') )  dropdownLoginHide();
		document.getElementById('dropdownSearch').style.visibility = 'visible';
		document.getElementById('dropdownSearch').style.zIndex = '3';
		return false;
	}
	
	// hide search panel
	function dropdownSearchHide() {
		document.getElementById('dropdownSearch').style.visibility = 'hidden';
		document.getElementById('dropdownSearch').style.zIndex = '0';
		return false;
	}
	

	
/* Ad Animation */
	
	var adPeriod = 10; // seconds - period between animation steps
	var adRunTime = 0; // seconds - stop the animation after this time (0 = never)
	var adSpacing = 89; // pixels - ads are offset vertically by this distance
	var ad = Array(); // ad links are kept here
	var adStep = 0; // current step in the animation (wraps)
	var adEnable = true; // ad animation is enabled
	
	// initialize ad animation
	function initAd() {
		// check browser support...
		if ( !document.getElementById )  return;
		// check page has the needed parts...
		if ( !document.getElementById('adArea') )  return;
		if ( !document.getElementById('adArea').getElementsByTagName('a') )  return;
		// ok...
		// put the ad links into an array
		ad = document.getElementById('adArea').getElementsByTagName('a');
		// take control of css positioning
		for (i=0; i<ad.length; i++) {
			ad[i].style.position = 'absolute';
			ad[i].style.top = i * adSpacing + 'px';
		}
		// get previous adStep from cookie, if available...
		var pos = document.cookie.indexOf("adStep=")
		if (pos != -1) {
			var start = pos + 7;
			var end = document.cookie.indexOf(";", start);
			if (end == -1) end = document.cookie.length;
			adStep = unescape(document.cookie.substring(start,end));
		}
		// stop the animation at a given time in the future...
		if ( adRunTime > 0 ) {
			window.setTimeout( 'adEnable=false;' , adRunTime*1000);
		}
		// begin animation
		adShift();
	}

	// finish ad animation
	function  finishAd() {
		// store the current ad step, so we can pick up were we left off on the next page
		document.cookie = 'adStep=' + adStep;
	}

	// shift the ads up one step
	function adShift () {
		if (!adEnable) return; // stop animating
		adStep++;
		adStep = adStep % ad.length;
		for (i=0; i<ad.length; i++) {
			if ( i >= adStep ) {
				ad[i].style.top = (i-adStep) * adSpacing + 'px';
			} else {
				ad[i].style.top = (i - adStep + ad.length) * adSpacing + 'px';
			}
		}
		window.setTimeout( 'adShift(adStep);' , adPeriod*1000);
	}


	
/* Calendar */		

	maxMonthsInFuture = 6;  // how many months forward can we go
	maxMonthsInPast = 6;	// how many months past..
	monthsExcursion = 0; // # months forward (+) or backwards (-) from calTodayDate
	calTodayDate = new Date(); // 'today' in the context of the calendar shown; highlighted date
	calShowDate = new Date(); // a date in the currently shown month; specifies which month is shown
	monthName = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");

	// initialize calendar
	function initCalendar() {
		// check browser support...
		if ( !document.getElementById )  return;
		if ( !document.implementation )  return;
		if ( !document.implementation.hasFeature )  return;
		if ( !document.implementation.hasFeature("html", "1.0") ) return;
		// check page has the needed part...
		if ( !document.getElementById('calendar') )  return;
		// ok...
		// bind the handlers...
		document.getElementById('calendarNextMonth').onclick = calendarNextMonth;
		document.getElementById('calendarPrevMonth').onclick = calendarPrevMonth;
		// get 'calendar date' as a date object (passed via javascript embedded in html)
		if (calendarToday && calendarToday.length >= 8 && calendarToday.length <= 10) {
			var theDate = calendarToday.split('-', 4);
			if (theDate.length == 3) {
				calTodayDate.setFullYear( theDate[0].valueOf(), theDate[1].valueOf()-1, theDate[2].valueOf() );
				calTodayDate.setHours(0, 0, 0, 0); // zero hours etc.
				calShowDate.setTime( calTodayDate.getTime() );
			}
		}
		updateCalendar();
	}

	// next month
	function calendarNextMonth() {
		var month = calShowDate.getMonth();
		if ( month < 11 ) {
			calShowDate.setMonth( month + 1 );
		} else {
			calShowDate.setFullYear( calShowDate.getFullYear() + 1, 0 );
		}
		monthsExcursion++;
		updateCalendar();
		return false;
	}

	// previous month
	function calendarPrevMonth() {
		var month = calShowDate.getMonth();
		if ( month > 0 ) {
			calShowDate.setMonth( month - 1 );
		} else {
			calShowDate.setFullYear( calShowDate.getFullYear() - 1, 11 );
		}
		monthsExcursion--;
		updateCalendar();
		return false;
	}

	// update the calendar
	function updateCalendar() {
		// month heading text node: needs to be exactly in this place in the calendar html		
		var monthHead = document.getElementById('calendarMonthYear').childNodes[0];
		// text for month heading (eg: February 2009 )
		var monthHeadText = monthName[calShowDate.getMonth()] + ' ' + calShowDate.getFullYear();
		// update the month heading
		monthHead.parentNode.replaceChild( document.createTextNode(monthHeadText), monthHead );
		
		// have we reached the month excursion limits? Set next/prev link visibility accordingly...
		if (monthsExcursion >= maxMonthsInFuture) {
			document.getElementById('calendarNextMonth').style.visibility = 'hidden';
		} else {
			document.getElementById('calendarNextMonth').style.visibility = 'visible';
		}
		if ( -monthsExcursion >= maxMonthsInPast) {
			document.getElementById('calendarPrevMonth').style.visibility = 'hidden';
		} else {
			document.getElementById('calendarPrevMonth').style.visibility = 'visible';
		}

		// document fragment: holds new calendar
		var daysFrag = document.createDocumentFragment();
		// <div class="days"></div>	
		var a = daysFrag.appendChild( document.createElement('div') );
		a.className = 'days' ;
		a.id = 'calendarDays' ;
		
		// find start date (a Sunday)
		var firstOfMonth = new Date(calShowDate.getFullYear(), calShowDate.getMonth(), 1);
		var startDate = new Date( firstOfMonth.getTime() - ((firstOfMonth.getDay() * 24 - 3) *60*60*1000 ) );
		// what's happening: 1) determine day of week [0-6]; then multiply that number by 24 hours to get the number of
		// days we need to back up; subtract 3 hours to bridge any daylight savings shift; then multiply by the
		// number of milliseconds in an hour; that gives us the millisecond representation of the start date - a Sunday.

		// find stop date (a Saturday)
		var firstOfNextMonth = new Date( calShowDate.getFullYear(), calShowDate.getMonth() + 1, 1 ); // 1st of next month
		var stopDate = new Date( firstOfNextMonth.getTime() + (( ((7-firstOfNextMonth.getDay())%7 - 1 ) * 24 + 3+12) *60*60*1000 ) );
		// what's happening: similar to startDate (above); a little modular arithmetic gives us the right number of days to move
		// forward or backward from the firstOfNextMonth: [-1 to 5]; the +12hrs makes sure the stop Date is later in the day than
		// start date, so the comparison (curDate < stopDate) is reliable.

		// build the html for the days
		var curDate = startDate;
		while (curDate < stopDate) { // (effectively, curDate <= stopDate, because stopDate is 12 h later in day)
			var b = a.appendChild( document.createElement('div') );
			// mark "today"...
			if ( curDate.getDate()==calTodayDate.getDate() && curDate.getMonth()==calTodayDate.getMonth() && curDate.getFullYear()==calTodayDate.getFullYear() ) {
				b.className = 'today' ;
				
			}
			// grey-out adjacent months...
			if ( curDate.getMonth() != calShowDate.getMonth() ) {
				b.className = 'grey' ;
			}
			// <a href=""></a>
			var c = b.appendChild( document.createElement('a') );
			c.href = 'calendar_of_events.html?day=' + curDate.getDate() + '&month=' + (curDate.getMonth()+1) + '&year=' + (curDate.getFullYear()) ;
			c.appendChild( document.createTextNode(curDate.getDate()) );
			// next day
			curDate.setTime( curDate.getTime() + (1000*60*60*24) ); // add 24 hours
		}

		// replace the existing calendar with the new one...
		var daysDiv = document.getElementById('calendarDays');
		daysDiv.parentNode.replaceChild( daysFrag, daysDiv );
	}

/* Clickthrough Tracking */


function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}


function registerClick(adid){
	var http = createRequestObject();
	http.open('get', '/ads/click.php?id='+adid, true);
	http.onreadystatechange = function () {
		if(http.readyState == 4){
			// we got a response
			return true;
		}
	}
	http.send(null);
}
