//if (DetectTierIphone()) {
//	location.href="/mobile";
//}

var directoryscrollerrunning=false;

$(function() {
	// Search box set up
	$('#searchbox').click(function() {
		$('#searchtext').show().focus();
	});
	$('#searchtext').blur(function() {
		if ($('#searchtext').val()=="")
			$('#searchtext').hide();
	});
	$('.searchbutton').click(function() {
		$('#searchform').submit();
	});
	if ($('#searchtext').val()!="")
		$('#searchtext').show().focus();

	$('.searchoption').click(function () {
		$('ul.searchoptions').slideToggle('medium');
	});
	$('.searchoption').click();
	
	$('.searchoptions_searchall').click(function() {
		setSearchOpts(0);	
	});
	$('.searchoptions_news').click(function() {
		setSearchOpts(1);	
	});
	$('.searchoptions_company').click(function() {
		setSearchOpts(2);	
	});
	$('.searchoptions_forums').click(function() {
		setSearchOpts(3);	
	});

	$('.directoryscroller').appear(function() {
		if (directoryscrollerrunning==false) {
			directoryscrollerrunning=true;
			morePremiumLogos();
		}
	});

	// Social media
	$('#addtolinkedin').click(function() {
		u=location.href;
		t=document.title;
		window.open('http://www.linkedin.com/shareArticle?mini=true&amp;ro=true&amp;url='+encodeURIComponent(u)+'&amp;source='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=660,height=460');
		return false;
	});
	$('#posttofb').click(function() {
		u=location.href;
		t=document.title;
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=660,height=460');
		return false;
	});
	$('#tweet').click(function() {
		u=location.href;
		t=document.title;
		window.open('http://twitter.com/share?url='+encodeURIComponent(u),'sharer','toolbar=0,status=0,width=500,height=343');
		return false;
	});

	$('#printnews').click(function() {
		window.print();
		return false;
	});
	
	// Links within the same page
	$('a.samepage').click(function(e) {
		e.preventDefault(); // Prevent a page reload when a link is pressed
		scrollToContent($(this).attr("id")); // Call the scroll function          
	});
	
	$.cookie('cookietest', 'ok');
	if ($.cookie('cookietest')!="ok") {
		hideCookieObjects();	
	}
});

function hideCookieObjects() {
	// This is called if cookies isn't set	
	if ($('#commentformcontainer').length>0) {
		$('#commentformcontainer').html("<em>In order to post comments you need to enable cookies on your web browser.</em>");	
	}
}

function setSearchOpts(opt) {
	switch(opt) {
		case 1:
			optText="Search news";
			break;
		case 2:
			optText="Search company";
			break;
		case 3:
			optText="Search forums";
			break;
		default:
			optText="Search all";
			break;
	}
	$('.searchoption').html(optText);	
	$('#searchoptionselected').html(opt)
	$('.searchoption').click();
}

function searchDirectory() {
	if ($('#searchtext').val()!="") {
		var filter=$('#searchoptionselected').html();
		if (filter!="")
			location.href="/search.html?s=" + $.URLEncode($('#searchtext').val()) + "&f=" + filter;
		else
			location.href="/search.html?s=" + $.URLEncode($('#searchtext').val());
	}
}

function morePremiumLogos() {
	$('#premiumlogoscontainer').animate({'left':'-800px'}, 500, function() {
		$('#premiumlogospanel1').html($('#premiumlogospanel2 > table').clone());
		$('#premiumlogoscontainer').css('left','0px');
		$('#premiumlogospanel2').load("/includes/premiumlogosloader.asp");
		setTimeout("morePremiumLogos()", 15000);
	});
}

function scrollToContent(id) {
	// Remove "link" from the ID
	id=id.replace("link", "");
	// Scroll
	$('html,body').animate({scrollTop: $("#"+id).offset().top}, 'slow');
}
	
function stripHTML(text) {
	var htmlStriper = /<(?:.|\s)*?>/g;
	text = text.replace(htmlStriper, " ");
	while (text.indexOf("  ") >= 0) {
		text = text.replace("  ", " ");
	}
	return text.replace(/^\s+|\s+$/g, "");
}

