/**
* @author: Rodrigo Bartels
* @author: Diego Pereira
* Date: October 3, 2008.
* Common Functions used around the application.
*/

Ext.Button.override({
	setTooltip: function(qtipText) {
		var btnEl = this.getEl().child(this.buttonSelector);
		Ext.QuickTips.register({
			target: btnEl.id,
			text: qtipText
		});
	}
});

var confirmFunction = function(){
	var message = "Your changes have been saved successfully";
	showConfirmationMessage(message, null, 'EMPTY', 'mylib-lo-view-window');
	var confirmationWindow = Ext.getCmp('confirmation-window');
	var f = function(){
		confirmationWindow.close();
	};
	setTimeout(f, 3000);
};

var alertCurriculaMode = function(message){       	
	showConfirmationMessage(message, null, 'EMPTY', 'curricula-window');
	var confirmationWindow = Ext.getCmp('confirmation-window');
	var f = function(){
		confirmationWindow.close();
	};
	setTimeout(f, 3000);
};   

function cleanFields(form, optional){
	var formElements = form.elements;
	for(var index = 0; index < formElements.length; index++) {
		if(formElements[index].type == "text") {
			formElements[index].value = "";
		}
		if(optional)
			optional.value="";
	}
};

function findPos(obj) {
	var curleft = 0
	var curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
};

function notNull(value){
	return (value != null && value != undefined);
};

function notEmpty(value){
	return value != "";
};

var hideSpotlight = function(){
	updateSpot(false);
};
var showSpotlight = function(){
	updateSpot(this.id);
};

var addExtTitleBarItems = function(config){if (this.header) {
	this.headerBody = this.header.createChild({
		tag: 'div',
		cls: 'ss-header-body'
	});
	if(config.title){
		// left section: title window
		this.headerBody.title = this.headerBody.createChild({
			tag: 'div',
			cls: 'ss-title'
		});
		this.headerBody.title.text = this.headerBody.title.createChild({
			tag: 'div',
			cls: 'ss-title-text',
			html: config.title
		});
	}
	if(config.close){
		// right section: close window
		this.headerBody.close = this.headerBody.createChild({
			tag: 'div',
			cls: 'ss-close'
		});
		this.headerBody.close.icon = this.headerBody.close.createChild({
			tag: 'div',
			cls: 'ss-header-icon ss-close-icon'
		});
		this.headerBody.close.text = this.headerBody.close.createChild({
			tag: 'div',
			cls: 'ss-close-text',
			html: config.close
		});
		this.setElementAction(this.headerBody.close, 'headerBody.close', 'close', 'ss-close-over');
	}
}};

var addExtElemClass = function(e, elem, cls){
	this.addClass(cls);
};
var removeExtElemClass = function(e, elem, cls){
	this.removeClass(cls);
};
var setExtElementAction = function(el, id, fn, cls){	
	var elem = (typeof el == 'string') ? this[el] = Ext.get(id) : el;
	if(elem){
		elem.on('click', this[fn] ? this[fn] : (window[fn] ? window[fn] : ''), this[fn] ? this : (window[fn] ? window : ''));
		elem.on('mouseover', addExtElemClass, elem, cls);
		elem.on('mouseout', removeExtElemClass, elem, cls);
	}
};

function toggleDisplay(id, display) {
	if(display) {
		document.getElementById(id).style.display = "";
	} else {
		document.getElementById(id).style.display = "none";
	}
};

function toggleVisibility(id, display) {
	if(display) {
		document.getElementById(id).style.visibility= "visible";
	} else {
		document.getElementById(id).style.visibility = "hidden";
	}
};

function processContactUsResult(data){
	if(data){
		toggleDisplay('resultMessage', false);
		toggleVisibility('resultMessage', false);
		toggleDisplay('resultMessageOk', true);
		cleanFields(document.getElementById('contactForm'), document.getElementById('message'));
		var f = function(){
			toggleDisplay('resultMessageOk', false);
		};
		setTimeout(f, 5000);
	}
};

function contactUs(name, email, subject, message){
	if(notEmpty(email) && notNull(email)){
		var emailRegExp = /(\w+)@(\w+\.)(\w+)(\.\w+)*/;
		if(email.match(emailRegExp)){
			ContactUs.sendMessage(name, message, email, subject,  processContactUsResult);
		}
	} else {
		var resultSpan = document.getElementById('resultMessage');
		resultSpan.style.visibility='visible';
	}
};

function showContactTab(){
	var tabPanel = Ext.getCmp('mainContentTabPanel');
	tabPanel.setActiveTab('contactTabPanel');
};

function registerSpotlightEventsToWindow(win){
	// Spotlight methods for the provided window
	win.showSpotlight = showSpotlight;
	win.hideSpotlight = hideSpotlight;
	// Spotlight events for the provided window
	win.on('beforeclose', win.hideSpotlight, win);
	win.on('beforehide', win.hideSpotlight, win);
	win.on('beforeshow', win.showSpotlight, win);
	win.on('collapse', win.showSpotlight, win);
	win.on('move', win.showSpotlight, win);
	win.on('resize', win.showSpotlight, win);
};
