/* common.js */

function page_init(thisPage, thisSubPage) { 
	//setRollovers(thisPage, thisSubPage);
	positionFooter();
	
	if ($('sitemap')) {
		$('sitemap').addEvent('click', function() {
			showSiteMap();
			return false;
		});
	}
	
	// sets the close button on the header popups 
	setHeaderPopupClose()
	
	if ($('mailing_list')) {
		$('mailing_list').addEvent('click', function() {
			closeHeaderPopups();
			$('mailing_list_popup').setStyle('display', 'block');
			return false;
		});
	}
	
	if ($('ops_login')) {
		$('ops_login').addEvent('click', function() {
			closeHeaderPopups()
			$('ops_popup').setStyle('display', 'block');
			return false;
		});
	}
	
	if ($('video_feeds')) {
		$('video_feeds').addEvent('click', function() {
			showVideoArchive()
			return false;
		});
	}
}

function setHeaderPopupClose() {
	$$('a.header_popup_close').each(function(item, index) {
		item.addEvent('click', function() {
			closeHeaderPopups();
			return false;
		});
	});
}

function closeHeaderPopups() {
	$$('div.header_popup').each(function(item, index) {
		item.setStyle('display', 'none');
	});
}

function video_archive_init() {
	$('items_container').scrollTo(0, 0);
	
	$('scroll_left').addEvent('click', function() {
		if ($('items_container').getScrollSize().x > 872) {
			if (($('items_container').getScroll().x - 872) <= 0) {
				$('scroll_left_img').src = 'images/left_arrow_off.gif';
			}
			if (($('items_container').getScroll().x) <= ($('items_container').getScrollSize().x + 1744)) {
				$('scroll_right_img').src = 'images/right_arrow.gif';
			}
			var x = $('items_container').getScroll().x - 872;
			var myFx = new Fx.Scroll('items_container', {
				duration: 500
			}).start(x, 0);
			return false;
		} else {
			return false;
		}
	});	
		
	$('scroll_right').addEvent('click', function() {
		if ($('items_container').getScrollSize().x > 872) {
			if (($('items_container').getScroll().x) >= ($('items_container').getScrollSize().x - 1744)) {
				$('scroll_right_img').src = 'images/right_arrow_off.gif';
			}
			if (($('items_container').getScroll().x + 872) != 0) {
				$('scroll_left_img').src = 'images/left_arrow.gif';
			}
			var x = $('items_container').getScroll().x + 872;
			var myFx = new Fx.Scroll('items_container', {
				duration: 500
			}).start(x, 0);
			return false;
		} else {
			return false;
		}
	});
}

function showSiteMap()
{
	showModal(800, '#fff');
	
	var loadModalContents = new Request({
		url: '/ajax-includes/sitemap.php',
		method: 'get',
		evalScripts: true,
		onSuccess: function(html) {
			$('modal_contents').set('text', '');
			$('modal_contents').innerHTML = html;
		},
		onFailure: function() {
			$('modal_contents').set('text', 'The request failed.');
		}
	});
	loadModalContents.send();
}


function showVideoArchive()
{
	slideInVideo();
	showModal(950, '#999');

	var loadModalContents = new Request({
		url: 'video_feeds.php',
		method: 'get',
		evalScripts: true,
		onSuccess: function(html) {
			$('modal_contents').set('text', '');
			$('modal_contents').innerHTML = html;

			video_archive_init();
		},
		onFailure: function() {
			$('modal_contents').set('text', 'The request failed.');
		}
	});
	loadModalContents.send();
}


function showVideoPlayer(video_file)
{
	videoPlaying = video_file;
	showModal(400, '#fff');
	$('modal_contents').innerHTML = '<div style="margin: 0px auto; width: 300px; height: 250px;"><div id="modal_video_player">&nbsp;</div></div>';
	
	var flashvars = {
		flvToPlay: "/uploads/"+video_file, 
		autoStart: "true",
		showScaleModes: "false",
		jsCallback: "true"
	};
	var params = {
		allowFullScreen: "true"
	};
	var attributes = {};
	
	swfobject.embedSWF("/cms/flash/flvPlayer.swf", "modal_video_player", "300", "225", "9.0.0","/flash/expressInstall.swf", flashvars, params, attributes);

	
}


