var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
document.write('<scri'+'pt type="text/javascript" src="js/analytics.js" ><\/scri'+'pt>');

var _base_href = "";
if (document.getElementsByTagName && document.getElementsByTagName("base") && document.getElementsByTagName("base").length > 0)
	_base_href = document.getElementsByTagName("base")[0].href;

//loading base libraries to keep compatibility, I don't know why I bother with that ??
if (isDojoReady)  {
	document.write('<scri'+'pt type="text/javascript" src="js/dojo/dojo.js" djConfig="parseOnLoad:true,isDebug:false" ><\/script>');
	document.write('<scri'+'pt type="text/javascript" src="js/dojo-adapter.js" ><\/scr'+'ipt>');		
} else {
	document.write('<scri'+'pt type="text/javascript" src="js/dhtmllib.js" ><\/scri'+'pt>');
}

/**
 * Common function that are compatible in any browser
 **/
 
function openWindow(args) {
		var defaults = {
				directories : "no",
				location : "no",
				menubar : "no",
				resizable : "no",
				scrollbars : "yes",
				status : "no",
				toolbar : "no",
				width: 0,
				height:0,
				left: 0,
				top: 0,
				url: null,
				name: "",
				center: true,
				fullscreen: false,
				replace: true, 
				properties: null
			};
		var windowArgs = mixin(defaults,args);
		var _url = "";
		if (windowArgs.url && windowArgs.url.href)
			_url = windowArgs.url.href;
		else if (windowArgs.url)
			_url = windowArgs.url;
		if (windowArgs.fullscreen) {
			windowArgs.height = screen.availHeight;
			windowArgs.width = screen.availWidth;		
		}
		if (windowArgs.width > screen.availWidth) {
			windowArgs.width = screen.availWidth;
			windowArgs.scrollbars = "yes";
		}
		if (windowArgs.height > screen.availHeight) {
			windowArgs.height = screen.availHeight;
			windowArgs.scrollbars = "yes";
		}
		if (windowArgs.center) {
			windowArgs.left = (screen.availWidth-windowArgs.width)/2;
			windowArgs.top = (screen.availHeight-windowArgs.height)/2;
		}
		windowArgs.left = windowArgs.left < 0 ? 0 : windowArgs.left;
		windowArgs.top = windowArgs.top < 0 ? 0 : windowArgs.top;
		var newWindow 
		if (windowArgs.properties)
			newWindow = window.open(_url,windowArgs.name, windowArgs.properties + 
				",width="+ windowArgs.width+",height="+ windowArgs.height+",left="+windowArgs.left+",top="+windowArgs.top);
		else
			newWindow = window.open(_url,windowArgs.name,		
		"directories="+ windowArgs.directories +
		",toolbar="+ windowArgs.toolbar +
		",status="+ windowArgs.status +
		",scrollbars="+ windowArgs.scrollbars +
		",resizable="+ windowArgs.resizable +
		",menubar="+ windowArgs.menubar +
		",location=" + windowArgs.location +
		",width="+ windowArgs.width+",height="+ windowArgs.height+",left="+windowArgs.left+",top="+windowArgs.top+"");
		if (newWindow.focus)
			newWindow.focus();
		return newWindow;
}

var newWindow;

/// Open a centered window with the sepcified url
/// obj is a string or a link obj, w is the width, h height of the window 
/// a third parameter can be set that will be the properties of the window
function popupWindow(obj, w, h, properties) {
    newWindow = openWindow({width:w,height:h,url:obj, properties: properties}); 
	return false;
}



/**
* Does the event gets bubbled from a child element
* See : http://www.quirksmode.org/js/events_mouse.html
**/
function isEventFromChild(e, elmt) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;	
	if (elmt && tg != elmt) return true;
	if (!elmt && tg.nodeName != 'DIV') return true;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return true;
	return false;
}
	
	/* 
		#############################################################
	    An image preloader, just do preloadImage(path, callback) 
		or preloadImage(imgObject, callback) 
		or preloadImage([path1, path2], callback)
		preloadImages contains all the images to preload
		allImagesLoaded points to a function listener, so to know when 
		all images are loaded just do : allImagesLoaded = function() {}
		#############################################################
	*/	
var preloadImages =  new Array();
var imgsLoadedTimer = null;
/* Empty listener called when there are no more images to preload*/
var allImagesLoaded = null;	

