/**
 * Additional onload functionality
 */
$(document).ready(function() {
	
	$('#business-tab').click(function () {
		changeTab(this,$('#searchFormHolder'));
		return false;
	});
	$('#people-tab').click(function () {
		changeTab(this,$('#search-people'));
		return false;
	});
	$('#category-tab').click(function () {
		changeTab(this,$('#search-category'));
		return false;
	});
	$('#website-tab').click(function () {
		changeTab(this,$('#search-website'));
		return false;
	});
	$('#numbers-tab').click(function () {
		changeTab(this,$('#search-numbers'));
		return false;
	});
	
	function changeTab(e,t) {
		$('.page-tabs a').removeClass('selected');
		$(e).addClass('selected');
		$('.page-form form:visible').hide();
		t.show();
	}
	
	$('#popular-searches a.psuedo-link').click(function (e) {
		$('.popsearch-popup').hide();
	    var $div = $(e.target).next('.popsearch-popup');
	    var pos = $(e.target).position();
		$div.css({
			top: pos.top - $div.height()/2 + "px",
			left: pos.left - $div.width()/2 + "px"});
		$div.fadeIn();
	    e.preventDefault();
	    return false;
	});
	
	$('.rounded').corner();
	
	if(typeof(pageMode)=='undefined') return;
	
	$('#search-category #keyword_input').suggestCat({maxResults: 25});
		
	if(pageMode != 'index') {
		$('#form-go').hover(function() {
			$(this).attr({src :'/images/pubs/mooco/go_small_active.png'});
		}, function() {
			$(this).attr({src: '/images/pubs/mooco/go_small.png'});
		});
		$('#form-go').focus(function() {
			$(this).attr({src :'/images/pubs/mooco/go_small_active.png'});
		});
		$('#form-go').blur(function() {
			$(this).attr({src :'/images/pubs/mooco/go_small.png'});
		});
	}
	
	if (curPage == 'white'){
		$('#people-tab').click();
	}else if (curPage == 'google'){
		$('#website-tab').click();
	}else if (curPage == 'category'){
		$('#category-tab').click();
	}else if (curPage == 'reverse'){
		$('#numbers-tab').click();
	}else{
		$('#business-tab').click();
	}
	
	$('#searchFormHolder #keyword_input').inputToggle('What');
	$('#searchFormHolder #location_input').inputToggle('Where');
	
	$('#search-people #firstname').inputToggle('First Name or Advertiser Name');
	$('#search-people #lastname').inputToggle('Last Name');
	
	$('#searchFormHolder').submit(function() {
		if($('#keyword_input').val() == 'What') $('#keyword_input').val('');
		if($('#location_input').val() == 'Where') $('#location_input').val('');
		if($('#location_input').val() == 'All') $('#location_input').val('');
	});
	$('#search-people').submit(function() {
		if($('#firstname').val() == 'First Name or Advertiser Name') $('#firstname').val('');
		if($('#lastname').val() == 'Last Name') $('#lastname').val('');
	});
	$('#searchFormHolder #location_input').suggest({resultsId: 'city-dd',data: ['Aberdeen', 'Cameron', 'Carthage', 'Eagle Springs', 'Highfalls', 'Jackson Springs (Foxfire)', 'Lakeview', 'Pinebluff', 'Pinehurst', 'Robbins', 'Seven Lakes', 'Southern Pines', 'Taylortown', 'Vass', 'West End', 'Whispering Pines', 'All'],minInput: 0});
	 
	
	if(pageMode=='search') {
		$('#related-searches-toggle').bind('click', function() {
			if($(this).text().match('.*More.*')) $(this).html($(this).html().replace('More','Less'));
			else $(this).html($(this).html().replace('Less','More'));
			$('#related-searches li.hidden').toggle();
			$(this).blur();
			return false;
		});
	}
});

function findPos(obj) {
	var curleft = obj.offsetLeft || 0;
	var curtop = obj.offsetTop || 0;
	while (obj = obj.offsetParent) {
		curleft += obj.offsetLeft
		curtop += obj.offsetTop
	}
	return {x:curleft,y:curtop};
}

jQuery.fn.isDefined = function() {
	return typeof($(this)[0]) != 'undefined';
}
jQuery.fn.inputToggle = function(phrase) {
	var t = $(this);
	if(t.val() == '') t.val(phrase);
	t.bind("focus", function(e) {
		if(t.val() == phrase) t.val('');
	});
	t.bind("blur", function(e) {
		if(t.val() == '') t.val(phrase);
	});
}