//  vim:ts=4:et
//  Copyright (c) 2009, LoveMachine Inc.
//  All Rights Reserved. 
//  http://www.lovemachineinc.com

$(function(){
	$('.stage-slide-out-div').show(); // to avoid div poping up on page load before Jquery is running
    $('.stage-slide-out-div').tabSlideOut({
        tabHandle: '.stage-handle',               //class of the element that will become your tab
        pathToTabImage: 'images/settings-tab.png',//path to the image for the tab //Optionally can be set using css
        imageHeight: '26px',                     //height of tab image           //Optionally can be set using css
        imageWidth: '130px',                      //width of tab image            //Optionally can be set using css
        tabLocation: 'top',                       //side of screen where tab lives, top, right, bottom, or left
        speed: 300,                               //speed of animation
        action: 'click',                          //options: 'click' or 'hover', action to trigger animation
        topPos: '0px',                            //position from the top/ use if tabLocation is left or right
        leftPos: '20px',                          //position from left/ use if tabLocation is bottom or top
        fixedPosition: false                      //options: true makes it stick(fixed position) on scroll
    });

	$('#settings').submit(function(e){
		var view_choosen = '';
		
		if( $('#view-both:checked').val() )	{
			view_choosen = $('#view-both:checked').val();
		}
		if( $('#view-feed:checked').val() )	{
			view_choosen = $('#view-feed:checked').val();
		}
		if( $('#view-cloud:checked').val() )	{
			view_choosen = $('#view-cloud:checked').val();
		}

		var refresh_interval = $('#interval').val();
		var msg_no = $('#msg_no').val();

		if( refresh_interval != '' && view_choosen != '' && msg_no != '' ){
			// If selected both we remove the old view and fade in
			// the cloud and the feed.
			if( view_choosen == 'both' )	{
				$('.feed-div').width( '49%' );
				$('.cloud-div').width( '48%' );
				$('.cloud-div').fadeIn( 400 );
				$('.feed-div').fadeIn( 400 );
			}
			// If selected the cloud we fade out the feed and
			// fade in the cloud.
			if( view_choosen == 'cloud' )	{
				$('.feed-div').fadeOut( 400 );
				$('.feed-div').hide();
				$('.cloud-div').width( '100%' );
				$('.cloud-div').fadeIn( 400 );
			}
			// if selected the feed we fade out the cloud and
			// fade in the feed.
			if( view_choosen == 'feed' )	{
				$('.cloud-div').fadeOut( 400 );
				$('.cloud-div').hide();
				$('.feed-div').width( '100%' );
				$('.feed-div').fadeIn( 400 );
			}
			
			// Set the update interval to the new value.
			setUpdateInterval( refresh_interval );
			// Set the number of messages to retrieve.
			setMessageAmount( msg_no );
			
			$('.stage-handle').click();
		}
		e.preventDefault();
		return false;
	});
	
	$('#interval').keypress( function(e)	{
		var key = e.which;
		var keychar = String.fromCharCode( key );
		
		if( ( key == null ) || ( key == 0 ) || ( key == 8 ) || ( key == 9 ) ||
			( key == 13 ) || ( key == 27 ) )
			return true;
		else if( ( ( '0123456789' ).indexOf( keychar ) > -1 ) )
			return true;
		else
			return false;
	});
	
	$('#msg_no').keypress( function(e)	{
		var key = e.which;
		var keychar = String.fromCharCode( key );
		
		if( ( key == null ) || ( key == 0 ) || ( key == 8 ) || ( key == 9 ) ||
			( key == 13 ) || ( key == 27 ) )
			return true;
		else if( ( ( '0123456789' ).indexOf( keychar ) > -1 ) )
			return true;
		else
			return false;
	});

});

