// JavaScript Document
/* MeridianFootball.com common */
var google_api_key = "ABQIAAAAG8JdcpaJoCKNRhchEUKjMhTVC14RHcAjEydJ6hViuNcPuh-9TxRjP8VOUvC2tbkKdXZQWD-VFMAKWw";

function include(destination) {
	var e=window.document.createElement('script');
	e.setAttribute('src',destination);
	e.setAttribute('type','text/javascript');
	window.document.body.appendChild(e);
}

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function setCookieV2( name, value, expires, path, domain, secure ){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function getCookie(c_name){
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function getCookieV2( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

var wait_img = new Image();
wait_img.src = 'http://www.meridianfootball.com/images/ajax-loader.gif';
var splash_overlay;

//Splash movie opener 
function openSplashAjax(url){
	var html = "<div style='margin:0px auto' id='overlay_wait'><img src='http://www.meridianfootball.com/images/ajax-loader.gif' border=0 /></div>";
	overlay = new Overlay().show(html, 
						{ position : 'center',
						cls: 'overlay_wait',
						modal : true,
						animate : false,
						opacity : 1,
						click_hide : true,
						auto_hide : false,
						bckg_opacity : 0.7 });

	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
		  openSplash(transport.responseText);
		  //alert(transport.responseText);
	  }
	});
	
}

function openSplash(s){
	Overlay.hide( $('overlay_wait') );
	
	var html = "<div id='closer' onClick=\"Overlay.hide();\" style='text-decoration: underline; font-weight: bold; text-align: right; cursor: pointer;'>Close</div>";
	overlay = new Overlay().show(html + s, 
						{ position : 'center',
						cls: 'overlay_content_splash',
						modal : true,
						animate : true,
						opacity : 1,
						click_hide : true,
						auto_hide : false,
						bckg_opacity : 0.7 });
}


/* Recent activity functions */
var activity_data;
var activity_init = false;

function activityAjax(){
	activityCreateMenu();
	var html = "<div id='activity_loader'><center><img src='/images/ajax-loader-square-1.gif' /></center></div>";
	$('activity_menu').update(html);

	new Ajax.Request("http://www.meridianfootball.com/ajax/activity.php", {
		  method: 'get',
		  onSuccess: function(transport) {
			 if(! activity_init){	
				  activity_data = transport.responseText.evalJSON();
				  activity_init = true;
				activityFillMenu();
			 } else {
				activityFillMenu();
			}
		  }
		});
}

function activityCreateMenu(){
	var src_height = 51;
	var src_width = 34;
	var dest_width = 200;
	var off_left = -125;
	
	var div = Element('div',{id:'activity_menu'}); 
	
	// insert it in the document
	document.body.appendChild(div);
	Element.clonePosition('activity_menu', 'activity_menu_item', {setLeft: true, setTop: true, setWidth: false, setHeight: false, offsetTop: src_height, offsetLeft: off_left });
	
}

function activityFillMenu(){
	var templ = new Template("<li><span class='activity_name'>#{short_name}</span><br/><span class='activity_title'><a href='#{link}' target='activity'>#{title}</a></span><br/><span class='activity_date'>#{date}</span></li>");
	
	var html = "<div id='activity_menu_inner'><ul id='activity_menu_list'>";
	
	for(var i = 0; i < activity_data.length; i++){
		html += templ.evaluate(activity_data[i]);
	}
	
	html += "</ul></div>";
	
	$('activity_menu').update(html);
	
	Event.observe('activity_menu', 'mouseleave', activityHide);
//	Event.observe('activity_menu_list', 'mouseout', activityHide);
	$('activity_menu').show();
}

function activityHide(){
	$('activity_menu').remove();
}

function activityOpenMenu(){
	
}

/* End activity functions */

/* Newsletter Subscribe */
var subscribe_html;
var subscribe_overlay;

function subscribeAjax(){
	new Ajax.Request("http://www.meridianfootball.com/shared/html/lists_signup.html", {
		  method: 'get',
		  onSuccess: function(transport) {
			  subscribe_html = transport.responseText;
			  subscribeOpenForm(subscribe_html);
		  }
		});
}

function subscribeOpenForm(s){
	
	var html = "<div id='closer' onClick=\"Overlay.hide('phplist_subscribe');\" style='text-decoration: underline; font-weight: bold; text-align: right; cursor: pointer;'>Close</div>";
	subscribe_overlay = new Overlay().show(html + s, 
						{ position : 'center',
						cls: 'overlay_content',
						modal : true,
						animate : true,
						opacity : 1,
						click_hide : false,
						auto_hide : false,
						bckg_opacity : 0.7 });
}


	/* PHP Lists Code */
				function checkform() {
			  for (i=0;i<fieldstocheck.length;i++) {
				if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].type") == "checkbox") {
				  if (document.subscribeform.elements[fieldstocheck[i]].checked) {
				  } else {
					alert("Please enter your "+fieldnames[i]);
					eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
					return false;
				  }
				}
				else {
				  if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value") == "") {
					alert("Please enter your "+fieldnames[i]);
					eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
					return false;
				  }
				}
			  }
			  for (i=0;i<groupstocheck.length;i++) {
				if (!checkGroup(groupstocheck[i],groupnames[i])) {
				  return false;
				}
			  }
			  
			  if(! compareEmail())
			  {
				alert("Email Addresses you entered do not match");
				return false;
			  }
			  return true;
			}
			
			var fieldstocheck = new Array();
			var fieldnames = new Array();
			function addFieldToCheck(value,name) {
			  fieldstocheck[fieldstocheck.length] = value;
			  fieldnames[fieldnames.length] = name;
			}
			var groupstocheck = new Array();
			var groupnames = new Array();
			function addGroupToCheck(value,name) {
			  groupstocheck[groupstocheck.length] = value;
			  groupnames[groupnames.length] = name;
			}
			
			function compareEmail()
			{
			  return (document.subscribeform.elements["email"].value == document.subscribeform.elements["emailconfirm"].value);
			}
			function checkGroup(name,value) {
			  option = -1;
			  for (i=0;i<document.subscribeform.elements[name].length;i++) {
				if (document.subscribeform.elements[name][i].checked) {
				  option = i;
				}
			  }
			  if (option == -1) {
				alert ("Please enter your "+value);
				return false;
			  }
			  return true;
			}
	
	/* End PHP Lists Code */

/* End newsletter subscribe */
