$(document).ready(function() {
	chrUrl = $('#chrHost').text() + 'ajaxGateway/ajaxGateway.cfm?ms=' + new Date().getTime();
	$.ajaxSetup({
		url: chrUrl,
		error: function(data) {
			alert('We apologise for any inconvenience, an error as occurred on the website');
			//js mail here?	
		}
	});
	
	$('.menuitem').bind('mouseover', function() {
		$(this).attr('class','highlight');
	});
	$('.menuitem').bind('mouseout', function() {
		$(this).attr('class','');
	});
	
	//if this is the portfolio page, check for specific to open
	if ($.ListFindNoCase($.jqURL.url(),'app.portfolio','/')) {
		
		$('.portfolioHeaderClosed').bind('click', function() {
			togglePortfolios(this.id);
		});
		
		$('.portfolioHeaderOpen').bind('click', function() {
			togglePortfolios(this.id);
		});
	
		
		if ($.Trim($.ListLast($.jqURL.url(),'/')) == 'haymarket') {
			togglePortfolios(2);
		}
		if ($.Trim($.ListLast($.jqURL.url(),'/')) == 'vodafone') {
			togglePortfolios(1);
		}
	}
	
	//if this is contact us page, give name field the focus
	if ($.ListFindNoCase($.jqURL.url(),'app.contactUs','/')) {
		$('#name').focus();
		
		$('#submitEnquiryLink').bind('click', function(event) {
			event.preventDefault();
			processFormEnquiry();
		});
		
	} 
	
	$('#passwordHolder').bind('focus', function() {
		$('#passwordHolder').hide();
		$('#password').show();
		$('#password').focus();
	});
	
	$('#username').bind('blur', function() {
		if (!$.Len($('#username').val())) {
			$('#username').val('username');
		}
	});
	
	$('#password').bind('blur', function() {
		if (!$.Len($('#password').val())) {
			$('#password').hide();
			$('#passwordHolder').show();
		}
	});
		
			
	$('#loginBtn').bind('click', function() {
		var chrUsername = $("#username").val();
		var chrPassword = $("#password").val();
		
		if(!$.Len(chrUsername) || !$.Len(chrPassword) || chrUsername == "Username" || chrPassword == "Password") {
			chrLoginMessage = "Please provide entries for both username and password";
		} else {
			chrLoginMessage = "Sorry we couldn't find those details";
		}
		$("#loginMessageContent").text(chrLoginMessage);
		
		$("#indicator").show();
		window.setTimeout( function()
			{
				$("#indicator").hide();
				$("#loginMessage").fadeIn('slow');
				window.setTimeout( function() {
					$('#password').val('');
					$('#username').val('username');
					$('#password').hide();
					$('#passwordHolder').show();
					$("#loginMessage").fadeOut('slow');	
				}
				,2500);
			}
			,1500);
	});
	
	$('#loginForm > input').bind('click', function() {
		$(this).val('');
	});
});

function togglePortfolios(id) {
	if (typeof id != 'number') {
		var numPortfolioId = parseInt(id.replace(/\D/g,""));
	} else {
		var numPortfolioId = id;
	}
	var numPortfolioHeaderId = '#portfolioHeader' + numPortfolioId;
	var chrCurrentClass = $(numPortfolioHeaderId).attr("class");
	var numPortfolioItemId = '#portfolioItem' + numPortfolioId;
	//if it's closed, close any that may be open and then open this one
	if (chrCurrentClass == 'portfolioHeaderClosed') {
		
		
		//item is being opened, check if one open already
		
		/*if ($('.portfolioHeaderOpen').size()) {
			var numOtherOpenPortfolioId = parseInt($('.portfolioHeaderOpen').attr('id').replace(/\D/g,""));
			var numOtherPortfolioHeaderId = '#portfolioHeader' + numOtherOpenPortfolioId;
			var numOtherPortfolioItemId = '#portfolioItem' + numOtherOpenPortfolioId;
			$(numOtherPortfolioItemId).slideUp("1200");
			$(numOtherPortfolioHeaderId).attr("class","");
			$(numOtherPortfolioHeaderId).attr("class","portfolioHeaderClosed");
		}*/
		
		$(numPortfolioItemId).slideDown("1200");
		$(numPortfolioHeaderId).attr("class","");
		$(numPortfolioHeaderId).attr("class","portfolioHeaderOpen");
		
	} else {
		$(numPortfolioItemId).slideUp("1200");
		$(numPortfolioHeaderId).attr("class","");
		$(numPortfolioHeaderId).attr("class","portfolioHeaderClosed");
	}
}


function processFormEnquiry() {
	$('#emailConfirm2').hide();
	var chrEmailAddress = $('#emailAddress').val();
	var chrName = $('#name').val();
	var chrPhone = $('#phone').val();
	var chrRef = $('#ref').val();
	var chrComments = $('#comments').val();
	lstMissingMessages = '';
	numMissingMessages = 0;
	//name is missing
	if (!$.Len(chrName)) {
		lstMissingMessages = lstMissingMessages + '<li>your name</li>';
		numMissingMessages++;
		$('#name').focus();
	}
	//phone and email address missing
	if (!$.Len(chrPhone) && !$.Len(chrEmailAddress)) {
		lstMissingMessages = lstMissingMessages + '<li>either a phone number or email address</li>';
		if (!numMissingMessages) {
			$('#emailAddress').focus();
		}
		numMissingMessages++;
	} else {
		//if email address supplied check that it is valid
		if($.Len(chrEmailAddress) && !emailCheck(chrEmailAddress)) {
			lstMissingMessages = lstMissingMessages + '<li>a valid email address</li>';
			if (!numMissingMessages) {
				$('#emailAddress').focus();
			}
			numMissingMessages++;
		} 
	}
	//check that comments are supplied
	if (!$.Len(chrComments)) {
		lstMissingMessages = lstMissingMessages + '<li>a message</li>';
		if (!numMissingMessages) {
			$('#comments').focus();
		}
		numMissingMessages++;
	} 
	
	if (!numMissingMessages) {
		$('#contactIndicator').show();
		$('#submitEnquiryLink').hide();
		sendEnquiryRequest(chrEmailAddress,chrName,chrPhone,chrRef,chrComments);
	} else {
		var emailMessage = 'Please supply:<ul> '	+ lstMissingMessages + '</ul>';
		$('#emailMessage').html(emailMessage);
		window.setTimeout( function()
			{
				$("#emailMessage").fadeIn('fast');
				window.setTimeout( function() {
					$("#emailMessage").fadeOut('slow');	
				}
				,2500);
			}
			,500);
	}
	


}

function sendEnquiryRequest(chrEmailAddress,chrName,chrPhone,chrRef,chrComments) {
	$('#contactForm').submit();
	/*$.ajax({
		data: {	'chrEmailAddress':chrEmailAddress,
				'chrName':chrName,
				'chrPhone':chrPhone,
				'chrRef':chrRef,
				'chrComments':chrComments,
				'cfc':'comms',
				'method':'sendContactForm'
		 },
		success: function() {
			$('#emailConfirm').text("Thank you, your request has been sent");
			window.setTimeout( function()
			{
				//DP_Debug.dump(data);
				$('#contactIndicator').hide();
				$('#submitEnquiryLink').show();
				$("#emailConfirm").fadeIn('fast');
				window.setTimeout( function() {
					$("#emailConfirm").fadeOut('slow');	
					$('#emailAddress').val('');
					$('#name').val('');
					$('#phone').val('');
					$('#ref').val('general enquiry');
					$('#comments').val('');
				}
				,2500);
			}
			,2500);
		}
	});*/
}
