/**********************
// sIFR Config
**********************/
var allowedDates;
var interstate = {
	src: 'assets/swf/interstate.swf'
};

sIFR.activate(interstate); // From revision 209 and onwards

sIFR.replace(interstate, {
	selector: '#step-3 .silver em',
	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:150,
	css: [
		'.sIFR-root { color: #535353; text-align:center;  }',
	]
});

sIFR.replace(interstate, {
	selector: '#step-3 .gold em',
	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:150,
	css: [
		'.sIFR-root { color: #D78D0A; text-align:center;  }',
	]
});

sIFR.replace(interstate, {
	selector: '#step-3 .promo em',
	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:50,
	css: [
		'.sIFR-root { color: #FFFFFF; text-align:center; font-size:18px;  }',
	]
});

sIFR.replace(interstate, {
	selector: '.basket-total 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: -5,
	wmode:'transparent',
	thickness:50,
	css: [
		'.sIFR-root { color: #FFFFFF; Font-size:18px; text-align:center;  }'
	]
});
		
sIFR.replace(interstate, {
	selector: '#preview-full-advert .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: -5,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #333333; font-size:16px; margin-top:15px; }'
	]
});
sIFR.replace(interstate, {
	selector: '#step-3 .featured-component 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: -3,
	wmode:'transparent',
	thickness:190,
	css: [
		'.sIFR-root { color: #ffffff; font-size:14px; margin-top:15px; }'
	]
});
sIFR.replace(interstate, {
	selector: '#step-3 .featured-component 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: -3,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #ffffff; font-size:16px; margin-top:15px; }'
	]
});
sIFR.replace(interstate, {
	selector: '#step-3 .component-1 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: -3,
	wmode:'transparent',
	thickness:30,
	css: [
		'.sIFR-root { color: #0D4B7A; font-size:16px; margin-top:15px; }'
	]
});
sIFR.replace(interstate, {
	selector: 'h1, #store 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: -6,
	wmode:'transparent',
	thickness:50,
	css: [
		'.sIFR-root { color: #F25600; }'
	]
});
sIFR.replace(interstate, {
	selector: '#login 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: -5,
	wmode:'transparent',
	thickness:50,
	css: [
		'.sIFR-root { color: #01111A; }'
	]
});
sIFR.replace(interstate, {
	selector: '.summary-content h5',
	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: -5,
	wmode:'transparent',
	css: [
		'.sIFR-root { color: #FFFFFF; }'
	]
});

sIFR.replace(interstate, {
	selector: '#basket h3.warning',
	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: -5,
	thickness:100,
	wmode:'transparent',
	css: [
		'.sIFR-root { color: #ff6600; }'
	]
});

sIFR.replace(interstate, {
	selector: '#welcome-panel h4',
	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],
	thickness: 50,
	tuneHeight: -5,
	wmode:'transparent',
	css: [
		'.sIFR-root { color: #FFFFFF; }'
	]
});
jQuery.noConflict();
/**********************
// jQuery plugins
**********************/

// Function to set a maximum character length on textareas with class '.count'

jQuery.fn.maxlength = function() {
	jQuery("textarea.count").keypress(function(e) {
		var key = e.which;
		if( key >= 33 || key == 13) {  
			var maxLength = jQuery(this).attr('title').replace(new RegExp(/^limit of /),"");
			var length = this.value.replace(/\r\n/g,'\n').length;
			if(length >= maxLength) {
				e.preventDefault();
			}
		}
	});  
}

// Function to display the maximum character length on textareas

jQuery.fn.limitTextarea = function() {
	var input = jQuery(this);
	var str = input.val();
	var maxlength = input.attr('title').replace(new RegExp(/^limit of /),"");
	if (str.length > maxlength) input.parent('td').find('p.limit').html('<em>' + str.length + '</em>' + '/' + maxlength);
	else input.parent('td').find('p.limit').html(str.length + '/' + maxlength);
	if (jQuery(this).val().length > maxlength) {
		jQuery(this).val(jQuery(this).val().substring(0, maxlength));
		this.scrollTop = this.scrollHeight;
		if (this.createTextRange) {
		r = this.createTextRange();
		r.move('character', maxlength);
		}
		else {
			this.selectionStart = maxlength;
			this.selectionEnd = maxlength;
		}
	}
}

// Function to display the maximum character length on inputs

jQuery.fn.limitInput = function() {
	var input = jQuery(this);
	var str = input.val();
	input.next('p.limit').html(str.length + '/' + input.attr('maxlength'));
}

