// JavaScript Document


//This is used to switch the background images when subnav content changes
var image_timeout_id;
var special_box = null;

//Need these to execute ASAP
YAHOO.util.Event.onAvailable('content_slider', function(){
		YAHOO.util.Dom.setStyle('content_slider', 'width', '0' );
																												});
YAHOO.util.Event.onContentReady('sub_nav_wrapper',function(){
																							if(sub_navs_count > 0){
		var subnavs = YAHOO.util.Dom.getElementsByClassName('sub_nav')
		YAHOO.util.Dom.setStyle(subnavs, 'top', '-42px');
		var overlays = YAHOO.util.Dom.getElementsByClassName('overlay');
		YAHOO.util.Dom.setStyle(overlays, 'top', '42px');
	}
																						});
YAHOO.util.Event.onAvailable('content_background', function(){
		YAHOO.util.Dom.setStyle('content_background', 'opacity', '.5');
																												});
YAHOO.util.Event.onAvailable('content', function(){
		YAHOO.util.Dom.setStyle('content', 'opacity', '0');	
																												});
YAHOO.util.Event.onAvailable('special_area', function(){
																									document.getElementById('special_area').setAttribute('href', 'javascript:void(0)');
																											});
																																			


YAHOO.util.Event.onDOMReady(function(){	
																		 
																		 	var els = YAHOO.util.Dom.getElementsByClassName('js_remove_link', 'a');
																			for(i=0; i<els.length; i++){
																					els[i].removeAttribute('href');
																			}
																		// var myLogReader = new YAHOO.widget.LogReader();
																		 	if(document.getElementById("content_image_top").complete){
																				window.setTimeout(function(){init();}, 500);
																			}
																			else{
																		 		document.getElementById("content_image_top").onload = window.setTimeout(function(){init();}, 500);
																			}
																		 
																		 });

//window.onload = 	init;

window.setTimeout(function(){
												loadMap();
														 }, 4000);
window.setTimeout(function(){
												special_box = new YAHOO.widget.Panel("special_overlay", { context:["main_content", "tl","tl", ["beforeShow", "windowResize"]],
																						 								visible:false,
																														modal:true,
																														zIndex:20,
																														effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}});
												special_box.render();
														 }, 1000);			


function init(){
	

	document.getElementById("content_image_top").onload = null;

	//YAHOO.util.Dom.setStyle('content_slider', 'width', '0' );
//	YAHOO.util.Dom.setStyle('content_background', 'opacity', '.5');
	//YAHOO.util.Dom.setStyle('content', 'opacity', '0');	
	//imageObj.onLoad(function(){alert "here";});
	

	
	
	var links = YAHOO.util.Dom.getElementsByClassName('no_script', 'a');
	
	for(i = 0; i<links.length; i++){
		links[i].removeAttribute('href');
	}




	if(sub_navs_count > 0){
		var subnavs = YAHOO.util.Dom.getElementsByClassName('sub_nav')
		YAHOO.util.Dom.setStyle(subnavs, 'top', '-42px');
		var overlays = YAHOO.util.Dom.getElementsByClassName('overlay');
		YAHOO.util.Dom.setStyle(overlays, 'top', '42px');
	}

	 
	/*imageObj = new Image();*
	imageObj.src = '/common/images/content/saskatoon/'+content_images[section][0];*/
	
	if(content_images[section].length > 1){
		window.setTimeout(function(){
														 changeImage(section,1);
														 }, 4000);
	}
	
	

	//window.setTimeout(init_effects, 2000);

/*
function init_effects(){
	*/
	var attributes = { width: {to : 330} };
	
	//var anim = new YAHOO.util.Anim('content_slider', attributes, 1.5, YAHOO.util.Easing.easeOut);
	var anim = new YAHOO.util.Anim('content_slider', attributes, 74, YAHOO.util.Easing.easeOut);
	anim.useSeconds =false;
	var fad_attrs = {opacity: {to : 1} };
	var fade = new YAHOO.util.Anim('content', fad_attrs, 70);
	fade.useSeconds = false;
	if(sub_navs_count >0){
		fade.onComplete.subscribe(function(){
																			 droptabs();
																			 });
	}
	anim.onComplete.subscribe(function(){
																		 fade.animate();
																		 });
	 anim.animate();

	
}






