var dom = document.getElementById ?	true:false;
var ie  = document.all ? true:false;
var saf = (navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
var opr = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);
var classAttribute = (ie && !opr) ? 'className' : 'class';
var map;

$(document).ready( function() {
	if($('#homeTabs').length != 0){
		$('#homeTabs LI A').click(function(e){
			currentTab = $(this).attr('href').substr(1,$(this).attr('href').length);
			// not using show() and hide() as they apply/remove display:block which breaks the layout
			switch(currentTab){
			case "welcomeTab":
			{
				$('#welcomeTab').removeClass('inactive');
				$('#newsTab').addClass('inactive');
				$('#homeTabs').removeClass('news').addClass('welcome');
			}
			break;
			case "newsTab":
			{
				$('#welcomeTab').addClass('inactive');
				$('#newsTab').removeClass('inactive');
				$('#homeTabs').removeClass('welcome').addClass('news');
			}
			break;
			}
			e.preventDefault();
		})
		$('#homeTabs .welcomeButton A').click();
	}
	
	if ($(".googleMap").size() > 0) {
	
		function createMap() {
			map = new GMap2(document.getElementById("googlePlaceholder"));
	    	map.setCenter(new GLatLng($('#googleMapLatitude').text(), $('#googleMapLongitude').text()), 15);
	    	var mm = new GMarkerManager(map); 
	    	mm.addMarker( new GMarker(new GPoint($('#googleMapLongitude').text(),$('#googleMapLatitude').text())) ,0,17);
		}
		
		$(document).ready( function() {
			if (GBrowserIsCompatible()) {
		    	createMap(false);
			}
		});
		
		$(window).unload( GUnload );
	
		$(".googleMap A.expandmap").click( function(e) {
			e.preventDefault();
			if ($(this).hasClass('expand')) {
				$(this).removeClass('expand').addClass('trim');
				$('.googleMap').addClass('googleMapBig');
				createMap(true);
			}
			else {
				$(this).removeClass('trim').addClass('expand');
				$('.googleMap').removeClass('googleMapBig');
				createMap(true);
			}
		});
	}
	
	if($(".investorForm").size() > 0) {
		$("#corporate").click( function() {
			$("#private_form").hide('slow', function() {
				$("#corporate_form").show('slow');
			});	
			
			
			$("#corporate_form .head A:first").filter('.expand').click();
		});
		
		$("#private").click( function() {
			$("#corporate_form").hide('slow', function() {
				$("#private_form").show('slow');
			});
			
			
			$("#private_form .head A:first").filter('.expand').click();
		});
	
		$(".investorForm .head A").click( function(e) {
			e.preventDefault();
			
			if ($(this).hasClass('expand')) {
				//$(".investorForm .head A").removeClass('collapse').addClass('expand');
				//$(".investorForm .content").hide();
				
				$(this).removeClass('expand').addClass('collapse');
				$("#"+$(this).attr('rel')).show();
			}
			else {
				if (!$(this).hasClass('focused')) {
					$(this).removeClass('collapse').addClass('expand');
					$("#"+$(this).attr('rel')).hide();
				}
				
				$(this).removeClass('focused');
			}
			
			e.stopPropagation();
		});
		
		$(".investorForm .head A").focus( function(e) {
			
			if ($(this).hasClass('expand')) {
				$(this).addClass('focused');
				
				$(this).removeClass('expand').addClass('collapse');
				$("#"+$(this).attr('rel')).show();
				setTimeout( "$('A[rel=\""+$(this).attr('rel')+"\"]').removeClass('focused')", 500 );
			}
			
			e.stopPropagation();
		});
		
		
		//$(".investorForm .content").hide();
		
		/*$("span.error").parents('.content').each( function() {
			$('a[rel="'+$(this).attr('id')+'"]').click();
		});*/
		
		
		
		//form validation
		function investorValidateRequired(field, message) {
			$('span#error_'+field.attr('id')).remove();
			
			if ($.trim(field.val())=='') {
				field.after('<span class="error" id="error_'+field.attr('id')+'">'+message+'</span>');
			}
		}
		
		function investorValidateEmail(field, message) {
			$('span#error_'+field.attr('id')).remove();
			
			var filter = /^[A-z0-9][\w.-]*@[A-z0-9][\w\-\.]+\.[A-z0-9]{2,6}$/;
			
			if ($.trim(field.val())=='' || false == filter.test(field.val()) ) {
				field.after('<span class="error" id="error_'+field.attr('id')+'">'+message+'</span>');
			}
		}
		
		$('input#pf_fname,input#cf_fname').blur( function() { investorValidateRequired($(this), 'Please enter your first name'); });
		$('input#pf_sname,input#cf_sname').blur( function() { investorValidateRequired($(this), 'Please enter your last name'); });
		//$('input#pf_hometel,input#cf_hometel').blur( function() { investorValidateRequired($(this), 'Please enter your home telephone number'); });
		$('input#pf_email,input#cf_email').blur( function() { investorValidateEmail($(this), 'Please enter a valid email address'); });
		$('select#pf_hear_about,select#cf_hear_about').blur( function() { investorValidateRequired($(this), 'Please enter how you heard about us'); });
		$('input#pf_hh_other').blur( function() { 
			if ($('select#pf_hear_about').val()=='Other') {
				investorValidateRequired($(this), 'Please enter how you heard about us');
			}
		});
		$('input#cf_hh_other').blur( function() { 
			if ($('select#cf_hear_about').val()=='Other') {
				investorValidateRequired($(this), 'Please enter how you heard about us');
			}
		});
	}	
	
	// apply onfocus state change for main navigation for IE6/7
	$('.mainnav LI A')
	.mouseover(function(){
		// clear focus state on menu entry if the user hovers over another menu item
		$(".mainnav LI[class*='_focus'] A").each(function(){
			var menuNode = $(this).parent('LI').slice(0,1);
			var menuClass = $(menuNode).attr('class');
			var menuMin = menuClass.substr(0,(menuClass.length - 6));
			$(menuNode).removeClass(menuClass).addClass(menuMin);
		});
	})
	.focus(function(){
		var menuNode = $(this).parent('LI').slice(0,1);
		var menuClass = $(menuNode).attr('class');
		if(menuClass.indexOf('_on') == -1){
			$(menuNode).removeClass(menuClass).addClass(menuClass + '_focus');
		} else {
		}
	})
	.blur(function(){
		var menuNode = $(this).parent('LI').slice(0,1);
		var menuClass = $(menuNode).attr('class');
		if(menuClass.indexOf('_on') == -1 && menuClass.indexOf('_focus') != -1){
			var menuMin = menuClass.substr(0,(menuClass.length - 6));
			$(menuNode).removeClass(menuClass).addClass(menuMin);
		}
	});
	
	// handle user clicking on 'Print page' button
	$('.printLink').click(function(e){
		window.print();
		e.preventDefault();
	});
	
	
	//news ticker
	$("ul#ticker").liScroll({travelocity: 0.05});
	
	// initialise sIFR
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:".lead H1,.wideLead H1", sFlashSrc:"/assets/swf/georgia.swf", sColor:"#ffffff", sLinkColor:"#000000", sBgColor:"#FF0000", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0", sWmode: 'transparent'}));
		sIFR.replaceElement(named({sSelector:".lead H2,.wideLead H2", sFlashSrc:"/assets/swf/georgia.swf", sColor:"#ffffff", sLinkColor:"#000000", sBgColor:"#FF0000", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0", sWmode: 'transparent'}));
		sIFR.replaceElement(named({sSelector:".lead H3,.wideLead H3", sFlashSrc:"/assets/swf/georgia.swf", sColor:"#ffffff", sLinkColor:"#000000", sBgColor:"#FF0000", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0", sWmode: 'transparent'}));
	}
});