/* ------------------------------------------------------------------------------------
Website: Encams Keep Britain Tidy
File: Internal pages javascript (jquery)
Author: Reading Room
Created: Feb 2009
------------------------------------------------------------------------------------ */
jQuery.fn.equalHeight = function () {
    var height        = 0;
    var maxHeight    = 0;

    // Store the tallest element's height
    this.each(function () {
        height        = jQuery(this).outerHeight();
        maxHeight    = (height > maxHeight) ? height : maxHeight;
    });

    // Set element's min-height to tallest element's height
    return this.each(function () {
        var t            = jQuery(this);
        var minHeight    = maxHeight - (t.outerHeight() - t.height());
        var property    = jQuery.browser.msie && jQuery.browser.version < 7 ? 'height' : 'min-height';

        t.css(property, minHeight + 'px');
    });
};
$(document).ready(function() {
	$("body").addClass("js");
	$("#leftCol li li:last").addClass("last");
	$("#leftCol li li:first").addClass("first");
	
/* ------------------------------------------------------------------------------------
-- Flash map
------------------------------------------------------------------------------------ */
    $('#regionFlash').flash(
        { 
          src: '/GreenFlag/images/flash/regionsMap.swf',
          width: 180,
          height: 236,
		  wmode:"transparent",
          flashvars: { MYURL: '/Get_Involved/Your_region/North_West' }
        },
        { version: 8 }
    );

/* ------------------------------------------------------------------------------------
-- Our Expertise box
------------------------------------------------------------------------------------ */
	$(".interiorMinorFeatureInfo").hide();
	function interiorMinorFeatureShow(el){
		el.find(".interiorMinorFeatureInfo").show();
		el.find(".interiorMinorFeatureLink a").addClass("active");
	};
	function interiorMinorFeatureHide(el){
		el.find(".interiorMinorFeatureInfo").hide();
		el.find(".interiorMinorFeatureLink a").removeClass("active");
	};
	$(".interiorMinorFeature").hover(
		function () {
			interiorMinorFeatureShow($(this));
		}, 
		function () {
			interiorMinorFeatureHide($(this));
		}
	).click(function(){
		document.location.href=$(this).find("a:first").attr("href");
		return false;
	}).find("a").focus( function(){
		interiorMinorFeatureShow($(this).parent().parent());
	}).blur( function(){
		interiorMinorFeatureHide($(this).parent().parent());
	});

/* ------------------------------------------------------------------------------------
-- Parks List
------------------------------------------------------------------------------------ */
	$('#parksList span.expand').click(function() {
		$(this).parent().find('ul:first').toggle().find('ul').hide();;
		return false;
	});

/* ------------------------------------------------------------------------------------
-- Park images
------------------------------------------------------------------------------------ */
	$("#parkImages li a").click( function(){
		$("#parkImages img:first").attr("src", $(this).find("img").attr("src"))
		return false;
	});

/* ------------------------------------------------------------------------------------
-- Park facilities
------------------------------------------------------------------------------------ */

    $('#facilities li').hover(
		function () {
			$(this).find("em").show();
		}, 
		function () {
			$(this).find("em").hide();
		}
	);
	
/* ------------------------------------------------------------------------------------
-- Show/hide delivery address
------------------------------------------------------------------------------------ */
	$('.genForm .deliveryAddress').hide();
	$('.genForm .showHideDeliveryAddress input').change(function() {
		$('.genForm .deliveryAddress').slideToggle();
	});


/* ------------------------------------------------------------------------------------
-- Optional items on application form
------------------------------------------------------------------------------------ */

	$(".optionalOptions").hide();
	$(".optionalYes label, .optionalYes input").click( function(){
		$(".optionalOptions").show();
	});
	$(".optionalNo label, .optionalNo input").click( function(){
		$(".optionalOptions").hide();
	});
	if ($(".optionalYes input").attr("checked")){
		$(".optionalOptions").show();
	}

/* ------------------------------------------------------------------------------------
-- Add extra form fields
------------------------------------------------------------------------------------ */
	$("#applyForm .multiple").each( function(){
		if($(".multiple").index(this)!=0){
			vals=0;
			$(this).find("input").each( function(){
				if($(this).val() !=""){
					vals++;
				}
			});
			if (vals==0){
				$(this).hide();
			}
		}
	});
	$(".multiple:hidden").each( function(){
		multipleType = $("#multipleType").val();
		if($(".multiple:hidden").index(this)==0){
			$(this).prev().append("<a class='multipleAdd' href='#'>Add another "+multipleType+"</a>");
			$(this).append("<a class='multipleAdd' href='#'>Add another "+multipleType+"</a>");
		} else if($(".multiple:hidden").index(this)!=$(".multiple:hidden").length-1) {
			$(this).append("<a class='multipleAdd' href='#'>Add another "+multipleType+"</a>");
		}
	});
	$("#applyForm .multiple .multipleAdd").click( function(){
		$(this).parent().next().show();
		$(this).remove();
		return false;
	});

/* ------------------------------------------------------------------------------------
-- Wordcount on text areas
------------------------------------------------------------------------------------ */
	// 250 word limit
	$("textarea.wordLimited250").each( function(){
		countChars($(this), 250)
		$(this).keyup( function(){
			countChars($(this), 250);
		}).mouseup( function(){
			countChars($(this), 250);
		});
	});
	
	/*
	// 300 word limit
	$("textarea.wordLimited300").each( function(){
		countChars($(this), 300)
		$(this).keyup( function(){
			countChars($(this), 300);
		}).mouseup( function(){
			countChars($(this), 300);
		});
	});
	*/
	
	// make headers same height
	$('.interiorMinorFeature h2, .interiorMinorFeature h3').equalHeight();

});


function countChars(txtArea,maxWords){
	var y=txtArea.val(); //get the text from the textarea
	var wordCount = 0; //initialise the wordcount
	a = y.replace(/\s/g,' '); //convert line breaks to spaces
	a = a.split(' '); //split 
	for (z=0; z<a.length; z++) {
		if (a[z].length > 0){
			wordCount++;
		};
	};
	msg = txtArea.parent().find(".wordsLeft");
	if (wordCount > maxWords){
		msg.html("<strong>Word count: "+wordCount+"</strong>");
	} else {
		msg.html("Word count: "+wordCount);
	}


}; 
