var Postmark = {

	initialize: function(){
		Postmark.initFormFields();
		Postmark.tooltips();
		Postmark.announcement();
		Postmark.copyToClipboard();

		$('.header .i-credits.warning b').blink();

		$('.action.i-print').click(function(e){
			e.preventDefault();

			window.print();
		});

		$('.confirm').click(function(e){
			return confirm('Are you sure?');
		});
	},

	initFormFields: function(){
		$('input.empty, textarea.empty').each(function(){
			var el 					= $(this);
			var emptyValue 	= el.attr('title');

      if (el.val() == '') el.val(emptyValue);
      else el.removeClass('empty');

			el.focus(function(){
				if (el.hasClass('empty')) el.removeClass('empty');

				if (el.val() == emptyValue) el.val('');
			});

			el.blur(function(){
				if (el.val() == '') {
					if (!el.hasClass('empty')) el.addClass('empty');
					el.val(emptyValue);
				}
			});

      $(el.get(0).form).submit(function() {
        if (el.val() == emptyValue) el.val('');
      });
		});
	},

	tooltips: function(){
		var selector = '[rel=tooltip]';

		if ($(selector).length > 0) $(selector).simpleTooltip();
	},

	announcement: function(){
		$('#announcement .hide a').click(function(e){
			e.preventDefault();

			$.post($(this).attr('href'), function(){
				$('#announcement').slideUp();
			});
		});
	},

	copyToClipboard: function(){
		$('.copy-to-clipboard').copyable(function(e, clip) {
			clip.setText($(this).attr('rel'));
		});
	}

}

$(document).ready(Postmark.initialize);



/* Helpers */

jQuery.fn.extend({

  showMe: function(){
		return this.removeClass('hidden');
  },

  hideMe: function(){
		return this.addClass('hidden');
  },

	toggleMe: function(){
		return this.toggleClass('hidden');
	},

	isHidden: function(){
		if (this.hasClass('hidden'))
			return true;
		else
			return false;
	},

	centerize: function(){
		return this.css({
			top		: $(window).height()/2 - this.height()/2,
			left	: $(window).width()/2 - this.width()/2
		});
	},

	setRandomID: function(){
		var random = 'id-' + parseInt(Math.random() * 1000).toString() + new Date().getTime().toString();

		return this.attr({ id: random, name: random });
	},

	blink: function(){
		var el = this;
		var visible = true;

		var interval = window.setInterval(function(){
			if (visible){
				el.fadeOut();
				visible = false;
			} else {
				el.fadeIn();
				visible = true;
			}
		}, 500);
	}

});

jQuery.preloadImages = function(){
  for(var i = 0; i < arguments.length; i++){
    jQuery('<img>').attr('src', arguments[i]);
  }
}

jQuery.fn.copyable = function(getTextFunction) {
  var self = this.setRandomID();

	self.each(function(){
		var copyable 		= $(this);
		var copyableID 	= copyable.attr("id");
		var containerID = copyable.attr("id") + "_container";

		var clip = copyable.data('clip');

		if (clip === undefined) {
			clip = new ZeroClipboard.Client();
			copyable.data('clip', clip);
		}

		copyable.wrap('<div id="' + containerID + '" style="position: absolute; display: inline;"></div>');
		copyable.mousedown(getTextFunction);

		clip.glue(copyableID, containerID);
		clip.setHandCursor(true);

		clip.addEventListener('mouseDown', function(client) {
			copyable.trigger("mousedown", client);
		});
		clip.addEventListener('mouseOver', function(client) {
			copyable.trigger("mouseover", client);
		});
		clip.addEventListener('mouseOut', function(client) {
			copyable.trigger("mouseout", client);
		});
		clip.addEventListener('mouseUp', function(client) {
			copyable.trigger("mouseup", client);
		});
	});

  return self;
};

function exists(selector){
	if ($(selector).length > 0)
		return true;
	else
		return false;
}

function isNumber(string){
	var valid_chars = '0123456789';
	var is_number		= true;
	var chars;

	for (i = 0; i < string.length && is_number == true; i++){
		chars = string.charAt(i);

		if (valid_chars.indexOf(chars) == -1) {
			is_number = false;
		}
	}

	return is_number;
}
