$(document).ready(function() 
{

	// Cufon title replacement
	Cufon.replace('#features .title')('h1')('h2')('#nav-main li a:not(#nav-main li div a)',{ hover: true })('.dropped-list .close')('.nav-segment p')('#hero .content p',{ hover: true })('.date');

	hasSearched = false;
	
	if ($('#hero').length > 0)
	{
		$('#hero').cycle({
			fx:      'scrollLeft',
			timeout: 10000,
			next:   '#next', 
			prev:   '#prev'
		});
	}
	
	$(".about-us #tabs ul li a").attr("href",function() { return $(this).attr("href") + "?show=nohead" });
	$("#tabs:not(.nojquery)").tabs({
		cookie: { expires: 30 },
		load: function(event, ui) { Cufon.replace('h1')('h2')('h3'); }
	});
	
	// Form validation
	$('#form-event').validate();
	$('#form-contact').validate();
	
	// Form "title" replacement - input must have class of "title-replace"
	if ($(".title-replace").length > 0)
	{
		$('.title-replace').each( function() {
			if ($(this).val() == '' || $(this).val() == $(this).attr("title")) {
				$(this).val($(this).attr("title"));
				$(this).addClass("form-light");
			}		
		});
		
		$("input.title-replace").focus( function() {
			if ($(this).val() == $(this).attr("title")) {
				$(this).val('');
				$(this).removeClass("form-light");
			}
		});

		$("input.title-replace").blur( function() {
			if ($(this).val() == "") {
				$(this).val($(this).attr("title"));
				$(this).addClass("form-light");
				
				if ($("#practice-button").length > 0){
					$("#full-alpha-list").show();
					$("#full-search-results").html("");
					$("#full-search-results").hide();
				}
				if ($("#resource-button").length > 0 && hasSearched == true){
					hasSearched = false;
					$("#full-alpha-list").html("<div class='loading'>Loading...</div>");
					$("#full-alpha-list").load("/resources-publications/list/All/All/",function(){generateAllTab();});
				}
			}
		});
	}

	// Main navigation drop downs
	if($.browser.msie && $.browser.version=="6.0") {
	
	} else {
		cloneContent = '';
		cloneContent = $('.services-dropped').clone();
		$('.nav-programs-services').append(cloneContent).prepend('<span class="nav-tab"></span>').addClass('hastab');
		cloneContent = '';
		cloneContent = $('.resources-dropped').clone();
		$('.nav-resources-publications').addClass('hastab').append(cloneContent).prepend('<span class="nav-tab"></span>');
	
		$(".nav-programs-services, .nav-resources-publications").hover(
			function(){
				if($.browser.msie){
					$("div",this).show();
				}else{
					$("div",this).fadeIn(100);
				}
				$("a:first", this).toggleClass("hover")
			},
			function(){	
				if($.browser.msie){
					$("div",this).hide();
				}else{
					$("div",this).fadeOut(300);
				}
				$("a:first", this).toggleClass("hover")
				Cufon.replace('#nav-main li a:not(#nav-main li div a)',{ hover: true })
			}
		);
	}
	
//	$('.dropped-list').click(function(){return false;})
	
	// Filter list toggle
	if ($('.filter-list').length > 0)
	{
		$('.filter-list h3').prepend('<span class="button"></span>');
		$('.filter-closed h3 span.button').toggleClass('closed');
		$('.filter-closed ul').toggle();
		
		$('.filter-list h3 span.button').click( function() {
			$(this).toggleClass('closed');
			$(this).parent().siblings("ul").toggle();
		});
	}
	
	if ($("#alpha-list").length > 0)
	{
		generateAllTab();
		
		$("#filter-category li a").click(function(){
			$("#filter-category li").removeClass("here");
			$("#filter-category li span").remove();
			$(this).parent().addClass("here");
			$(this).parent().prepend("<span>&nbsp;</span>");
			if (!$("#frm-resources-search #resource-keywords").hasClass("form-light")) {
				$("#frm-resources-search #resource-keywords").val($("#frm-resources-search #resource-keywords").attr("title")).addClass("form-light");
			}
			generateAlphaList();
			return false;
		});
		$("#filter-program li a").click(function(){
			$("#filter-program li").removeClass("here");
			$("#filter-program li span").remove();
			$(this).parent().addClass("here");
			$(this).parent().prepend("<span>&nbsp;</span>");
			if (!$("#frm-resources-search #resource-keywords").hasClass("form-light")) {
				$("#frm-resources-search #resource-keywords").val($("#frm-resources-search #resource-keywords").attr("title")).addClass("form-light");
			}
			generateAlphaList();
			return false;
		});
	}
	
	
	
	if ($(".alpha-list-group").length > 0)
	{
//		i = 1;
//		$(".alpha-list-group li:not('.alpha-item')").after( '<li class="clear"></li>' );

		$(".alpha-list-group").each(function(){
			i=1;
			$("li:not(.alpha-item)",this).each(function(){
				if (i==1) {
					i=2;
				}else if (i==2) {
					$(this).after( '<li class="clear"></li>' );
					i=1;
				}
			});
		});

	}
	
	if ($("#resource-button").length > 0)
	{
		$("#resource-button").click(function() {
			value = $("#frm-resources-search #resource-keywords").val();
			
			$("#filter-category li").removeClass("here");
			$("#filter-category li span").remove();
			$("#filter-category li:first").addClass("here");
			$("#filter-category li:first").prepend("<span>&nbsp;</span>");

			$("#filter-program li").removeClass("here");
			$("#filter-program li span").remove();
			$("#filter-program li:first").addClass("here");
			$("#filter-program li:first").prepend("<span>&nbsp;</span>");

			$("#full-alpha-list").html("<div class='loading'>Loading...</div>");
			hasSearched = true;
			$("#full-alpha-list").load("/resources-publications/list/All/All/"+escape(value),function(){generateAllTab();});
			return false;
		} );

	}
	
	
	
	
	if ($("#practice-button").length > 0)
	{
		$("#practice-button").click(function() {
			if (!$("#practice-keywords").hasClass("form-light"))
			{
				value = $("#practice-keywords").val();
			
				$("#full-alpha-list").hide();
				$("#full-search-results").html("<div class='loading'>Loading...</div>");
				$("#full-search-results").show();
				$("#full-search-results").load("/practices/list/"+escape(value),function(){
					loadMap();
				});
			}
			return false;
		} );
		
		
		$("#clear-practice-search").click(function() {
			$("#full-search-results").html("");
			$("#full-search-results").hide();
			$("#full-alpha-list").show();
			$("#practice-keywords").val($("#practice-keywords").attr("title")).addClass("form-light")
			return false;
		});

	}	
	
	
	
	
	$("label.error").attr("generated","true");
	

	function generateAlphaList(){
		txt_program = $("#filter-program li.here a").html();
		txt_category = $("#filter-category li.here a").html();
		$("#full-alpha-list").html("<div class='loading'>Loading...</div>");
		$("#full-alpha-list").load("/resources-publications/list/" + escape(txt_program) + "/" + escape(txt_category) + "/",function(){generateAllTab();});
	}

	function generateAllTab(){
		$("#alpha-list").prepend('<li><a href="" class="here alpha-list-all">All</a></li>')
		$("#alpha-list li a:not(.alpha-list-all)").click(function(){
			$(".alpha-list-group").hide();
			letter = $(this).html();
			if (letter == "#") letter = "hash";
			$(".alpha-list-"+letter).show();	
			$("#alpha-list li a").removeClass("here");
			$(this).addClass("here");
			return false;
		});
		$("#alpha-list li a.alpha-list-all").click(function(){
			$(".alpha-list-group").show();
			$("#alpha-list li a").removeClass("here");
			$(this).addClass("here");
			return false;
		});
	}

	if ($("a.gallery").length>0)
		$("a.gallery").fancybox({ 'zoomSpeedIn': 500, 'zoomSpeedOut': 500, 'overlayOpacity' :0.5 }); 

});

function loadMap(){
	var addr = new Array();
	$(".hidden-address").each(function(i){
		addr[i] = $(this).html();
	});
	$("#generated-map").googlemap({
	     controls: true,
	     labels: true,
	     addresses: addr
	});
}


