

var PowercorSite;

PowercorSite =
{
	onDocumentReady:	function()
	{
		
		//	Get template type
		var page_template = $('body').attr('id');
		
		//	Test getHeaderLinks();
		//this.getHeaderLinks();

		
		//	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);
			}
		}
		
	},
	
	
	//	Returns an associative array of links in the header, by looking at the
	//	HTML content.
	getHeaderLinks:	function()
	{
		//	Find out which sitenav link is active (is the current page)
		var active_link = $('#sitenav .active');
		var active_link_href = 	(active_link.length > 0) 
								? active_link.attr('href')
								: '';
										
		var links = 
		{
			'logo_citipower':		$('#logo_citipower').attr('href'),
			'logo_powercor':		$('#logo_powercor').attr('href'),
			'home_link':			$('#home_link').attr('href'),
			'corporate_link':		$('#corporate_link').attr('href'),
			'electricity_link':		$('#electricity_link').attr('href'),
			'other_businesses_link':$('#other_businesses_link').attr('href'),
			'career_link':			$('#career_link').attr('href'),
			'contractors_link':		$('#contractors_link').attr('href'),
			'community_link':		$('#community_link').attr('href'),
			'customer_link':		$('#customer_link').attr('href'),
			'active_link':			active_link_href
		};
		
		return links;
	},
	
	
	//	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 = '/public/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);
					}
				}
			);
	},
	
	increaseTextSize: function()
	{
		size = $.cookie('text-size');
		
		switch (size)
		{
			case 'biggest':
				break;
				
			case 'bigger':
				PowercorSite.applyTextSize('biggest');
				break;
				
			case 'big':
				PowercorSite.applyTextSize('bigger');
				break;
				
			default:
				PowercorSite.applyTextSize('big');
		}
	},
	
	decreaseTextSize: function()
	{
		size = $.cookie('text-size');
		
		switch (size)
		{
			case 'biggest':
				PowercorSite.applyTextSize('bigger');
				break;
				
			case 'bigger':
				PowercorSite.applyTextSize('big');
				break;
				
			case 'big':
				PowercorSite.applyTextSize('normal');
				break;
				
			default:
		}
	},
	
	applyTextSize: function(size)
	{
		var cookie_options = 	{
									expires: 7, // days
									path: '/'
								};
		
		if (size==null)
		{
			size = $.cookie('text-size');
		}
		
		
		$('body').removeClass('font_biggest');
		$('body').removeClass('font_bigger');
		$('body').removeClass('font_big');
				
		switch (size)
		{
			case 'biggest':
				$('body').addClass('font_biggest');
				$.cookie('text-size', 'biggest', cookie_options);
				break;
			
			case 'bigger':
				$('body').addClass('font_bigger');
				$.cookie('text-size', 'bigger', cookie_options);
				break;
			
			case 'big':
				$('body').addClass('font_big');
				$.cookie('text-size', 'big', cookie_options);
				break;
				
			case 'normal':
			default:
				$.cookie('text-size', null, cookie_options);
				break;
				
		}
	},
	
	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 = PowercorSite.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 = PowercorSite.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 (PowercorSite.elementBehaviours['h3']);
			PowercorSite.elementBehaviours['#latest_news_summary > h3'] = function(elms)
			{
				PowercorSite.imageHeadings(elms, 'Latest News Title');
			}
			
			PowercorSite.elementBehaviours['#latest_news_summary ul h3'] = function(elms)
			{
				PowercorSite.imageHeadings(elms, 'Subheading Grey');
			}
			
		}
	},
	
	elementBehaviours:
	{
		
		//	Replace H2s with Image Heading
		'h2': function(elms)
		{
			PowercorSite.imageHeadings(elms, 'Subheading White');
		},
		
		//	Replace H3s with Image Heading
		'#content_main_container h3': function(elms)
		{
			PowercorSite.imageHeadings(elms, 'Subheading White');
		},
		
		'#sidebar h3': function(elms)
		{
			PowercorSite.imageHeadings(elms, 'Subheading Grey');
		},
		
		
		//	Put accordion functionality on H5
		'#content_main_container h5': function(elms)
		{
			PowercorSite.imageHeadings(elms, 'Subheading White');
			
			if ($('#print').length == 0)
			{
				
				$('#content_main_container').Accordion(
					{
						header:		'h5',
						active:		false,
						alwaysOpen:	false
					}
				);
				
				//	Add :hover support via .hover class
				if ($.browser.msie)
				{
					$('#content_main_container h5').mouseover(
						function()
						{
							$(this).addClass('hover');
						}
					);
					
					$('#content_main_container h5').mouseout(
						function()
						{
							$(this).removeClass('hover');
						}
					);
				}
			}
		},
		
		//	Replace H1s with Image Heading
		'#page h1': function(elms)
		{
			PowercorSite.imageHeadings(elms, 'Page 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("/public/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();
						
				}
			}
		},

		'#outages_map': function(elm)
		{
			var template_type = elm.attr('class'); // citipower, powercor

			var width = 0;
			var height = 0;
			var url = false;
			
			switch (template_type)
			{
				case 'selector_map':
					url = '/selector_map.swf';
					width = 548;
					height = 426;
					break;
			
				case 'citipower_outages_map':
					url = '/citipower_outages_map.swf';
					width = 548;
					height = 390;
					break;
					
				case 'powercor_outages_map':
					url = '/powercor_outages_map.swf';
					width = 548;
					height = 590;
					break;
				default:
			}
			
			if (!url) {
				return;
			}

			var so = new SWFObject(url, "flashmap", width, height, "8", "#ffffff");
			
			so.addParam('scale','noScale');
			
			so.addVariable('vic_url', '/Power_Outages/Powercor_-_Power_Outages/');
			so.addVariable('melb_url', '/Power_Outages/CitiPower_-_Power_Outages/');
			so.addVariable('melb_window_mode', '_self');
			so.addVariable('vic_window_mode', '_self');
			
			so.addVariable('template', elm.attr('class'));
			if ( ! ($('#print').size() > 0))
			{
				
				if (document.location.hash == '#noflash')
				{
					//alert('Flash map disabled');
				} else {
					so.write("outages_map");
				}
			}
		},
		
		//	Add text-sizing functionality
		'#page_tools > .apply_font_big': function(elms)
		{
			
			PowercorSite.applyTextSize();
			
			$('#page_tools > .apply_font_big a').click(
				function()
				{
					PowercorSite.increaseTextSize();
				}
			);
			
			$('#page_tools > .apply_font_normal a').click(
				function()
				{
					PowercorSite.decreaseTextSize();
				}
			);
		},
		
		//	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');
					}
				}
			)
		},
		
		//	News fading-ticker on homepage
		
		'#latest_news_summary':	function(elm)
		{
			if ( ! ($('#home_page').length > 0)) return;
			if ( ! ($('#print').length == 0)) return;
			
			$('#latest_news_summary .last_visible').removeClass('last_visible');
			$('#latest_news_summary .invisible').removeClass('invisible');
				
			$('#latest_news_summary > ul').innerfade({
				speed:		'slow',
				timeout:	10000
			});
			
			if (false && elm)
			{
				
			
				//	Hide all items
				$('li', elm).hide();
				var tickerFunction;
				
				tickerFunction = function()
				{
					clearInterval(tickerFunction.intervalId);
					
					var last_visible = $('.last_visible', elm);
					var next_item;
					if (last_visible.length > 0)
					{
						last_visible.removeClass('last_visible');
						
						next_item = last_visible.next();
						
						if (next_item.length == 0)
						{
							next_item = $('li:first', elm);
						}
						
					} else {
						last_visible = null;
						
						next_item = $('li:first', elm);
					}
					
					next_item
						.addClass('last_visible')
						.fadeIn(1000)
						.animate({opacity: 1.0}, 8000)
						.fadeOut(1000);
					
					tickerFunction.intervalId = setInterval(tickerFunction, 10500);
				};
				
				tickerFunction();			
			}
		}
	}
	
};

$(document).ready( function()	{ PowercorSite.onDocumentReady();	}	);