$(document).ready(function(){

	$('div.lightbox a').lightBox();

	// highlight fields on focus
	$(function(){$("fieldset input").focus(function(){$(this).parent('div.input').addClass("focus");});});
	$(function(){$("fieldset input").blur(function(){$(this).parent('div.input').removeClass("focus");});});
	$(function(){$("fieldset select").focus(function(){$(this).parent('div.input').addClass("focus");});});
	$(function(){$("fieldset select").blur(function(){$(this).parent('div.input').removeClass("focus");});});
	$(function(){$("fieldset textarea").focus(function(){$(this).parent('div.input').addClass("focus");});});
	$(function(){$("fieldset textarea").blur(function(){$(this).parent('div.input').removeClass("focus");});});

	// datepicker fields
	//$("input.datepicker_single").datepicker({dateFormat: 'dd/mm/yy'}); 

	// simple accordion
	$('div#accordion').accordion({
    	header: 'h3', 
		autoHeight: false,
		active: false,
		collapsible: true
	});
	
	// make table row clickable
	$('div#feature-list div.feature-clickable').bind('click', function(e){
		window.location = $(this).children('div.text').children('h3').children('a').attr('href');
	});

	// make promo-box clickable
	$('div.clickable').bind('click', function(e){
		url1 = $(this).children('h2').children('a').attr('href');
		url2 = $(this).children('div.padding').children('div.text').children('a').attr('href');
		//url3 = $(this).children('div').children('h2').children('a').attr('href');
		if (url1 != undefined) {
			window.location = url1;
		} else if (url2 != undefined) {
			window.location = url2;
		}
	});

	// make promo-box clickable
	$('div.news-promo-box ul.nav li').bind('click', function(e){
		window.location = $(this).children('a').attr('href');
	});

	$('input.searchbox').focus(function(){
		$(this).val('');
	});

	$('input#NewsSignupEmail').focus(function(){
		if ($(this).val() == 'Enter your email address') {
			$(this).val('');
		}
	});
	
	$('div#jobs-list div.more-details').hide();
	$('div#jobs-list a.view-more-link').bind('click', function(e){
		$(this).hide();
		$(this).parent().parent().children('div.more-details').fadeIn();
		return false;
	});
	
	$('a.close').bind('click', function(e){
		$('div#accordion div.ui-accordion-content-active').slideUp();
		$('div#accordion h3.ui-state-active').removeClass('ui-state-active').addClass('ui-state-default');
		$('div#accordion h3').attr('aria-expanded', 'false')
		return false;
	});

});

var ajaxSignUp = function() {
	if (echeck($('input#NewsSignupEmail').val())) {
		$("div#status-message").text('Adding your email address...');
		$.get("/news/signup/" + $("input#NewsSignupEmail").val(), function(data){
			$('div#status-message').text('Thanks, your email address has been added!');
			$("div#status-message").addClass('success');
		});
	} else {
		$("div#status-message").text('Please enter a valid email address');
		$("div#status-message").addClass('error');
	}
	return false;
}

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 return true					
}


