

var MsbSite;

MsbSite =
{
	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();
		
		var ie6 = document.getElementById('ie6');
		if (ie6 == undefined) {
			var pb = document.getElementById('page-background');
			pb.style.backgroundImage = '';
		}
	},
	
	//	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];
							
							var margin = '';
							
							if (word.indexOf("'") > 0) {
								margin = 'style="margin-top:-1px;"';
							}
							
							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=''" + margin  + " />";
							}
						}
						
						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
		'h2': function(elms)
		{
			MsbSite.imageHeadings(elms, 'Text Headings');
		},
		
		//	Replace H3s with Image Heading
		'h3': function(elms)
		{
			MsbSite.imageHeadings(elms, 'Sub Headings');
		},
		
		'h4': function(elms)
				{
					MsbSite.imageHeadings(elms, 'Sub Headings Selected');
		},
		
		'h5': function(elms)
						{
							MsbSite.imageHeadings(elms, 'Divider Selected');
		},
		
		//	Replace H3s with Image Heading
		'#content_main_container h3': function(elms)
		{
			MsbSite.imageHeadings(elms, 'Subheading White');
		},
		
		'#sidebar h3': function(elms)
		{
			MsbSite.imageHeadings(elms, 'Subheading Grey');
		},
		
		
		//	Put accordion functionality on H5
		'#content_main_container h5': function(elms)
		{
			MsbSite.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)
		{
			MsbSite.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("/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');
					}
				}
			)
		},
		//  date picker
		".datePicker" :function($elms){ 
			$(".datePicker").datepicker({ 
			    showOn: "both", 
			    buttonImage: "/images/icon_calendar.gif", 
			    buttonImageOnly: true 
			});
		},		
	
		//	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 display_enquiry_frame() {
					var selected = document.getElementById("enquiry_type").value;
					if (selected == 'booking') {
						document.getElementById("booking_enquiry").style.display = 'block';
						document.getElementById("enquiry_common").style.display = 'block';
						document.getElementById("general_enquiry").style.display = 'none';
						document.getElementById("formText").style.height = '1110px';
					
					} else if (selected == 'general') {
						document.getElementById("general_enquiry").style.display = 'block';
						document.getElementById("enquiry_common").style.display = 'block';
						document.getElementById("booking_enquiry").style.display = 'none';
						document.getElementById("formText").style.height = '1600px';
						/*if ie7... and error exists - extend block more.

				
						if(window.XMLHttpRequest && document.all  ){
						      $("div").each(function () {
						        if (this.id == "error_notices") {
							        	document.getElementById("formText").style.height = '2000px';
						        } else {
						          //do nothing
						        }
						      });
				
				
						}*/
						//document.getElementById("msb_add_contact_form").style.height = '2051px';
					} else {
						document.getElementById("booking_enquiry").style.display = 'none';
						document.getElementById("general_enquiry").style.display = 'none';
						document.getElementById("enquiry_common").style.display = 'none';
						document.getElementById("formText").style.height = '450px';
					}  

}
//makes the current link bold and removes the boldness from the previous link
function setBold(target){
	//remove current links bold
        $(".customUl #isBold").attr("style", "font-weight:normal;");
        $(".customUl #isBold").removeAttr("id");
	//set new bold
 	$(".customUl li a:eq(" + target + ")").attr("style", "font-weight:bold; color:#CC0001;");
        $(".customUl li a:eq(" + target + ")").attr("id", "isBold");
}

//ensure that all tables have their first td of each row as red bg
function render_tables(){
//	$("td:first").addClass("redBg");
//	 $("tr:has(td)").addClass("redBg");
	//only set this to tables that class are null
		if($("table").attr("class") == undefined){
			  $("table").attr("width", 500);
			  $("table").addClass("rateTable");
			  $("table").attr("cellspacing", 2);
			  $("#text table").attr("border", 1);
		          $("tr td:first-child").addClass("redBg");
                          $("table tr td:first-child").prepend("<div class='whiteLine' style='width:100%; border-top:solid 1px white;'></div>");


			  $("table tr:first-child").addClass("redText");
		}

}
$(document).ready(function()	{
	MsbSite.onDocumentReady();
	//if page contains form.... then.. call this..
      //  display_enquiry_frame();
	so.write("header");
	po.write("pics");
	if (typeof(pb) != "undefined") {
		pb.write("below_feature");
	}


						      $("div").each(function () {
						        if (this.id == "error_notices") {
                                                              display_enquiry_frame();
						        }
	   						 });


	if (typeof(co_collection) != 'undefined') {
		for (var i = 0; i < co_collection.length; i++) {

			co_collection[i].write(co_collection[i].divId);

		}
	}

});

function clearForms()
{
  $(':input').each(function() {
   var type = this.type;
   var tag = this.tagName.toLowerCase();
	if (type == 'text' || type == 'password' || tag == 'textarea')
		this.setAttribute('value','');
  });
}

function poppage(mypage,w,h,s){
	var scr = s;
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	settings='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,resizable=yes,dependent=no'
	windowname = randomString(6);
	win=window.open(mypage,windowname,settings)
	win.focus()
}
