
//to do on page load
$(document).ready(function() {
  
  $("a.iframe").fancybox({
      'autoDimensions'    : false,
      'width'             : 490,
      'height'            : 500,
      'transitionIn'      : 'elastic',
      'transitionOut'     : 'elastic',
      'type'              : 'iframe',
    'showCloseButton': false

  });
  
	/*
	 * News Drop Down
	 */
	$('#news_drop').hide();
	$('a.show_news').click(function() {
		$('#news_drop').slideToggle();
	});
  
  $('#select_area_drop').hide();
  $('a.show_area').click(function() {
    $('#select_area_drop').slideToggle();
  });
  
  
  $('#jumpToContent').hide();
  $('img#jumpTo').click(function() {
    $('#jumpToContent').slideToggle();
  });
  
  //on page load, set certain subnav closed - if the cookie exists
  current_subnav_state = $.cookie('subnav_state');
  
  if (current_subnav_state != '' && current_subnav_state != null) {
		
	  //build an array of states
		stateArray = current_subnav_state.split(";");
		
		
		//find the state id's and their relevant positions
		for (i = 0; i < stateArray.length; i++) {
			
			//current 
			nav_option = stateArray[i].split(":");
			
			//only if they are closed do we need to worry about it.
			if (nav_option[1] == 'c') {
				
				subnav_id = 'subnav' + nav_option[0];
				$('#' +subnav_id).find("+ ul").css('display','none');
				$('#' +subnav_id).addClass('closed');
				$('#' +subnav_id).css("background-image","url(images/leftnav_closed.gif)");
			}
		}
  }
	
	
	/*
	 * Left Nav Animation
	 */
	// Collapse everything but the first menu:
	//$("#leftnav_list > li > a").not(":first").find("+ ul").slideUp(1);
	// Expand or collapse:
	$("#leftnav_list > li > a").click(function() {
		clicked_id = $(this).attr("id");
		
		clicked_id = clicked_id.replace('subnav','');
		
		
		
		
		$(this).find("+ ul").slideToggle("fast");
		
	
		if (!$(this).hasClass('only')) { 
			if ($(this).hasClass('closed')) {
				state = 'o';
				$(this).removeClass('closed');
				$(this).css("background-image","url(images/leftnav_open.gif)");
			} else {
				state = 'c';
				$(this).addClass('closed');
				$(this).css("background-image","url(images/leftnav_closed.gif)");
			}
		}
		
		newCookieString = '';
		
		oldState = $.cookie('subnav_state');
		
		if (oldState != '' && oldState != null) {
		
			//build an array of states
			stateArray = oldState.split(";");
			
			
			//rebuild the old array from the cookie
			for (i = 0; i < stateArray.length; i++) {
				
				//check that they haven't altered a previously altered state
				old_clicked_id_array = stateArray[i].split(":");
				
				if (old_clicked_id_array[0] != clicked_id) {
					newCookieString += stateArray[i];
					newCookieString += ';';
				}
			}
		}
		
		//append the new option
		newCookieString += clicked_id + ':' + state;
		
		$.cookie('subnav_state',newCookieString, { expires: 28 });
	});
    
  /*
   * Spotlight area
   */
  $('#spotlight_list li a').bind('click', { param: $(this) }, change_spotlight_school);
  
  $('img.spotlight_left').bind('click', { param: $(this) }, prev_spotlight);
  $('img.spotlight_right').bind('click', { param: $(this) }, next_spotlight);
});

//transform forms
$(function() {
    //find all form with class jqtransform and apply the plugin
  //  $("form.jqtransform").jqTransform();
});


//mouse rollover
image_dir = 'images/';
function rollover(element_id, new_image) { 
	document.getElementById(element_id).src = image_dir + new_image;
}

  
//read more expand/collapse
 function read_more(link) {
  outerdiv = link.parent().parent();
  outerdiv.find('p.news_excerpt').toggle();
  outerdiv.find('div.news_content').toggle();
  if (link.text().indexOf('Read more') >= 0) {
    link.text("<< Hide");
  } else {
    link.text("Read more >>");
  }
  return false;
}

function toggle_subnav(item) {
  item.siblings('ul').toggle();
  return false;
}

function change_spotlight_school(event) {
  old_index = $('#spotlight_list li.selected').index();
  new_index = $(this).parent().index();
  
  $('#spotlight_list li.selected').removeClass('selected');
  $('#spotlight_list li:eq(' + new_index + ')').addClass('selected');
  
  $('#spotlight_content > div:eq(' + old_index + ')').toggle();
  $('#spotlight_content > div:eq(' + new_index + ')').show();
}

function next_spotlight(event) {
  selected = $(this).siblings('.spotlight_images').find('img:visible');
  if (selected.next().length > 0) {
    selected.next().show();
    selected.toggle();
  } else {
    $(this).siblings('.spotlight_images').find('img:first').show();
    selected.toggle();
  }
  
  // swap text
  selected = $(this).siblings('.spotlight_text').children('div:visible');
  if (selected.next().length > 0) {
    selected.next().show();
    selected.toggle();
  } else {
    $(this).siblings('.spotlight_text').children('div:first').show();
    selected.toggle();
  }
}

function prev_spotlight(event) {
  selected = $(this).siblings('.spotlight_images').find('img:visible');
  if (selected.prev().length > 0) {
    selected.prev().show();
    selected.toggle();
  } else {
    $(this).siblings('.spotlight_images').find('img:last').show();
    selected.toggle();
  }
  
  // swap text
  selected = $(this).siblings('.spotlight_text').children('div:visible');
  if (selected.prev().length > 0) {
    selected.prev().show();
    selected.toggle();
  } else {
    $(this).siblings('.spotlight_text').children('div:last').show();
    selected.toggle();
  }
}
