

var LemsecSite;

LemsecSite =
{
	onDocumentReady:	function()
	{
		
		//	Get template type
		var page_template = $('body').attr('id');
		
		//	Initialise behaviours for this template
		if (this.pageBehaviours[page_template])
			this.pageBehaviours[page_template]();		

		//	Initialise behaviours for elements, if they exist
		for (var element_id in this.elementBehaviours)
		{
				
			var elm = $(element_id);

			if (elm.length > 0)
			{
				
				this.elementBehaviours[element_id](elm);
			}
		}
		
		fNewPDFWindows();
		
	},
	
	//	Perform image replacement on elements containing text only.
	//	If the element is contained within  <a href=''></a> then 
	//	the tag is rebuilt inside the elements, to fix the links not
	//	working in IE.
	imageHeadings: function(elms, heading_style)
	{
		var heading_style_safe = escape(heading_style);
		
		elms.each(
				function()
				{
					var _this = $(this);
					var replacement_html = '';
					
					
					var text = _this.text();
					
					if (text.length > 0)
					{
						
						var words = text.split(' ');
						
						for (word_index in words)
						{
							var word = words[word_index];
							
							if (word.length > 0)
							{
								var heading_url = '/cms/modules/image_headings/render_heading.php?text=' + escape(word) + '&amp;stylename=' + heading_style_safe;
								replacement_html = replacement_html + "<img src='" + heading_url + "' alt='' />";
							}
						}
						
						if (_this.parent().attr('href') != null)
						{
							var href = _this.parent().attr('href');
							replacement_html = "<a href='" + href + "'>" + replacement_html + "</a>";
						}
						_this.html(replacement_html);
					}
				}
			);
	},
	
	
	pageBehaviours:	
	{
		standard_page:	function()
		{
			
				
			//	Disable Flash if using a print layout
			if ((document.location.hash != '#noflash') && $('#print').length == 0)
			{
				//	Add swf version of sitenav
				var so = new SWFObject("/sitenav.swf", "sitenav_flash", "750", "158", "8", "#ffffff");
				
				//	Set the Nav template to use
				so.addVariable("template", "standard_page");
				
				//	Get header links
				var links = MsbSite.getHeaderLinks();
				
				//	Add links as variables for the swf
				for (var var_name in links)
				{
					so.addVariable('url_' + var_name, escape(links[var_name]));	
				}
				
				so.write("header");
			}
						
		},
		
		external_page:	function()
		{
			//	Set iframe to a proportion of screen height
			
			if (window.innerHeight > 0)
			{
				$('#external_page_iframe').css('height',  Math.floor(Math.max(350, window.innerHeight * 0.8)));
			}
			
			
			
			this.standard_page();
			
			
			
		},
		
		home_page:		function()
		{
			
			//	Disable Flash if using a print layout
			if ((document.location.hash != '#noflash') && $('#print').length == 0)
			{
				//	Add swf version of sitenav
				var so = new SWFObject("/sitenav.swf", "sitenav_flash", "750", "278", "8", "#ffffff");
				
				//	Set the Nav template to use
				so.addVariable("template", "home_page");
				
				//	Get header links
				var links = MsbSite.getHeaderLinks();
				
				//	Add links as variables for the swf
				for (var var_name in links)
				{
					so.addVariable('url_' + var_name, escape(links[var_name]));	
				}
					
				so.write("header");
			}
			
			//	Use a different heading style for h3 on the Home page
			delete (MsbSite.elementBehaviours['h3']);
			MsbSite.elementBehaviours['#latest_news_summary > h3'] = function(elms)
			{
				MsbSite.imageHeadings(elms, 'Latest News Title');
			}
			
			MsbSite.elementBehaviours['#latest_news_summary ul h3'] = function(elms)
			{
				MsbSite.imageHeadings(elms, 'Subheading Grey');
			}
			
		}
	},
	
	elementBehaviours:
	{
		//	Replace H2s with Image Heading
		'h3': function(elms)
		{
			LemsecSite.imageHeadings(elms, 'Sub Heading');
		},
		
		//	Presence of Print format -- start printing
		'#print':	function(elm)
		{
			$('body').css('background-color','white');
			window.print();
		},
		
		//	Presence of search form: Auto fill with "Enter search here"
		'#quick_search input.textfield': function(elm)
		{
			var empty_string = elm.val();
			
			elm.focus( 
				function()
				{
					if (this.value == empty_string)
					{
						this.value = '';
					}
				}
			);
			
			elm.blur(
				function()
				{
					if ( ! (this.value.length > 0))
					{
						this.value = empty_string;
					}
				}
			);
		},
		
		
		//	Calendar box for date inputs
		'input.date':	function(elms)
		{
			elms.each(
				
				function()
				{
					return;
					var calendar = new ctlSpiffyCalendarBox('date_' + this.id, '', this.id, 'btnDate1', this.value);
					
					calendar.dateFormat = 'yyyy-MM-dd';
					
					$(this).append( calendar.writeControl(true) );
					
					
					//alert($(elm).html());
					//$(elm).append( calendar.writeControl(false) );
					//$(elm).css('border', '1px solid red;');
					//$(elm).wrap("<div style='border:1px solid red;'></div>");
					
					
					//calendar.writeControl();
					
				}
			);
		},
		
		//	Confirmation dialog for resetting a form 
		'input.reset': function(elms)
		{
			elms.click(
				function(e)
				{
					var confirm_result = confirm('Are you sure you want to clear this form?');
					
					return confirm_result;
				}
			);
		},
		
		
		'#map_content': function(elm)
		{
			var template_type = elm.attr('class'); // citipower, powercor

			var width = 0;
			var height = 0;
			
			switch (template_type)
			{
				
				case 'citipower':
					width = 466;
					height = 426;
					break;
				case 'powercor':
					width = 466;
					height = 540;
					break;
				default:
			}

			var so = new SWFObject("/cms/modules/flashmap/flashmap.swf", "flashmap", width, height, "8", "#ffffff");
			
			so.addParam('scale','noScale');
			//if ( ! template_type) template_type = 'citipower';	
			
			so.addVariable('template', elm.attr('class'));
			if ( ! ($('#print').size() > 0))
			{
				
				if (document.location.hash == '#noflash')
				{
					//alert('Flash map disabled');
				} else {
					so.write("map_content");
				}
				
				
				//	Check if SWFObject didn't succeed (leaves the list of locations in place)
				if ($('#map_content .locations').size() > 0)
				{
					// Javascript fall-back on the flash map
					
					$('.markers a').click(
						function(event)
						{
								
							$('.locations dd').hide();
							
							var target_class = '.details_' + this.hash.replace('#', '');
							
							
							//$(target_class + ' h2').show();
							$(target_class).slideDown('normal');

							event.preventDefault();
						}
					);
					
					$('.locations dd').hide();
						
				}
			}
		},
		
		//	Set external site links to open in new window, unless target specified
		'a[@href^=http:]': function(elms)
		{
			var site_host = document.location.protocol + '//' + document.location.host;	
			
			elms.each(
				function()
				{
					if ((this.href.indexOf(site_host) != 0)
						&& ! (this.target.length > 0))
					{
						$(this).attr('target', '_blank');
					}
				}
			)
		},
		
		//	Set pdf links to open in new window, unless target specified
		'a[@href$=.pdf]': function(elms)
		{
			
			elms.each(
				function()
				{
					if (! (this.target.length > 0))
					{
						$(this).attr('target', '_blank');
					}
				}
			)
		}
	}
	
};