/**********************
// Map functionality
**********************/

	
function mapOn() {
	jQuery('.mapper').attr('usemap','#imgmap-' + jQuery('.mapper').attr('alt'));
}

// Function to disable image mapping

function mapOff() {
	if (jQuery('.mapper').attr('usemap').length > 0) {
		jQuery('.mapper').attr('alt',(jQuery('.mapper').attr('usemap').substring(8)));
	}
	jQuery('.mapper').attr('usemap','');
}

/**********************
// The following triggers on document load
**********************/

jQuery(function(){
			
				
	jQuery('#step-3 .show-more a').live('click',function() {
					
		var rel = jQuery(this).attr('rel');
		jQuery('#package-content-'+rel).slideDown();
		jQuery(this).fadeOut();
					
		return false;
	});
				
	jQuery('#step-3 .show-less').live('click',function() {
					
		var rel = jQuery(this).attr('rel');
		jQuery('#package-content-'+rel).slideUp();
		jQuery('#step-3 .show-more a').fadeIn();
					
		return false;
	});
				
	if(jQuery('input[name="london"]').val() == 'London')
	{
		jQuery('.location-list > ul > li').addClass('display');
	}

	
	if(jQuery('.private-trade-selector').length > 0 && jQuery('.private-trade-selector input:checked').length < 1)
	{
			jQuery('.component-1-content').addClass('ieHide');
			var width = jQuery('.component-1-content').width() + 30;
			var height = jQuery('.component-1-content').height() + 30;
			var position = jQuery('.component-1-content').position();
			var left = position.left + 2;
			var top = position.top -2;
			
			jQuery('.component-1-content div').hide();
			
			var selectlist = jQuery('#parentCategory').clone();
	
			jQuery('.private-trade-selector label, .private-trade-selector input').live('click',function() {
																										 
				jQuery('#parentCategory').html(selectlist.html());
				
				if(jQuery('.private-trade-selector input:checked').length > 0)
				{
					if(jQuery.browser.msie && jQuery.browser.version=="6.0")
					{
						jQuery('#tradehint,#privatehint').css({'left':'-99999px'});
					}
					if(jQuery('.private-trade-selector input:checked').val() == 1)
					{
						// its trade so hide private cats
						jQuery('input[value="private"]').each(function() {
							jQuery('#parentCategory option[value="'+jQuery(this).attr('name')+'"]').remove();											   
						});

					}
					else
					{
						// its private so hide trade cats
						// its trade so hide private cats
						jQuery('input[value="trade"]').each(function() {
							jQuery('#parentCategory option[value="'+jQuery(this).attr('name')+'"]').remove();												   
						});
					}
					jQuery('.component-1-content div').show();
				}
			});
	}
	
	// step one disable buttton until ready for next step
	if(jQuery('#step-1').length > 0)
	{
			jQuery('#step-1 input.btn-next').attr('src','assets/images/btn-next-step-inactive.png');
			jQuery('#step-1 input.btn-next').live('click',function(){ return false; })
			
			jQuery('#step-1 #catSelect, #step-1 #category').change(function() {
				jQuery('#step-1 input.btn-next').attr('src','assets/images/btn-next-step-inactive.png');
				jQuery('#step-1 input.btn-next').live('click',function(){ return false; })
			});

			jQuery('#step-1 #classification').change(function() {
				jQuery('#step-1 input.btn-next').attr('src','assets/images/btn-next-step.png');
				jQuery('#step-1 input.btn-next').die('click');
			});
	}

	if(jQuery('#step-5a').length > 0)
	{
		if(jQuery('#ad-previewed').length <= 0)
		{
			jQuery('#step-5a input.btn-next').attr('src','assets/images/btn-next-step-inactive.png');
			jQuery('#step-5a input.btn-next').live('click',function(){ return false; });
			jQuery('#step-5a input.btn-next').attr('onClick','');
		}
		
		jQuery('.spellbtn').live('click',function() {
			checkSpelling(jQuery(this).siblings('textarea').get(0)); 
			jQuery('#spellcheck').css({'top':jQuery(this).offset().top+jQuery(this).height()+'px'});
			jQuery('#spellcheck').show();
			return false;										  
		});	
		
		
	}

	if(jQuery('#step-5c').length > 0)
	{
		if(jQuery('#tandc').not(':checked').length > 0)
		{
			jQuery('#step-5c .btn-next input').attr('src','assets/images/btn-next-step-inactive.png');
			jQuery('#step-5c .btn-next input').live('click',function(){ return false; });
		}

		jQuery('#tandc').live('click',function() 
		{
			if(jQuery('#tandc:checked'))
			{
				jQuery('#step-5c .btn-next input').attr('src','assets/images/btn-next-step.png');
				jQuery('#step-5c .btn-next input').die('click');
			}
			if(jQuery('#tandc').not(':checked').length > 0)
			{
				jQuery('#step-5c .btn-next input').attr('src','assets/images/btn-next-step-inactive.png');
				jQuery('#step-5c .btn-next input').live('click',function(){ return false; });
			}
		});

	}	
	
	if(jQuery('#step-6').length > 0)
	{
		if(jQuery('.component-2-content li.step-6 strong').html() != '')
		{
			jQuery('#step-6 .btn-next img').attr('src','assets/images/btn-add-to-basket.gif');
			jQuery('#step-6 .btn-next').die('click');
		}
		else
		{
			jQuery('#step-6 .btn-next img').attr('src','assets/images/btn-add-to-basket-inactive.gif');
			jQuery('#step-6 .btn-next').live('click',function(){ return false; });
		}
	}
	

	if(jQuery('#step-2').length > 0)
	{
		if(jQuery('#map-count').val() == 0)
		{
			jQuery('#step-2 .submit a.clear-all').hide();
		}
		
		if(jQuery('#map-count').val() < jQuery('#map-min').val())
		{
			jQuery('#step-2 .submit a.next-btn img').attr('src','assets/images/btn-next-step-inactive.png');
			jQuery('#step-2 .submit a.next-btn').live('click',function(){ 
				//if(jQuery('#map-count').val() == 0)
				//{
					return false;	
				//}
				//jQuery('.message-c').removeClass('hidden');
				//return false; 
			});
			jQuery('.message-c a').live('click',function() {
				jQuery('.message-c').fadeOut();		
				return false; 
			})
			jQuery('#location-list li a, #map area:not(.selected,.nonprint)').live('click',function() {
				jQuery('.message-c').fadeOut();		
			})
		}
	}
	
	// without JS, map is hidden. Show on load.
	jQuery("#step-2 #map").show();
	
	// append a jQuery Stylesheet only if JS is supported
	jQuery('head').append('<link type="text/css" rel="stylesheet" href="assets/css/styles-ui.css" media="screen" />');
	
	if(jQuery('.mapper').length > 0)
	{
		jQuery('.mapper').maphilight();
	}
	if(jQuery('#map').length > 0)
	{
		jQuery('#map area').each(function(){
			jQuery.data(jQuery(this).get(0), 'area', { title: jQuery(this).attr('title'), alt: jQuery(this).attr('alt') });
		});
		
		jQuery().mousemove(function(e){
			jQuery('#tip').css({'left':e.pageX +'px','top': e.pageY +'px'});
		}); 

		jQuery('#map area').hover(function(){
			if(jQuery('.message-b').css('left') == '-9999px')
			{
				showTip();
				var areaClass = jQuery(this).attr('class');
				var contentList = '<ul>' + jQuery('.location-list .'+areaClass+' ul').html() + '</ul>';
				if(!jQuery(this).hasClass('selected') && !jQuery(this).hasClass('nonprint'))
				{
					jQuery('#tip').removeClass('nonprint');
					if(jQuery('.location-list .'+areaClass+' ul li').length > 0)
					{
						jQuery('#tip p').html('<h6>'+jQuery(this).attr('title')+'</h6>Titles Available:' + contentList);
					}
					else
					{
						jQuery('#tip p').html('<h6>'+jQuery(this).attr('title')+'</h6>No Titles Found');
					}
				}
				else if(jQuery(this).hasClass('selected'))
				{
					jQuery('#tip').removeClass('nonprint');
					jQuery('#tip p').html('<h6>'+jQuery(this).attr('title')+'</h6><em>click map to remove selection</em>');
				}
				else if(jQuery(this).hasClass('nonprint'))
				{
					jQuery('#tip').addClass('nonprint');
					jQuery('#tip p').html('<h6>'+jQuery(this).attr('title')+'</h6>Jumbo24 Website Only');
				}
			}
		},
		function() {
			hideTip()
		});
		
		if(jQuery('.location-list').length > 0)
		{
			jQuery('#map area.nonprint,.location-list li.nonprint').live('click',function(){
				return false;		
			});
			
			jQuery('.location-list li.nonprint a').live('click',function(){
				jQuery(this).parent('li').find('ul').show();
				return false;		
			});
		}
		
		if(jQuery('#map-max').val() <= jQuery('#map-count').val() && jQuery('.message-b .base p:first-child').html() != '')
		{
			jQuery('#map area:not(.selected,.nonprint)').live('click',function(){
				var href = jQuery(this).attr('href');
				jQuery('.message-b a').attr('href',href);
				jQuery('#tip').hide();
				jQuery('.message-b').css({'top':'180px','left':'130px'});
				jQuery('#map canvas').hide();
				return false;		
			});
			jQuery('.location-list li:not(.selected,.nonprint) a').live('click',function(){
				var href = jQuery(this).attr('href');
				jQuery('.message-b a').attr('href',href);
				jQuery('#tip').hide();
				jQuery('.message-b').css({'top':'670px','left':'130px'});
				jQuery('#map canvas').hide();
				return false;		
			});
			jQuery('#btn-add-location').live('click',function(){
				var href = '#form';
				jQuery('.message-b a').attr('href',href);
				jQuery('#tip').hide();
				jQuery('.message-b').css({'top':'-84px','left':'233px'});
				jQuery('#map canvas').hide();
				return false;	
			});
			jQuery('.message-b a').live('click',function(){
				if(jQuery('#map-count').val() == 0)
				{
					return false;	
				}
				var dontSend = 1;
				if(jQuery('#should-send-request').length > 0)
				{
					if(jQuery('#should-send-request').val() == 'true')
					{
						dontSend = 0;
					}
				}
				jQuery('.message-b').css({'top':'-9999px','left':'-9999px'});
				jQuery('#map canvas').show();
				if(dontSend)
				{
					return false;
				}
				else
				{
					if(jQuery(this).attr('href') == '#form')
					{
						jQuery('form.main-content').submit();
					}
					else
					{
						return true;
					}
				}
			});
		}

	}
	
	// jQuery UI datepicker
	// Wave provide yyyy-mm-dd
	if (jQuery('.non-js-date-selector').length > 0) {
		
		jQuery('.non-js-date-selector').append("<p><input name='" + jQuery('.non-js-date-selector select').attr('name') + "' type='hidden' value='' id='newDateField' />Select a <strong>start date</strong> for your ad from the calender below and click the update button:</p><div id=\"datepicker\"></div>");
		jQuery('.non-js-date-selector select').attr('name','replaced').css({'position':'absolute','top':'-9999px','left':'-9999px'});
		
		var start = jQuery('.non-js-date-selector select option:first-child').val();
		start = start.split('-');
		start = new Date(start[1]+'/'+start[2]+'/'+start[0]);
		
		var end =  jQuery('.non-js-date-selector select option:last-child').val();
		end = end.split('-');
		end = new Date(end[1]+'/'+end[2]+'/'+end[0]);
		
		var selected =  jQuery('.non-js-date-selector select option:selected').val();
		jQuery('#newDateField').val(selected);
		selected = selected.split('-');
		selected = new Date(selected[1]+'/'+selected[2]+'/'+selected[0]);
		
		jQuery('.non-js-date-selector select option').each(function() {
			var tmpdate = jQuery(this).val();
			tmpdate = tmpdate.split('-');
			tmpdate = '[' + tmpdate[1] + '' + tmpdate[2] + '' + tmpdate[0]+'],';
			allowedDates += tmpdate;															
		});

		jQuery('#datepicker').datepicker({
			maxDate: end,
			minDate: start,
			defaultDate: selected,
			dateFormat: 'yy-mm-dd',
			onSelect: function(dateText, inst) {
				jQuery('#newDateField').val(dateText);
			},
			beforeShowDay: filterDisabled
		});
		
	}

	
	// Maxlength on textareas with class="count"
	jQuery().maxlength();
	
	// Description page input counting
	jQuery.each(jQuery('textarea.count'), function(){
		jQuery(this).limitTextarea();
	});
	jQuery('textarea.count').keyup(function() {
		jQuery(this).limitTextarea();
	});
	
	// Description page input counting
	jQuery.each(jQuery('input.count'), function(){
		jQuery(this).limitInput();
	});
	jQuery('input.count').keyup(function() {
		jQuery(this).limitInput();
	});
	
	// Show an alert for retrospective navigation
	
	jQuery('.current').prevAll('li').children('a').click(function(){
		if (!window.confirm('Warning: You will lose any unsaved information on this page')) return false;
	});
	
	jQuery('body').click(function(){
		//remove any infopops
		jQuery('#infopop').remove();
	});
	
	jQuery('a.help').click(function(e){
		//remove any infopops
		jQuery('#infopop').remove();
		
		//construct infopop, append to content
		var content = '<div id="infopop"><p></p></div>';
		jQuery('body').append(content);
		jQuery('#infopop p').append(jQuery(this).siblings('.w3c').text());
		jQuery('#infopop').children().removeClass('w3c');
		
		//getting height and width of the message box
		var height = jQuery('#infopop').height();
		var width = jQuery('#infopop').width();
		
		//calculate offset from selected link and apply to infopop
		var offset = jQuery(this).offset();
		leftVal = offset.left + "px";
		topVal = offset.top + "px";
		jQuery('#infopop').css({
			left: leftVal,
			top: topVal
		})
		
		//remove infopop and restore image
		jQuery(this).blur(function(){
			jQuery('#infopop').remove();
		});
		
		return false;
	});
	
	// iframe for IE6 welcome page popup
	
	jQuery('#welcome-container').before('<iframe frameborder=0 class="welcome" width="500px" height="450px"></iframe>');
	
	jQuery('.welcome-continue').click(function(){
		jQuery('#welcome-container').hide();
		jQuery('iframe').hide();
		return false;
	});
	
	jQuery('.tradehint').hover(function(){jQuery('#tradehint').show();},function(){jQuery('#tradehint').hide();});
	jQuery('.privatehint').hover(function(){jQuery('#privatehint').show();},function(){jQuery('#privatehint').hide();});
			
	// display web preview
	jQuery('#step-4a #web-preview a, #step-4a .preview a').click(function() {
																		  
		jQuery('.preview input,.btn-prev').hide();
		
		jQuery('html, body').animate({scrollTop: '180px'}, 500,'swing');

		var rel = jQuery(this).attr('rel');
		jQuery('.preview-full-advert.'+rel).addClass('showing');
		jQuery('.preview-full-advert.'+rel).css({"left":"0px","top":"0px"});
		
		jQuery('.preview-full-advert.'+rel+' .close,.preview input,.btn-prev').click(function() {
			jQuery('.preview-full-advert').css({"left":"-9999px","top":"-9999px"});
			jQuery('.preview input,.btn-prev').show();
			if(jQuery('.preview-full-advert.'+rel).hasClass('showing'))
			{
				jQuery('.preview-full-advert.'+rel).removeClass('showing');
				return false;
			}
		});
		jQuery('body').click(function() {
			jQuery('.preview-full-advert').css({"left":"-9999px","top":"-9999px"});
			jQuery('.preview input,.btn-prev').show();
		});
		jQuery('.preview-full-advert.'+rel).click(function(event) {
			 event.stopPropagation();
		});
		return false;
	});
	
	// display web preview
	jQuery('#step-5c #web-preview a').click(function() {
		jQuery('#preview-full-advert').css({"left":"0px","top":"0px"});
		jQuery('#preview-full-advert .close').click(function() {
			jQuery('#preview-full-advert').css({"left":"-9999px","top":"-9999px"});
			return false;
		});
		jQuery(' body').click(function() {
			jQuery('#preview-full-advert').css({"left":"-9999px","top":"-9999px"});
		});
		jQuery('#preview-full-advert').click(function(event) {
			 event.stopPropagation();
		});
		return false;
	});
	
	jQuery('#preview-full-advert .gallery .active em').fadeTo(1,0.7);
	
	jQuery('#preview-full-advert .gallery li a').click(function(){
											  
		jQuery('#preview-full-advert .gallery em').fadeTo(1,1);
		jQuery('#preview-full-advert .gallery a').removeClass('active');
		jQuery(this).addClass('active');
		jQuery(this).children('em').fadeTo(1,0.7);
		var largeSrc = jQuery(this).find('img').attr('src').replace(/\/thumbnail/,'/fullsize');
		jQuery('#preview-full-advert .gallery p img').attr('src',largeSrc);
		jQuery(this).blur();
		return false;
		
	});
	
	if(jQuery('p.print').length > 0)
	{
		jQuery('p.print a').live('click',function() {
			window.print();
			return false;								 
		});
	}
	
});
var timer;
function showTip()
{
	clearTimeout(timer);
	jQuery('#tip').show();
}
function hideTip()
{
	timer = setTimeout("jQuery('#tip').hide()",100);
}
function filterDisabled(date){
  var checkDate = date.getDate();
  var checkMonth = date.getMonth();
  checkMonth++;
  
  if(checkDate <= 9)
  {
	checkDate = '0' + checkDate;  
  }
  if(checkMonth <= 9)
  {
	checkMonth = '0' + checkMonth;  
  }
  
  var checkYear = date.getFullYear();
  var thisDate = checkMonth + '' + checkDate + ''  + checkYear;
  var flag = 1;
  if(allowedDates.search(thisDate) == -1)
  {
	flag = 0;  
  }
  return [flag, ''];
}; 
