/**
 * @author		Kato Media http://www.katomedia.com/
 * @copyright	&copy; Kato Media 2009
 */

window.km = {};

$(function(){
	
	// Quicksearch
	var quicktext = 'enter your search term'
	$( '#quicksearch' ).submit(function(){
		var search = $( '#quicksearchField' ).val();
		if ( search == '' || search == quicktext ) {
			return false;
		} else {
			return true;
		}
	});
	$( '#quicksearchField' ).focus(function(){
		if ( $( '#quicksearchField' ).val() == quicktext ) {
			$( '#quicksearchField' ).val( '' );
		}
	}).blur(function(){
		if ( $( '#quicksearchField' ).val() == '' ) {
			$( '#quicksearchField' ).val( quicktext );
		}
	});
	
	// Tidy up hrefs
	$.each( $( 'a' ), function( index, el ){
		var href = $( el ).attr( 'href' );
		
		// Force external links to open in new window
		if ( strstr( href, 'http://' ) !== false && strstr( href, window.km.baseHref ) === false ) {
			$( el ).attr( 'target', '_blank' );
		}
		
		// Nullify bad email links with no mailto
		if ( strstr( href, '@' ) !== false && strstr( href, 'mailto:' ) === false ) {
			$( el ).replaceWith( $( el ).text() );
		}
		
	});
	
	
});


/**
 * Wrapper for console.log to prevent it causing errors on browsers that do not support it
 * @param mixed item The item to send to error console
 */
function debug( item ){
	
	if ( typeof console != 'undefined' ) {
		
		console.log( item );
		
	}
	
}

function dumpSession(){
	
	$.ajax({
		url: '/ajax/dumpSession/',
		success: function( r ){
			debug( r );
		},
		error: function( x, t, e ){
			debug( x.responseText );
		}
	})
	
}

