/************************************************
 * 
 * All these functions have to be defined in HEAD 
 * so they can be available anytime, other functions 
 * will be available only once the page load is 
 * completed 
 * 
 * 
 ************************************************/

/**
 * Helper for document.getElementById
 * 
 * @param domid
 * @return
 */
function $(domid) {
	return document.getElementById(domid);
}

function _(s) {
	if (typeof(i18n)!='undefined' && i18n[s]) {
		return i18n[s];
	}
	return s;
}

function _img(s) {
	if (typeof(i18n_images)!='undefined' && i18n_images[s]) {
		return i18n_images[s];
	}
	return s;
}

/**
 * ajoute un événement sans écraser l'existant.
 * 
 * @param obj
 * @param evType
 * @param fn
 * @param capt
 * @return
 */
function addEvent(obj, evType, fn, capt) {

	if (!obj) {
		return false;
	}

	if (obj.addEventListener) { // NS6+
		obj.addEventListener(evType, fn, capt);
	} else {
		if (obj.attachEvent) {
			obj.attachEvent("on" + evType, fn) // IE 5+
		} else {
			return false;
		}
	}
}
 
/**
 * Returns the elements associated to a class
 * 
 * @param string tag name
 * @param string class name
 * @return array
 */
if (typeof document != 'undefined') {
	document.getElementsByTagAndClassName = function(tagName, className) {
		var retnode = [];
		var myclass = new RegExp('\\b' + className + '\\b');
		var elem = this.getElementsByTagName(tagName);
	
		for ( var i = 0; i < elem.length; i++) {
			var classes = elem[i].className;
			if (myclass.test(classes)) {
				retnode.push(elem[i]);
			}
		}
		return retnode;
	}
}

/**
 * function recieves a url parameter and sets the class variable siteBaseUrl to that parameter
 * this function eliminates having a hard coded base url in the js file.
 * it is called in the head section of main.xslt
 *
 * @param baseUrl
 */
function setSiteBaseUrl(baseUrl) {
	
	siteBaseUrl = baseUrl;
}

function callUrlPreview() {
	
	// create the url that checks if a visitor is allowed to see a preview 
	return siteBaseUrl + 'remoting';
}

function displayPreviewForbidden() {
	
	if(is_marqueblanche) {
		document.location = siteBaseUrl + 'mb/videos/preview_forbidden';
	} else {
		document.location = siteBaseUrl + 'videos/preview_forbidden';
	}
}

function getLightboxLink(url, width, height) {
	
	if (url.indexOf('?') > 0) {
		
		url = url + '&';
	} else {

		url = url + '?';
	}
	
	url = url + 'lightbox=1&TB_iframe=true&modal=true&height=' + height + '&width=' + width;
	
	return url;
}

function maximizeWindow() {
	
	window_width = screen.availWidth;
	window_height = screen.availHeight;
	
	if (parent.frames.length < 1) {
		window.moveTo(0,0);
		window.resizeTo(window_width,window_height);
	}
}

