// JavaScript Document
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}


function reset() 
        {
              for (i=0; i<document.forms['aspnetForm'].length; i++)
              {
                    doc = document.forms['aspnetForm'].elements[i];
                    switch (doc.type.toLowerCase()) 
                    {
                          case "text" :
                                doc.value = "";
                                break;

                          case "textarea" :
                                doc.value = "";
                                break;
                          case "checkbox" :
                                doc.checked = false;
                                break;    
                          case "radio" :
                                doc.checked = false;
                                break;                
                          case "select-one" :
                                doc.options[0].selected = true;
                                break;                      
                          case "select-multiple" :
                                while (doc.selectedIndex != -1) 
                                {
                                      indx = doc.selectedIndex;
                                      doc.options[indx].selected = false;
                                }
                                doc.selected = false;
                                break;
                                    
                          default :
                                break;
                    }
              }
        }


function IEHoverPseudo() { // This function makes CSS menus work in IE 6
	var navItems = document.getElementById("primary-nav").getElementsByTagName("li");
	for (var i=0; i<navItems.length; i++) {
		if(navItems[i].className == "menuparent") {
			navItems[i].onmouseover=function() { this.className += " over"; }
			navItems[i].onmouseout=function() { this.className = "menuparent"; }
		}
	}
}
function getSeason() { // For the rotating header background image based on the season (according to the user's desktop clock)
	var now = new Date();
	var month = now.getMonth() + 1;
	var date = now.getDate();
	var year = now.getYear();
	var season;
	if (month > 1 && month <= 3) season = "winter";
	if (month == 3 && date > 19) season = "spring";
	if (month > 3 && month <= 6) season = "spring";
	if (month == 6 && date > 20) season = "summer";
	if (month > 6 && month <= 9) season = "summer";
	if (month == 9 && date > 21) season = "fall";
	if (month > 9 && month <= 12) season = "fall";
	if (month == 12 && date > 20) season = "winter";
	return season;
}

// HOME MAKEOVERS FUNCTIONS
function switchImage(imageURL) {
	$('#photoBig').attr('src',imageURL)
}

function getSWF(movieName) { // FLash ExternalInterface - Get the flash object from the DOM
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return document[movieName];
	} else {
		if(document[movieName].length != undefined){
			return document[movieName][1];
		}
		return document[movieName];
	}
}
function callAS(flvClip) { // FLash ExternalInterface Call
	movie = getSWF('video_player');
	movie.playVideo(flvClip);
}


