String.prototype.endWith=function(oString){  
	var reg = new RegExp(oString+"$");  
	return reg.test(this); 
}
String.prototype.trim=function(){  
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

function addTrail(node){
	if(node.parent("ul").hasClass('nice-menu')){
		return false; 
	} else {
		var pli = node.parents("li");
		pli.children('a').addClass('active_trail');
		addTrail(pli);
		return true;
	}
}

$(document).ready(function(){
	
	// add a class to the blocks on the right sidebar
	$('#layout-tool-bar .right-col .block').each(function(i){
		if(i>0) $(this).addClass('mt30');
	});

	// display a image while the client does not support flash
	if(!FlashDetect.installed){
		$('#home .banner').html('<img src="sites/default/themes/purehacking/images/banner.jpg" alt="Pure Hacking Leaders in Internet Security" title="Pure Hacking Leaders in Internet Security" usemap="#bannermap" /><map name="bannermap"><area shape="rect" coords="48,226, 232,260" href="products-service" alt="Find out more" title="Find out more" /></map>');
	}

	if($('#inner .banner').length){
		if(!$('#inner .banner img').length){
			$('#inner .banner').html('<div class="content"><img title="Pure Hacking Leaders in Internet Security" alt="Pure Hacking Leaders in Internet Security" src="sites/default/themes/purehacking/images/banner-product-services.jpg"></div>');	
		}
	}
	
	// search block on top right
	$('.block .input_search').focus(function(){
		if($(this).val() == 'Search') {
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val().trim() == '') {
			$(this).val('Search');
		}
	})
	$('.search-form #edit-keys').focus(function(){
		if($(this).val() == 'Search') {
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val().trim() == '') {
			$(this).val('Search');
		}
	})

	$('#search-block-form-1').submit(function(e){
		var key = $(this).find('input.form-text');
		if(key.val().trim() == 'Search') {
			key.val('');
		}
		if(key.val().trim().length < 3) {
			alert('You must include at least one positive keyword with 3 characters or more.');
			e.preventDefault();
		} else {
			key.val(key.val()+' type:blog');
		}
		key.val(key.val().replace('&', '&amp;'));
	});


	// replace '&' by '&amp;' in search function
	$('#search-block-form').submit(function(e){
		var key = $(this).find('input.form-text');
		if(key.val().trim() == 'Search') {
			key.val('');
		}
		if(key.val().trim().length < 3) {
			alert('You must include at least one positive keyword with 3 characters or more.');
			e.preventDefault();
		}
		key.val(key.val().replace('&', '&amp;'));
	})

	if($('#search-form').length){
		$('#search-form').submit(function(){
			var key = $(this).find('input#edit-keys');
			if(key.val().trim() == 'Search') {
				key.val('');
			}
			if(key.val().trim().length < 3) {
				alert('You must include at least one positive keyword with 3 characters or more.');
				e.preventDefault();
			}
			key.val(key.val().replace('&', '&amp;'));
		})
		$('input#edit-keys').val($('input#edit-keys').val().replace('&amp;', '&'));
	}
	
	// remove unnecessary menus of footer menu
	$('#layout-footer .footermenu').each(function(i){
		$(this).find('li.menu-mlid-199').remove(); //Home
		//$(this).find('li.menu-mlid-204').remove(); //Blog
	});
	
	// add active trail for main menu
	if($("ul.nice-menu a.active").length){
		addTrail($("ul.nice-menu a.active").parent("li"));
	}

	// change the layout of newsletter block in inner pages
	if($("#left-col .block-newsletter_edm").length){
		var newsletter = $("#left-col .block-newsletter_edm");
		$('<h2></h2>').html(newsletter.find('h3').html()).insertBefore(newsletter.find('.sec-newsletter .content'));
		newsletter.find('h3').remove();
	}
		
	if($("#webform-component-other_interests").length){
		$("#webform-component-other_interests").hide();
		$("#webform-component-areas_of_interest").find('input:last').change(function(){
			if($(this).get(0).checked){
				$("#webform-component-other_interests").slideDown();
			} else {
				$("#webform-component-other_interests").slideUp();
			}
		});

		$("#webform-component-other_industry").hide();
		$("#webform-component-industry").find('select').change(function(){
			if($(this).val() == 'Other'){
				$("#webform-component-other_industry").slideDown();
			} else {
				$("#webform-component-other_industry").slideUp();
			}
		});
	}

	$('#main-container .block-print a.font-reduce').click( function(e) {
		e.preventDefault();
		var font_size = parseInt($('body').css('font-size').replace('px', ''));
		$('body').css('font-size', (font_size > 8 ? font_size - 1 : font_size) + 'px');
	});
	$('#main-container .block-print a.font-reset').click( function(e) {
		e.preventDefault();
		$('body').css('font-size', '11px');
	});
	$('#main-container .block-print a.font-enlarge').click( function(e) {
		e.preventDefault();
		var font_size = parseInt($('body').css('font-size').replace('px', ''));
		$('body').css('font-size', (font_size < 13 ? font_size + 1 : font_size) + 'px');
	});
	
	//faq pages
	$(".faq h4").each(function(i){
		$(".faq h4").eq(i).toggle(
		  function () {
			$(".faq .faq-description").removeClass("active");
			$(".faq .faq-description").eq(i).addClass("active");
		  },
		  function () {
			$(".faq .faq-description").removeClass("active");
		  }
		);
	});

	$('a.videolink').click(function(e){
		e.preventDefault();
		popupVideo($(this).attr('href'));
	});
	
	$('#layout-footer .top a').click(function(e){
		e.preventDefault();
		window.scrollTo(0,0);
	});

	$('#client-portal-panel div.block:last').addClass('last');

	$('input#edit-name').focus(function(){
		if($(this).val() == 'Anonymous'){
			$(this).val('');
		}
	});
});

function popupVideo(href)
{
	var width = 484;
	var height = 364;
	var x = screen.availWidth;
	var y = screen.availHeight;
	var left = (x - width) /2;
	var top = (y - height) /3;
	window.open(href, 'video', 'left = ' + left + ', top = ' + top + ', width = ' + width + ', height = ' + height + ', scrollbars = yes, status = no, menubar = no, location = no, directories = no, resizable = no, titlebar = no, toolbar = no');
}


