dojo.require("dojo.fx"); 
dojo.addOnLoad(function(){
    dojo.query(".panelbtn").onclick(function(e){

		var month = gup( 'month' );

		panel_action(e, month);
    });
});

var gloDestId;

function panel_action(e, month){
	var a = dojo.fadeOut({node: e.target, duration:250});
	a.play();
	name = e.target.id.replace('img', '');
	show_panel(name, month);
}


function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function draw_panels(){
	document.write("<table class='panels'>");
	document.write("<tr>");
	document.write("	<td id='mainpanel'><img id='mainimg' class='panelbtn' src='i/main.jpg' /></td>");
	document.write("	<td id='inseat777panel'><img id='inseat777img' class='panelbtn' src='i/777inseat.jpg' /></td>");
	document.write("	<td id='playerpanel'><img id='playerimg' class='panelbtn' src='i/player.jpg' /></td>");
	document.write("	<td id='inseat757panel'><img id='inseat757img' class='panelbtn' src='i/757inseat.jpg' /></td>");
	document.write("</tr>");
	document.write("</table>");
}

function show_panel(name, month){
	deselect_panels(name);
	
	switch(name) {
		case('main'):
			show('route_movies');
			break;
		case('inseat777'):
			hide('route_movies');
			//window.open('inseat.php?month=' + month, '777_inseat_popup', 'toolbar=no, status=no, width=640, height=480, scrollbars=no, resizable=no');
			window.open('aircraft-systems/777/index.php?month=' + month, '777_inseat_popup', 'toolbar=no, status=no, width=640, height=480, scrollbars=no, resizable=no');
			break;
	
		case('inseat757'):
			hide('route_movies');
			window.open('aircraft-systems/757/index.php?month=' + month, '757_inseat_popup', 'toolbar=no, status=no, width=1024, height=600, scrollbars=no, resizable=no');
			break;
			
		case('player'):
			hide('route_movies');
			window.open('aircraft-systems/767/index.php?month=' + month, 'player_popup', 'toolbar=no, status=no, width=1024, height=600, scrollbars=no, resizable=no');
			break;
	}
	return false;
}

function deselect_panels(name){
	dojo.query(".panelbtn").forEach(
		function(btn) {
			if (btn.id != name + 'img')
			dojo.fadeIn({node: btn, duration:250}).play();
		}
	);
}


function set_selected(id){
	var route_list = document.getElementById('sub_list');
	var routes = route_list.getElementsByTagName("*");
	
	for (var i = 0; i < routes.length; i++) {
		this_elem = routes[i];
		if(this_elem.id) this_elem.className = '';
	}

	var elem = document.getElementById(id);
	elem.className = 'selected';
}

function show_target(id){

	var route_divs = document.getElementById('route_movies');


	var route_movies = route_divs.getElementsByTagName("*"); // *** error here jrd

	for (var i = 0; i < route_movies.length; i++) {
		this_elem = route_movies[i];
		if(this_elem.id) this_elem.style.display = 'none';
	}

	show(id);
}

function show_route(id_string){
/*
	if (id_string == 'italy') {

		id_string = 'italy2';
	}
*/




    gloDestId = id_string;	
	set_selected('l_' + id_string);

	new_text = get_text('l_' + id_string);
	//alert(new_text);

	set_text('sub_select',new_text);

	show_target('m_' + id_string);

	//hide('sub_list');
	menuHideMenus();

	show_panel('main');

	fadePlayers(id_string);

	dojo.fadeOut({node: 'mainimg', duration:250}).play();


		if (id_string == 'hawaii') {
			changeOpac(40, 'inseat777panel');
			changeOpac(40, 'playerpanel');
			changeOpac(40, 'inseat757panel');
		}


}

function getDestId() 
{
	return gloDestId;
}


function get_text(id){
	var elem = document.getElementById(id);
	return elem.innerHTML;
}

