/* ======================================================================================
 * Filename: theme/javascript/functions.js
 * Author: Webmistress, info@webmistress.com.au, www.webmistress.com.au
 * Copyright: Webmistress except where noted
 * Date: 18 November 2011
 * Description: JavaScript functions for Acutel Consulting's website.
 *
 * This file may not be used for any purpose other than for the Acutel Consulting 
 * website and may not be copied or modified without written permission from the author.
 * ======================================================================================
 */
 
function initialise_gmap() 
{
	if ($('#gmap').length)
	{
		var centrepoint = new google.maps.LatLng(-42.8628340,147.3707790);
		var latlng = new google.maps.LatLng(-42.8668205,147.3708484);
		var myOptions = {
			zoom: 15,
			center: centrepoint,
			mapTypeId: google.maps.MapTypeId.HYBRID 
		};
		var map = new google.maps.Map(document.getElementById("gmap"),myOptions);
		var marker1 = new google.maps.Marker({
			position: latlng,
			map: map
		});
		
		var contentString = '<div><img src="../images/acutel_office.jpg" alt="Acutel Consulting office, 5/13 Bayfield St, Rosny Park Tasmania 7018" /><br />5/13 Bayfield St, Rosny Park</div>';
		
		var infoWindow = new google.maps.InfoWindow({
		    content: contentString,
		    maxWidth: 200
		});
	  
		google.maps.event.addListener(marker1, 'click', function() { infoWindow.open(map,marker1); });
		infoWindow.open(map, marker1);
	}
}

$(document).ready(function()
{	
	$("a[rel='external']").attr("target","_blank");
	
	$(".page #content .col2").wrap("<div class=\"col2-wrapper\"></div>");
	
	var heightintro = $("#intro").height();
	
	if ($('body.home #intro .col2').length)
	{
		$("#intro .col2").css("height",(heightintro-44)+"px");
	}
	
	if ($('body.landing #intro .col2').length)
	{
		$("#intro .col2").css("height",(heightintro-44)+"px");
	}
	
	if ($('body.page #content .col2').length)
	{
		$("#content .col2-wrapper").css("margin-top","-"+(heightintro-8)+"px");
		$("#content .col1").css("margin-top","0");
	}
	
	$("h3").each(
		function( intIndex )
		{
			var h3TextWidth = $(this).textWidth();
			$(this).css("width",(h3TextWidth + 5)+"px");
		}
	);
	
	$(".expandable .expand").hide();
	
	$(".expandable").each(
		function( intIndex )
		{
			var closeLink = $("<p class=\"expandlink\"><a href=\"#\">Read more</a></p>");
			var expandDiv = $(this).find(".expand");
			var expandHeading = $(this).find("h2");
			
			$(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").addClass("closelink").text("Close");
					return false;
				} 
				else 
				{
					$(this).prev("div.expand").eq(0).slideUp();
					$(this).find("a").removeClass("closelink").text("Read more");
					return false;
				}
			});
			
		}
	);
	
	initialise_gmap();
});
 
//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;
};
