/**
 * Create a new instance of pEvent.
 *
 * @classDescription	This class creates a new Event.
 * @return {Object}	Returns a new Event object.
 * @constructor
 *--------------------------------------------------------------------------*/
function pEvent() {
	this.events = [];
	this.teste = [];
	this.builtinEvts = [];
}

/**
 * Gets the index of the given action for the element
 *
 * @memberOf Event
 * @param {Object} obj The element attached to the action.
 * @param {String} evt The name of the event.
 * @param {Function} action The action to execute upon the event firing.
 * @param {Object} binding The object to scope the action to.
 * @return {Number} Returns an integer.
 *--------------------------------------------------------------------------*/
pEvent.prototype.getActionIdx = function(obj,evt,action,binding) {
	if(obj && evt) {

		var curel = this.events[obj][evt];
		if(curel) {
			var len = curel.length;
			for(var i = len-1;i >= 0;i--) {
				if(curel[i].action == action && curel[i].binding == binding) {
					return i;
				}
			}
		} else {
			return -1;
		}
	}
	return -1;
};

/**
 * Adds a listener
 *
 * @memberOf Event
 * @param {Object} obj The element attached to the action.
 * @param {String} evt The name of the event.
 * @param {Function} action The action to execute upon the event firing.
 * @param {Object} binding The object to scope the action to.
 * @return {null} Returns null.
 *--------------------------------------------------------------------------*/
pEvent.prototype.addListener = function(obj,evt,action,binding) {
	if(this.events[obj]) {
		if(this.events[obj][evt]) {
			if(this.getActionIdx(obj,evt,action,binding) == -1) {
				var curevt = this.events[obj][evt];
				curevt[curevt.length] = {action:action,binding:binding};
			}
		} else {
			this.events[obj][evt] = [];
			this.events[obj][evt][0] = {action:action,binding:binding};
		}
	} else {
		this.events[obj] = [];
		this.events[obj][evt] = [];
		this.events[obj][evt][0] = {action:action,binding:binding};
	}
};

/**
 * Removes a listener
 *
 * @memberOf Event
 * @param {Object} obj The element attached to the action.
 * @param {String} evt The name of the event.
 * @param {Function} action The action to execute upon the event firing.
 * @param {Object} binding The object to scope the action to.
 * @return {null} Returns null.
 *--------------------------------------------------------------------------*/
pEvent.prototype.removeListener = function(obj,evt,action,binding) {
	if(this.events[obj]) {
		if(this.events[obj][evt]) {
			var idx = this.getActionIdx(obj,evt,action,binding);
			if(idx >= 0) {
				this.events[obj][evt].splice(idx,1);
			}
		}
	}
};

/**
 * Fires an event
 *
 * @memberOf Event
 * @param e [(event)] A builtin event passthrough
 * @param {Object} obj The element attached to the action.
 * @param {String} evt The name of the event.
 * @param {Object} args The argument attached to the event.
 * @return {null} Returns null.
 *--------------------------------------------------------------------------*/
pEvent.prototype.fireEvent = function(e,obj,evt,args) {
	if(!e){e = window.event;}

	if(obj && this.events) {
		var evtel = this.events[obj];
		if(evtel) {
			var curel = evtel[evt];
			if(curel) {
				for(var act in curel) {
					var action = curel[act].action;
					if(curel[act].binding) {
						action = action.bind(curel[act].binding);
					}
					action(e,args);
				}
			}
		}
	}
};

/**
 * Pad a function, added to Math object
 *
 * @param {Integer} n The number to pad.
 * @param {Integer} c The number of zeros to add.
 * @return {String} Returns the integer padded converted to string.
 *--------------------------------------------------------------------------*/
Math.pad = function(n, c) {
	if( (n = n + "").length < c ) {
		return new Array(++c - n.length).join("0") + n;
	} else {
		return n;
	}
};

Function.prototype.bind = function (object) {
    if (arguments.length < 2 && arguments[0] == undefined)
		return this;

    var __method = this, args = $.makeArray(arguments), object = args.shift();
    return function() {
      return __method.apply(object, args.concat($.makeArray(arguments)));
    }
};

$.toArray = function (object) {
	var arr = [];
	for (var i in object)
		if (parseInt(i, 10) > -1)
			arr.push(object[i]);
	
	return arr;
}

String.prototype.truncate = function(length, truncation) {
    length = length || 30;
    truncation = truncation == undefined ? '...' : truncation;

	if (this.length < length)
		return String(this);
	
	var splited = this.substr(0,length).split(" ");
	splited.pop();
	return splited.join(" ") + truncation;
};

/*  Fixing the target="_blank"
 *
 *--------------------------------------------------------------------------*/
var fixTarget = function() {
	$('a[rel=_blank]')
		.attr('target', '_blank');
};

/*  Action by rel
 *
 *--------------------------------------------------------------------------*/
var actionByRel = function(rel, action) {
	$('a[rel=' + rel +']')
		.attr('href','javascript:;')
		.click(action);
};

/*  Auto Load
 *
 *--------------------------------------------------------------------------*/
 $(document).ready(function() {
	fixTarget();
});

/*  Cookies
 *
 *--------------------------------------------------------------------------*/
function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length)))
		return null;

	if (start == -1 ) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}
	
function setCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
		today.setTime(today.getTime());

	if (expires)
		expires = expires * 1000 * 60 * 60 * 24;

	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name+"="+escape(value) +
		((expires) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		((path) ? ";path=" + path : "" ) +
		((domain) ? ";domain=" + domain : "" ) +
		((secure) ? ";secure" : "" );
}
	
function deleteCookie(name, path, domain) {
	if (getCookie(name)) document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Sac() {
	window.callWindow({url: 'sac.html', width: 650, height: 415, scrollbars: 1});
}