/* ======================================================================================
 * Filename: javascript/functions.js
 * Author: Rebecca Skeers, rebecca@webmistress.com.au, www.webmistress.com.au
 * Copyright: Rebecca Skeers
 * Date: 1 December 2010
 * Description: JavaScript functions for the Parkside Foundation Staff website.
 * Visual design: Kieran Bradley, kieran@workhorse.net.au, www.workhorse.net.au
 *
 * This file may not be used for any purpose other than for the Parkside Foundation website 
 * and may not be copied or modified without written permission from the author.
 * ======================================================================================
 */
 
 
//http://stackoverflow.com/questions/1582534/calculating-text-width-with-jquery
$.fn.textWidth = function()
{
	var html_org = $(this).html();
	var html_calc = '<span>' + html_org + '</span>'
	$(this).html(html_calc);
	var width = $(this).find('span:first').width();
	$(this).html(html_org);
	return width;
};


$(document).ready(function()
{	
	$("#nav li").hover(
		function () 
		{
			$(this).addClass("hover");
		},
		function()
		{
			$(this).removeClass("hover");
		}
	);
	
	$('a.lightbox').lightBox();

	$("a[rel='external']").attr("target","_blank");
	
	$("p img.image-left").closest("p").css("padding","0");
	$("p img.image-right").closest("p").css("padding","0");
	
	$("p img.image-landscape").removeClass("image-landscape").closest("p").addClass("image-landscape");
	$("p img.image-landscape-left").removeClass("image-landscape-left").closest("p").addClass("image-landscape-left");
	$("p img.image-landscape-right").removeClass("image-landscape-right").closest("p").addClass("image-landscape-right");
	$("p img.image-portrait").removeClass("image-portrait").closest("p").addClass("image-portrait");
	$("p img.image-portrait-left").removeClass("image-portrait-left").closest("p").addClass("image-portrait-left");
	$("p img.image-portrait-right").removeClass("image-portrait-right").closest("p").addClass("image-portrait-right");
	$("p img.image-square").removeClass("image-square").closest("p").addClass("image-square");
	$("p img.image-square-left").removeClass("image-square-left").closest("p").addClass("image-square-left");
	$("p img.image-square-right").removeClass("image-square-right").closest("p").addClass("image-square-right");

	
	
	$(".home #col2 p").addClass("caption");
	$(".home #col2 p img").closest("p").removeClass("caption").addClass("image");
	
	
	var thumbnailSet= $("<div></div>");
	$(".home #col2 h2").each(
		function( intIndex )
		{
			var thumbnailDiv = $("<div class=\"featurebox\"></div>");
			$(this).clone().appendTo(thumbnailDiv);
			$(this).nextUntil('h2').detach().appendTo(thumbnailDiv);
			$(this).detach();
			thumbnailSet.append(thumbnailDiv);
		}
	);
	$(".home #col2").append(thumbnailSet);
	
	
	
	
	$(".home #col2 p.image img").each(
		function( intIndex )
		{
			var bgSrc = $(this).attr('src');
			$(this).closest("div.featurebox").css("background","transparent url("+bgSrc+") no-repeat scroll 0 0");
			$(this).closest("p.image").addClass("hidden");
		}
	);
	
	
	
	
	
	$(".featurebox").click(function()
	{
		window.location=$(this).find("a").attr("href");
		return false;
	});
	
	
	
	
	
	
	
	$(".featurebox").wrap("<div class=\"featureboxborder\"></div>");
	$(".featurebox").wrapInner("<div class=\"featureboxinner\"></div>");
	
	
	$(".home div.featurebox div.featureboxinner p.caption").hide(); 
	$(".home div.featurebox").mouseenter(
		function () 
		{
			$(this).children("div.featureboxinner").css("height","140px");
			if ($(this).find("p.caption").is(":hidden"))
				$(this).find("p.caption").show('fast');
		}
	);
	$(".home div.featurebox").mouseleave(
		function () 
		{
			$(this).children("div.featureboxinner").css("height","auto");
			if ($(this).find("p.caption").not(":hidden"))
				$(this).find("p.caption").hide('fast');
		}
	);
	
	
	
	
	$(".expandable h2").each(
		function( intIndex )
		{
			var closeLink = $("<p class=\"expandlink\"><a href=\"#\">Read more</a></p>");
			var expandDiv = $("<div class=\"expand\"></div>");
			
			$(this).next().next().nextUntil('h2').detach().appendTo(expandDiv);
			var introPara = $(this).next().next();
			$(introPara).after(expandDiv);
			$(expandDiv).after(closeLink);
			$(expandDiv).hide();
			
			$(closeLink).click(function () 
			{
				if ($(this).prev("div.expand").eq(0).is(":hidden")) 
				{
					$(this).prev("div.expand").eq(0).slideDown();
					$(this).find("a").text("Close");
					return false;
				} 
				else 
				{
					$(this).prev("div.expand").eq(0).slideUp();
					$(this).find("a").text("Read more");
					return false;
				}
			});
			
		}
	);
	$(".expandable h2").next("p").addClass("image");
	$(".expandable p").addClass("intro");
	$(".expandable .expand p").removeClass("intro");
	
	
	
	$(".expandable h2").each(
		function( intIndex )
		{
			var bookNowLink = $(this).find("a").eq(0);
			if(bookNowLink.length)
			{
				var bookNowPara = $("<p class=\"booknow\"></p>");
				
				var newBookNowLink = bookNowLink.clone();
				bookNowPara.append(newBookNowLink);
				bookNowLink.detach();
				$(this).after(bookNowPara);
			}
			
			var h2Width = $(this).textWidth();
			$(this).css("width",(h2Width+10)+"px");
			$(this).css("padding-right","0");
		}
	);
	
	$(".expandable .expand").each(
		function( intIndex )
		{
			$(this).find("p").first().addClass("clear");
		}
	);
	
	
	$(".expandable").append("<div class=\"layoutclear\"></div>");
	
	
	
	
	var landingSet= $("<div></div>");
	$(".landingindex h2").each(
		function( intIndex )
		{
			var landingItem = $("<div class=\"landingitem\"></div>");
			$(this).clone().appendTo(landingItem);
			$(this).nextUntil('h2').detach().appendTo(landingItem);
			$(this).detach();
			landingSet.append(landingItem);
		}
	);
	$("#main").append(landingSet);
	$(".landingitem p img").closest("p").addClass("image");
	
	var landingindextest = $("#main .landingitem h2");
	var paginationtest = $("#main .pagination");
	if(landingindextest.length && paginationtest.length)
	{
		paginationtest.clone().appendTo("#main");
		paginationtest.detach();
	}
	
	$(".landingitem").hover(
		function () { $(this).addClass("landingitemhover"); },
		function() { $(this).removeClass("landingitemhover"); }
	);
	
	
	
	
	
	
	var searchWidth = $("#search p").width()-67;
	$("#slogan").css("right",searchWidth+"px");
	
	
	
	
	
	
	
	
	$("img[alt^='Facilities: ']").each(
		function( intIndex )
		{
			var tooltip = $(this).attr("alt").replace('Facilities: ','');
			$(this).attr("title",tooltip);
		}
	);
	
	
	// Tooltips
	/* Tooltip code from: http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/ */
	$('img[alt^="Facilities: "]').tooltip(
	{
		track: true,
		delay: 0,
		showURL: false,
		fade: 100
	});
	$('#socialmedia a').tooltip(
	{
		track: true,
		delay: 0,
		showURL: false,
		fade: 100
	});
});





/* Google map functions */
$(window).resize(function()
{
	if(typeof loadMap==='function')
		loadMap();
});

$(window).unload(function()
{
	if(typeof GUnload==='function')
		GUnload();
});

$(document).ready(function()
{
	if(typeof loadMap==='function')
		loadMap();
});
