// JavaScript Document

window.onload =function() {
	nonObtrusiveNav();
}

function nonObtrusiveNav() {	
	//loop over <a> tags inside nav div 
	// add onclick to make ajaxcall
	// add &ajax=true to end of each A tag 

	// declare vars for IE compatibility
	var navlist = document.getElementById('main_holder');
	var navlistlinks= navlist.getElementsByTagName('a');

	for (i=0; i<navlistlinks.length; i++) {
		console.log(navlistlinks[i]);
		if (navlistlinks[i].target=='_blank' || navlistlinks[i].target=='_self') {
			navlistlinks[i].onclick=function() {				
				return true;						
			}
		} else if ((navlistlinks[i].href==server+'#') ||(navlistlinks[i].href=='#') || (navlistlinks[i].className=='js_link')) {
			// open the javascript in the template onClick()
		// this condition is needed to accomodate popups using onClick. Make sure to set the link class="js_link"
		} else if ((navlistlinks[i].idName=='thickbox')) {
			return false;
		} else {
			// or else just add ajax=true to all links so only the content refreshes and not the media player
			navlistlinks[i].onclick=function() {		
				ajaxcall(this.href+"&ajax=true");
				return false;	
			}	
		}	
//alert(navlistlinks[i].onmouseover);
	}
	
}

function ajaxcall(info) {
// holder is on the index.tpl page
	loadPage(info,'main_holder');
}

function flashajaxcall(info) {
	// Ezra Chan - 3/4/08
	// Work around for flash banners with embedded getURL() anchor tags 
	// so clicking them won't reset the flash audio player. Works within the ajax framework
	// This function is called from the flash swf file's getURL() function 
	// IE: getURL("javascript: flashajaxcall('/index.php?module=lineup&lineup_item=lineup_rosie_odonnell&ajax=true')");

	// holder is on the index.tpl page
	loadPage(info,'main_holder');
}


var page;
function loadPage(url, containerid) {	

	page = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) { 
    	try {
			page = new XMLHttpRequest(); 
        } catch(e) {
			page = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	page = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          	page = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          	page = false;
        	}
		}
    }

	if(page) {
		page.onreadystatechange = function() {
			popdiv(page, containerid);			
		}
		page.open("GET", url, true);

		page.send("");
	} else {
		alert('no page');
	}
}

function popdiv(page, containerid) { 
	if (page.readyState == 4 && (page.status==200 || window.location.href.indexOf("http")==-1)) { 
		document.getElementById(containerid).innerHTML=page.responseText;
		
		document.getElementById(containerid).scrollTop = 0; 
		nonObtrusiveNav();
		
		/** Ezra Chan 6/24/08
			MUST call thickbox init() here again to get gallery to work in the ajax/non_obtrusive_nav environment.
			When ajax is running, it only loads thickbox the first time it goes into the index page.
			All other links and pages after this are controlled by non_obtrusive_nav and thickbox is not parsed again.
			So putting the thickbox init() here allows thickbox to be parsed again by non_obtrusive_nav.
		*/
		tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;
	}
}
