//Document Ready
$(function(){
		   
	/* General */
	//Active tab in primary nav.
	var url = window.location.pathname
	if(url.indexOf('defense') > -1){
		$('#primary li:eq(0)').addClass('selected');
		$('#primary li:eq(1)').css('background', 'none');
	}
	else if(url.indexOf('public-safety') > -1){
		$('#primary li:eq(1)').addClass('selected');
		$('#primary li:eq(2)').css('background', 'none');
	}
	else if(url.indexOf('maritime') > -1){
		$('#primary li:eq(2)').addClass('selected');
		$('#primary li:eq(3)').css('background', 'none');
	}
	else if(url.indexOf('transportation') > -1){
		$('#primary li:eq(3)').addClass('selected');
		$('#primary li:eq(4)').css('background', 'none');
	}
	else if(url.indexOf('mining-oil-gas') > -1){
		$('#primary li:eq(4)').addClass('selected');
	}
	
	
	//Hide/Display message in search textbox in header.
	$('#search input[type=text]').focus(function(){
		$(this).val('');
	});
	$('#search input[type=text]').blur(function(){
		if($('#search input[type=text]').val() == ""){
			$(this).val('Search our site');
		}
	});
	
	//Bread Crumbs last link color.
	if($('#breadCrumbs').length > 0){
		$('#breadCrumbs a:last').addClass('selected');
	}
	
	//Contact lightbox trigger. - Uses jqModal plugin.
	if($('#about-us #footer > a[href*="contact"]').length > 0){
		$('#footer ul').append('<li></li>');
		//$('#footer ul li:last').append($('#about-us #footer > a[href*="contact"]'));
		alert("test");
	}
	
	$('#contactLightBox').jqm({ajax: '/contact', trigger: 'a[href*="contact"]'});
	
	/* Home Page */
	//Flash
	if($('#home').length > 0){
		swfobject.registerObject("homeFlash");
	}
	
	//Hide/Display message in subscriber fields.
	$('#CLFullName').focus(function(){
		$(this).val('');
	});
	$('#CLFullName').blur(function(){
		if($('#CLFullName').val() == ""){
			$(this).val('Enter name');
		}
	});
	$('#CLEmailAddress').focus(function(){
		$(this).val('');
	});
	$('#CLEmailAddress').blur(function(){
		if($('#CLEmailAddress').val() == ""){
			$(this).val('Enter email address');
		}
	});
	
	$('#CAT_Custom_206157').focus(function(){
		$(this).val('');
	});
	$('#CAT_Custom_206157').blur(function(){
		if($('#CAT_Custom_206157').val() == ""){
			$(this).val('Enter company');
		}
	});

	$('#CAT_Custom_206158').focus(function(){
		$(this).val('');
	});
	$('#CAT_Custom_206158').blur(function(){
		if($('#CAT_Custom_206158').val() == ""){
			$(this).val('Enter country');
		}
	});
	
	//OpenClose
	$('#openClose li:first h3, #openClose li:first div').addClass('selected').show();//Initially display the first items content.


	//$('#openClose li h3').click(function(){
	$('#openClose li').click(function(){
		$('#openClose .selected').removeClass('selected');
		$(this).children('div').addClass('selected');
		$(this).children('h3').addClass('selected');
		$('#openClose div').each(function(index){
			if($(this).hasClass('selected') == false){
				$(this).slideUp(400);
			}
			else{
				$(this).slideDown(400);
			}
		});
	});
	
	//Format product titles.
	//This will slow the site down.
	$('h1, h2, p, li, td').each(function(){
		$(this).html(fnReplaceTitle($(this).html()));
	});
	
	/* Products */
	if($('#product').length > 0){
		//Format product titles.
		$('#product h1, #product h2, #product p, #product li, #product td').each(function(){
			$(this).html(fnReplaceTitle($(this).html()));
		});
		
		//Product image.
		if($('#product .thumbnail img').length == 0){
			$('#product .thumbnail').remove();
		}
		else if($('#product .thumbnail .hide').html() != ""){
			$('#product .thumbnail').html($('#product .thumbnail').html() + "<a href='#' class='enlargeImage'></a>");
		}
		
		//Give the first block the "selected" class on load.
		$('#product .block:first').addClass('selected');
		fnDisplayBrochureBtn();
		
		//Block navigation.
		var currentTab;
		$('.blockNav a').click(function(){				
			if($(this).parent('li').hasClass('selected') == false){
				$(this).addClass('selected');
				$('.blockNav li.selected').removeClass('selected');
				$('.block.selected').hide().removeClass('selected');
				currentTab = $(this).attr('href');
				$('.block.'+ currentTab +'').show().addClass('selected');
				fnDisplayBrochureBtn();
			}
			return false;
		});
				
	}
	
	//Correct widths of sub-category/product images.
	if($('ul#products').length > 0){
		var liWidth = 0;
		$('ul#products li').each(function(){
			liWidth = $(this).children('a').children('img').width();
			liWidth = liWidth - (parseInt($(this).children('h3').children('a').css('paddingLeft')) + parseInt($(this).children('h3').children('a').css('paddingRight')));
			$(this).children('h3').children('a').width(liWidth);
		});
	}
	if($('ul#subCategories').length > 0){
		var liWidth = 0;
		$('ul#subCategories li').each(function(){
			liWidth = $(this).children('a').children('img').width();
			liWidth = liWidth - (parseInt($(this).children('h3').children('a').css('paddingLeft')) + parseInt($(this).children('h3').children('a').css('paddingRight')));
			$(this).children('h3').children('a').width(liWidth);
		});
	}
	
	//Alter layout if there are more than 4 sub-categories.
	if($('ul#subCategories li').length > 3){
		$('ul#subCategories').addClass('multiRow');
		$('ul#subCategories li:even').addClass('newRow');
	}
	
	//Show brochure button if required.
	function fnDisplayBrochureBtn(){
		if($('#product .selected .brochure').length > 0){
			if($('#product .selected .brochure').attr('href') != ''){
				$('#product .selected .brochure').removeClass('hide');
			}
		}
	}
	
	function fnReplaceTitle(string){
		string = string.replace(/&reg;/g, '');
		//string = string.replace(/?/g, '');
		string = string.replace(/SwitchplusIP /g, 'Switch<em><strong>plusIP</strong></em>&reg; ');
		string = string.replace(/SwitchplusIP\./g, 'Switch<em><strong>plusIP</strong></em>&reg;. ');
		string = string.replace(/SwitchplusIP&reg;/g, 'Switch<em><strong>plusIP</strong></em>&reg;');
		string = string.replace(/SwitchplusIP/g, 'Switch<em><strong>plusIP</strong></em>');
		return string;
	}
	
	//Format competition forms.
	if($('#main form table').length > 0){
		var formAction = $('#main form').attr('action');
		if(formAction.indexOf('https://app1.salesnet.com/') > -1){
			$('#main form').addClass('salesnetForm');
		}		
	}
	
});

/* Validate Subscription Form */
function checkWholeForm80200(theForm){
	var why = "";
	if (theForm.FullName.value=='Enter name' || theForm.FullName.value==''){ why +='- Please enter your name \n';}
	if (theForm.EmailAddress){ why += checkEmail(theForm.EmailAddress.value);}
	if (why != ""){alert(why);return false;}theForm.submit();
	return false;
}
