/*
 * Author:		Adam Collison [adam.collison@soak.co.uk]
 * Agency:		Soak Digital Ltd.
 * Client: 		Archant
 * Project: 	Jumbo 24
 * Comment:		Main JS

----------------------
Contents:

1. SIFR
2. Generic JS Functions
3. Additional JQuery custom functions

/*--------------------------------------------------------------------------
	SIFR
----------------------------------------------------------------------------*/
var t;

var interstate = {
	src: '/assets/swf/interstate.swf'
};

sIFR.activate(interstate); // From revision 209 and onwards

sIFR.replace(interstate, {
	selector: 'h1',
	ratios: [9, 1.16, 16, 1.09, 29, 1.05, 34, 1.02, 49, 1.01, 75, 1, 79, 0.99, 82, 1, 0.99],
	tuneHeight: 0,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #F25600; font-size:20px; }'
	]
});

sIFR.replace(interstate, {
	selector: '.detail .added',
	ratios: [9, 1.16, 16, 1.09, 29, 1.05, 34, 1.02, 49, 1.01, 75, 1, 79, 0.99, 82, 1, 0.99],
	tuneHeight: 0,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #333333; font-size:16px; }'
	]
});

sIFR.replace(interstate, {
	selector: '.why-advertise h2 span',
	ratios: [9, 1.16, 16, 1.09, 29, 1.05, 34, 1.02, 49, 1.01, 75, 1, 79, 0.99, 82, 1, 0.99],
	tuneHeight: 0,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #ffffff; font-size:18px; }'
	]
});

sIFR.replace(interstate, {
	selector: '.account-panel h3',
	ratios: [9, 1.16, 16, 1.09, 29, 1.05, 34, 1.02, 49, 1.01, 75, 1, 79, 0.99, 82, 1, 0.99],
	tuneHeight: -7,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #333333; font-size:20px; }'
	]
});

sIFR.replace(interstate, {
	selector: '.how-jumbo-works h3',
	ratios: [9, 1.16, 16, 1.09, 29, 1.05, 34, 1.02, 49, 1.01, 75, 1, 79, 0.99, 82, 1, 0.99],
	tuneHeight: -7,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #F25600; font-size:20px; }'
	]
});

sIFR.replace(interstate, {
	selector: '.inspiration h3',
	ratios: [9, 1.16, 16, 1.09, 29, 1.05, 34, 1.02, 49, 1.01, 75, 1, 79, 0.99, 82, 1, 0.99],
	tuneHeight: -7,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #F25600; font-size:20px; }'
	]
});

sIFR.replace(interstate, {
	selector: '.detail .information h2',
	ratios: [9, 1.16, 16, 1.09, 29, 1.05, 34, 1.02, 49, 1.01, 75, 1, 79, 0.99, 82, 1, 0.99],
	tuneHeight: -7,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #333333; font-size:16px; margin-top:15px; }'
	]
});


/*--------------------------------------------------------------------------
	Generic JS Functions
----------------------------------------------------------------------------*/

