// Run when the page has loaded
$('document').ready(function() {
		
	var model_base = {
		'titleShow' : false,
		'speedIn'		: 500,
		'speedOut'		:	200,
		'overlayShow' : true,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'type' : 'iframe',
		'autoDimensions' : false,
		'width' : 480,
		'height' : 350,
		'showCloseButton' : true
	}
	
	var text_modal = {
		'titleShow' : false,
		'speedIn'		: 500,
		'speedOut'		:	200,
		'overlayShow' : true,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',

		'autoDimensions' : true,
		'autoScale' : true,
		'width' : false,
		'height' : false,
		'showCloseButton' : true
	}
	
	var text_modal_full = {
		'titleShow' : false,
		'speedIn'		: 500,
		'speedOut'		:	200,
		'overlayShow' : true,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'autoDimensions' : false,
		'autoScale' : false,
		'width' : 630,
		'height' : 450,
		'showCloseButton' : true
	}
	
	
	// video modal conf
	var video_modal = $.extend({}, model_base);
	video_modal.width = 320;
	video_modal.height = 244;
	
	// image modal conf
	var image_mobal = $.extend({}, model_base);
	image_mobal.width = false;
	image_mobal.height = false;
	image_mobal.autoDimensions = true;
	image_mobal.autoScale = true;
	image_mobal.type = 'image';
	
	
	$("a.general_modal").fancybox(model_base);
	$("a.image_modal").fancybox(image_mobal);
	$("a.text_modal").fancybox(text_modal);
	$("a.text_modal_full").fancybox(text_modal_full);

	if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/MIDP/i))) {
		$('#barracudas_video_launcher').attr('href', '/video/barracudas_video.mp4');
	} else {
		$("a.video_modal").fancybox(video_modal);
	}
	
	$('hr').each(function() {
		$(this).replaceWith('<div class="hr '+ $(this).attr('class') +'"></div>');
	});
	
	// Save £5 Flashing
	
	$('div.right_side_panel.save_5_pounds p').fadeOut(500).delay(400).fadeIn(500);
	setInterval(function() {
		$('div.right_side_panel.save_5_pounds p').fadeOut(500).delay(400).fadeIn(500);
	}, 3500);
	
	$('ul li:first-child').addClass('first-child');
	
	// Table zebra
	$('table.item_list, table.remaining_payments').each(function() {
		$(this).find('tr:odd').addClass('even');
		$(this).find('tr:even').addClass('odd');
	});
	
	$('ul#menu li').hover(function() {
		$(this).css('background-image', 'none');
	}, function() {
		$(this).css('background-image', '');
	});

	
	$('input[type=submit].replace').each(function() {
		$(this).after('<a class="button submit ' + $(this).attr('class')  + '" href="#submit"><span>' + $(this).val() + '</span></a>');
		$(this).addClass('hide');
	});

	$('a.submit').live('click', function() {
		$(this).prev('input').click();
		return false;
	});
	
	
	
	/* News Fader
	--------------------------------------------------------------------------------*/
	fade_to_next_item()
	
	// Only run if there's more than 1 news item
	if ($('div#news_fader li').length > 1) {
		setInterval(function() {
			if (!$('div#news_fader').hasClass('pause')) {
				fade_to_next_item()
			}
		}, 5000);
	}
	
	// Pause class 'marker' and anchor underline
	$('div#news_fader').parent().hover(function() {
		$('div#news_fader').addClass('pause');
		$('div#news_fader li a').css('text-decoration', 'underline');
	}, function() {
		$('div#news_fader').removeClass('pause');
		$('div#news_fader li a').css('text-decoration', '');
	});
	/*------------------------------------------------------------------------------*/
	
	// Add PNG fade out
	$('div#news_fader').prepend('<span id="news_fade_out"></span>');

	// Whole news bar to be clickable - go to current item
	$('div#news_fader').parent().css('cursor', 'pointer')
	$('div#news_fader').parent().click(function() {
		window.location = $('div#news_fader li.current a').attr('href');
		return false;
	});
	
	fade_to_next_photo();

	// Only run if there's more than 1 news item
	if ($('#home_photo_fader img').length > 1) {
		setInterval(function() {
			fade_to_next_photo()
		}, 8000);
	}
	
	
	$('a.auto_trigger').click();

});


window.current_news_silhouette = 0;
window.total_news_silhouettes = 13;

function fade_to_next_item() {
	
	// The browser_name conditions below are specifically for IE 6
	// The fades caused cursor flickering flickering, so workarounds applied
	
	mark_current_news_item();
	
	if (browser_name == 'standard') {
		$('div#news_fader li').not('.current').fadeOut(1000);
	} else {
		$('div#news_fader li').not('.current').hide();
	}
	
	// Fade out silhouette, move to next silhouette
	if (window.current_news_silhouette >= 13) window.current_news_silhouette = 0;
	
	if (browser_name == 'standard') {
		$('span.news_silhouette').fadeOut(1000, function() {
			$('span.news_silhouette').css('background-position', '-' + (window.current_news_silhouette * 75) + 'px 0');
			window.current_news_silhouette++;
		});
	} else {
		$('span.news_silhouette').hide();
		$('span.news_silhouette').css('background-position', '-' + (window.current_news_silhouette * 75) + 'px 0');
		window.current_news_silhouette++;
	}
	
	// Fade in next news item
	if (browser_name == 'standard') {
		$('div#news_fader li.current').delay(1000).fadeIn(1000);
	} else {
		$('div#news_fader li.current').delay(1000).show();
	}
	
	// Fade in silhouette
	if (browser_name == 'standard') {
		$('span.news_silhouette').fadeIn(1000);
	} else {
		$('span.news_silhouette').show();
	}
	
	
	// Forms
	
	$('form.standard').find('input[type!="submit"], checkbox, select, radio, textarea').focus(function() {
		$(this).parent('div').css('background-color', '#f7fff0').siblings('div').css('background-color', '#fff');
		$('span.form_field_help').not($(this).parents('div').prev('span.form_field_help')).fadeOut(50);
		$(this).parent('div').prev('span.form_field_help').fadeIn(200);
	}).blur(function() {
		$(this).parent('div').css('background-color', '#fff').prev('span.form_field_help').fadeOut(200);
	});
	
	$('form.standard').find('input[type!="hidden"], checkbox, select, radio, textarea').eq(0).focus();
	
}

function mark_current_news_item() {
	$('div#news_fader li').not(get_next_news_item().addClass('current')).removeClass('current');
}

function get_next_news_item() {
	
	// Determine next item
	var next_item = $('div#news_fader li.current:last').next();
	
	// Defatult to first item if no items are selected
	if (next_item.length < 1) next_item = $('div#news_fader li:first');
	
	return next_item;
}









function fade_to_next_photo() {
	
	mark_current_fader_photo();

	$('#home_photo_fader img').not('.current').fadeOut(1000);

	$('#home_photo_fader img.current').fadeIn(1000);

	
}



function mark_current_fader_photo() {
	$('#home_photo_fader img').not(get_next_fader_photo().addClass('current')).removeClass('current');
}

function get_next_fader_photo() {
	
	// Determine next item
	var next_photo = $('#home_photo_fader img.current:last').next();
	
	// Defatult to first item if no items are selected
	if (next_photo.length < 1) next_photo = $('#home_photo_fader img:first');
	
	return next_photo;
}


