/*
 * File: apme.main.js
 * Author: Joao Fonseca (j.fonseca@netcabo.pt)
 */
/*
// object.watch
if (!Object.prototype.watch)
Object.prototype.watch = function (prop, handler) {
var oldval = this[prop], newval = oldval,
getter = function () {
return newval;
},
setter = function (val) {
oldval = newval;
return newval = handler.call(this, prop, oldval, val);
};
if (delete this[prop]) { // can't watch constants
if (Object.defineProperty) // ECMAScript 5
Object.defineProperty(this, prop, {
get: getter,
set: setter
});
else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
Object.prototype.__defineGetter__.call(this, prop, getter);
Object.prototype.__defineSetter__.call(this, prop, setter);
}
}
};
// object.unwatch
if (!Object.prototype.unwatch)
Object.prototype.unwatch = function (prop) {
var val = this[prop];
delete this[prop]; // remove accessors
this[prop] = val;
};
 */
Array.prototype.exists = function (search) {
	for (var i = 0; i < this.length; i++)
		if (this[i] == search)
			return true;
	return false;
};


Array.prototype.average = function () {
    var av = 0;
    var cnt = 0;
    var len = this.length;
    for (var i = 0; i < len; i++) {
        var e = +this[i];
        if (!e && this[i] !== 0 && this[i] !== '0') e--;
        if (this[i] == e) { av += e; cnt++; }
    }
    return av / cnt;
}

var apme = apme || {};

apme.main = {
	menuTimer : null,
	menuTimerPeriod : 500,
	cancelMenuTimer : function () {
		if (this.menuTimer != null) {
			clearTimeout(this.menuTimer);
			this.menuTimer = null;
		}
	},
	emailRegEx : /(?:[A-Za-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[A-Za-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/,
	setMenuTimer : function (selector) {
		this.cancelMenuTimer();
		this.menuTimer = setTimeout('$(\'' + selector + '\').hide()', this.menuTimerPeriod);
	},
	initMenus : function () {
		var that = this;
		$('.menu_item').live('mouseover', function () {
			that.cancelMenuTimer();
			$('.barra_submenu').hide();
		});
		$('.has_submenu').live('mouseover', function () {
			$($(this).attr('data-submenu')).show();
		}).live('mouseout', function () {
			that.setMenuTimer($(this).attr('data-submenu'));
		});
		$('.barra_submenu').live('mouseover', function () {
			that.cancelMenuTimer();
		}).live('mouseout', function () {
			that.setMenuTimer('#' + $(this).attr('id'));
		});
	},
	initButtons : function () {
		$('[data-section]').live('click', function () {
			apme.content.section = $(this).attr('data-section');
			apme.content.page = 1;
			apme.content.filter = '';
			apme.content.clientPagination = false;
		});
		$('[data-page]').live('click', function () {
			apme.content.page = $(this).attr('data-page');
		});
		$('[data-language]').live('click', function () {
			apme.content.language = $(this).attr('data-language');
		});
		$('[data-filter]').live('click', function () {
			apme.content.filter = $(this).attr('data-filter');
		});
		$('[data-section], [data-language]').live('click', function () {
			apme.content.update();
		});
	},
	init : function () {
		this.initMenus();
		this.initButtons();
		$('#privacyLink').click(function () {
			$(window).scrollTop(0);
			apme.visuals.createOverlay();
			apme.visuals.createBox('privacyBox').load('templates/privacy.html', function () {
				$('.closeButton').click(function () {
					apme.visuals.deleteOverlay();
					$('#privacyBox').remove();
				});
			});
		});
		$.datepicker.setDefaults($.datepicker.regional["pt"]);

		if (!$.browser.msie) {
			$("body").bind("DOMNodeInserted", function (e) {
				apme.gallery.show();
				apme.dictionary.update();
			});
		} else {
			window.attachEvent('onload', function () {
				//create a "watching" method.  this will get applied to allelements current found in the DOM
				//this method gets triggered when something happens to one of the elements.  the cache will
				//then be cleared and we will re-assign the watch to elements below.
				var checking = false,
				watch = function () {};
				watch = function (event) {
					//don't execute this if we're already processing
					//now if only there were a way to check if an attribute was actually an HTML attribute or not...
					if (checking || event.propertyName != "innerHTML")
						return;
					checking = true;
					//wait till the rest of the script execution is done to look at other elements
					setTimeout(function () {
						checking = false;
						//now watch all elements below this
						var descendants = event.srcElement.getElementsByTagName("*"),
						length = descendants.length;
						//atach the listener to all descendants
						for (var i = 0; i < length; ++i)
							descendants[i].attachEvent("onpropertychange", watch);
					}, 0);

					apme.gallery.show();
					apme.dictionary.update();
				};

				//watch everything
				var descendants = document.getElementsByTagName("*"),
				length = descendants.length;
				for (var i = 0; i < length; ++i)
					descendants[i].attachEvent("onpropertychange", watch);
			});
		}
	}
};