function droptabs(){
	var i;
	var overlay_anim0 = new Array();
	var anim = new Array();
	for(i = sub_navs_count; i >= 1 ; i--){	

		var overlay = YAHOO.util.Dom.getElementsByClassName('overlay', 'div', 'sub_nav_'+i);
		var overlay_att = { top: {by: -42} }; 
		
		//overlay_anim[i] = new YAHOO.util.Anim(overlay[1], overlay_att, 0.3, YAHOO.util.Easing.easeOut);
		
		var temp = new YAHOO.util.Anim(overlay[0], overlay_att, 0.3, YAHOO.util.Easing.easeOut);
		overlay_anim0.push(temp);
		overlay_anim0.push(new YAHOO.util.Anim(overlay[1], overlay_att, 0.3, YAHOO.util.Easing.easeOut) );
		overlay_anim0.push(new YAHOO.util.Anim(overlay[2], overlay_att, 0.3, YAHOO.util.Easing.easeOut) );
		

		var attributes = { top: {by : 42} };
		anim.push( new YAHOO.util.Anim('sub_nav_'+i, attributes, 0.3,YAHOO.util.Easing.easeOut));
		
		if(i != sub_navs_count){
			
			temp.onComplete.subscribe(function(){overlay_anim0.pop().animate();
																																			 });
			temp.onComplete.subscribe(function(){overlay_anim0.pop().animate();
																																			 });
			temp.onComplete.subscribe(function(){overlay_anim0.pop().animate();
																																			 });
			temp.onComplete.subscribe(function(){anim.pop().animate();
																																			 });



		}
	}

	overlay_anim0.pop().animate()
	overlay_anim0.pop().animate()
	overlay_anim0.pop().animate()
	anim.pop().animate();

}




function toggleHeader(section){
	
	if(section == "maps"){
		var height = 315;
	}
	else if(section == "contact"){
		var height = 260;
	}
	else if(section == "careers"){
		var height = 270;
	}
	else if(section == "community"){
		var height = 150;
	}	
	
	var current_height = YAHOO.util.Dom.getStyle('header_content', 'height');
	if(current_height == '0px'){
		YAHOO.util.Dom.addClass(section, 'selected');
		
		var attributes = { height: {to : height} };

		
		var anim = new YAHOO.util.Anim('header_content', attributes, 0.8, YAHOO.util.Easing.easeOut);
		anim.animate();
		YAHOO.util.Dom.removeClass(section+'_content', 'hidden');
	}
	else{
		
		if(YAHOO.util.Dom.hasClass(section, 'selected')){
			var attributes = { height: {to : 0} };
			YAHOO.util.Dom.removeClass(section, 'selected');
			
			var anim = new YAHOO.util.Anim('header_content', attributes, 0.8, YAHOO.util.Easing.easeOut);
			anim.onComplete.subscribe(function(){
																		 YAHOO.util.Dom.addClass(section+'_content', 'hidden');
																		 });
			anim.animate();
		}
		else{
			var attributes = { height: {to : height} };
			var anim = new YAHOO.util.Anim('header_content', attributes, 0.8, YAHOO.util.Easing.easeOut);
			anim.animate();
			
			YAHOO.util.Dom.removeClass('contact', 'selected');
			YAHOO.util.Dom.removeClass('maps', 'selected');
			YAHOO.util.Dom.removeClass('careers', 'selected');
			YAHOO.util.Dom.removeClass('community', 'selected');
			YAHOO.util.Dom.addClass(section, 'selected');
			YAHOO.util.Dom.addClass('contact_content', 'hidden');
			YAHOO.util.Dom.addClass('maps_content', 'hidden');
			YAHOO.util.Dom.addClass('careers_content', 'hidden');
			YAHOO.util.Dom.addClass('community_content', 'hidden');
			YAHOO.util.Dom.removeClass(section+'_content', 'hidden');
		}
	}
}




/*int img_count =  the next index of background images
	cycles through the various background images
*/
function changeImage(section, img_count){
	
	var bottom_img = document.getElementById('content_image_bottom');
	bottom_img.src = "/common/images/content/"+cur_location+"/"+content_images[section][img_count];

	var attributes = { opacity: {to : 0} };
	var anim = new YAHOO.util.Anim('content_image_top', attributes, 1.5);
	
	anim.onComplete.subscribe(function(){
			 var top_img = document.getElementById('content_image_top');
				top_img.src = bottom_img.src;
				
				window.setTimeout(function(){
					YAHOO.util.Dom.setStyle(top_img, 'opacity', '1');
				}, 100);
	});
	
	bottom_img.onload =function(){	anim.animate(); };

	if(img_count+1 >= content_images[section].length){
		img_count = 0;
	}
	else{
		img_count ++;
	}
	image_timeout_id = window.setTimeout(function(){
														 changeImage(section, img_count);
														 }, 4500);
}

