/* jQuery isEmail */

jQuery.isEmail = function(email) {
	if ((email == undefined) || (email) == "") return false;

	var email = new String(email);

	var splitted = email.match("^(.+)@(.+)$");
	if (splitted == null) return false;
	if (splitted[1] != null ) {
		var regexp_user = /^\"?[\w-_\.]*\"?$/;
		if (splitted[1].match(regexp_user) == null) return false;
	}
	if (splitted[2] != null) {
		var regexp_domain = /^[\w-\.]{2,}\.[A-Za-z]{2,4}$/;
		if (splitted[2].match(regexp_domain) == null) {
			var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if (splitted[2].match(regexp_ip) == null) return false;
		}
		return true;
	}
	return false;
}

$.fn.extend({

    disable: function() {
    	$(this).each(function() {
	    	$(this).addClass("disabled");
	    	if ($(this).attr("tagName") == "INPUT") $(this).attr("disabled", "disabled");
    	});
    },
    enable: function() {
    	$(this).each(function() {
	    	$(this).removeClass("disabled");
	    	if ($(this).attr("tagName") == "INPUT") $(this).attr("disabled", "");
    	});
    },
    toggleEnabled: function() {
    	if ($(this).hasClass("disabled")) {
    		$(this).enable();
    	} else {
    		$(this).disable();
    	}
    },

	// href utils for fancybox
	isLinkToVideo: function() {
		if (!$(this).is("a")) return false;
		return ($(this).attr("href").match(/youtube\.com|vimeo\.com|video\.google\.com\/videoplay/i) != null);
	},

	hrefToSWF: function() {
		if (!$(this).is("a")) return false;

		var href = $(this).attr("href");

		var matches = href.match(/youtube\.com|vimeo\.com|video\.google\.com\/videoplay/i);
		if (matches == null) return href;

		switch (matches[0]) {
			case "youtube.com":
				return href.replace(/watch\?v=/i, 'v/');
				break;
			case "vimeo.com":
				return href.replace(/vimeo.com\/.*#|vimeo.com\//i, 'vimeo.com/moogaloop.swf?clip_id=') + "&fullscreen=1"
				break;
			case "video.google.com/videoplay":
				return href.replace(/video\.google\.com\/videoplay\?docid=/i, 'video.google.com/googleplayer.swf?docId=');
		}

		return href;
	},

	myFancybox: function(settings) {
		$(this).each(function() {

			if ($(this).isLinkToVideo()) {
				$(this).fancybox($.extend({
					padding: 0,
					href : $(this).hrefToSWF(),
					type: 'swf',
					swf: {
						wmode: 'transparent',
						allowfullscreen: true
					}
				}, settings));
			} else {
				$(this).fancybox($.extend({ padding: 0}, settings));
			}
		});
	}

});


jQuery.isEditorEmpty = function(editorId) {
	var editor = $(editorId);
	if (editor.size() == 0) return true;
	if (typeof editor.data("wysiwyg") != "object") return true;

	if ($.trim(editor.val()) == "&nbsp;") return true;

	var valueAsDOM = $($.trim(editor.val()));
	var valueAsText = $.trim(editor.val());
	var invalid = false;

	if (valueAsDOM.size() == 0) {
		invalid = true;
		if (valueAsText != "") invalid = false;
	} else {
		if ($.trim(editor.val()) == "") invalid = true;
	}

	return invalid;
}

function shakeEditor(editor) {
	if (!editor) return false;

	editor.animate({
		"margin-left": "-=20px"
	}, 100, function() {
		$(this).animate({
			"margin-left": "+=40px"
		}, 100, function() {
			$(this).animate({
				"margin-left": "-=40px"
			}, 100, function() {
				$(this).animate({
					"margin-left": "+=20px"
				}, 100);
			})
		});
	});
}

function focusEditor(id) {
	tinymce.execCommand('mceFocus', false, id);
	$("#" + id).next().addClass("focus");

	var wrap = $("#" + id + "_parent");
	if (wrap.size() != 0) $(window).scrollTop($(wrap).offset().top);
}

/* Global event */

$(function() {

	$(".sidebar-menu li a").click(function(event) {
		if ($(this).parent().hasClass("disabled")) {
			event.stopImmediatePropagation();
			return false;
		}
	});

});

var defaultEditorSettings = {
	script_url: '/js/tinymce/tiny_mce.js',
	content_css : '/css/editorContent.css',
	plugins: "safari,pagebreak,inlinepopups,paste,xhtmlxtras,fullscreen,preview,pdw,phpimage,embed,print,emotions,media",
	theme_advanced_plugins_disable: "advimage",
	theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,link,unlink,|,undo,redo,|,preview,fullscreen,|,|,|,pdw_toggle",
	theme_advanced_buttons2: "formatselect,|,cleanup,removeformat,code,|,outdent,indent,|,phpimage,embed,|,emotions,charmap,|,pasteword,print",
	extended_valid_elements: "iframe[*],param[*],embed[*],object[*]",
	accessibility_warnings: 0,
    pdw_toggle_on: 0,
    pdw_toggle_toolbars: "2",
	width: 503,
	height: 350,
	relative_urls: false,
	remove_script_host: false,
	language: 'pt',
	theme: 'advanced',
	skin: 'cirkuit',
	inlinepopups_skin: 'smskin2',
	setup: function(ed) {
		$("body").click(function(evt) {
			if ($(evt.target).parents("span.mceEditor").size() == 0) $(".mceEditor.focus").removeClass("focus"); 
		});

		ed.onInit.add(function() {
			$("span.mceEditor a.mceButton").tipTip({
				defaultPosition: 'top',
				delay: 200
			});
		});

		ed.onMouseDown.add(function(ed) {
			$(ed.getContainer()).addClass("focus");
			if ((ed != lastFocusEditor) && (lastFocusEditor != null)) $(lastFocusEditor.getContainer()).removeClass("focus");
			lastFocusEditor = ed;
		});
	}
};

var lastFocusEditor = null;



