/**
 * Main
 */
var Main = {
	
	init : function () {
		$(".anchor-act").click(Main.scrollEffect);
		$(".goto-login-act").click(Main.gotoLogin);
		$(".field-focus").focus();
	},
	
	scrollEffect : function (event, callback) {
		if (typeof(event) == 'object') {
			var full_url = this.href;
			var parts = full_url.split("#");
			var trgt = parts[1];
			var target_offset = $("a[name="+trgt+"]").offset();
			event.preventDefault();
		} else
			var target_offset = $(event).offset();
		
		var target_top = target_offset.top;
		$('html').animate({scrollTop : target_top}, 500, function () {if (callback) callback();});
	},
	
	gotoLogin : function (e) {
		Main.scrollEffect("#top .login form", function () {
			$("#top .login form .fld-login").focus();
		});
		e.preventDefault();
	}
};
$(Main.init);

/**
 * Tradução
 */
function __(str) {
	return str;
}

/**
 * Alerts
 */
var Alert = {
	idIdent : 0,
	int : []
};
Alert.set = function setAlert(text, type, int, obj_ident) {
	if (obj_ident == undefined) obj_ident = "alerts";
	if (int == undefined) int = 5;
	var _alert = $('#' + obj_ident);
	
	Alert.idIdent++;

	if (_alert.length == 0) {
		$("body").prepend('<div id="' + obj_ident + '"></div>');
		_alert = $('#' + obj_ident);
	}

	var ret = '<p class="alert-text">'+text+'</p>';
	ret = '<div class="alert-textarea">'+ret+'</div>';
	ret = '<div class="alert-bg"></div>'+ret;
	ret = '<div id="alert_id_' + Alert.idIdent  +'" class="alert alert-'+type+'">'+ret+'</div>';
	
	_alert.append(ret);
	Alert.actions(Alert.idIdent, int);
	if (Alert.idIdent>15) Alert.idIdent = 0;
}
Alert.actions = function (id, intt) {
	var _alert = $(".alert");
	
	$("object").css("visibility", "hidden");
	$("embed").css("visibility", "hidden");

	$("#alert_id_"+id).slideDown("normal");
	$("form").submit(function(){_alert.click()});	
	_alert.click(function(){Alert.remove(this.id);});
	Alert.int[id] = setTimeout(function(){
		clearInterval(Alert.int[id]);
		Alert.remove("alert_id_"+id);
	}, ((intt * 1000))+200);
}
Alert.remove = function (id) {
	var _alert = $("#"+id);
	
	_alert.slideUp("fast", function(){
		_alert.remove();
		$("object").css("visibility", "visible");
		$("embed").css("visibility", "visible");
	});
	id = id.split('_');
	id = id.pop();
	clearInterval(Alert.int[id]);
}
Alert.json = function (object, int) {
	Alert.set(object.msg, object.type, int);	
}

/**
 * Jquery Extends
*/
$.fn.insertAtCaret = function (tagName) {
	return this.each(function(){
		if (document.selection) {
			//IE support
			this.focus();
			sel = document.selection.createRange();
			sel.text = tagName;
			this.focus();
		}else if (this.selectionStart || this.selectionStart == '0') {
			//MOZILLA/NETSCAPE support
			startPos = this.selectionStart;
			endPos = this.selectionEnd;
			scrollTop = this.scrollTop;
			this.value = this.value.substring(0, startPos) + tagName + this.value.substring(endPos,this.value.length);
			this.focus();
			this.selectionStart = startPos + tagName.length;
			this.selectionEnd = startPos + tagName.length;
			this.scrollTop = scrollTop;
		} else {
			this.value += tagName;
			this.focus();
		}
	});
};

$.fn.insertRoundCaret = function (tagName) {
	return this.each(function(){
		strStart = '['+tagName+']';
		strEnd = '[/'+tagName+']';
		if (document.selection) {
			//IE support
			stringBefore = this.value;
			this.focus();
			sel = document.selection.createRange();
			insertstring = sel.text;
			fullinsertstring = strStart + sel.text + strEnd;
			sel.text = fullinsertstring;
			document.selection.empty();
			this.focus();
			stringAfter = this.value;
			i = stringAfter.lastIndexOf(fullinsertstring);
			range = this.createTextRange();
			numlines = stringBefore.substring(0,i).split("\n").length;
			i = i+3-numlines+tagName.length;
			j = insertstring.length;
			range.move("character",i);
			range.moveEnd("character",j);
			range.select();
		}else if (this.selectionStart || this.selectionStart == '0') {
			//MOZILLA/NETSCAPE support
			startPos = this.selectionStart;
			endPos = this.selectionEnd;
			scrollTop = this.scrollTop;
			this.value = this.value.substring(0, startPos) + strStart + this.value.substring(startPos,endPos) + strEnd + this.value.substring(endPos,this.value.length);
			this.focus();
			this.selectionStart = startPos + strStart.length ;
			this.selectionEnd = endPos + strStart.length;
			this.scrollTop = scrollTop;
		} else {
			this.value += strStart + strEnd;
			this.focus();
		}
	});
};