function contentSwitch(num){
	
	var tabs = YAHOO.util.Dom.getElementsByClassName("sub_nav");
	YAHOO.util.Dom.removeClass(tabs, 'selected');
	

	var tab = document.getElementById("sub_nav_"+(num+1));
	YAHOO.util.Dom.addClass(tab, 'selected');
	document.getElementById("content").innerHTML = document.getElementById("content_"+num).innerHTML;

	if(typeof(content_images[num+1]) != "undefined"){
		
		window.clearTimeout(image_timeout_id);
		changeImage(num+1, 0);
	}

}
function loadMap(){	
      if (GBrowserIsCompatible()) {
				if(cur_location == "saskatoon"){
			
					var opts = {size: new GSize(433,270)}
					var map = new GMap2(document.getElementById("map"), opts);
					map.setCenter(new GLatLng(52.1371809, -106.670214), 15);
				
					var redIcon = new GIcon(G_DEFAULT_ICON);
								
					// Set up our GMarkerOptions object
					markerOptions = { icon:redIcon,
														title:'Saskatoon Ramada'};
	
	
					map.addControl(new GSmallMapControl());
	
					var point = new GLatLng(52.1371809, -106.670214);
					map.addOverlay(new GMarker(point, markerOptions));

				}
				else{
			
					var opts = {size: new GSize(433,270)}
					var map = new GMap2(document.getElementById("map"), opts);
					map.setCenter(new GLatLng(50.447224, -104.6067788), 15);
				
					var redIcon = new GIcon(G_DEFAULT_ICON);
					
								
					// Set up our GMarkerOptions object
					markerOptions = { icon:redIcon,
														title:'Saskatoon Ramada'};
	
					map.addControl(new GSmallMapControl());
	
					var point = new GLatLng(50.447224, -104.6067788);
					map.addOverlay(new GMarker(point, markerOptions));
				}
			}
	
}
	
function openSpecials(){ 
	
	
	if(special_box == null){
		return false;
	}
	special_box.show();
	
	
}
	
	
function validate_contact_form(thisform){
 var valid = true;
 var err_msg = document.getElementById("contact_err");
 err_msg.innerHTML = "";
 var els = YAHOO.util.Dom.getElementsByClassName("error");
 YAHOO.util.Dom.removeClass(els, "error");
	with(thisform){
		if(contact_name.value == null || contact_name.value==""){
			YAHOO.util.Dom.addClass("contact_name", "error");
			valid = false;
			err_msg.innerHTML = "Please fill in the required (*) fields.";
		}
		if(contact_comments.value == null || contact_comments.value==""){
			YAHOO.util.Dom.addClass("contact_comments", "error");
			valid = false;
			err_msg.innerHTML = "Please fill in the required (*) fields.";
		}
		if(contact_email.value != null && contact_email.value != ""){
			apos=contact_email.value.indexOf("@");
			dotpos=contact_email.value.lastIndexOf(".");
			if (apos<1||dotpos-apos<2) {
				valid = false;
				YAHOO.util.Dom.addClass("contact_email", "error");
				err_msg.innerHTML += "<br />Invalid email address.";
			}
		}
	}


	if(valid == false){
		return false;
	}
	
	return true;
}
	
function validate_careers_form(thisform){
 var valid = true;
 var err_msg = document.getElementById("careers_err");
 err_msg.innerHTML = "";
 var els = YAHOO.util.Dom.getElementsByClassName("error");
 YAHOO.util.Dom.removeClass(els, "error");
	with(thisform){
		if(careers_name.value == null || careers_name.value==""){
			YAHOO.util.Dom.addClass("careers_name", "error");
			valid = false;
			err_msg.innerHTML = "Please fill in the required (*) fields.";
		}
	/*	if(careers_comments.value == null || careers_comments.value==""){
			YAHOO.util.Dom.addClass("careers_comments", "error");
			valid = false;
			err_msg.innerHTML = "Please fill in the required (*) fields.";
		}*/
		if(file_browser.value == null || file_browser.value == ""){
			YAHOO.util.Dom.addClass("file_upload", "error");
			valid = false;
			err_msg.innerHTML = "Please fill in the required (*) fields.";
		}
		else{
			if(file_browser.value.lastIndexOf(".doc") == -1 && file_browser.value.lastIndexOf(".pdf") == -1){
				YAHOO.util.Dom.addClass("file_upload", "error");
				valid = false;
				err_msg.innerHTML += "<br />Invalid file type.";
			}
		}
		if(careers_email.value != null && careers_email.value != ""){
			apos=careers_email.value.indexOf("@");
			dotpos=careers_email.value.lastIndexOf(".");
			if (apos<1||dotpos-apos<2) {
				valid = false;
				YAHOO.util.Dom.addClass("careers_email", "error");
				err_msg.innerHTML += "<br />Invalid email address.";
			}
		}

	}


	if(valid == false){
		return false;
	}
	
	return true;
}	
	
	
	
function validate_email(field,alerttxt){
	with (field){
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
 			 {alert(alerttxt);return false;}
		else {return true;}
	}
}	