function showAudioPlayer(audio_file)
{
	// this is for google analytics
	videoPlaying = audio_file;
	flvStart();
	
	showModal(400, '#fff');
	$('modal_contents').innerHTML = '<div style="padding-top: 50px; margin: 0px auto; width: 200px; height: 100px;"><div id="modal_audio_player">&nbsp;</div></div>';
	
	var flashvars = {
		mp3: "/uploads/"+audio_file,
		autoplay: '1'
		
	};
	var params = {};
	var attributes = {};
	
	swfobject.embedSWF("/cms/flash/player_mp3.swf", "modal_audio_player", "200", "20", "9.0.0","/flash/expressInstall.swf", flashvars, params, attributes);

	
}


function newsletterSignup(form_id,check_id,elem_id)
{
	if ($defined(check_id) && $(check_id).checked !== true)
	{
		$(check_id).getParent().getParent().setStyle('background-color','#FFA');
		if(elem_id){
			$(elem_id).setStyle('background-color','#FFA');			
		}
		return false;
	}
	else if($defined(check_id))
	{
		$(check_id).getParent().getParent().setStyle('background-color','transparent');
		if(elem_id){
			$(elem_id).setStyle('background-color','transparent');	
		}
	}

	showModal(950, '#FFF');

	var loadModalContents = new Request({
		url: '/ajax-includes/modal_iframe.php?iframe_height=675',
		method: 'get',
		evalScripts: true,
		onSuccess: function(html) {
			$('modal_contents').set('text', '');
			$('modal_contents').innerHTML = html;
			$(form_id).submit();
		},
		onFailure: function() {
			$('modal_contents').set('text', 'The request failed.');
		}
	});
	loadModalContents.post();
}


function positionFooter(domReady)
{
	
	// If the dom is not ready, don't resize or js cannotrecover
	if (domReady)
	{
	//$('footer').setStyle('margin-top', footerMargin);
	var height = window.getSize().y;
	var scrollHeight = window.getScrollSize().y;
	var contentHeight = $('page_container').getSize().y;

	//if (scrollHeight == height && contentHeight < height) {
		var footerMargin = height - contentHeight;
		$('footer').setStyle('margin-top', footerMargin);
	//}
	}
	else
	{
		//alert(domReady)
	}
	
}

// there was enough of a change from removing the sticky menus that i copied the function and changed it
// the original function still exists commented below. //>SA.
/* removed this function completely in favor of text/css based menu /*
function setRollovers(thisPage, thisSubPage)
{
	$$('a.main_nav').each(function(item, index) {
		if (item.id == 'nav_'+thisPage) {
			$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';

			if ($('sub_'+item.id)) {
				$('sub_'+item.id).setStyle('display', 'inline');
			}
		}

		item.addEvent('mouseover', function(){
			$$('a.main_nav').each(function(item, index) {
				$(item.id+'_img').src = 'images/nav/'+item.id+'.gif';
			});
			$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
		});
	});

	$('main_nav').addEvent('mouseout', function() {
		$$('a.main_nav').each(function(item, index) {
			if (item.id == 'nav_'+thisPage) {
				$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
			} else {
				$(item.id+'_img').src = 'images/nav/'+item.id+'.gif';
			}
		});
	});


	$$('a.sub_nav').each(function(item, index) {
		if (item.id == 'nav_'+thisSubPage) {
			$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
		}

		item.addEvent('mouseover', function(){
			$$('a.sub_nav').each(function(item, index) {
				$(item.id+'_img').src = 'images/nav/'+item.id+'.gif';
			});
			$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
		});
	});

	$('main_nav').addEvent('mouseout', function() {
		$$('a.sub_nav').each(function(item, index) {
			if (item.id == 'nav_'+thisSubPage) {
				$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
			} else {
				$(item.id+'_img').src = 'images/nav/'+item.id+'.gif';
			}
		});
	});
}

/*
function setRollovers(thisPage, thisSubPage)
{
	$$('a.main_nav').each(function(item, index) {
		if (item.id == 'nav_'+thisPage) {
			$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';

			if ($('sub_'+item.id)) {
				$('sub_'+item.id).setStyle('display', 'inline');
			}
		}

		item.addEvent('mouseenter', function(){
			$$('a.main_nav').each(function(item, index) {
				$(item.id+'_img').src = 'images/nav/'+item.id+'.gif';
				if ($('sub_'+item.id)) {
					$('sub_'+item.id).setStyle('display', 'none');
				}
			});
			$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
			if ($('sub_'+item.id)) {
				$('sub_'+item.id).setStyle('display', 'inline');
			}
		});
	});

	$('main_nav').addEvent('mouseleave', function() {
		$$('a.main_nav').each(function(item, index) {
			if (item.id == 'nav_'+thisPage) {
				$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
				if ($('sub_'+item.id)) {
					$('sub_'+item.id).setStyle('display', 'inline');
				}
			} else {
				$(item.id+'_img').src = 'images/nav/'+item.id+'.gif';
				if ($('sub_'+item.id)) {
					$('sub_'+item.id).setStyle('display', 'none');
				}
			}
		});
	});


	$$('a.sub_nav').each(function(item, index) {
		if (item.id == 'nav_'+thisSubPage) {
			$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
		}

		item.addEvent('mouseenter', function(){
			$$('a.sub_nav').each(function(item, index) {
				$(item.id+'_img').src = 'images/nav/'+item.id+'.gif';
			});
			$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
			if ($('sub_'+item.id)) {
				$('sub_'+item.id).setStyle('display', 'inline');
			}
		});
	});

	$('main_nav').addEvent('mouseleave', function() {
		$$('a.sub_nav').each(function(item, index) {
			if (item.id == 'nav_'+thisSubPage) {
				$(item.id+'_img').src = 'images/nav/'+item.id+'_on.gif';
			} else {
				$(item.id+'_img').src = 'images/nav/'+item.id+'.gif';
			}
		});
	});
}
*/

