
var defaultQuoteValue = 'Type your quote here...';

$(document).ready(function(){
	if ($('#passiton_quote_input').length) {
		$('#passiton_quote_input').PassitonDefaultValue(defaultQuoteValue);
		$('textarea[maxlength]').limitMaxlength();
		loadLatestPassitonQuote();
	}
        if ($('.badge_builder').length) {
            initBadgeBuilder();
        }
});


var zeroclip;

function initBadgeBuilder() {
    ZeroClipboard.setMoviePath( '/zeroclipboard/ZeroClipboard10.swf' );
    zeroclip = new ZeroClipboard.Client();
    zeroclip.setText( 'FOOOO' );
    zeroclip.setHandCursor(true);

    zeroclip.glue('bb_copy_code_button');

    zeroclip.addEventListener( 'mouseDown', function(client) {
        var code =  $('#bb_code').val();
        if (code) {
            zeroclip.setText(code);
            $('.bb_code_copied').text('Code copied');
        } else {
            $('.bb_code_copied').text('No code to copy!');
        }
    } );


    $('.bb_color_selector a').bind('click', function(e){
        e.preventDefault();
        var selectedColor = $(e.currentTarget).attr('class').substr(9);
        selectedColorDisplay = selectedColor.substr(0,1).toUpperCase() + selectedColor.substr(1);
        $('.bb_selector_selected_color').text(selectedColorDisplay);
        $('.bb_preview').css('background-image', 'url(/img/badge_builder/badges/' + selectedColor + '.png)');
        var code = '<iframe src="http://' + window.location.host + '/badge.php?color=' + selectedColor + '" frameborder="0"  width="100" height="100" allowtransparency="true"></iframe>';
        $('#bb_code').val(code);
    });

}

function loadLatestPassitonQuote() {
	if ($('#passiton_quote_input').length) {	
		$.ajax({
			  url: '/loadLatestPassitonQuote.php',
			  success: function(data) {processLoadLatestPassitonQuoteResult(data);},
			  dataType: 'json',
			  error: function(XMLHttpRequest, textStatus, errorThrown) {console.log('An error occurred loading the latest quote: ' + errorThrown);}
			});
	}
}

function processLoadLatestPassitonQuoteResult(data) {
//	console.log(data);
	if (data.id) {
		$('#passiton_quote').text(data.quote);
		$('#passiton_name_username').text(data.username);
		$('#passiton_name_posted').text(data.posted);
		$('#passiton_count').text(data.count);
	}
}

function showPassitonForm() {
	var offset = $('#passiton_link').position();
	$('#passiton_form').css('top', offset.top - 25)
		.css('left', offset.left + 10);
	$('#passiton_form').fadeIn();
}

function hidePassitonForm() {
	$('#passiton_form').fadeOut();	
}

function passitonPostQuote() {
	var quoteVal = $('#passiton_quote_input').val();
	if (quoteVal != defaultQuoteValue) {
		$.ajax({
			  url: '/postPassitonQuote/',
			  data: {quote: quoteVal},
			  success: function(data) {processPassitonPostQuoteResult(data);},
			  dataType: 'text',
			  error: function(XMLHttpRequest, textStatus, errorThrown) {alert('An error occurred posting your quote: ' + errorThrown);}
			});
	}
	$('#passiton_form').fadeOut();
}

function processPassitonPostQuoteResult(data) {
	if(data == 'OK') {
		loadLatestPassitonQuote();
	} else if (data == 'BADWORDS') {
		alert('Your quote has been rejected due to inappropriate content');
	} else if (data == 'NOQUOTE') {
		alert('No quote was received!');
	} else if (data == 'NOTLOGGEDIN') {
		alert('You are not logged in!');
	} else {
		alert('An error occurred posting your quote: ' + data);
	}
}

jQuery.fn.PassitonDefaultValue = function(text){
    return this.each(function(){
		//Make sure we're dealing with text-based form fields
		if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
			return;
		
		//Store field reference
		var fld_current=this;
		
		//Set value initially if none are specified
        if(this.value=='') {
			this.value=text;
		} else {
			//Other value exists - ignore
			return;
		}
		
		//Remove values on focus
		$(this).focus(function() {
			if(this.value==text || this.value=='') {
				this.value='';
			}
		});
		
		//Place values back on blur
		$(this).blur(function() {
			if(this.value==text || this.value=='') {
				this.value=text;
			}
		});
		
		//Capture parent form submission
		//Remove field values that are still default
		$(this).parents("form").each(function() {
			//Bind parent form submit
			$(this).submit(function() {
				if(fld_current.value==text) {
					fld_current.value='';
				}
			});
		});
    });
};


jQuery.fn.limitMaxlength = function(options){

	var settings = jQuery.extend({
		attribute: "maxlength",
		onLimit: function(){},
		onEdit: function(){}
	}, options);

	// Event handler to limit the textarea
	var onEdit = function(){
		var textarea = jQuery(this);
		var maxlength = parseInt(textarea.attr(settings.attribute));

		if(textarea.val().length > maxlength){
			textarea.val(textarea.val().substr(0, maxlength));

			// Call the onlimit handler within the scope of the textarea
			jQuery.proxy(settings.onLimit, this)();
		}

		// Call the onEdit handler within the scope of the textarea
		jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
	}

	this.each(onEdit);

	return this.keyup(onEdit)
				.keydown(onEdit)
				.focus(onEdit)
				.live('input paste', onEdit);
}

