function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

$(document).ready(function() {

		/* homepage image link styles */
    $('body#homepage .home-links li:lt(3)').addClass("top");
    $('body#homepage .home-links li:nth-child(3n)').addClass("right");
    
    /* setup the navigation menu styles and initialize the dropdown menu */
	/* this selector is a bit funky, should be one css selector but can't figure out how to limit it to the top level */
	$('#navigation > ul').addClass('menu').children('li').children('a').wrapInner("<span></span>").children('span').each(function(i) {
		var ltext = $(this).html();
		//if (ltext.indexOf("-") > 0) {
		//	var ltitle = ltext.substr(0, ltext.indexOf("-") - 1);
		//	var lsub = ltext.substr(ltext.indexOf("-") + 2);
		//	//$(this).html("<strong>" + ltitle + "</strong>" + lsub);
			$(this).html("<strong>" + ltext + " <img src='/site/new/images/arrow.png' border='0' /></strong>" );
		//} else {
		//	$(this).wrapInner("<strong></strong>");
		//}
	});
	
	/* add indicators for menus that have submenus */
    $("#navigation ul li li:has(ul)").addClass("sub-bullet");
	/* last two menus should popup to the left instead of the right */
	$("#navigation > ul > li:gt(3)").addClass("sub-left");
	
	$("ul.menu").superfish({
        hoverClass: "sfHover"
    });
    
    /* search box default values */
    searchDefault($('input#searchQuery'));
    searchDefault($('input#homeName'));
    
    
    // make all external links open in new window
	$.each( $("a"), function() {
		 var el = $(this);
			try{
			 if ( el.attr("href") && el.attr("href").indexOf("http://") > -1 && !(el.attr("href").indexOf("accreditation.org.au") > -1) && !(el.attr("href").indexOf("amplify.com.au") > -1)  )
				el.attr({ target: "_blank" });
		 }
		 catch(ex){}
	});	
    
    
    /* text resizing links */
    $('.text-resize ul li a').click(function() {
        var fsize = $(this).parent().attr("class");
        $('body').removeClass().addClass("textresize-"+ fsize);
        $(this).parent().addClass("current").siblings('li').removeClass("current");
        // some ajaxy stuff to cookie it
        
        return false;
    });
	
	/* home reports search field autocomplete */
	$("#homeName").autocomplete("/modules/reports_search/reports_auto.php", {
		width: 242,
		scroll: 40
	});
	
	
	/* setup home reports advanced search date fields */
	$('#content form#homeSearchAdvanced #homeFromDate, #content form#homeSearchAdvanced #homeToDate').datepicker({
		dateFormat: 'yy-mm-dd'
	});
	
	/* validate home reports advanced search */
	$('form#homeSearchAdvanced').submit(function() {
		var fok = false;
		
		if (($('#homeFromDate').val() && $('#homeToDate').val() == "") || ($('#homeToDate').val() && $('#homeFromDate').val() == "")) {
			$('#homeError').show().html("Please select both a From Date and a To Date");
			return false;
		} else if ($('#homeFromDate').val() && $('#homeToDate').val()) {
			fok = true;
		}
		
		if (!fok && ($('#homeNameAdvanced').val() || $('#homeRacs').val() || $('#homeSuburb').val() || $('#homeState').val()))
			fok = true;
			
		if (!fok) {
			$('#homeError').show().html("Please enter some search parameters");
			return false;
		}
		
		return true;
	});
	
	/* convert all document links to new window and add relevant icon */
	$("a[href*=.pdf], a[href*=.doc]").each(function() {
		$(this).addClass("application-"+ $(this).attr('href').substr($(this).attr('href').length-3))
    .append("<span></span>");
	});
  
  /* validate the application software download registration form */
  if (document.getElementById("appdload")) {
   $("#appdload").validate();
  }
 
  /* Hide some modules away on certain pages */
  if (document.getElementById('mod_content_2425') && jQuery.url.segment(0)=='residents-relatives'){
    $('#mod_content_2425').hide();
  }
  
  /* Confirm report delete requests */
 $("a[id^='delswitch']").click(function(){
    if(!confirm("Are you sure you want to delete this report?\n\nThis action cannot be undone.")){
      return false;
      $(this).preventDefault();
    } 
  });
  
  /* Handles the report edit links */
  $("a[id^='editswitch']").click(function(){
    var bits = $(this).attr('id').split('_');
    home_id = bits[1];
    report_id = bits[2];
    mod_reportsadmin_editor();
    return false;
    $(this).preventDefault();
    });

  /* Tab Management for Meet the Team page */
  if($('.tabme').length>0 && typeof( window[ '_page_status' ] ) == "undefined"){
    // wrap it all up in a new div
    $('.tabme').wrapAll('<div id="tabs" />');
    var tabmeul = '<ul class="idTabs">';
    // pull the first h3 from each tabme and build a list from it
    $('.tabme').each(function(i) {
      var id = $(this).attr('id');
      var txt = $('h3:first', this).text();
      $('h3:first', this).remove();
      tabmeul += '<li><a href="#' + id + '">' + txt + '</a></li>';
    });
    tabmeul += '</ul>';
    $('#tabs').prepend(tabmeul);
    $.getScript('/site/js/jquery.idtabs.js');
  }
});

function searchDefault(sbox)
{
    var sdef = sbox.val();

    sbox.focus(function() {
        if ($(this).val() == sdef)
            $(this).val("");
    }).blur(function() {
        if ($(this).val() == "")
            $(this).val(sdef);
    });
}
