setupHomepageTabs = function() {
	$('.tab_c').hide();
	$('#tab1_c').show();
	
	$('.home #tabs li a').click(function() {
		clearInterval(gTimer);
		var tabId = $(this).attr('id');
		$('.tab_c').hide();
		$('#'+tabId+'_c').show();
		
		$('#tabs li a').removeClass('active');	
		$(this).addClass('active');
		
		return false;
	});
}

is_iPhone = function() {
	return ((navigator.userAgent.match(/iPhone/i)));
}

iPhoneHideToolbar = function() {
	setTimeout(function() {window.scrollTo(0, 1)}, 100);
}

var currentWidth=0;
iPhoneUpdateLayout = function() {
	if(window.innerWidth != currentWidth) {
		currentWidth = window.innerWidth;
		var orient = (currentWidth == 480) ? "portrait" : "landscape";
		if(orient == 'portrait') {
			$('body').removeClass('landscape');
			$('body').addClass('portrait');
		}
		if(orient == 'landscape') {
			$('body').removeClass('portrait');
			$('body').addClass('landscape');
		}
	}
	
	if(document.getElementById('newsticker')) {
		document.getElementById('newsticker').style.display='none';
	}
}

/* 
* google maps loading code
*/
var map;
var gDir;
var point

function gMapLoad() {
      if (GBrowserIsCompatible()) {
        
        map = new GMap2(document.getElementById("map"));
        point= new GLatLng(52.772487,-1.204956);
        map.setCenter(point, 11);
        map.addControl(new GSmallZoomControl());
        
        var marker=new GMarker(point);
        var windowContent="<b>Baca</b><br />19/20 Baxter Gate<br />Loughborough<br />Leicestershire<br/>LE11 1TG<br /><br /><strong>01509 550438</strong><br /><br /><form action=\"http://maps.google.co.uk/maps\" method=\"get\" target=\"_blank\"><input id=\"daddr\" class=\"text\" type=\"hidden\" size=\"10\" value=\"LE11 1TG\" name=\"daddr\"/><label for='saddr'>Enter your postcode</label><br /><input id=\"saddr\" class=\"text\" type=\"text\" size=\"10\" value=\"\" name=\"saddr\" style=\"margin-right:10px;\" /><input type='submit' value='Get Directions!' name='submit'  /><br/>&nbsp;<br/>&nbsp;</form>"
        map.addOverlay(marker);
        marker.openInfoWindowHtml(windowContent);
        GEvent.addListener(marker,"click",function(){
          marker.openInfoWindowHtml(windowContent);
        });
        
        gDir=new GDirections(map,document.getElementById("directionsCon"));
        GEvent.addListener(gDir,"load",onGDirectionsLoad);
        GEvent.addListener(gDir, "error", handleErrors);
      }
      
    }

    function setDirections(fromAddress){
      gDir.load("from: "+fromAddress+", UK to: 52.772487,-1.204956"); 
    }
    
    function onGDirectionsLoad(){
    }
    
    function handleErrors(){
        if (gDir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gDir.getStatus().code);
        else if (gDir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gDir.getStatus().code);
        
        else if (gDir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gDir.getStatus().code);
        
        else if (gDir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gDir.getStatus().code);
        
        else if (gDir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gDir.getStatus().code);
        
        else alert("An unknown error occurred.");
     }

function validateContactForm() {
	var isValid=true;
	var isEmailValid=true;
	
	if($('#pname').val() == '') {
		showErr('pname');
		isValid=false;
	} else {
		hideErr('pname');
	}
	
	if($('#pemail').val() == '') {
		showErr('pemail');
		isValid=false;
		isEmailValid=false;
	}
	
	if(!validateEmail($('#pemail').val())) {
		showErr('pemail');
		isValid=false;
		isEmailValid=false;
	}
	
	if(isEmailValid) {
		hideErr('pemail');
	}
	
	if($('#pmessage').val() == '') {
		showErr('pmessage');
		isValid=false;
	} else {
		hideErr('pmessage');
	}
	
	return isValid;
}

function showErr(elId) {
	$('#'+elId+'Err').slideDown('slow');
}

function hideErr(elId) {
	$('#'+elId+'Err').slideUp('slow');
}

function validateEmail(email) {
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(email)){         
		return true;   
    } else {   
		return false;
    }
}

function initNewsTicker() {
	var options = {
		newsList: "#newsticker",
		startDelay: 10,
		tickerRate: 140,
		controls: false,
		placeHolder1: " "
	}
	$.ajax({
		type: "GET",
		url: "/news/rss",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('item').each(function() {
				var desc = $(this).find('title').text();
				var link = $(this).find('link').text();
				var idate= $(this).find('pubDate').text();
				idate = idate.split(':');
				idate = idate[0];
				idate = idate.substring(0, idate.length -3);
				$('<li><a href="'+link+'">LATEST: '+desc+' ('+idate+')</a></li>').appendTo('ul#newsticker');
			});
			$().newsTicker(options);
		}
	});
}

toggleHomepageTabs = function() {
	if($('#tab1_c').css('display') == 'block') {
		$('#tab1').removeClass('active');
		$('#tab2').addClass('active');
		
		$('#tab1_c').fadeOut('slow', function() {
			$('#tab2_c').fadeIn('slow');
		});
	} else {
		$('#tab2').removeClass('active');
		$('#tab1').addClass('active');
		
		$('#tab2_c').fadeOut('slow', function() {
			$('#tab1_c').fadeIn('slow');
		});
	}
}
