function getWaitImage()
{
	var wait = $(document.createElement('img'));
	wait.attr('src', '/images/ajax.gif');
	return wait;
}

function initializeSearchText() {
	var search_field = $('#search-field');
	if ( search_field.length != 0 )
	{
		// Initialize
		if ( search_field.val() == '' )
		{
			search_field.val('Enter your search terms here');
		}
		search_field.focus(function(){
			if ( this.value == 'Enter your search terms here' )
				this.value = '';
		});
		search_field.blur(function(){
			if ( this.value == '' )
				this.value = 'Enter your search terms here';
		});
	}
}

function setCharacterLimit(element, maxlimit)
{
	if (element.length == 0)
		return;
	maxlimit = parseInt(maxlimit);
	
	var name = element.attr('name');
	var counter = $('#' + name + '-counter');
	
	// Create the counter if it does not exist
	if ( counter.length == 0 ) {
		element.after('<br /><span class="small" id="' + name + '-counter"></span>');
		var counter = $('#' + name + '-counter');
	}
	
	if (element.val().length > maxlimit)
		element.val(element.val().substring(0, maxlimit));

	counter.html((maxlimit - element.val().length) + ' characters remaining');
}

function setTextLimit(element, limit)
{
	if (element.length == 0)
		return;
	setCharacterLimit(element, limit);
	element.keyup(function(){setCharacterLimit(element, limit);});
	element.keydown(function(){setCharacterLimit(element, limit);});
}

function initializeSearchAutocomplete()
{
	var search_field = $('#search-field');
	if ( search_field.length != 0 )
	{
		var token = $('input[name=token]').val();
		search_field.autocomplete('/ajax/search.php?token=' + token);
	}
}

function reScale()
{
	if ( screen.width <= 640 ) // most handhelds are less than 640px width and will use native scaling
		return;
	
	// First get width depending on browser
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
		// Non-IE - standards compliant
		myWidth = window.innerWidth;
	}
	else if( document.documentElement && document.documentElement.clientWidth )
	{
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	}
	else if( document.body && document.body.clientWidth )
	{
		// IE quirks mode
		myWidth = document.body.clientWidth;
	}
	// Get width of wrapper
	var $wrapper = $('#wrapper');
	var wrapperWidth = $wrapper.width();
	// Trigger zoom if browser width is less than wrapper width
	if ( myWidth < wrapperWidth )
	{
		if (myWidth < 768)
			$wrapper.css("width", "738px");
		else
			$wrapper.css("width", (myWidth - 30) + "px");
	}
	else
	{
		if ( myWidth > 960 ) 
			$wrapper.css("width", "960px");
		else
			$wrapper.css("width", (myWidth - 30) + "px");
	}
}

$('<img>').attr('src', '/images/ajax.gif');

/**
 * http://www.kriesi.at/archives/create-simple-tooltips-with-css-and-jquery
 * @return
 */
function simple_tooltip()
{
//	var elements = $('a').add('span').add('input').add('img');
	var elements = $('.popup');
	
	elements.each(
	function(i) {
		if ($(this).attr('title') == '') return;
		
		$("body").append(
				"<div class='title' id='title" + i + "'><p>"
						+ $(this).attr('title') + "</p></div>");
		var my_tooltip = $("#title" + i);

		$(this).removeAttr("title").mouseover( function() {
			my_tooltip.css( {
				opacity : 0.8,
				display : "none"
			}).show();
		}).mousemove( function(kmouse) {
			my_tooltip.css( {
				left : kmouse.pageX -15,
				top : kmouse.pageY + 15
			});
		}).mouseout( function() {
			my_tooltip.hide();
		});
	});
}

$(document).ready(function()
{	
	// Set the search field
	initializeSearchText();
	
	// Add "Click to dismiss this message" to the message box
	if ( $('#message').length > 0 )
	{
		var message = $('#message');
		message.append(document.createTextNode(' '));
		
		var a = $(document.createElement('a'));
		a.html('Click to hide message.');
		a.attr('href', 'javascript:');
		a.click(function(){$('#message').hide();});
		
		message.append(a);
	}
	
	// Set the search auto-complete
	initializeSearchAutocomplete();
	
	// Use javascript to open links with a class of new-window in a new window/tab
    $('a.new-window').click(function(){
        window.open(this.href);
        return false;
    });
    
    // Rollover effects for SNS icons
    $('#twitter').mouseover(function(){$(this).attr('src', '/images/sns/Twitter-ICON.png');}).
    	mouseout(function(){$(this).attr('src', '/images/sns/Twitter-ICON-dark.png');});
    $('#facebook').mouseover(function(){$(this).attr('src', '/images/sns/Facebook-ICON.png');}).
    	mouseout(function(){$(this).attr('src', '/images/sns/Facebook-ICON-dark.png');});
    $('#digg').mouseover(function(){$(this).attr('src', '/images/sns/Digg-ICON.png');}).
    	mouseout(function(){$(this).attr('src', '/images/sns/Digg-ICON-dark.png');});
    
    //Rollover effect for Continue button on sign up pages
    $('.continue_button').mouseover(function(){$(this).attr('src', '/images/continue-smaller-over.png');}).
		mouseout(function(){$(this).attr('src', '/images/continue-smaller.png');});
    
//    simple_tooltip();
    
    // Rescale web page if window width is smaller than #wrapper width (generally 960px)
	// window.onresize = reScale;
//	reScale(); // Temporarily commented out because there are some width-pixel issues.
});