//MAIN JQUERY ROUTINE
$(document).ready(function(){
	var season = getSeason();
	$('#headerRow').addClass(season);
	$('#toolNav').addClass(season);
	$('#breadCrumbNavigation ul li:last a').css('background-image','none');
	$('div.alertBox *:last,div.editorialBox *:last').css({'margin-bottom':'0'})
	$('#typeSizeTopNav a').click(function(event){
		event.preventDefault();
		if (getActiveStyleSheet()!='largetype') {;
			setActiveStyleSheet('largetype');
		} else {
			setActiveStyleSheet('standard');
		}
	});
	$('#mainContent input:text, #mainContent input:password').css('border','solid 1px #ccc'); // Make input fields standardized look of border.
	$('input:radio, input:checkbox').css('vertical-align','text-bottom');
	$('a').tooltip({
		delay: 0, 
    	showURL: false,
		track: true
	});
	
	// Default Text in Fields
	$('#searchInput').focus(function() {
		if ($(this).val()=='Search this site') {
			$(this).val('');
			$(this).css('color','#000');
		}
	});
	$('#searchInput').blur(function(){
		if ($(this).val()=='') {
			$(this).val('Search this site');
			$(this).css('color','#999');
		};
	});
	
	// News Section Row highlighting
	$('body.news #mainContent table tbody a').hover(
		function(){
			$row = $(this).parent().parent(); // The containing <tr> tag
			$row.css('background-color','#f0f0f0');
			$('a',$row).css('color','#c03');
		},function(){
			$row = $(this).parent().parent();
			$row.css('background-color','transparent');
			$('a',$row).css('color','#999');
		}
	);
	
	//FAQ Widgets
	$('.faqAnswer:visible').hide(); // Hide everything initially
	$('.faqQuestion a').click(function(event){ // a.boxlink is clicked
		event.preventDefault(); // Stop the link from looking for a URL
		$faq = $(this).parents('.faq');
		$answer = $('.faqAnswer',$($faq));
		$question = $('.faqQuestion a');
		if ($($answer).is(':hidden')) {
			$(this).addClass('isOn'); 
			$($faq).css('background-color','#fbf9e4');
			$($answer).animate({'opacity':'show','height':'show'},'slow');
		} else {
			$(this).removeClass('isOn'); 
			$($faq).css('background-color','#fff');
			$($answer).animate({'opacity':'hide','height':'hide'},'slow');
		}
	});
	
	
	// Photo Gallery Widget - Begin
	$('.photoList li').hide();
	$('.photoList').each(function(){
		var photo_count = $('li',$(this)).size();
		$('li',$(this)).each(function(i){
			var newParagraph = document.createElement('p');
			var $newParagraph = $(newParagraph);
			$newParagraph.addClass('photoSubNav');
			if (i+1 > 1) {
				var newPrevLink = document.createElement('a');
				var $newPrevLink = $(newPrevLink);
				$newPrevLink.attr({'href':'#','title':'Previous Photo'}).addClass('prevLink').html('<img src="../../../assets/images/arrow_LeftPhoto.gif" alt="Previous Photo">');
				$newPrevLink.click(function(event){
					event.preventDefault();
					switchImage($(this).parent().parent().prev().attr('title'));
					$('.photoList li').hide();
					$(this).parent().parent().prev().show();
				});
			$newParagraph.append($newPrevLink);
			}
			if (i+1 < photo_count) {
				var newNextLink = document.createElement('a');
				var $newNextLink = $(newNextLink);
				$newNextLink.attr({'href':'#','title':'Next Photo'}).addClass('nextLink').html('<img src="../../../assets/images/arrow_RightPhoto.gif" alt="Next Photo">');
				$newNextLink.click(function(event){
					event.preventDefault();
					switchImage($(this).parent().parent().next().attr('title'));
					$('.photoList li').hide();
					$(this).parent().parent().next().show();
				});
				$newParagraph.append($newNextLink);
			}
			for (z=0;z<photo_count;z++) {
				if (z!=i) {
					var newlink = document.createElement('a');
					var $newlink = $(newlink);
					$newlink.attr('href',z);
					$newlink.text(z+1);
					$newlink.click(function(event){
						event.preventDefault();
						var showPhoto = parseInt($(this).attr('href'));
						var $thisGallery = $(this).parent().parent().parent();
						switchImage($('li',$thisGallery).eq(showPhoto).attr('title'));
						$('.photoList li').hide();
						$('li',$thisGallery).eq(showPhoto).show();
					});
				} else {
					var newlink = document.createElement('b');
					var $newlink = $(newlink);
					$newlink.text(z+1);
				}
				$newParagraph.append($newlink);
				$newParagraph.append(' ');
			}
			$(this).prepend($newParagraph);
		});
	});
	switchImage($('.photoList:first li:first').attr('title'));
	$('.photoList:first li:first').show();
	$('.photoGalleryThumbs a').click(function(event){
		event.preventDefault();
		$('.photoGalleryThumbs a.selected').removeClass('selected');
		$(this).addClass('selected');
		gallery = $(this).attr('href');
		$('.photoList li').hide();
		section = gallery + ' li';
		$(section).eq(0).show();
		switchImage($(section).eq(0).attr('title'));
	});
	$('.photoGalleryThumbs a:first').addClass('selected');
	// Photo Gallery Widget - End
	
	// Video Gallery Widget - Begin
	$('.videoGalleryThumbs a').click(function(event){
		event.preventDefault();
		$('.videoGalleryThumbs a.selected').removeClass('selected');
		$(this).addClass('selected');
		video = $(this).attr('href');
		callAS(video);
	});
	$('.videoGalleryThumbs a:first').addClass('selected');
	// Video Gallery Widget - End
	
	// MY PROFILE dynamic form editing - Begin
	$('.infoEditGroup a.editInfo').click(function(event){
		event.preventDefault();
		$('.editRow:visible').hide();
		$('.infoRow .editInfo').show();
		$('.infoRow').css({
			'background-color':'transparent',
			'border-color':'#ccc'
		});
		$('.editRow',$(this).parents('.infoEditGroup')).show();
		$(this).parents('.infoRow').css({
			'background-color':'#ececec',
			'border-color':'#fbf9e4'
		});
		$(this).hide();
	});
	$('.infoEditGroup a.cancel').click(function(event){
		event.preventDefault();
		$('.infoRow .editInfo',$(this).parents('.infoEditGroup')).show();
		$('.infoRow').css({
			'background-color':'transparent',
			'border-color':'#ccc'
		});
		$('.editRow:visible').hide();
	});
	// MY PROFILE dynamic form editing - End
	
	
	// Jump Menu Selection Boxes
	$('select.jumpmenu').change(function(){
		if ($(this).val() != "#") {
			document.location.href = $(this).val();
		}
	});
	

	//Email This Page
	function validateEmailToFriend($theForm) {
		var error = "";
		$('input',$theForm).each(function(){
			if (!$(this).val()) {
				$(this).css('border-color','#c00');	
				error = "Please fill out the indicated fields.";
			} else {
				$(this).css('border-color','#ccc');	
			}
		});
		/* These Validation - THESE LINES ARE NEW */
		if (($('#friendEmail',$theForm).val().indexOf('@')<0 || $('#friendEmail',$theForm).val().indexOf('.')<0) && ($('#yourEmail',$theForm).val().indexOf('.')<0 || $('#yourEmail',$theForm).val().indexOf('@')<0)) {
			error += "\nPlease enter a valid e-mail address for both addresses";
		} else if ($('#friendEmail',$theForm).val().indexOf('@')<0 || $('#friendEmail',$theForm).val().indexOf('.')<0) {
			error += "\nPlease enter a valid e-mail address for your friend.";
		} else if ($('#yourEmail',$theForm).val().indexOf('.')<0 || $('#yourEmail',$theForm).val().indexOf('@')<0) {
error += "\nPlease enter a valid e-mail address for yourself.";
}
		/* NEW LINES END */
		if (error) {
			alert(error);
			return;
		} else {
			var yourName = $('#yourName',$theForm).val();
			var yourEmail = $('#yourEmail',$theForm).val();
			var friendName = $('#friendName',$theForm).val();
			var friendEmail = $('#friendEmail',$theForm).val();
			var url = document.location.href;


			var sendSuccess = SendPageToMail(yourName, yourEmail, friendName, friendEmail, url); 
			
			if (sendSuccess == true)
			{
				$('#email-div').empty();
				$('#email-div').animate({'height':'78px'},'normal',function(){
				$('#email-div').html('<form><p><b class="red">Your message has been sent.</b><br /><a href="#" class="btn" id="email-cancel">Close</a></p></form>');
						$('#email-cancel').click(function(event){
							event.preventDefault();
							$('#email-div').remove();
						});
					});
			}

			/* var dataString = 'yourName='+ yourName + '&yourEmail=' + yourEmail + '&friendName=' + friendName  + '&friendEmail=' + friendEmail + '&url=' + url; 
			 $.ajax({  
				type: "POST",  
				url: "/ComEd/site/emailToFriend.php",  
				data: dataString,  
				success: function() {  
					$('#email-div').empty();
					$('#email-div').animate({'height':'78px'},'normal',function(){
						$('#email-div').html('<form><p><b class="red">Your message has been sent.</b><br /><a href="#" class="btn" id="email-cancel">Close</a></p></form>');
						$('#email-cancel').click(function(event){
							event.preventDefault();
							$('#email-div').remove();
						});
					});
				}  
			}); */
		}
	}
	$('#emailPageTopNav a').click(function(event){
		event.preventDefault();
		/*
		$('body').prepend('<div id="email-div">Hello</div>');
		$('#email-div').html('<form name="email-form" id="email-form" method="post" action="email_confirmation.html"><h3>Email this page to a friend</h3><label>Your Name<br /><input type="text" name="yourName" id="yourName"  width="130px" /></label><label>Your Email Address<br /><input type="text" name="yourEmail" id="yourEmail" /></label><label>Your Friend\'s Name<br /><input type="text" name="friendName" id="friendName" /></label><label>Your Friend\'s Email<br /><input type="text" name="friendEmail" id="friendEmail" /></label><label style="margin-top:15px;"><a href="#" class="btn" id="email-submit">Send</a> <a href="#" class="btn gray" id="email-cancel">Cancel</a></label></form>');
		$('#email-submit').click(function(event){
			event.preventDefault();
			validateEmailToFriend($('#email-form'));
		});
		$('#email-cancel').click(function(event){
			event.preventDefault();
			$('#email-div').remove();
		}); */
	});
});