// Get the name of the anchor for the remote page
var str = '';
function splitAnchor(str){
  var anchor = str.split('#');
  return anchor[1];
}

$(function(){
  // Add div to list item, and fill it with course description from remote page
  $('ul.courses a').click(function(e){
    var anchor = splitAnchor($(this).attr('href'));
    var xPos = e.pageX;
    var yPos = e.pageY;
    var cssObj = {'position':'absolute', 
                  'top' : yPos + 'px', 
                  'left' : xPos + 'px'};    
    $('div.course').remove();
    $('body').append('<div class=\"course\"></div>');
    $('div.course').css(cssObj);
    $('div.course').load('COURSES/index.html div#' + anchor);
    return false;
  })
  
  // Add div to hold expand/collapse controls
  $('div.siteList').before('<div class=\"controls\"></div>');

  var collapseLink = "<a href=\"#\" class=\"collapse\"><img src=\"/assets/images/ui/collapse.gif\" alt=\"Collapse\"/></a>";
  var expandLink = "<a href=\"#\" class=\"expand\"><img src=\"/assets/images/ui/expand.gif\" alt=\"Expand\" /></a>";
  var $controls = $('div.controls');
  var $grade = $.cookie('currentGrade');

  function toggleLink(link) {
    if (link.hasClass("collapse")) {
      link.replaceWith(expandLink);
    } else if (link.hasClass("expand")) {
      link.replaceWith(collapseLink);
    }
  }

  function hideContent(elm) {
    elm.parent().next().hide();
    toggleLink(elm);
    setAllHeights();
  }

  function showContent(elm) {
    elm.parent().next().show();
    toggleLink(elm);
    setAllHeights();
  }
  
  // Read cookie & call appropriate behaviors based on its value
	if (window.location.hash == "#seventhAndEighth") {
    $("#seventhAndEighth").prev().append(collapseLink);
    $("#eighthNinthTenth").hide();
    $("#eighthNinthTenth").prev().append(expandLink);	
	} else if (window.location.hash == "#eighthNinthTenth") {
    $("#eighthNinthTenth").prev().append(collapseLink);
    $("#seventhAndEighth").hide();
    $("#seventhAndEighth").prev().append(expandLink);		
	} else if (!$grade) {
    $("#seventhAndEighth").hide();
    $("#seventhAndEighth").prev().append(expandLink);
    $("#eighthNinthTenth").hide();
    $("#eighthNinthTenth").prev().append(expandLink);
  } else if ($grade == "8") {
    $controls.append(collapseLink);
  } else if ($grade == "7") {
    $("#seventhAndEighth").prev().append(collapseLink);
    $("#eighthNinthTenth").hide();
    $("#eighthNinthTenth").prev().append(expandLink);
  } else if ($grade == "9" || $grade == "10") {
    $("#eighthNinthTenth").prev().append(collapseLink);
    $("#seventhAndEighth").hide();
    $("#seventhAndEighth").prev().append(expandLink);
  }

	// if ($grade != null && $grade != "8") {
	// 	$(".attn8").hide();
	// }
  
  // Handle hide/show on click
  $('div.controls a').livequery("click", function(){
    if ($(this).hasClass("collapse")) {
      hideContent($(this));
      return false;
    } else if ($(this).hasClass("expand")){
      showContent($(this));
      return false;
    }
  })

	// Event to set radio button if 4, 5, or 6 are selected from the grade pulldown
  $("select[name='grade']").change(function() {
		if ($(this).val() == "4" || $(this).val() == "5" || $(this).val() == "6") {
			$("input[name='testTaken']").val(["no"]);
		}
	});

  $('div.course h3').livequery(function(){
    $(this).append("<a href=\"#\" class=\"close\"><img src=\"/assets/images/ui/close.gif\" alt=\"Close\"/></a>");
    $('h3 a').click(function(){
      $(this).parent().parent().parent().hide();
      return false;
    });
  });
  
  // 'accordion' for /summer_programs/experience/
  $('div.response').hide();
  $('h3.question').wrapInner("<a href=\"#\"></a>");
  	$('h3.question').next().prepend("<a href=\"#\" class=\"close\"><img src=\"/assets/images/ui/close.gif\" alt=\"Close\"/></a>");
   $('a.close').click(function() {
     $(this).parent().hide();
     return false;
   });
   $('h3.question').click(function() {
       $(this).next().toggle();
       setAllHeights();
       return false;
   });
  
  // /summer_programs/summer/
  var $ageSpecific = $('div.ageSpecific');
  $ageSpecific.hide().prepend("<a href=\"#\" ><img src=\"/assets/images/ui/close.gif\" alt=\"Close\"/></a>");
  $('div.ageSpecific a').livequery('click', function(){
    $(this).parent().hide();
    return false;
  })
  $('a#readMore').click(function(){
    $('div.ageSpecific').show();
    return false;
  })
  
  // help link
  $('a.help').click(function(e) {
		if ($("div#helpPopUp").length > 0) {
			$('div#helpPopUp').remove();
      return false;
		}
		
    var xPos = e.pageX - 250;
    var yPos = e.pageY + 20;
    var cssObj = {'position':'absolute', 
                  'top' : yPos + 'px', 
                  'left' : xPos + 'px'};              
    $('body').prepend("<div id=\"helpPopUp\"></div>");
    $('div#helpPopUp').css(cssObj);
    $('div#helpPopUp').load('/summer_programs/help.html div.help', function() {
      $(this).prepend("<a href=\"#\" class=\"close\"><img src=\"/assets/images/ui/close.gif\" alt=\"Close\"/></a>");
	    $('div#helpPopUp').livequery(function() {
	      $('div#helpPopUp a.close').click(function() {
	        $('div#helpPopUp').remove();
	        $('a.help').bind('click');
	        return false;
	      });
	    });

    });
    
    return false;
  });
  // print
  $('a.print').click(function(){
    window.print();
    return false;
  })
})