var console;

if(!console) {
    console = { log: function() {} } 
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
$( document ).ready( function() {
	// Disable buttons on form submiting.
	$( 'form' ).submit( function() {
		// Generate id of the form, if no
		if( $(this).attr('id') == '' )
			$(this).attr('id', 'id' + Math.round ( Math.random ( ) * Math.random ( ) * 1000 + 1 ) + '-' + Math.round ( Math.random ( ) * Math.random ( ) * 1000 + 1 ) );

		var id = $(this).attr('id');

		$('form#' + id + ' input:submit').attr( { 'disabled': true } )
	});

	var clearAllValues = new Array( '#header #search-text', '#content #search-my', '#content #search-in-community', '#content #search-recipe', '#content #search-blog', '#content #search-qa', '#content #search-forum', '#content #jump-to-top', '#content #jump-to-bottom', '#search-groups', '#gr-members-search' );
	for( var i = 0; i < clearAllValues.length; i++ ) {
		var object = $( clearAllValues[i] );
		var value = object.val();
		clearValues( object, value );
	}
	
	$('#jump-to-page').click(function(){
		var page = $('#jump-to-top').val();
		if( parseInt( page ) > 0 ) {
			var location = document.location.toString().replace(/\/p:[0-9]{1,}/ig, '');
			if( location[ location.length - 1 ] == '/' ) var append = 'p:'+page+'/';
			else var append = '/p:'+page+'/';
			document.location = location + append;
		}
	})
	$('#jump-to-page_bot').click(function(){
		var page = $('#jump-to-bottom').val();
		if( parseInt( page ) > 0 ) {
			var location = document.location.toString().replace(/\/p:[0-9]{1,}/ig, '');
			if( location[ location.length - 1 ] == '/' ) var append = 'p:'+page+'/';
			else var append = '/p:'+page+'/';
			document.location = location + append;
		}
	})
	$('#jump-to-page-form, #jump-to-page-form_bot').submit(function(){ return false; });

	$('a.follow, a.unfollow').click(function() {
		// follow/unfollow links
		if (this.ajaxInProgress)
			return;
		this.ajaxInProgress = true;

		var rel = this.rel.split('-');
		var type = rel[0], id = rel[1];
		var anchor = $(this);
		var action = anchor.hasClass('follow')? 'follow': 'unfollow';
		//console.log('switch '+rel+' to '+action);

		$.ajax({
			type: 'POST', dataType: 'json', url: '/my/',
			data: { action: action, type: type, id: id },
			success: function(data) {
				anchor[0].ajaxInProgress = false;
				if (data.status == 'success')
					anchor.removeClass(action).addClass((action == 'follow')? 'unfollow': 'follow');
			},
			error: function(req, status) {
				//console.log('error: '+status);
				anchor[0].ajaxInProgress = false;
			}
		});

		return false;
	});

	$('a.inviteMember').click(function(){
		var memberId = $(this).attr('rel');
		var memberUsername = $(this).attr('title');
		jPrompt('Write your invitation here:', '', 'Invite '+memberUsername+' to join this group', function( value ){
			if( value == "" || !value) return false;
			if( value.length > 1 ) {
				$.ajax({
					type:		'POST',
					dataType:	'json',
					url:		'/groups/',
					data:		{
						action: 'invite',
						memberId: memberId,
						memberUsername: memberUsername,
						invitation: value,
						groupId: groupId
					},
					success:	function(data){
						jAlert(data.msg, data.success?'Invitation was send successfully':'Error');
					}
				});
			} else {
			
			}
		});
		return false;
	});
	
	$('div.backtotop a, a.backtotop').click(function() {
		window.scroll(0, 0);
		return false;
	});
});

function clearValues( object, value ) {
	object.focus( function() {
		if ( $( this ).val() == value )
			object.val( '' );
	});
	
	object.blur( function() {
		if ( $( this ).val() == '' )
			object.val( value );
	});	
} 


function Carousel( carousel_section, carousel_length, carousel_left, carousel_right, carousel_footer, carousel_active ) {
	hideNextPrev( 0 );
	carousel_left.click( function() {

		//carousel_footer.slideUp();
			
		//carousel_section.eq( carousel_active + 1 ).hide( 'slow' );
		carousel_section.eq( carousel_active - 1 ).show( 'slow' );
		//carousel_section.eq( carousel_active ).animate({
		//	marginLeft: '649px'
		//}, 0, function() {
			//carousel_footer.slideDown();
			carousel_active--;
			hideNextPrev( carousel_active );
		//});
		
		return false;
	});
		
	carousel_right.click( function() {
		
		carousel_active++;
		
		//carousel_footer.slideUp();
		
		carousel_section.eq( carousel_active - 1 ).hide( 'slow' );
		carousel_section.eq( carousel_active + 1 ).show( );
		
		//carousel_section.eq( carousel_active ).animate({
		//	marginLeft: '-649px'
		//}, 0, function() {
			//carousel_footer.slideDown();
			hideNextPrev( carousel_active + 1 );
		//});
		
		return false;
	});
		
	function hideNextPrev( carousel_active ) {
	
		if ( carousel_active == carousel_length ) carousel_right.hide();
		else carousel_right.show();
			
		if ( carousel_active == 0 ) carousel_left.hide();
		else carousel_left.show();
			
	}	
}