function alternate_rows(element_array)
{
	element_array.each(function(el)
	{
		var row_class = 'odd';
		el.getElements('tr').each(function(row,index)
		{
			if (row.className == '')
			{
				row.className = row_class;
			}
			if (row_class == "odd")
			{
				row_class = 'even';
			}
			else
			{
				row_class = 'odd';
			}
		});
	});
}

function showOverlay(layer)
{
	if (typeof(layer) == 'undefined')
	{
		var layer = '';
	}
	var zindex = 99999 + layer;
	var id = 'overlay'+layer;

	var overlay = new Element('div', {
		'id': id,
		'styles': {
			'position': 'absolute',
			'z-index': zindex,
			'top': 0,
			'left': 0,
			'opacity': 0,
			'display': 'none',
			'background-color': '#000',
			'width': window.getScrollWidth()+'px',
			'height': window.getScrollHeight()+'px'
		}
	}).injectInside(document.body);
	overlay.setStyle('display','block');
	overlay.tween('opacity', 0, .9);

	window.addEvent('scroll', function() {
		$(id).setStyles({
			'width': window.getScrollWidth()+'px',
			'height': window.getScrollHeight()+'px'
		});
	});
}

function hideOverlay(layer)
{
	if (typeof(layer) == 'undefined')
	{
		var layer = '';
	}
	$('overlay'+layer).dispose();
	if (layer == '')
	{
		window.removeEvents('scroll');
	}
}

function showModal(width, backgroundColor, layer)
{
	if(typeof(layer) == 'undefined')
	{
		var layer = '';
		var closeLink = 'Close <img src="images/close_btn.gif" alt="Close" />';
	}
	else
	{
		var closeLink = 'Back to Tutorial';
	}

	showOverlay(layer);

	var zindex = 100000 + layer;
	var popup = 'popup'+layer;

	var windowScroll = window.getScroll();
	var width = width;
	var left = ((window.getWidth() - width)/2).round();
	var top = windowScroll.y + 50;
	var popup = new Element('div', {
		'id': popup,
		'styles': {
			'position': 'absolute',
			'z-index': zindex,
			'top': top,
			'left': left,
			'width': width,
			'background-color': backgroundColor
		}
	}).injectInside(document.body);

	$(popup).innerHTML = '<div style="text-align: right; padding: 10px 15px; background: #fff;"><a href="#" onclick="javascript: hideModal('+layer+'); return false;">'+closeLink+'</a></div><div id="modal_content'+layer+'"><div id="modal_contents'+layer+'"></div></div>';
}

function hideModal(layer)
{
	if (typeof(layer) == 'undefined')
	{
		var layer = '';
	}
	$$('#modal_contents'+layer+' textarea.mceEditor').each(function(el){
		var myID = $(el).getProperty('id');
		toggleEditor(myID);
	});

	$('popup'+layer).dispose();
	hideOverlay(layer);
}


function printThis(url)
{
	if (typeof(url) == 'undefined')
	{
		var url = ''+window.location;
	}
	else
	{
		var url = 'print_modal.php?page='+url.substring(1);
	}

	if (url.contains('?'))
	{
		uri = url+'&mode=print';
	}
	else
	{
		uri = url+'?mode=print';
	}
	var printWindow = window.open(uri,'printThis');
}


function flvStart() {
	pageTracker._trackPageview(videoPlaying);
}

function flvEnd() {
	// this needs to be here or the video player will get a js error
}

