

/**
 *
 * Controller
 * Lauches stuff when document is ready
 *
 **/
$( document ).ready( function() {

	inputFieldHandler( "#search-query" );
	loginHandler();
	fontSizeHandler();
	columnHeightHandler();
	//weatherLoader();
	firstPageSpecialsHandler();
	firstPageHotHeadlinesHandler();
} );


/**
 *
 * Search box handler
 *
 */
function inputFieldHandler( selector ) {
	// store defaults
	$( selector ).data( "default-value", $( selector ).val() );

	$( selector ).focus( function() {
		if( $( selector ).val() == $( selector ).data( "default-value" ) ) {
			$( selector ).val( "" );
		}
		else {
			$( selector ).select();
		}
	} );

	$( selector ).focusout( function() {
		if( $( selector ).val() == "" ) {
			$( selector ).val( $( selector ).data( "default-value" ) );
		}
	} );
}


/**
 *
 * Login box field handler
 *
 */
function loginHandler() {

	inputFieldHandler( "#username" );

	$( "#fake-password" ).focus( function() {
		$( "#fake-password" ).hide();
		$( "#password" ).show();
		$( "#password" ).select();
	} );

	$( "#password" ).focusout( function() {
		if( $( "#password" ).val() == "" ) {
			$( "#fake-password" ).show();
			$( "#password" ).hide();
		}
	} );

	$( "#password" ).focus( function() {
		$( "#password" ).select();
	} );

}


/**
 *
 * Handles font sizer
 *
 * */
function fontSizeHandler() {
	$( ".font-sizer .increase" ).click( function() {
		increaseFont();
		columnHeightHandler();
	} );
	$( ".font-sizer .decrease" ).click( function() {
		decreaseFont();
		columnHeightHandler();
	} );
}


/**
 *
 * Increase font
 *
 * */
function increaseFont() {
	var currentFontSize = parseFloat( $( "html" ).css( "font-size" ) );
	$( "html" ).css( "font-size", currentFontSize + 2 );
}


/**
 *
 * Decrease font
 *
 * */
function decreaseFont() {
	var currentFontSize = parseFloat( $( "html" ).css( "font-size" ) );
	$( "html" ).css( "font-size", currentFontSize - 2 );
}


/**
 *
 * Makes sure all columns are equal height and fits all content
 *
 * */
function columnHeightHandler() {

	$( window ).load( function() {

		var winH = $( window ).height();
		var docH = $( document ).height();
		var sidebH = $( ".sidebar" ).height();
		var contH = $( ".content" ).height();

		var maxH = Math.max( winH, docH, sidebH, contH );

		$( ".sidebar" ).height( maxH + 6 );
		$( ".content" ).height( maxH );

	} );

}



/**
 *
 * Loads weather data in top info bar
 *
 * */
function weatherLoader( prefix ) {
	$.get( prefix + "/weather/", function( data ) {
		$( "#info-bar .weather" ).html( data );
	} );
}



/**
 *
 * @access public
 * @return void
 **/
function firstPageSpecialsHandler() {
	$( ".specials .links li" ).mouseover( function() {
		var thisId = $( this ).attr( "id" );
		var postId = thisId.substr( 0, thisId.length - 4 );

		$( ".specials .links li" ).removeClass( "active" );
		$( this ).addClass( "active" );

		$( ".specials .post" ).hide();
		$( "#"+postId ).show();

	} );
}


/**
 *
 * @access public
 * @return void
 **/
function firstPageHotHeadlinesHandler(){
	$( ".hot-headlines .box-switch a" ).click( function() {
		var thisId = $( this ).attr( "id" );
		var boxId = thisId.substr( 0, thisId.length - 4 );
		//alert( boxId );

		$( ".hot-headlines .box-switch a" ).removeClass( "active" );
		$( this ).addClass( "active" );

		$( ".hot-headlines .box" ).removeClass( "active" );
		$( "#"+boxId ).addClass( "active" );

		return false;
	} );
}