function preloadImage(src, callback) {	
		if (document.images) {
			
			var _imgs = new Array();
			//src is an array
			if (isArray(src)) {			
				_imgs = src;		
			} else {
				_imgs = [src];
			}
			for (var i = 0; i < _imgs.length; i++) {	
				var path = _imgs[i];		
				var img  = null;	
				
				if (isString(path))	{
				  img = new Image();
				  img.src = path;
				} else {
		   		  img = path; //we suppose it is an image
				}												
				preloadImages[preloadImages.length] = {image: img, loaded: false, listener: callback};
			}
		if (!imgsLoadedTimer) 
			imgsLoadedTimer = setTimeout("checkLoadedImages()", 100);	
	}
}

function allImagesLoadedListener(f) {
	var oldF = null;
	if (allImagesLoaded && isFunction(allImagesLoaded)) 
		oldF = allImagesLoaded;
	allImagesLoaded = function() {		
		if (oldF)
			oldF();
		f();
	};
}

function checkLoadedImages() {
		if (preloadImages.length == 0) {
			clearTimeout(imgsLoadedTimer);			
			return;
		}
		var allComplete = true;
		for (var i = 0; i < preloadImages.length; i++) {
			if (!preloadImages[i].loaded && preloadImages[i].image.complete) {
				preloadImages[i].loaded = true;
				if (preloadImages[i].listener) preloadImages[i].listener(preloadImages[i].image);
			} 
			if (!preloadImages[i].loaded)
				allComplete = false;
		}
		if (!allComplete)
			imgsLoadedTimer = setTimeout(checkLoadedImages, 100);
		else {
			clearTimeout(imgsLoadedTimer);
			if (allImagesLoaded)
				allImagesLoaded();
		}
}

/**
* swap two tabs 
**/
function swapTab(_old,_new) {
	//var t = [];
	if (typeof _old.length == undefined)
		_old[0] = _old;	
	for (var i = 0; i < _old.length; i++) {
			if (byId(_old[i]))
				style(byId(_old[i]), 'display', 'none');
			if (byId(_old[i]+"Tab"))
				byId(_old[i]+"Tab").className = 'tabButton';
	}
	if (byId(_new))
	style(byId(_new), 'display', 'block');
	byId(_new+"Tab").className = 'tabButton-selected';
	return false;
}

/**
* Extract the correct source element for an event
**/
function extractSourceElement(evt) {
		if (!evt) var evt = window.event;
	    return (window.event) ? evt.srcElement : evt.target;	
}
function extractTargetElement(evt) {
		if (!evt) var evt = window.event;
	    return  (evt.relatedTarget) ? evt.relatedTarget : evt.toElement;	
}

/* Cookie functions */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function swapImage(imgName, src) {
	var img = byId(imgName);
	if (!img && findImage)
		img = findImage(imgName, document);
	if (img) {
		img.src = src;
	}
}

/*
* @id the id or name of the image src
* @images an array of strings representing the images
*/
function imageSkimming(id, images) {
	var elmt = byId(id);
	if (images.length < 1)
		return;
	images[images.length] = elmt.src;
	elmt._images = images;
	elmt._skimPos = 0;
	elmt._skim = imageSkimming.skim;
	connect(elmt, 'onmouseover', elmt['_skim']);
	//we need to preload the images
	preloadImage(images);
}

imageSkimming.speed =400;//in ms
imageSkimming.skim = function () {
	var img = this._images[this._skimPos];
	this.src = img;
	if (this._skimPos == this._images.length - 1) {
		this._skimPos = 0;
		return;
	} else {
		this._skimPos++;	
	}
	var that = this;
	window.setTimeout(function() { that._skim(); },imageSkimming.speed);
}

