/* Modificar los enlaces externos (aquellos con el atributo rel="external") para que se abran
 en ventana nueva. */
function loadExternalUrl(){
	if( !document.getElementsByTagName ){
        return;
    }
	var links=document.getElementsByTagName( 'a' );
	for( var i=0; i<links.length; i++ ){
		if( links[i].rel=="external" ){
			links[i].target="_blank";
		}
	}
}

/******************/
/* inici execució */
/******************/
var ie;
var ns;
if( navigator.userAgent.indexOf('MSIE')!=-1 ){
    ie = true;
    ns = false;
    window.attachEvent('onload', loadExternalUrl );
}		
if( navigator.userAgent.indexOf('Gecko')!=-1 ){
    ie = false;
    ns = true;
    window.addEventListener( 'load', loadExternalUrl, false );
}

function writeEmail( nom, dom ){
	document.write( nom+'@'+dom);
}

/********************/
/* FUNCIONS DE DATA */
/********************/
Date.prototype.toIso = function (){
	
	any = this.getFullYear();
	mes = this.getMonth()+1;
	dia = this.getDate();
	
	if (mes<10){
		mes = '0'+mes;
	}
	if (dia<10){
		dia = '0'+dia;
	}
	
	return any+'-'+mes+'-'+dia;
}
Date.prototype.resetHour = function (){
	this.setHours(0);
	this.setMinutes(0);
	this.setSeconds(0);
	this.setMilliseconds(0);
}

Date.prototype.toDma = function (){
	
	any = this.getFullYear();
	mes = this.getMonth()+1;
	dia = this.getDate();
	
	if (mes<10){
		mes = '0'+mes;
	}
	if (dia<10){
		dia = '0'+dia;
	}
	
	return dia+'-'+mes+'-'+any;
}
Date.prototype.toHour = function (){
	
	hor = this.getHours();
	min = this.getMinutes();
	
	if (hor<10){
		hor = '0'+hor;
	}
	if (min<10){
		min = '0'+min;
	}
	
	return hor+':'+min;
}

/*******************************************/
/*******************************************/
/*******************************************/

function removeChildNodes(obj){
	while (obj.hasChildNodes()){
		obj.removeChild(obj.firstChild);
	}
}

function marcaSelect(oSel,valorSel){
	
	for(i=0;i<oSel.options.length;i++){
		if (parseInt(oSel.options[i].value,10) == parseInt(valorSel,10)){
			oSel.options[i].selected = true;
		}else{
			oSel.options[i].selected = false;
		}
	}
	
}

function getOffsetLeft (el) {
    var ol = el.offsetLeft;
    while ((el = el.offsetParent) != null){
        ol += el.offsetLeft;
    }
	return ol;
}
function getOffsetTop (el) {
    var ot = el.offsetTop;
    while((el = el.offsetParent) != null){
        ot += el.offsetTop;
    }
    return ot;
}

/**/
/**
 * obrim popup per a mostrar la foto ampliada
 */
//function showPopup(tagImg,numEle,nomCot,desCot){
function showPopup(numEle,offsetX,offsetY){
	
	var objPop = document.getElementById('popupFoto');
	var objImg = document.getElementById('lupa_'+numEle);
	
	var pos_x = getOffsetLeft(objImg)+offsetX;
	var pos_y = getOffsetTop(objImg)+offsetY;
		
	//objPop.innerHTML = tagImg;
	removeChildNodes(objPop);
	oHref = document.createElement('a');
	oHref.href='javascript:void(0);';
	oHref.innerHTML = cotxes[numEle].foto;
	objPop.appendChild(oHref);
	
	oSep = document.createElement('div');
	oSep.className = 'separadorH';
	objPop.appendChild(oSep);
	
	oH3 = document.createElement('h3');
	
	/*
	cotxes[numEle].nom = cotxes[numEle].nom.replace(/\\+/g,'');
	cotxes[numEle].descripcio = cotxes[numEle].descripcio.replace(/\\+/g,'');
	*/
	
	oTxt = document.createTextNode(cotxes[numEle].nom);
	oH3.appendChild(oTxt);
	objPop.appendChild(oH3);
	
	oPar = document.createElement('p');
	linies = cotxes[numEle].descripcio.split('___');
	
	for(var i=0;i<linies.length;i++){
		oTxt = document.createTextNode(linies[i]);
		oBr = document.createElement('br');
		oPar.appendChild(oTxt);
		oPar.appendChild(oBr);
	}
	
	
	objPop.appendChild(oPar);
	
	objPop.style.top = pos_y+'px';
	objPop.style.left= pos_x+'px';
	objPop.style.display = 'block';
	
	objPop.onclick = function (){
		this.style.display = 'none';
	}
	
}


/**
 * Evita que el tag object tingui inicialment 
 * el focus al carregar la plana
 */
function voidIEObjectProblem(){
	if (ie){
		theObjects = document.getElementsByTagName("object");
		for (var i = 0; i < theObjects.length; i++) {
		    theObjects[i].outerHTML = theObjects[i].outerHTML;
		} 
	}
}