$(document).ready(function()
{
	var shadowbox_options = ''; // Will be appended to rel attribute for images and media
	
	//---------------------------------------------START CUSTOM JS//
	if(navigator.appVersion.indexOf("Mac")!=-1) // Sort out silly mac fat fonts
	{
		$('body').css('fontSize','64%');
		padCol1 = 54;
		padCol2 = 6;
	}
	else
	{
		padCol1 = 60;
		padCol2 = 8;	
	}
	
	/*
		new web quote form with town menus based on selection in Country menus
	*/
	$('form#web_quote select').change(function()
	{
		var n = $(this).attr('id');
		if(n.substring(0,8) == 'country_')
		{
			// it's a country select thing... let's update the town list below
			if($(this).val() != 'null')
			{
				// open the right javascript file with the array in
				$.getScript(_root+'media/js/town/'+$(this).val()+'.js', function()
				{
					// replace the options with the new town list
					$('#town_'+n[n.length-1]).empty();
					for (i=0;i<towns.length;i++){
						$('<option/>').val(towns[i]).html(towns[i]).appendTo('#town_'+n[n.length-1]);
					}
				});
			}		
		}
	});
	
	/*
		validating web quote form
	*/
	$('form#web_quote').submit(function()
	{
		var required_inputs = new Array('full_name','company_name','email_address','telephone_number');
		var required_select = new Array('type_of_goods','country_1','town_1','country_2','town_2');
		
		var no_errors = true;
		
		// clear all warnings
		$('form#web_quote input, form#web_quote select').each(function()
		{
			if($(this).parent().hasClass('warning'))
			{
				$(this).parent().removeClass('warning');
			}
		});
		if($('p.error').length > 0)
		{
			$('p.error').hide();
		}
		
		for(i=0;i<required_inputs.length;i++)
		{
			if($('input#'+required_inputs[i]).val() == '')
			{
				$('input#'+required_inputs[i]).parent().addClass('warning');
				no_errors = false;
			}
		}
		for(i=0;i<required_select.length;i++)
		{
			if($('select#'+required_select[i]).val() == 'null')
			{
				$('select#'+required_select[i]).parent().addClass('warning');
				no_errors = false;
			}			
		}

		// if country has been selected in LOAD 2,3 and 4, 
		// ensure all other menu options are set, 
		// else add warning class and stop form submission
		/*
		if($('select#country_3').val() != 'null')			
		{	
			if($('select#country_4').val() == 'null')
			{
				$('select#country_4').parent().addClass('warning');
				$('select#town_4').parent().addClass('warning');
				no_errors = false;
			}
		}
		if($('select#country_5').val() != 'null')			
		{	
			if($('select#country_6').val() == 'null')
			{
				$('select#country_6').parent().addClass('warning');
				$('select#town_6').parent().addClass('warning');
				no_errors = false;
			}
		}
		if($('select#country_7').val() != 'null')			
		{	
			if($('select#country_8').val() == 'null')
			{
				$('select#country_8').parent().addClass('warning');
				$('select#town_8').parent().addClass('warning');
				no_errors = false;
			}
		}
		
		// if user has ticked box to request a call back, make sure date and time are filled in
		if($('input#call_me_back').is(':checked'))
		{
			if($('input#contact_date').val() == '')
			{
				$('input#contact_date').parent().addClass('warning');
				no_errors = false;
			}
			if($('input#contact_time').val() == '')
			{
				$('input#contact_time').parent().addClass('warning');
				no_errors = false;
			}			
		}*/
		
		
		
		if(!no_errors)
		{
			if($('p.error').length > 0)
			{
				$('p.error').show();
			}
			else
			{
				$('<p class="error">Oops! You forgot some information! Please fill in the fields highlighted in red.</p>').insertAfter($('legend'));
			}
			return false;
		}
		else 
		{
			return true;
		}
	});

	$('input#call_me_back').change(function() {
		if($('input#call_me_back').is(':checked'))	
		{
			$('input#alternative_contact').parent().show();
			$('input#contact_time').parent().show();
			$('input#contact_date').parent().show();
			$('button.callback_button').show();
		}
		else 
		{
			$('input#alternative_contact').parent().hide();
			$('input#contact_time').parent().hide();
			$('input#contact_date').parent().hide();
			$('button.callback_button').hide();
		}
	});	
	
	$('input#additional_locations').change(function() {
		if($('input#additional_locations').is(':checked'))	
		{
			for(i=3;i<9;i++)
			{
				$('select#country_'+i).parent().show();
				$('select#town_'+i).parent().show();
			}
			$('button.submit_2').show();
		}
		else 
		{
			for(i=3;i<9;i++)
			{
				$('select#country_'+i).parent().hide();
				$('select#town_'+i).parent().hide();
			}
			$('button.submit_2').hide();
		}
	});	
	
	
	/*
		default settings for web quote form (hidden on load)
	*/
	if($('form#web_quote').length > 0)
	{
		$('input#alternative_contact').parent().hide();
		$('input#contact_time').parent().hide();
		$('input#contact_date').parent().hide();
		$('button.callback_button').hide();
		for(i=3;i<9;i++)
		{
			$('select#country_'+i).parent().hide();
			$('select#town_'+i).parent().hide();			
		}
		$('button.submit_2').hide();	

		$('form#web_quote ol:eq(2)').addClass('split_middle');
		
	};
	
	
	if($('ul#nav li a[title="Get A Web Quote"]').length > 0)
	{
		$('ul#nav li a[title="Get A Web Quote"]').bind('click',function()
		{
			$('html,body').animate({scrollTop: $('h1.web_quote').offset().top}, 1000);
			return false;
		});
	}
	
	if($('a.quote').length > 0)
	{
		$('a.quote').bind('click',function()
		{
			$('html,body').animate({scrollTop: $('h1.web_quote').offset().top}, 1000);
			return false;
		});
	}
	
	if($('div.news_column').length > 0)
	{
		if($('form#web_quote').length > 0)
		{
			$('div.news_column').css('height',($('form#web_quote').height()-padCol1)+'px');//54 for Mac, 60 for PC
		}
		else
		{
			$('div.news_column').css('height',($('form#contact_form').height()-padCol2)+'px');//6 for Mac, 8 for PC
		}
	}
	
	if($('a#jump_to_form').length > 0)
	{
		$('a#jump_to_form').bind('click',function()
		{
			$('html,body').animate({scrollTop: $('h1.enquiry_form').offset().top}, 1000);
			return false;
		});
	}	
	
	if($('form#web_quote').length > 0 || $('form#contact_form').length > 0)
	{
		jQuery.fn.overlabel = function()
		{
		    this.each(function(index)
			{
		        var label = $(this); var field;
		        var id = this.htmlFor || label.attr('for');
		        if(id && (field = document.getElementById(id)))
				{
		            var control = $(field);
		            label.addClass("overlabel-apply");
		            if (field.value !== '')
					{
		                label.css("text-indent", "-9999px");
		            }
		            control.focus(function () {label.css("text-indent", "-9999px");}).blur(function()
					{
		                if(this.value === '')
						{
		                    label.css("text-indent", "0px");
		                }
		            });
		            label.click(function()
					{
		                var label = $(this); var field;
		                var id = this.htmlFor || label.attr('for');
		                if (id && (field = document.getElementById(id)))
						{
		                    field.focus();
		                }
		            });
		        }
		    });
		}
		
		$('label').each(function()
		{
			if(!$(this).parent().parent().hasClass('inputgroup') && !$(this).next().hasClass('file'))
			{
				$(this).addClass('overlabel');
			}
			else if($(this).next().hasClass('file'))
			{
				$(this).parent().addClass('file');
			}
			
			$('label.overlabel').overlabel();
		});
		
		$('select option:first-child').each(function()
		{
			var parent_id = $(this).parent().attr('id');
			
			var label_text = $('label[for="'+parent_id+'"]').html();
			
			if(label_text.search('<span') !== false)
			{
				label_text = label_text.split('>');
				
				$(this).html(label_text[0]);
			}
			else
			{
				$(this).html(label_text);
			}
			
			$('label[for="'+parent_id+'"]').removeClass('overlabel-apply').hide();
		});
	};

	window.setTimeout(function() {
		$('ul.contact span.skype_pnh_container').html('');
		$('ul.contact span.skype_pnh_print_container').removeClass('skype_pnh_print_container');
	}, 2000);

	/* roundy corners */
	$.getScript(_root+'media/js/jquery-corner.js', function()
	{
		if(!$.browser.msie || $.browser.version > 7)
		{
			$('body.partner-survey div.page').corner('10px');
			$('form#survey_form').corner('10px');
			$('body.web-quote div.page').corner('10px');
			$('body.web-quote form#web_quote').corner('10px');
			$('body.web-quote form#web_quote ol').corner('10px');
			$('body.web-quote form#web_quote p.error').corner('10px');
		}
	});
	
	// Add date pickers
	if($("li.date input").length > 0)
	{
		$("li.date input").each(function()
		{
			$(this).datePicker({startDate:'01/01/1996'}).dpSetSelected(new Date().asString());

		});
	}	
	// remove label on date picker input field when user clicks on calendar icon
	$('a.dp-choose-date').click(function() 
	{
		$('label[for="contact_date"]').removeClass('overlabel-apply').hide();
	});
	
	//---------------------------------------------END CUSTOM JS//
	
	$("li.subnav").hover(function()
	{
		$(this).addClass("active");
		
		if($("a.nav_search").hasClass("active"))
		{
			$("a.nav_search").removeClass("active");
			$("form.search").animate({ opacity: 0 }, 250);
			
			search_height_timeout = setTimeout(function()
			{
				$("form.search").css("height","0");
			}, 1000);
		}
	},
	function()
	{
		$(this).removeClass("active");
	});
	
	$('a[rel*="external"]').click(function()
	{
		open($(this).attr('href'));
		return false;
	});
	
	$('a[rel*="shadowbox"]').each(function()
	{
		if(!$(this).hasClass('page_media'))
		{
			var enlarge_img = $(this).attr('rel').split(' ');
			
			if(enlarge_img != 'undefined' && enlarge_img != '')
			{
				$(this).removeAttr('rel');
				
				if(enlarge_img[1] != undefined)
				{
					$(this).attr('rel', enlarge_img[1]+shadowbox_options);
				}
				else
				{
					$(this).attr('rel', enlarge_img[0]+shadowbox_options);
				}
			}
		}
		else
		{
			var media_rel = $(this).attr('rel');
			
			$(this).removeAttr('rel');
			
			$(this).attr('rel', media_rel+shadowbox_options);
		}
	});
	
	$("ul.thumbs li a img").each(function()
	{
		$.preloadImages.add($(this).attr("src").replace("thumb", "mid"));
	});
	
	$("ul.thumbs li a").mouseover(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
	});
	
	$("ul.thumbs li:first a").click(function()
	{
		$("a#enlarge").click();
	});
	
	$("ul.thumbs li a").click(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
		return false;
	});
	
	if($('a.page_media').length > 0)
	{
		var page_media = $('a.page_media').attr('rel').split(' ');
		
		if(page_media != 'undefined' && page_media != '')
		{
			$('a.page_media').removeAttr('rel');
			$('a.page_media').attr('rel', page_media[1]);
		}
	}
	
	if($("li.help a").length > 0)
	{
		$("li.help a").click(function()
		{
			return false;
		});
		
		var help_timeout = 'undefined';
		
		$("li.help a").bind("mouseenter",function()
		{
			if(help_timeout != 'undefined')
			{
				clearTimeout(help_timeout);
			}
			$("li.help div").fadeOut();
			$(this).next().addClass('active').fadeIn();
		}).bind("mouseleave",function()
		{
			help_timeout = setTimeout(function()
			{
				$('li.help div.active').removeClass('active').fadeOut();
			},2000);
		});
		
		$('input').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('select').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('textarea').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
	}
	
	if($('a[rel*="shadowbox"]').length > 0)
	{
		if($('a.page_audio').length > 0 || $('a.page_video').length > 0)
		{
			Shadowbox.init({
								continuous		:	true,
								ext				: 	{
										                img		:	['png', 'jpg', 'jpeg', 'gif', 'bmp'],
										                swf		:	['swf'],
										                flv		:	['flv', 'm4v', 'mp4', 'mp3'],
										                qt		:	['dv', 'mov', 'moov', 'movie'],
										                wmp		:	['asf', 'wm', 'wmv'],
										                qtwmp	:	['avi', 'mpg', 'mpeg']
										            },
								players			: 	['img', 'flv', 'iframe'],
								useSizzle		: 	false
							});
		}
		else
		{
			Shadowbox.init({
								continuous		: 	true,
								players			: 	['img'],
								useSizzle		: 	false
							});
		}
	}
});