/**
* creates a slideshow of images on an existing image
* @id : id of the image where the slideshow takes place
* @images :  an array of image path or an array of objects {src,title,onclick}
* @onchange : gets called every time an image is swapped
*/
function slideshow(id, images, onchange) {
	
	var elmt = byId(id);
	if (images.length < 1 || typeof(elmt) == 'undefined')
		return;		
	if (!slideshow.contains(images, elmt.src))
		images[images.length] = elmt.src;
	if (images.length < 2) return;
	var preloadImgs = [];
	//we setup every images
	for (var i = 0; i < images.length; i++) {
		if (isString(images[i]) )
			images[i] = {src:images[i], title: elmt.title, onclick:elmt.onclick};
		preloadImgs.push(images[i].src);	
	}
	elmt._slideshowPos = 1;
	elmt._slideshowImages = images;
	elmt._slideshowPlay = slideshow.play;
	elmt._slideshowOnchange = onchange; 
	connect(elmt,'onclick',elmt, slideshow.onclick);		
	preloadImage(preloadImgs);
	allImagesLoadedListener(
	function() {
		window.setTimeout(function() {
						elmt._slideshowPlay(); }, slideshow.timeout);
		});
	if (isDojoReady) {
		var div = document.createElement('div');
		div.id = "div_" + elmt.id;		
		div.style.position= "relative";		
		div.style.height = elmt.height+"px";
		div.style.width = elmt.width+"px";
		addOnLoad(function() { //for safari, because the image width is 0 until loaded
			div.style.height = elmt.height+"px";
			div.style.width = elmt.width+"px";
			var img2 = byId(elmt.id+"_fader");
			img2.height = elmt.height;
			img2.width = elmt.width;
		});		
		elmt.parentNode.replaceChild(div, elmt);		
		elmt.style.position = "absolute";
		elmt.style.top = "0px";
		elmt.style.left = "0px";
		elmt.style.zIndex = "1";
		elmt.style.display = "block";
		div.appendChild(elmt);
		var img2 = document.createElement('img');
		img2.src = images[1].src;
		img2.id = elmt.id + "_fader";
		img2.style.display = "none";
		img2.style.position = "absolute";
		img2.style.top = "0px";
		img2.style.left = "0px";
		img2.style.zIndex = "0";
		img2.height = elmt.height;
		img2.width = elmt.width;
		div.appendChild(img2);		
	}
	
}

//Checks if the Array a contains the elemet elmt
slideshow.contains = function (a, elmt) {
	for (var i = 0;i < a.length; i++) {
		var s = isString(a[i]) ? a[i] : a[i].src;
		if ( elmt.indexOf(s) > -1) return true;
	}
	return false;
}

//called whenever an image has been clicked
slideshow.onclick = function() {
	var imgInfo = this._slideshowImages[this._slideshowPos];//which image has been clicked
	this._slideshowOnClick = null;
	if (!isString(imgInfo) && imgInfo.onclick) {
		if (isString(imgInfo.onclick))
			window.location.href = imgInfo.onclick;
		else {
			this._slideshowOnClick = imageInfo.onClick;
			this._slideshowOnClick();
		}
	}
}

slideshow.timeout = 10000;
slideshow.play = function() {
	var me = this;
	var img = this._slideshowImages[this._slideshowPos];	
	if (isDojoReady) {
		var fader = byId(this.id+"_fader");
		fader.src = this.src;
		style(fader, 'display', 'block');
		window.setTimeout( function() { //we need a delay because IE 6 won't load the image correctly, and a ghost of the previous image will be displayed
			style(fader,'opacity', 1);	
			style(me, 'opacity', 0);		
			me.src = img.src;
			if (img.title)
				me.alt = me.title = img.title;
			dojo.fadeOut({node:fader, end:0, onEnd: function() {style(fader, 'display', 'none'); }}).play();
			dojo.fadeIn({node:me, end:1}).play();
		}, 150);
	} else {
		this.src = img.src;	
		if (img.title)
			this.alt = this.title = img.title;
	}
	if (this._slideshowOnchange)
		this._slideshowOnchange();
	if (this._slideshowPos == this._slideshowImages.length - 1) {
		this._slideshowPos = 0;
	} else {
		this._slideshowPos++;	
	}	
	window.setTimeout(function() { me._slideshowPlay(); }, slideshow.timeout);
}

/** Protecting text from selection **/
//form tags to omit in NS6+:
//http://eking.in
/*
var omitformtags=["input", "textarea", "select"];

omitformtags=omitformtags.join("|");

function disableselect(e){
	if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)
		return false;
	return true;
}

function reEnable(){
	return true;
}

if (typeof document.onselectstart!="undefined")
	document.onselectstart=new Function ("return false");
else {
	document.onmousedown=disableselect;
	document.onmouseup=reEnable;
}
*/