/* General Config */
/* *********************************************************************************/

var WEB_ROOT = ((document.location.protocol == 'https:') ? 'https://' : 'http://') + document.location.host;
var BASE_SERVER = location.host.substr(location.host.indexOf('.') + 1);
var SOAK = {};

/* Window onload events */
/* *********************************************************************************/

$(window).load(function(){

	
	
});

$(function(){
	
	SOAK.slideShow_init();
	
	// clear intellilabels on form submit
	$('form').submit(function() {
		$('input[type="text"]', $(this)).each( function() {
			if ($(this).attr('title') == $(this).val() || $(this).prev('label').text() == $(this).val()) {
				$(this).val('').css('color','#333');
			}
		});
		return true;
	});
	
});

// initialise slideshow
SOAK.slideShow_init = function() {

	$('#frm-postcode').intelliLabel();
	
	var nav = '';
	
	$('#slideshow li').each(function(i){
		if (i == 0) {
			nav += '<li class="selected"><a href="#">' + (i + 1) + '</a></li>';
		}
		else {
			nav += '<li><a href="#">' + (i + 1) + '</a></li>';
		}
	});
	
	$('#slideshow-container').append('<ul id="slideshow-nav">' + nav + '</ul>');
	
	$('#slideshow-nav a').live('click', function() {
		return false;
	});
	
	$('#slideshow-nav li:not(.selected) a').live('click', function() {
		var position = $('#slideshow-nav li').index($(this).parent('li'));
		clearTimeout(slideShow_t);
		SOAK.slideShow(position);
	});
	
	slideShow_t = setTimeout("SOAK.slideShow()", 10000);
	$('#slideshow-container').hover(function() {
		clearTimeout(slideShow_t);
	},
	function() {
		clearTimeout(slideShow_t);
		slideShow_t = setTimeout("SOAK.slideShow()", 10000);
	});
	
	Cufon.refresh();
}

// main slideshow function
SOAK.slideShow = function(position) {
	if (!$('#slideshow').hasClass('working')) {
		$('#slideshow').addClass('working');
		$('#slideshow .selected').fadeOut(700, function() {
			$('#slideshow .selected, #slideshow-nav .selected').removeClass('selected');
			var next = $(this).next('li');
			if (position || position == 0) {
				next = $('#slideshow li:eq(' + position + ')');
			}
			else if (next.length == 0) {
				next = $('#slideshow li:first-child');
			}
			$('#slideshow-nav li:eq(' + $('#slideshow li').index(next) + ')').addClass('selected');
			Cufon.refresh();
			next.fadeIn(500, function() {
				$(this).addClass('selected');
				$('#slideshow').removeClass('working');
			});
		});
		if (!position) {
			clearTimeout(slideShow_t);
			slideShow_t = setTimeout("SOAK.slideShow()", 10000);
		}
	}
}

/* Moves label into a text input and handles focus / blur events */
/* *********************************************************************************/
jQuery.fn.intelliLabel = function(e){
	var intelliDelay;
	var field = $(this);
	this.each(function(){

		field.data('label', e);
		if (!e){
			field.data('label', $('label[for=' + field.attr('id') + ']').text());
		}
		
		$('label[for=' + field.attr('id') + ']').live('click', function(){
			clearTimeout(intelliDelay);
		});
		
		$('.search-field').live('click', function(){
			clearTimeout(intelliDelay);
		});
		
		$('#' + field.attr('id')).live('focus', function(){
			clearTimeout(intelliDelay);
			if ($(this).val() == $(this).data('label')) $(this).val('').css('color','#333');
		}).live('blur', function(){
			if ($(this).val() == '') {
				var intelliObj = $(this);
				intelliDelay = setTimeout(function() {
					intelliObj.val(intelliObj.data('label'));
					if (intelliObj.data('color')) {
						intelliObj.css('color', intelliObj.data('color'));
					}
				}, 400);
			}
		});
		
		if (field.val() == '' || field.val() == field.data('label')){
			field.val(field.data('label'));
			if (field.data('color')){
				field.css('color', field.data('color'));
			}
		}
	});
}
