var d = document;
var config = {
		facebox: {},
		actions: {
			alertHTML: '<div class="ui-state-highlight ui-corner-all" style="padding: 0pt 0.7em; margin-top: 20px;"><p style="margin-top: 8px;"><span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"></span><strong>Notice: </strong>%message%</p></div><br/>',
			errorHTML: '<div class="ui-state-error ui-corner-all" style="padding: 0pt 0.7em;"><p style="margin-top: 8px;"><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span><strong>Error: </strong>%message%</p></div><br/>'
		}
};

$(document).ready(function() {
	/* $('a[rel*=facebox]').click(function(){
		$.facebox('This function is currently disabled.');
		return false;
	}); */
	$('a[rel*=facebox]').facebox();
	$('form[class*=ajax]').submit(function(){
		var b = this.getAttribute('class').substr(5,this.getAttribute('class').length-6);
		$.ajax({url: this.getAttribute('action'),
			cache: false,
			type: this.getAttribute('method'),
			data: $(this).serialize(),
			dataType: 'json',
			success: function(response) {
			switch (b) {
				case 'facebox':
					$.facebox(actions_handle_message(response, 'facebox'));
					var fbtimeout = setTimeout("$(document).trigger('close.facebox')", 3000);
					break;
				case 'alerter':
					$.alerter(actions_handle_message(response, null));
					break;
					
				default:
					break;
			}
			$(document).trigger(response.action, [response]);
			}
		});
		return false;
	});
	$('form[class*=facebox]').submit(function(){
		$.ajax({url: this.getAttribute('action'),
			cache: false,
			type: this.getAttribute('method'),
			data: $(this).serialize(),
			dataType: 'html',
			success: function(response) {$.facebox(response);}
		});
		return false;
	});
	$('input[class*=disabled]').keypress(function(e){e.preventDefault();});
	$('a.control-link').click(function() {
		var rel = this.getAttribute('rel').split(' ');
		var action = rel[0];
		var target = d.getElementById(rel[1]);
		switch (action) {
			case 'copy':
				target.select();
				break;
			case 'save':
			 	e = $.Event("submit");
				$(target).trigger(e);
				break;
			case 'facebook':
				fbs_click();
				break;
			default:
				return true;
				break;
		}
		return false;
	});
	$('a[rel*=soon], a[rel*=email]').click(function(){
		$.alerter('Coming Soon');
		return false;
	});
});

/* message handler */
function actions_handle_message(response, returnType) {
	/* {"section":"portfolio","action":"new-comment","item":"7","error":false,"success":true,"data":{"username":"Anonymous.-1062731676","comment":"fghfghfghg","timestamp":"Oct 30, 2009"}} */
	if (response.error) {
		message = response.error.message;
	} else {
		message = (response.success) ? 'Your request was completed successfully' : 'There was an error while processing your request.';
	}
	
	if (returnType == 'facebox') return (response.error) ? config.actions.errorHTML.replace('%message%', message) : config.actions.alertHTML.replace('%message%', message);
	
	return message;
}

/* facebook share */
function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');}

/* alerter object
 * $.alerter('Please work for me');
 */
(function($) {
	$.alerter = function(data, time) {$.alerter.load(); $.alerter.show(data, time);};
	$.extend($.alerter, {
		alerterHTML: '<div id="alerterOverlay"><span></span></div>',
		load: function() {if ($('#alerterOverlay').length == 1) return true; $(window).bind('scroll', function(){$('#alerterOverlay').css('top', $(this).scrollTop());}); $('body').prepend($.alerter.alerterHTML); return true;},
		show: function(data, time) {if (!time){time = 3000;} $('#alerterOverlay span').text(data);$.alerter.display(time);return true;},
		display: function(time) {$('#alerterOverlay').css('top', $(window).scrollTop()); $('#alerterOverlay').animate({height: $('#alerterOverlay span').height()+30}, 400, function(){var timeout = setTimeout('$.alerter.hide()', time);});},
		hide: function() {$('#alerterOverlay').animate({height: 0}, 400, function(){$('#alerterOverlay span').empty();});}
	});	
})(jQuery);