/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

$(document).ready(function(){
	/* =BODY CLASSES
	----------------------------------------------------*/
	
	$('html').addClass('js');
	
	var CMS = false;

	if ($('form.cmsForm').length > 0) {
		CMS = true;
		$('html').addClass('cms');
	}
	
	var IE = false;

	if ($.browser.msie && ($.browser.version < 9.0)) {
		IE = true;
	}

	var Home = false;
	
	if ($('body').hasClass('page-home')) {
		Home = true;
	}
	

	/* =LINKS
	----------------------------------------------------*/
	// open rel="external" links in new windows
	
	$("a[rel~='external']").click(function(){
		window.open($(this).attr('href'));
		return false;
	});

	$("#sidebar #map").addClass('click-box').click(function(){
		  window.location=$(this).find("a").attr("href"); return false;
	});
	
	/* =LIGHTBOX
	----------------------------------------------------*/
	
	$('img[class$="popup"]').parent('a').colorbox();
	$('#map-page .dark-button').colorbox();


	/* =TABLES
	----------------------------------------------------*/
	// stripe tables
	
	$('.table-striped tr:even').addClass('even');


	/* =CONTACT US
	----------------------------------------------------*/
	$('#contact-form .required').parent().prepend('<span class="req">* </span>');
    
     $('#contact-form').before('<p>Required fields are marked <span class="req">* </span>.</p>');
	
	
	/* =Event List 
	-----------------------------------------------------*/
	/* $('input[name=switch]').change(function() {		
			window.location = $(this).val()	
	}); */
	
	/*
	
	// To add RSS Link to calendar.. experimental...
	if($('tr:last-child .DateBox:last-child').hasClass('Inactive')) {
		$('tr:last-child .DateBox:last-child').append($('.calendar-events-rss-link').html());		
	}
	
	$('.MonthNavNext').click(function() {
	
		if($('tr:last-child .DateBox:last-child').hasClass('Inactive')) {
			$('tr:last-child .DateBox:last-child').append($('.calendar-events-rss-link').html());		
		}
	
	});
	
	
	$('.MonthNavPrev').click(function() {
	
		if($('tr:last-child .DateBox:last-child').hasClass('Inactive')) {
			$('tr:last-child .DateBox:last-child').append($('.calendar-events-rss-link').html());		
		}
	
	});
	
	*/
	

	/* =PHOTO GALLERY
	----------------------------------------------------*/

	// photo gallery listing

	$("#photo-gallery-listing ul li:nth-child(3)").addClass('row-end');

	$("#photo-gallery-listing .gallery ul li a span.title").hide();
	
	$("#photo-gallery-listing .gallery ul li a").hover(      
		function () {
			$(this).children('.title').show();
		}, 
		function () {
			$(this).children('.title').hide();
		}
	);

	
	$("#photo-gallery ul li:nth-child(3n)").addClass('row-end');
	
	$("#photo-gallery ul li a span.title").hide();
	
	$("#photo-gallery ul li a").hover(      
		function () {
			$(this).children('.title').show();
		}, 
		function () {
			$(this).children('.title').hide();
		}
	);

	
	/* =FONT RESIZE
	----------------------------------------------------*/
	// add code
	
	$('#accessibility').after('<p id="text-size"> <!-- open TEXT-SIZE --><strong>Text size:</strong> <a href="#" class="size smaller" title="Decrease Font Size">A-</a> <a href="#" class="size larger" title="Increase Font Size">A+</a></p> <!-- close TEXT-SIZE -->');
	
	var CookieName = 'textSize';
	
	// Font Size
	var fontSize;
	var textBase = 85;
	var increment = 10;
	var plus = textBase + increment;
	var textCeiling = textBase + (2 * increment);
	var textFloor = textBase - (2 * increment);
	
	// Check for cookie, to load previous size choice
	
	var cookieSize = $.cookie(CookieName);
	
	if (cookieSize > 0) {
		fontSize = Number(cookieSize);
		$('body').css('font-size', fontSize + '%');
		if (fontSize >= plus) {
			$('body').addClass('large');
		}
		if (fontSize == textCeiling) {
			$('.larger').addClass('inactive');
		}
		if (fontSize == textFloor) {
			$('.smaller').addClass('inactive');
		}

	}
	else {
		fontSize = textBase;
		$('body').css('font-size', fontSize + '%');
	}
	
	// Check for clicks
	
	$('.smaller').click(function(){
		fontSize = fontSize - increment;
		if (fontSize < textFloor) {
			fontSize = textFloor;
		};
		$('body').css('font-size', fontSize + '%');
		$('.larger').removeClass('inactive');
		if (fontSize == textFloor) {
			$('.smaller').addClass('inactive');
		}
		else {
			$('.smaller').removeClass('inactive');
		}
		$.cookie(CookieName, fontSize, { path: '/', expires: 365 });
		return false;
	});
	
	$('.larger').click(function(){
		fontSize = fontSize + increment;
		if (fontSize > textCeiling) {
			fontSize = textCeiling;
		};
		$('body').css('font-size', fontSize + '%');
		
		$('.smaller').removeClass('inactive');
		if (fontSize == textCeiling) {
			$('.larger').addClass('inactive');
		}
		else {
			$('.larger').removeClass('inactive');
		}
		$.cookie(CookieName, fontSize, { path: '/', expires: 365 });
		return false;
	});

		/* =HOME
	----------------------------------------------------*/
	if (Home) {
	
		// news item height
		
		var h0 = 0;
		var h1 = 0;
		var h2 = 0;
		var h3 = 0;
		
		h0 = $("#content ul li:eq(0)").height();
		h1 = $("#content ul li:eq(1)").height();
		h2 = $("#content ul li:eq(2)").height();
		h3 = $("#content ul li:eq(3)").height();
		
		
		var Tallest = Math.max(Math.max(h0, h1),Math.max(h2, h3));
		
		$("#content ul li").height(Tallest);
		
		$('#content ul li').append('<p class="read-more">Read More &raquo;</p>');
		
		$("#content ul li").addClass('click-box').click(function(){
		  var toGo = $(this).find("a").attr("href");
		  if(toGo == null) {
			toGo = "#";
		  }
		  
		  window.location=toGo; return false;
		});

		
     } // end Home

	
	/* =SITEMAP
	----------------------------------------------------*/
	
	if ($('.section-site-map ').height() > 0) {
		
		// sliders & controls

		$("#sitemap-controls").after('<div id="sitemap-intro"><p>To view the sitemap for each area, click on the corresponding coloured bar. To hide the area, click on the bar once more.</p><ul><li>The +/- icons indicate whether the area is opened/closed.</li><li>To view the sitemap in full, click "Show All".</li></ul><p id="activate"><a id="show-all" href="javascript:void(0);" title="Show All">Show All</a> | <a id="hide-all" href="javascript:void(0);" title="Hide All">Hide All</a></p></div>');
	
		$("#sitemap .slider ul li ul.sitemap-1").hide();
		$("#sitemap .slider ul li h3 a").addClass("closed");
	
		$("a#hide-all").click(function () {
			$("#sitemap .slider ul li ul.sitemap-1").hide();
			$("#sitemap .slider ul li h3 a").removeClass("open");
			$("#sitemap .slider ul li h3 a").addClass("closed");
			return false;
		});
		
		$("a#show-all").click(function () {
			$("#sitemap .slider ul li ul.sitemap-1").show();
			$("#sitemap .slider ul li h3 a").removeClass("closed");
			$("#sitemap .slider ul li h3 a").addClass("open");
			return false;
		});
		
		$("#sitemap .slider ul li h3 a").click(function () {
			$(this).parents("li").children("ul.sitemap-1").toggle();
			$(this).toggleClass("closed");
			$(this).toggleClass("open");
			return false;
		});

	}
	
});