$(function(){
	
	// Bind inspiration actions
	updateInspirationActions();
	
	// Define all vars
	var currentInspiration = 1;
	var maxCurrentInspiration = $('#homepage .inspiration > div > ul').children().size();
	
	// Detail page - email form display toggle
	$('.detail .contact-details a img').click(function() {
		if(!$(this).data('state') || $(this).data('state') == 'hidden')
		{
			$('.detail .email_form').slideDown();
			var newSrc = $(this).attr('src').replace(/btn-send-email-to-user/,'btn-cancel-email-to-user');
			$(this).attr('src',newSrc);
			$(this).data('state','showing');
		}
		else
		{
			$('.detail .email_form').slideUp();
			var newSrc = $(this).attr('src').replace(/btn-cancel-email-to-user/,'btn-send-email-to-user');
			$(this).attr('src',newSrc);
			$(this).data('state','hidden');
		}
		$(this).parent('a').blur();
		return false;
	});
	
	// Inspiration widget code
	$('#homepage .inspiration .actions').click(function() {
		if($(this).hasClass('next'))
		{
			if(maxCurrentInspiration != currentInspiration)
			{
				currentInspiration++;
			}
		}
		else
		{
			if(currentInspiration != 1)
			{
				currentInspiration--;
			}
		}
		$('#homepage .inspiration .selected').fadeOut('normal',function() {
			$(this).removeClass('selected');
			$('#homepage .inspiration div > ul > li:nth-child('+currentInspiration+')').fadeIn('normal',function() {
				$(this).addClass('selected');			
				updateInspirationActions();
			});											 
		});		
		$(this).blur();
		return false;
	});
	
	// Quick jump code
	$('.quick-jump').each(function(x, el) {
		$(el).find("input[type='submit']").hide();			   
		$(el).find("select").change(function() {
			$('body form').submit();								 
		});			   
	});
	
	// Detail Gallery code
	$('.detail .gallery .active em').fadeTo(1,0.7);
	$('.detail .gallery li a').click(function(){
		$('.detail .gallery em').fadeTo(1,1);
		$('.detail .gallery a').removeClass('active');
		$(this).addClass('active');
		$(this).children('em').fadeTo(1,0.7);
		var largeSrc = $(this).find('img').attr('src').replace(/\/Thumbnail/,'/Fullsize');
		$('.detail .gallery p img').attr('src',largeSrc);
		$(this).blur();
		return false;
	});
	
	// Search form pre-fill code
	$(".advanced-search input[type='text'], .quick-search input[type='text']").each(function() {
		$(this).intelliLabel();
	});
	

	if($('.why-advertise').length > 0)
	{
		t = setTimeout("movePromoAlong()",4000);
	}

});
function movePromoAlong() {
	
	clearTimeout(t);
	
	if($('.why-advertise li.selected + li img').length > 0)
	{
		$('.why-advertise li.selected').addClass('previous').removeClass('selected');
		$('.why-advertise li.previous + li img').css({'display':'none'});
		$('.why-advertise li.previous + li').addClass('selected');
		
		$('.why-advertise li.previous img').fadeOut('normal',function() {
			$('.why-advertise li.previous + li img').fadeIn('normal',function() {
				$('.why-advertise li.previous').removeClass('previous');
			});															  
		});
	}
	else
	{
		$('.why-advertise li.selected').addClass('previous').removeClass('selected');
		$('.why-advertise li:first-child img').css({'display':'none'});
		$('.why-advertise li:first-child').addClass('selected');
		
		$('.why-advertise li.previous img').fadeOut('normal',function() {
			$('.why-advertise li:first-child img').fadeIn('normal',function() {
				$('.why-advertise li.previous').removeClass('previous');
			});															  
		});
	}
	
	t = setTimeout("movePromoAlong()",4000);
	
}
function amovePromoAlong() {
	
	clearTimeout(t);
	
	if($('.why-advertise li.selected + li img').length > 0)
	{
		$('.why-advertise li.selected + li img').css({'display':'none','top':'50px','left':'9px'});
		$('.why-advertise li.selected img').fadeOut('normal',function() {
			$(this).parent('li').addClass('previous');
			$('.why-advertise li.selected').removeClass('selected');
			$('.why-advertise li.previous + li img').fadeIn('normal',function() {
				$('.why-advertise li.previous').removeClass('previous');
				$(this).parent('li').addClass('selected');												  
			});															  
		});
	}
	else
	{
		$('.why-advertise li:first-child img').css({'display':'none','top':'50px','left':'9px'});
		$('.why-advertise li.selected img').fadeOut('normal',function() {
			$(this).parent('li').addClass('previous');
			$('.why-advertise li.selected').removeClass('selected');
			$('.why-advertise li:first-child img').fadeIn('normal',function() {
				$('.why-advertise li.previous').removeClass('previous');
				$(this).parent('li').addClass('selected');												  
			});															  
		});
	}
	
	t = setTimeout("movePromoAlong()",6000);
	
}

/*--------------------------------------------------------------------------
	Additional JQuery custom functions
----------------------------------------------------------------------------*/
jQuery.fn.intelliLabel = function() {
	$(this).css({'color':'#666666'});
	var label = $(this).val();
	$(this).focus(function(){
		if ($(this).val() === label) 
		{
			$(this).val('');
			$(this).css({'color':'#000000'});
		}
	}).blur(function(){
		// simulate a delay before reverting to placeholder
		$(this).fadeTo(500, 1, function(){
			if ($(this).val() === '') 
			{
				$(this).val(label);
				$(this).css({'color':'#666666'});
			}
		});
	});
}

function updateInspirationActions()
{
	if($('#homepage .inspiration div > ul > li:first').hasClass('selected'))	
	{
		$('#homepage .inspiration .prev').fadeOut('fast');	
	}
	else
	{
		$('#homepage .inspiration .prev').fadeIn('fast');		
	}
	if($('#homepage .inspiration div > ul > li:last').hasClass('selected'))	
	{
		$('#homepage .inspiration .next').fadeOut('fast');	
	}
	else
	{
		$('#homepage .inspiration .next').fadeIn('fast');	
	}
}