function set_text(id, text) {
	if (document.getElementById)
	{
		x = document.getElementById(id);
		
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

function show_all(){
	set_selected('l_all');
	set_text('sub_select','All Routes');
	menuHideMenus();
	
	var route_divs = document.getElementById('route_movies');
	var route_movies = route_divs.getElementsByTagName("*");


	for (var i = 0; i < route_movies.length; i++) {
		this_elem = route_movies[i];


		if((this_elem.id && this_elem.id != 'm_asia')) {



			if ((this_elem.id != 'm_italy') && (this_elem.id != 'm_russia')) {
				this_elem.style.display = 'block';
			}



			if ((this_elem.id == 'm_italy') || (this_elem.id == 'm_russia')) {
				/*
				 *	This IF statement fixes a bug in which, for example, Russia
				 *	would show up twice. Once being the original the second being
				 *	the special copy we included for russia and italy.
				 *
				 *	Russia (or Italy) Would appear twice only if you selected
				 *	russia from the drop down list then selected 'All Routes'
				 *
				 *									- Jarrod 09 july 09
				 *
				 */
				this_elem.style.display = 'none';
			}

			
		}
	}
	
	show(id);
}

function showselect(string){
	mainimg = document.getElementById('mainpanel');
	inseatimg = document.getElementById('inseatpanel');
	playerimg = document.getElementById('playerpanel');

	switch(string){
		case "main":
			mainimg.src = "i/pmaino.jpg";
			inseatimg.src = "i/pinseat.jpg";
			playerimg.src = "i/pplayer.jpg";
			show('route_movies');
			break;
		case "inseat":
			mainimg.src = "i/pmain.jpg";
			inseatimg.src = "i/pinseato.jpg";
			playerimg.src = "i/pplayer.jpg";
			hide('route_movies');
			break;
		case "player":
			mainimg.src = "i/pmain.jpg";
			inseatimg.src = "i/pinseat.jpg";
			playerimg.src = "i/pplayero.jpg";
			hide('route_movies');
			break;
		case "inseat-special":
			mainimg.src = "i/pmainf.jpg";
			inseatimg.src = "i/pinseat.jpg";
			playerimg.src = "i/pplayerf.jpg";
			show('route_movies');
			break;
	}
}

function togglepanel(obj){
	src = obj.src;

	//turn off all others
	untoggleall(src);

	//turn on selected panel
	newsrc = nameswitch(src, 'add');
	
	obj.src = newsrc;
}

function nameswitch(path, action){
	switch (action){
		case "add":
			//check if the overstate needs to be added
			if (path.indexOf('o.jpg') == -1) return path.replace('.jpg','o.jpg');
			break;
		case "remove":
			//check if the overstate needs to be removed
			return path.replace('o.jpg','.jpg');
			break;
	}
	return path;
}

function untoggleall(path){
	//grab all the panels
	var panels = document.getElementById('panels');
	var links = panels.getElementsByTagName("*");
	
	//loop through panels and turn off / unselect others
	for (var i = 0; i < links.length; i++) {
		this_elem = links[i];
		if(this_elem.src && this_elem.src != path){
			newsrc = nameswitch(this_elem.src, 'remove');
			this_elem.src = newsrc;
		} 
	}

}

// JavaScript Document
function show(id)
{
	dojo.query('#'+id).style({display:'block', opacity: 0});
	dojo.fadeIn({node: id, duration:250}).play();
}
function hide(id){
	dojo.fadeOut({node: id, duration:250}).play();
}
function hideAll(){
	hide("pnmonth");
	hide("pnday");
	hide("pnrange");
	hide("pnairline");
}

function delayHide(id){
	setTimeout('hide("' + id + '")',5000);
}

function show_hide(id, id2){
	elem = document.getElementById(id);
	vis = elem.style;
	switch(vis.display){
		case 'none':
			dojo.query('#'+id).style({display:'block', opacity: 0});
			dojo.fadeIn({node: id, duration:250}).play();
			break;
		case 'block':
			
			//var animate = dojo.fadeOut({node: id, duration:250});
			//animate.play();
			//break;
			dojo.fx.chain([
			    dojo.fadeOut({ node: id, duration: 250,
			        onEnd: dojo.query('#'+id).style({display:'none'})
			        })
			]).play();
			break;
	}
}

function togglestyle(id){
	elem = document.getElementById(id);
	vis = elem.className;
	switch(vis){
		case 'audioshow':
			elem.className = 'audiohide';
			break;
		case 'audiohide':
			elem.className = 'audioshow';
			break;
	}
}

function toggle_visible(id){
	elem = document.getElementById(id);
	vis = elem.style;
	switch(vis.display){
		case 'none':
			vis.display = 'block';
			break;
		case 'block':
			vis.display = 'none';
			break;
	}
}

function toggleSubLayer(id){
	
	elem = document.getElementById("subrow_"+id);
	label = document.getElementById("anchor_"+id);
	vis = elem.style;

	if(vis.display == 'none'){
		label.innerHTML = "Hide Details";
		vis.display = 'block';
		vis.display = 'table-row';
	}else{
		label.innerHTML = "Show Details";
		vis.display = 'none';
	}
}
function toggleLayer(id){
	
	if( document.getElementById ){ // this is the way the standards work
		elem = document.getElementById(id);
	}else if( document.all ){ // this is the way old msie versions work
		elem = document.all[id];
	}else if( document.layers ){ // this is the way nn4 works
		elem = document.layers[id];
	}
	vis = elem.style;

	if(vis.display == 'none'){
		vis.display = 'block';
		vis.display = 'table-row';
	}else{
		vis.display = 'none';
	}
}
function silentErrorHandler() {return true;}
window.onerror=silentErrorHandler;








/******************************************************************************

	The below code makes the fading possible. There is probably code above to
	do the same thing... GOODLUCK! hah.

	Yeah. Use the below.

		-j

	Ref: http://brainerror.net/scripts/javascript/blendtrans/

*******************************************************************************/



function fadePlayers(country)
{
	var countryCase;
	countryCase = country;

	//alert(countryCase);

	switch(countryCase)
	{

		case "sanjuan" :
		case "central" :
	
			changeOpac(100, 'inseat777panel');
			opacity('playerpanel', 100, 40, 300);
			changeOpac(100, 'inseat757panel');
	
			break;
			
		case "uk" :
		case "germany" :			
		case "france_montreal" :
		case "spain" :
		case "brazil" :
		case "asia" :
	
			changeOpac(100, 'inseat777panel');
			opacity('playerpanel', 100, 40, 300);
			opacity('inseat757panel', 100, 40, 300);
	
			break;

		case 'russia':
			opacity('inseat777panel', 100, 40, 300);
			opacity('inseat757panel', 100, 40, 300);
			break;
		case 'italy':
			opacity('inseat777panel', 100, 40, 300);
			opacity('inseat757panel', 100, 40, 300);
	  		break;
		default:
			changeOpac(100, 'inseat777panel');
			changeOpac(100, 'playerpanel');
			changeOpac(100, 'inseat757panel');
	}

}

// --------------------------------------------------------



function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}


//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}


function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

// --------------------------------------------------------

