/*
*  Extens Solucions S.L.
*  common.js
*/

var iStrip = null;
var tmpData = null;
var tel = null;

function empresa(pans, scroller){ iStrip = new imageStrip(4, pans, scroller); }

function fitxa(pans, scroller){ iStrip = new imageStrip(3, pans, scroller); }

function trim(str){
	var trimed, rg, i = 0;
	while ((i < str.length) && (str.charAt(i) == ' ')) i++;
	trimed = str.substring(i, str.length);
	rg = trimed;
	i = rg.length;
	while ((i > 0) && (rg.charAt(i-1) == ' ')) i--;
	trimed = rg.substring(0,i);
	return trimed;
}


function visit(url){ document.location.href = url; }

function spring(atag){ window.open(atag.href, '_blank'); }

function springw(atag){ window.open(atag.href, '_blank', 'toolbar=no, resizable=yes, copyhistory=no, status=no, width=800, height=auto'); }

function remarkBox(){
	document.getElementById('acceptbox').className = "noaccept";
	setTimeout("document.getElementById('acceptbox').className = 'accept'", 500);
	setTimeout("document.getElementById('acceptbox').className = 'noaccept'", 1000);
	setTimeout("document.getElementById('acceptbox').className = 'accept'", 1500);
	setTimeout("document.getElementById('acceptbox').className = 'noaccept'", 2000);	
	setTimeout("document.getElementById('acceptbox').className = 'accept'", 2500);
}

function vergross(atag, width, height){
	width = width ? width : 'auto'; 
	height = height ? height : 'auto'; 	
	nw = window.open('', 'photo', 'toolbar=no, resizable=no, copyhistory=no, status=no, width=' + width + ', height=' + height);
	nw.document.open();  
	nw.document.write("<html><title>Mobles All&eacute;s</title>");  
	nw.document.write("<body style=\"backround-color:#FFFFFF; margin:0; padding:0; width:" + width + "px; height:" + height + "\">");  
	nw.document.write("<img src=\"" + atag.href + "\" width=\""+ width +"\" height=\""+ height +"\" alt=\"" + atag.alt + "\">");  
	nw.document.write("</body></html>");  
	nw.document.close();  
	nw.focus();
}

imageStrip = function(vpans, pans, scroller){ 
	this.steps = 20;	
	this.duration = 400;
	this.stepCounter = 0;
	this.interval = this.duration / this.steps;
	this.pantag = 'pan';
	this.cnpan = 0;
	this.npans = (pans - vpans);
	this.panw = 0;
	this.panh = 0;
	this.scroller = document.getElementById(scroller);
	this.direction = null;
	var pan = document.getElementById(this.pantag + this.cnpan);
	if(pan && this.scroller)
	{
		this.scroller.scrollLeft = 0;
		this.panw = pan.clientWidth;
		this.panh = pan.clientHeight;		
	}

	imageStrip.prototype.forward = function(){
		if(this.cnpan >= this.npans)
			return;
		else
			this.active('f');
	}
	
	imageStrip.prototype.back = function (){
		if(this.cnpan <= 0)
			return;
		else
			this.active('b');		
	}	
		
	imageStrip.prototype.up = function (){
		return;
	}
	
	imageStrip.prototype.down = function (){
		return;
	}		

	imageStrip.prototype.active = function (cmd){
		if(this.panw && this.panh){
			switch(cmd){
				case 'f':
					this.stepGrow = this.panw / this.steps;
					this.direction = true;
					this.stepHorAnimation();
					break;
				case 'b':
					this.stepGrow = this.panw / this.steps;
					this.direction = false;	
					this.stepHorAnimation();					
					break;
				case 'u':
				case 'd':
				default:
					return;						
			}
		}else{
			return;	
		}		
	}
	
	imageStrip.prototype.stepHorAnimation = function (){
		++this.stepCounter;
		this.animateHor();
		(this.stepCounter >= this.steps) ? this.terminatedHor() : this.startHor();
	}
	
	imageStrip.prototype.startHor = function (){
		var self = this;
		setTimeout(function() { self.stepHorAnimation(); }, this.interval);
	}
	
	imageStrip.prototype.animateHor = function (){
		this.scroller.scrollLeft = this.direction ? (this.scroller.scrollLeft + this.stepGrow) : (this.scroller.scrollLeft - this.stepGrow);
	}
	
	imageStrip.prototype.terminatedHor = function(){
		this.stepCounter = 0;
		this.cnpan = this.direction ? (this.cnpan + 1) : (this.cnpan -1);		
	}
} 

function setImage(path, ref, width, height, ig){
	tmpData = new Array(ig, new Image(width, height), ref, width, height);
	tel = document.getElementById('tel');
	tel.style.visibility = "visible";
	tmpData[1].onload = showImage; 
	tmpData[1].src = path;
}

function showImage(){
	var img = document.getElementById('default')
	img.src = tmpData[1].src;
	img.width  = tmpData[3];
	img.height = tmpData[4];
	document.getElementById('ref').innerHTML = tmpData[2] ? tmpData[2] : '&nbsp;';	
	if(tmpData[0])
	{
		atag = document.getElementById('fitxap');
		if(atag){
			var href = atag.href;
			atag.href = href.substring(0, (href.lastIndexOf('=')+1)) + tmpData[0];
		}
		ctag = document.getElementById('cref');		
		if(ctag){
			var href = ctag.href;
			ctag.href = href.substring(0, (href.lastIndexOf('=')+1)) + tmpData[2];		
		}
	}
	tmpData = null;
	tel.style.visibility = 'hidden';
	tel = null;
}

function arrange(col1, col2, bott){
	var c1 = document.getElementById(col1);
	var c2 = document.getElementById(col2);	
	if(c1 && c2){
		if(c1.offsetHeight > c2.offsetHeight)
			c2.style.height = c1.clientHeight + (bott ? bott : 25) + "px";
	}
}

function checkForm(frm){
	
	if(frm.accept.checked){
		tosubmit = true;	
	}else{
		remarkBox();
		tosubmit = false;
	}
	
	return tosubmit;
}