//
//	Open links with pdf documents in a new window
//
function fNewPDFWindows()  {  
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var eleLink=0; eleLink < links.length; eleLink ++) {
		if (links[eleLink].href.indexOf('.pdf') !== -1) {
			links[eleLink].onclick =  function() {
				window.open(this.href,'resizable,scrollbars');
				return false;  
			}    
		}  
	}  
}

function cr_frame(caller) {
	var curr = document.getElementById("cr_table_page1");
	var curr_foot = document.getElementById("cr_table_page1_foot");
	var past = document.getElementById("cr_table_page2");
	var past_foot = document.getElementById("cr_table_page2_foot");
	if (caller.id == 'past_cr_link') {
		curr.style.display = 'none';
		curr_foot.style.display = 'none';
		past.style.display = 'block';
		past_foot.style.display = 'inline';
	} else {
		curr.style.display = 'block';
		curr_foot.style.display = 'inline';
		past.style.display = 'none';
		past_foot.style.display = 'none';
	}
}

function switch_staff(caller, numElems) {
	var item = document.getElementById(caller);
	var content_main = document.getElementById('content_main');
	
	for (var c=1; c <= numElems; c++) {
		var elem = 'team_detail' + c;
		if (elem != caller) {
			element = document.getElementById(elem);
			element.style.display = 'none';
		}
	}
	
	content_main.style.display = 'none';
	item.style.display = 'inline';
	
	return false;
}

function select_all(item) {
	if ((item.value == 'First Name') || (item.value == 'Last Name') || (item.value == 'Email Address') || (item.value == 'Your Message')) {
		item.focus();
		item.select();
	}
}



$(document).ready( function()	{ 
	LemsecSite.onDocumentReady();		

	if (typeof co_collection != 'undefined') {
		for (var i = 0; i < co_collection.length; i++) {
			co_collection[i].write(co_collection[i].divId);	
		}
	}
});
