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

/*init http object */
var http = createRequestObject();
var dataurl = 'd.php';

/* Global vars */
var chosen_section;
var chosen_subsection;


/* Returns the y height that a image should slide on rollover / rollout */
function getDivHeight(id) {
	var _short = id.toString().substr(0,3);
	this.classes = {};
	this.classes['nav'] = 16;
	this.classes['sub'] = 14;
	this.classes['log'] = 35;
	return this.classes[_short];
}

/* Captions underneath thumbnails */
function captionRollover(id) {
	setY(id, -12);
}
function captionRollout(id) {
	setY(id, 0);
}


function clearSections() {
	if (chosen_section != null && chosen_section != 'logo_news') {
		s_rollout(chosen_section);
		chosen_section = null;
	}
	if (chosen_subsection != null) {
		ss_rollout(chosen_subsection);
		chosen_subsection = null;
	}
}

function getAbout() {
	if (chosen_section == 'nav_about') {
		return;
	}
	clearDivs(Array('details', 'movie'));
	clearSections();
	chosen_section = 'nav_about';
	document.getElementById('nav_about').blur();
	http.open('get', 'd.php?a');
	http.onreadystatechange = onNoMoviesResponse;
	http.send(null);
}

function onAJAXResponse() {
	if (http.readyState == 4){
		document.getElementById('details').innerHTML = http.responseText;
	}
}

function shiftDetailsForMovies() {
	setY('details', 396);
}
function shiftDetailsForNoMovies() {
	setY('details', 274);
}
function onNoMoviesResponse() {
	if (http.readyState == 4){
		shiftDetailsForNoMovies();
		document.getElementById('details').innerHTML = http.responseText;
	}
}
function onMoviesResponse() {
if (http.readyState == 4){
		shiftDetailsForMovies();
		document.getElementById('details').innerHTML = http.responseText;
	}
}

function getNews() {
	if (chosen_section == 'logo_news') {
		return;
	}
	clearDivs(Array('details','movie'));
	clearSections();
	
	chosen_section = 'logo_news';

	http.open('get', 'd.php?n=1');
	http.onreadystatechange = onNoMoviesResponse;
    http.send(null);
}


//Accessor for a CSS object's top property to work w/ Mozilla + Webkit + IE
function getY(id) {
	var _style = document.getElementById(id).style;
	var _str = '';
	if (ie) {
		var _bg = document.getElementById(id).style.top;
	} else {
		
		var _bg = document.getElementById(id).style.getPropertyValue('top');
	}
	var _bg_split = _bg.split(' ');
	var _y = parseInt(_bg_split[_bg_split.length-1]);
	return _y;
}

//Accessor for a CSS object's top property to work w/ Mozilla + Webkit + IE
function setY(id, val) {
	var _style = document.getElementById(id).style;
	if(ie) {
		document.getElementById(id).style.top = val;
	} else {
		var _formatted_value = val;	
		_style.setProperty('top', _formatted_value, '');
	}
}


function sectionRollOver(id) {
	if (chosen_section == id && id != 'logo_news') {
		return;
	}
	var _y = getY(id);
	_y -= getDivHeight(id);
	setY(id, _y);
}

function s_rollout(id) {
	var _y = getY(id);
	_y += getDivHeight(id);
	setY(id, _y);
}

function sectionRollOut(id) {
	if (chosen_section != id || id == 'logo_news') {
		s_rollout(id);
	}
}

function ss_rollover(id) {
	var _y = getY(id);
	_y -= getDivHeight(id);
	setY(id, _y);
}

function subSectionRollOver(id) {
	if (chosen_subsection == id) {
		return;
	}
	ss_rollover(id);
}
function ss_rollout(id) {
	var _y = getY(id);
	_y += getDivHeight(id);
	setY(id, _y);
}

function subSectionRollOut(id) {
	if (chosen_subsection != id) {
		ss_rollout(id);
	}
}

function clearDivs(sec_arr) {
	for(var i=0; i<sec_arr.length; i++) {
		if (sec_arr[i] == 'movie') {
			if (document.getElementById('mov') != undefined && navigator.appName != "Microsoft Internet Explorer") {
				document.getElementById('mov').Stop();
				var movie_outer = document.getElementById('movie');
				var movie_inner = document.getElementById('mov');
				movie_outer.removeChild(movie_inner);
				//document.getElementById('movie').innerHTML = '';
			}
		}
		if (document.getElementById(sec_arr[i]) != undefined) {
			document.getElementById(sec_arr[i]).innerHTML = '';
		}
	}
}

function clearSectionList() {
	//if there's an existing subnavigation div, rename it to subnavigation_out and tween it out.
	var _sn = document.getElementById('subnavigation');
	if (_sn != undefined) {
		_sn.id = 'subnavigation_out';
		_sn.style.zIndex = 99;
		removeSubnavigationOut();
	}
}

function removeSubnavigationOut() {
	var _sn = document.getElementById('subnavigation_out');
	document.body.removeChild(_sn);
}



function getSectionList(id, dom_id) {
	if(dom_id == chosen_section) {
		return;
	}
	/*Navigation handling*/
	clearDivs(Array('details', 'movie'));
	//clear previously set section items
	if (chosen_subsection != null) {
		//main section item was previously selected, and it displayed details
		ss_rollout(chosen_subsection);
		chosen_subsection = null;
	} 
	
	if (chosen_section != null && chosen_section != 'logo_news') {
		//main section was previously selected, and it displayed a section list.
		s_rollout(chosen_section);
	} 
	var _unfocus = document.getElementById(dom_id).blur();
	
	/*If subnavigation currently exists, transition it out.*/
	if (document.getElementById('subnavigation') != undefined) {
		clearSectionList();
	}

	var _subnav_div = document.createElement('div');
	_subnav_div.id = 'subnavigation';
	document.body.appendChild(_subnav_div);
	_subnav_div.style.zIndex = 100;
	chosen_section = dom_id;
	http.open('get', 'd.php?s='+id);
	http.onreadystatechange = onGetSectionList;
	http.send(null);
}


function onGetSectionList() {
	if(http.readyState == 4){
		document.getElementById('subnavigation').innerHTML = http.responseText;
	}
}

function getDetails(id, dom_id) {
	if(dom_id == chosen_subsection) {
		return;
	}
	clearDivs(Array('details','movie'));
	
	if (chosen_subsection != null) {
		ss_rollout(chosen_subsection);
	}
	

	if (dom_id.toString().substring(0,3) == 'nav' && chosen_section != null && chosen_section != 'logo_news') {
		s_rollout(chosen_section);
		chosen_section = null;
	} else if (chosen_section == 'logo_news') {
		chosen_section = null;
	}
	
	var div = document.getElementById(dom_id);
	
	div.blur();
	
	chosen_subsection = dom_id;
	http.open('get', 'd.php?d='+id);
	//watching for contact
	if (dom_id == 'nav_42') {
		http.onreadystatechange = onNoMoviesResponse;
	} else {
		http.onreadystatechange = onMoviesResponse;
	}
	http.send(null);
}



function playMovie(_url, _width, _height) {
		clearDivs(Array('movie'));
		var _mov_holder = document.getElementById("movie").style;
		var _baseline = 358;
		if (ie) {
			_mov_holder.top = _baseline - _height;
		} else {
			_mov_holder.setProperty('top', _baseline - _height, '');
		}
		var myQTObject = new QTObject(_url, "mov", _width, _height+15);
		myQTObject.addParam("target", "myself");
		myQTObject.addParam("autoplay", "true");
		myQTObject.write('movie');
}
