$(document).ready(function() {
/* ---- 
All links on page open in new window if not internal
------*/
$('a').live('mouseup', function(){
		var fullUrl = $(this).attr('href');
		var splitUrl = fullUrl.split("/");
		
		if((splitUrl[2] != 'www.nenetwork.org') && (splitUrl[2] != 'nenetwork.org') && (splitUrl[2] != undefined)){
				$(this).attr("target","_blank");
				//If listview page
				pageUrl = window.location.href;
				thePage = pageUrl.split('/');
				theLength = thePage.length;
				if(thePage[theLength-1] == 'list_view.php?all=list'){
					window.open(fullUrl);
				}
		}	
	});


/*---------
All submenus need their margins brought up to 
compensate for padding by the nested lists
---------*/
numSub = $('.submenu').size();
for(i=0;i<=numSub-1;i++){
	numChild = $('.submenu:eq('+i+')').children('ul').children().size();
	$('.submenu:eq('+i+')').css({'margin-bottom':numChild*(-27)});
}
/*


/*-----
Cookie script modified off of one found at 
http://scripts.franciscocharrua.com/javascript-cookies.php
-----*/

function setCookie(name, value){
  if(name != ''){document.cookie = name + '=' + value;}
}
function getCookie(name){
 //Without this, it will return the first value 
 //in document.cookie when name is the empty string.
 if(name == '')
   return('');
         
  name_index = document.cookie.indexOf(name + '=');
         
  if(name_index == -1)
  return('');
         
  cookie_value =  document.cookie.substr(name_index + name.length + 1, document.cookie.length);
         
 //All cookie name-value pairs end with a semi-colon, except the last one.
  end_of_cookie = cookie_value.indexOf(';');
  if(end_of_cookie != -1)
     cookie_value = cookie_value.substr(0, end_of_cookie);

  //Restores all the blank spaces.
  space = cookie_value.indexOf('+');
  while(space != -1)
   { 
    cookie_value = cookie_value.substr(0, space) + ' ' + 
    cookie_value.substr(space + 1, cookie_value.length);
							 
    space = cookie_value.indexOf('+');
   }

   return(cookie_value);
}   
function clearCookie(name){                  
  expires = new Date();
  expires.setYear(expires.getYear() - 1);

  document.cookie = name + '=null' + '; expires=' + expires; 		 
}
          
          
          	
/*---------
 Menu help box
---------*/	
	$("#links li a").mouseover(function(e){
		var linkTitle = $(this).attr("title");
		switch(linkTitle)
			{
			case "add_location":
			  instruct = "Adding a location to the map is as easy as clicking here!";
			  break;
			case "edit_location":
			  instruct = "Edit/delete the text and location of any previously added locations.";
			  break;
			case "add_user":
			  instruct = "Add a user to work on the project.";
			  break;
			case "edit_user":
			  instruct = "Edit/delete an existing user.";
			  break;
			case "backup":
			  instruct = "Backup the locations into a file to e-mail your web developer.";
			  break;
			case "icons":
			  instruct = "Choose which icons to use for each type of location.";
			  break;
			case "approve_comments":
				instruct = "Approve all the comments that were made since you last logged in.";
				break;
			case "add_category":
				instruct = "Add categories used to tag opportunities.";
				break;
			case "edit_category":
				instruct = "Edit categories used to tag opportunities.";
				break;
			/*case "view_all_map":
				instruct = "View all of the opportunities on the map.";
				break;
			case "view_list":
				instruct = "View all of the opportunities in a list.";
				break;
			case "view_categories":
				instruct = "View all of the categories to search on.";
				break;*/
			default:
				instruct = 0;
			  break;
			}
			if(instruct!= 0){
				$("#helpBox").html(instruct).css({'left':e.pageX+50,'top':e.pageY}).fadeIn("fast");		
			}
			else{
				$('#helpBox').fadeOut('fast');
			}
	}).mouseout(function(){
			
	});
	
	//Hide helpbox if not hidden properly
	$("#content,#content-home,#helpBoxBox").mouseover(function(){
		$("#helpBox").hide();
	});
	
	
/*---------
 Home page helpbox
---------*/		
var showTips = getCookie('showInfo');

if(showTips != '0'){
	
	//cycle through tips
	$('#helpBoxHome').cycle({ 
    fx:     'scrollUp', 
    timeout: 6000, 
    delay:  -2000 
});
}
	
if(showTips == '0'){
	$("#showInfo").attr('checked', true);
	$("#helpBoxBox a").hide();
}

/*
var i = 0;
$("#helpBoxBox a").click(function(){
	var nextTip = $(this).attr("title");
	if(nextTip == 'next'){
		i++;
	}
	if(nextTip == 'prev'){
		i--;
	}
	//Reset counter if at end
	if(i>tipArray.length-1){
		i=0;
	}
	//Go to end if at beginning
	if(i<0){
		i=tipArray.length-1;
	}
	$('#helpBoxHome').hide();
	$('#helpBoxHome').html(tipArray[i]).fadeIn('slow');
});
*/
         
//Check to hide info
$("#showInfo").click(function(){
	var checkBox = $("#showInfo").attr("checked");
	if(checkBox == true){
	setCookie('showInfo','0');
	$('#helpBoxHome').slideUp('slow');
	$("#helpBoxBox a").slideUp('slow');
	}
	else{
	clearCookie('showInfo');
	$('#helpBoxHome').slideDown('slow');
	$("#helpBoxBox a").slideDown('slow');
	}
});
	

/*---------
 Hover state for form inputs
---------*/			
$("input:not(.submitButton,#showInfo,#login input,.deleteButton,#fileName,#comments input,#siteCommentForm input,#eventForm .icon, #siteComments input,#chooseIcon,#catName,#categoryList input),select:not(#approveEvent select,#selectEvent select,#selectUser select,#eventType1,#loadComments select, #catType)").mouseover(function(){
	$(this).parent().css({'background':'#c7ee5b'});
}).mouseout(function(){
		$(this).parent().css({'background':''});
});	
	


/*---------
  Popup contact form
---------*/		
$('#emailcontact').live("click",function(){
	$('#contactbox').load('contact_form.php').fadeIn('slow');
});


//Get the current event type so that only that page's tips are closed
currentEvent = getCookie('eventCookie');
//Get the current cookie value
showInfoBottom = getCookie('showInfoBottom');

//When pulling the cookie array back in, it is a string not an array.
showInfoSplit = showInfoBottom.split(",");

showArray = new Array();
currPage = window.location.href;
endPage = currPage.split("?");

if(showInfoBottom != ''){
	for(i=0;i<=3;i++){
		showArray[i] = showInfoSplit[i];
	}
}
if((endPage[1] == 'news=1#')||(endPage[1] == 'news=1')){
  	showArrayVal = '0';
  }
  else{
 	 showArrayVal = showArray[currentEvent];
	}   
	
if((showArrayVal == '1')||(showArrayVal == undefined)||(showArrayVal == '')){
	$('#toggleLocationInfo').css({'bottom':'200px'}).html('<a href="#" id="closeBox">Hide the Introduction to this Category</a>');
}
if(showArrayVal == '0'){
	$('#locationInfo').css({'display':'none'});
	$('#toggleLocationInfo').css({'bottom':'0'}).html('<a href="#" id="openBox">Read About this Category</a>');
}

/*---------
 Bottom of page help text
---------*/		
$('#closeBox').live("click",function(){
	botPos = $('#toggleLocationInfo').css('bottom');
	$('#locationInfo').slideUp();
	$('#toggleLocationInfo').animate({ 
			bottom:0
      }).html('<a href="#" id="openBox">Read About this Category</a>');
  if((endPage[1] == 'news=1#')||(endPage[1] == 'news=1')){
  	showArray[0] = '0'; 
  }
  else{
 	 showArray[currentEvent] = '0'; 
	}   
  setCookie('showInfoBottom', showArray);
});

$('#openBox').live("click",function(){
	$('#locationInfo').slideDown();
	$('#toggleLocationInfo').animate({ 
			bottom:'200px'
      }).html('<a href="#" id="closeBox">Hide the Introduction to this Category</a>'); 
  if((endPage[1] == 'news=1#')||(endPage[1] == 'news=1')){
  	showArray[0] = '1'; 
  }
  else{
 	 showArray[currentEvent] = '1'; 
	}      
  setCookie('showInfoBottom',showArray);
});

/*---------
 List view alternate bg color
---------*/	
$('div.listView').css({'background-color':'#e8e8e8'});

//Show more info
$('div.listView a.expand').click(function(){
	$(this).next('.listViewDesc').toggle('slow');
	return false;
});

//Toggle each category
$('.listToggle').click(function(){
	$(this).next('div').children().slideToggle('slow');
});


/*---------
 Submenu Nav
---------*/	
$('.submenu>a').mouseover(function(){
	$('.submenu ul').css({'visibility':'hidden'});;
	$(this).next('ul').css({'visibility':'visible'});
	$('.submenu ul').attr('fade','false');
});

$('.submenu ul').mouseover(function(){
	$('.submenu ul').attr('fade','false');
});

$('.submenu').mouseout(function(){
	$('.submenu ul').attr('fade','true');
	setTimeout(function() { if ($('.submenu ul').attr("fade") == "true") $('.submenu ul').css({'visibility':'hidden'}); }, 100);
});


});