// cookie functions

function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(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;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}


// show or hide text input value

$.fn.search = function() {
	return this.focus(function() { if( this.value == this.defaultValue ) { this.value = ""; } }).blur(function() { if( !this.value.length ) { this.value = this.defaultValue; } });
};


// function to set the active state on text resize icons

function toggleActive(activeItem) {

	$(".resize_icons").find("a").each(function(){
		$(this).parent("li").removeClass("active");
	});
	$(".resize_icons").find("." + activeItem).parent("li").addClass("active");

}

// function to set text size

function setText(text_size) {
    _gaq.push(['_trackEvent', 'text-resize', text_size, 'Click']);
	if (text_size == "normal") {
		$("body").css({ "font-size" : "62.5%" });
        $(".text_resize p").removeClass();
        $(".text_resize p").addClass("sizea");
		toggleActive("resize_small");
	}
	if (text_size == "larger") {
		$("body").css({ "font-size" : "72%" });
        $(".text_resize p").removeClass();
        $(".text_resize p").addClass("sizeb");
		toggleActive("resize_med");
	}
	if (text_size == "largest") {
		$("body").css({ "font-size" : "81.25%" });
        $(".text_resize p").removeClass();
        $(".text_resize p").addClass("sizec");
		toggleActive("resize_large");
	}
}


// function to set active state on where we work nav items

function toggleActiveMap(activeItem) {
	$(".where_we_work_nav").find("li").each(function(){
		$(this).removeClass("active");
	});
	$(".where_we_work_nav").find("." + activeItem).parent("li").addClass("active");
}


// function to set promo heights

function checkPromoHeights() {
	// promo header heights
	$('.promo_row').each(function() {
		var each_row = $(this);
		tallest_header = 0;
		each_row.find('h2').each(function() {
			$(this).css({"height" : "auto"}); // clear any previously set heights
			current_height = $(this).height();
			if ($(this).height() > tallest_header) {
			tallest_header = $(this).height();
			}
		});
			each_row.find('h2').each(function() {
			$(this).height(tallest_header);
		});
	});
	// promo text heights
	$('.promo_row').each(function() {
		var each_row = $(this);
		tallest_text = 0;
		each_row.find('.promo_text').each(function() {
			$(this).css({"height" : "auto"}); // clear any previously set heights
			current_height = $(this).height();
			if ($(this).height() > tallest_text) {
			tallest_text = $(this).height();
			}
		});
			each_row.find('.promo_text').each(function() {
			$(this).height(tallest_text);
		});
	});
}


// functions to control slideshows

function showPauseShow() {
	$(".pause_show").css({"display" : "block"});
	$(".play_show").css({"display" : "none"});
}

function showPlayShow() {
	$(".pause_show").css({"display" : "none"});
	$(".play_show").css({"display" : "block"});
}

function pauseShow() {
	$(".slideshow").cycle('pause');
	showPlayShow();
}







$(document).ready(function() {

	// check for textSize cookie
	if (getCookie("textSize")) {
		var cookieVal = getCookie("textSize");
		if (cookieVal == "normal") {
			setText("normal");
		}
		if (cookieVal == "larger") {
			setText("larger");
		}
		if (cookieVal == "largest") {
			setText("largest");
		}
	}

	// superfish menus
	jQuery('ul.sf-menu').superfish({
		delay: 400,
		speed: 'fast',
		autoArrows: false,
		dropShadows: false,
		pathClass: 'active'
	});


	// text resize
	$(".resize_small").click(function(event){
		setCookie('textSize', 'normal', 14);
		setText("normal");
		event.preventDefault();
		checkPromoHeights();
	});

	$(".resize_med").click(function(event){
		setCookie('textSize', 'larger', 14);
		setText("larger");
		event.preventDefault();
		checkPromoHeights();
	});

	$(".resize_large").click(function(event){
		setCookie('textSize', 'largest', 14);
		setText("largest");
		event.preventDefault();
		checkPromoHeights();
	});


	// text field values show/hide
	$("#searchfield").search();
	$("#newsletter_field").search();


	// prevent bg image flickering on anchor links in ie6
	if($.browser.msie) {
		try { document.execCommand("BackgroundImageCache", false, true);
		}catch(err){}
	}


	// slideshows
	if ($(".slideshow").length != 0) {
		$(".play_show").hide();
		$('.slideshow')
		.after('<div class=".slideshow_nav">')
		.cycle({
			fx:  			'fade',
			speed:  		500,
			timeout: 		4000,
			pager:  		'.slideshow_nav',
			onPagerEvent:	pauseShow
		});
	}

	// pause slideshow
	$(".pause_show").click(function(event) {
		$(".slideshow").cycle('pause');
		showPlayShow();
		event.preventDefault();
	});

	// play slideshow
	$(".play_show").click(function(event) {
		$(".slideshow").cycle('resume');
		showPauseShow();
		event.preventDefault();
	});


	// promo heights
	if ($(".promo_row").length != 0) {
		checkPromoHeights();
    }

	// email share - fancybox
	if ($(".email_share").length != 0) {
		$(".email_share").fancybox({
			'overlayColor'		: '#fff',
			'overlayOpacity'	: 0.5,
			'width'				: 500,
			'height'			: 700,
			'padding'			: 0,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});
	}	

	// patrons
	if ($(".people_images").length != 0) {
		// hover
		$(".people_images").find("img").hover(function() {
			$(this).css({"opacity" : "0.5"});
     	}, function() {
			$(this).css({"opacity" : "1"});
		});
	}


	// newsletter signup form
	if ($(".signup").length != 0) {

		// check the checkboxes on page load
		if ($('#dvd_cb').attr('checked') || $('#printed_newsletter_cb').attr('checked')) {
			$(".address_details").show();
		}
		else {
			$(".address_details").hide();
		}

		// check the other field on page load
		if ($("#hear_about_us").val() == "other") {
			$(".other_specify").show();
		}
		else {
			$(".other_specify").hide();
		}

		// checkboxes
		$("#printed_newsletter_cb").click(function(event) {
			if (!$('#dvd_cb').attr('checked')) {
				//$(".address_details").toggle(this.checked);
				$(".address_details").toggle(300);
			}
		});
		$("#dvd_cb").click(function(event) {
			if (!$('#printed_newsletter_cb').attr('checked')) {
				$(".address_details").toggle(300);
			}
		});

		// other field
		$("#hear_about_us").change(function() {
			var selected = $(this).val();
			if (selected == "other") {
				$(".other_specify").show(300);
			}
			else {
				$(".other_specify").hide(300);
			}
		});
	}

    $('#newsletter_form a.more').click(function(event) {
        event.preventDefault();
        $("#newsletter_form").submit();
    });
    
    $('#volunteer_form a.submit_btn').click(function(event) {
        event.preventDefault();
        $("#volunteer_form").submit();
    });

    $('#newsletter_form a.submit_btn').click(function(event) {
        event.preventDefault();
        $("#newsletter_form").submit();
    });


    // email links
    $("a[href^=mailto]").each(function() {
        var old_contents = $(this).attr('href');
        var new_contents = $(this).attr('href') + "?subject=Enquiry%20from%20Hope%20and%20Homes%20for%20Children%20website";
        this.href = this.href.replace(old_contents, new_contents);
    });

});

$(window).load(function() {

	// patrons
	if ($(".people_images").length != 0) {
		$(".people_images ul li:nth-child(4n)").addClass("last");
		// active state
		active_element = $(".people_images").find(".active");
		$(".patron_active").css({"display" : "block", "top" : active_element.position().top + "px", "left" : active_element.position().left + "px"});
	}
    
});
