// JavaScript Document

var px = document.layers || window.opera ? "" : "px";
function make (id) {
this.elm = document.getElementById ? document.getElementById(id) : document.all ? document.all[id] : null;
if (!this.elm) return null;
this.css = this.elm.style;
this.obj = id + 'Obj';
eval(this.obj + ' = this');
this.x = this.elm.offsetLeft ? this.elm.offsetLeft : 0;
this.y = this.elm.offsetTop ? this.elm.offsetTop : 0;
this.w = this.elm.offsetWidth ? this.elm.offsetWidth : 0;
this.h = this.elm.offsetHeight ? this.elm.offsetHeight : 0;
}
make.prototype.moveTo = function(x, y) {
if (x != null) {
x = Math.round(x);
this.x = x;
this.css.left = x + px;
}
if (y != null) {
y = Math.round(y);
this.y = y;
this.css.top = y + px;
}
}
make.prototype.moves = new Array();     
make.prototype.cancelMoves = false;
make.prototype.moving = false;
make.prototype.timeSlide = function(x,y,duration,acc,func,cancel,timeout) {
if(!cancel) var cancel = false;
if(!func) var func = "null";
if(!timeout) var timeout = 5;
if(!acc) var acc = 0;
var startTime = new Date().valueOf();
var endTime = startTime + duration;

if(!this.moving) {
this.moving = true;
this.timeSlideAux(this.x,this.y,x,y,acc,startTime,endTime,func,cancel,timeout);
} else {
if(cancel) {
this.cancelMoves = true;
this.moves = new Array();
}
var strExec = this.obj + ".timeSlide(" + x + "," + y + "," + duration + "," + acc + ",'" + func + "'," + cancel + "," + timeout + ")";
this.moves.push(strExec);
}
}

make.prototype.timeSlideAux = function(startX,startY,endX,endY,acc,startTime,endTime,func,cancel,timeout) {
var curTime = new Date().valueOf();
if(this.cancelMoves) {
this.moving = false;
this.cancelMoves = false;
if(this.moves[0]) eval(this.moves.shift());

} else if(curTime >= endTime) {
this.moveTo(endX,endY);
this.moving = false;
if(func) eval(func);
if(this.moves[0]) eval(this.moves.shift());

} else {
var percent = (curTime - startTime) / (endTime - startTime); //percentage of way through animation
var startPos = new coord(1,1);
var endPos = new coord(0,0);

if(acc!=0) var c1 = new coord(0.5+(acc/2),0.5-(acc/2));
else c1 = null;
var pos = getBezier(percent,startPos,endPos,c1);
var stage = pos.y;
this.moveTo(((endX-startX)*stage)+startX,((endY-startY)*stage)+startY);
var strExec = this.obj + ".timeSlideAux(" + startX + "," + startY + "," + endX + "," + endY + "," + acc + "," + startTime + "," + endTime + ",'" + func + "'," + cancel + "," + timeout + ")";
setTimeout(strExec,timeout);

}
}
// si no se utliza aceleración eliminar a partir de aqui
make.prototype.bezierSlide = function(x,y,c1x,c1y,c2x,c2y,duration,acc,func,cancel,timeout) {
if(!cancel) var cancel = false;
if(!func) var func = "null";
if(!timeout) var timeout = 5;
if(!acc) var acc = 0;
var startTime = new Date().valueOf();
var endTime = startTime + duration;
if(!this.moving) {
this.moving = true;
this.bezierSlideAux(this.x,this.y,x,y,c1x,c1y,c2x,c2y,acc,startTime,endTime,func,cancel,timeout);
} else {
if(cancel) {
this.cancelMoves = true;
this.moves = new Array();
}
var strExec = this.obj + ".bezierSlide(" + x + "," + y + "," + c1x + "," + c1y + "," + c2x + "," + c2y + "," + duration + "," + acc + ",'" + func + "'," + cancel + "," + timeout + ")";
this.moves.push(strExec);
}
}

make.prototype.bezierSlideAux = function(startX,startY,endX,endY,c1x,c1y,c2x,c2y,acc,startTime,endTime,func,cancel,timeout) {
var curTime = new Date().valueOf();
if(this.cancelMoves) {
this.moving = false;
this.cancelMoves = false;
if(this.moves[0]) eval(this.moves.shift());

} else if(curTime >= endTime) {
this.moveTo(endX,endY);
this.moving = false;
if(func) eval(func);
if(this.moves[0]) eval(this.moves.shift());

} else {
var percent = (curTime - startTime) / (endTime - startTime); //percentage of way through animation
var startPos = new coord(0,0);
var endPos = new coord(1,1);

if(acc!=0) var c1 = new coord(0.5-(acc/2),0.5+(acc/2));
else c1 = null;
var pos = getBezier(percent,startPos,endPos,c1);
var stage = pos.y;

var startPos = new coord(startX,startY);
var endPos = new coord(endX,endY);
var c1 = new coord(c1x,c1y);
var c2 = new coord(c2x,c2y);
var pos = getBezier(stage,startPos,endPos,c1,c2);
this.moveTo(pos.x,pos.y);

var strExec = this.obj + ".bezierSlideAux(" + startX + "," + startY + "," + endX + "," + endY + "," + c1x + "," + c1y + "," + c2x + "," + c2y + "," + acc + "," + startTime + "," + endTime + ",'" + func + "'," + cancel + "," + timeout + ")";
setTimeout(strExec,timeout);

}
}
make.prototype.doCancelMoves = function(keepCurrent) {
if(keepCurrent) this.cancelMoves = true;
this.moves = new Array();}
B1 = function(t) { return (t*t*t); }
B2 = function(t) { return (3*t*t*(1-t)); }
B3 = function(t) { return (3*t*(1-t)*(1-t)); }
B4 = function(t) { return ((1-t)*(1-t)*(1-t)); }
coord = function (x,y) { if(!x) var x=0; if(!y) var y=0; return {x: x, y: y}; }
function getBezier(percent,startPos,endPos,control1,control2) {
if(!control2 && !control1) var control2 = new coord(startPos.x + 3*(endPos.x-startPos.x)/4, startPos.y + 3*(endPos.y-startPos.y)/4);
if(!control2) var control2 = control1;
if(!control1) var control1 = new coord(startPos.x + (endPos.x-startPos.x)/4, startPos.y + (endPos.y-startPos.y)/4);
var pos = new coord();
pos.x = startPos.x * B1(percent) + control1.x * B2(percent) + control2.x * B3(percent) + endPos.x * B4(percent);
pos.y = startPos.y * B1(percent) + control1.y * B2(percent) + control2.y * B3(percent) + endPos.y * B4(percent);
return pos;
}












/*****************
 Funciones clip
 *****************/

d=document;
var ie = (d.all)?1:0;
var dom = (d.getElementById)?1:0;
var pX = 0;
var pY = 0;
function eventoClipDespues(){d.getElementById('imgClip').style.display='none';} // oculta el clip
var jClip;

function identifica(clip){
jClip = new make("imgClip"); // Inicia el objeto clip que animaremos
d.getElementById('imgClip').style.display='block'; //  Muestra el clip
if(ie)eventoClip();if(dom && !ie)d.onclick=eventoClip;
}

function eventoClip(e) {
    if(ie){
        pX=event.clientX+d.body.scrollLeft;pY=event.clientY+document.documentElement["scrollTop"];
        if (window.navigator.appVersion.indexOf("MSIE 6")==-1) {  
            pX=event.clientX+d.body.scrollLeft;pY=event.clientY+d.body.scrollTop;
        }
    }
    else{pX=e.clientX;pY=e.clientY+document.documentElement["scrollTop"];} // Detecta posici?n del rat?n Moz
    jClip.moveTo(pX-12,pY-12); // Situa el clip en la posici?n inicial
    if(dom && !ie)document.onclick = null; // Para la detecci?n de la posici?n del rat?n para Moz
    //ddeX=d.getElementById('herramientasSpan').offsetLeft+((d.getElementById('herramientasSpan').offsetLeft>600)?128:100);ddeY=d.getElementById('herramientas').offsetTop; // Calcula d?nde est? la posici?n final: "Mis Clips"
    ddeX=d.getElementById('MisClips').offsetLeft;ddeY=d.getElementById('herramientas').offsetTop; // Calcula d?nde est? la posici?n final: "Mis Clips"
    if(document.all){
        obj=d.getElementById('MisClips');
        if (obj.offsetParent)
            while (obj.offsetParent) {
                ddeX+=obj.offsetLeft;
                obj=obj.offsetParent;
            }  
        ddeX=ddeX-70;
    }
    jClip.timeSlide(ddeX,ddeY,800,-1,"eventoClipDespues()"); // Desliza el clip hasta la posicion final: "Mis Clips"
    return true;
}

function remove(s, t) {
  /*
  **  Remove all occurrences of a token in a string
  **    s  string to be processed
  **    t  token to be removed
  **  returns new string
  */
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + remove(s.substring(i + t.length), t);
  return r;
}


function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600');");
}

function popUpComplete(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=800,height=600');");
}

function nl2br(str) {
   return str.replace(/\n/g,"<br>");
}

function trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function show(what) {
if (document.getElementById){
var tipos=document.getElementById(what);
tipos.style.display= "block";
}
}

function hide(what) {
if (document.getElementById){
var tipos=document.getElementById(what);
tipos.style.display= "none";
}
}

function showError(what) {
if (document.getElementById){
var tipos=document.getElementById(what);
tipos.className= "formerror";
}
}

function hideError(what) {
if (document.getElementById){
var tipos=document.getElementById(what);
tipos.className= "formok";
}
}

/*
**  Add engine to Firefox search plugins
*/
function addEngine(name,ext,cat,type)
{
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function")) {
    window.sidebar.addSearchEngine(
      "http://mycroft.mozdev.org/install.php/" + type + "/" + name + ".src",
      "http://mycroft.mozdev.org/install.php/" + type + "/" + name + "."+ ext, name, cat );
  } else {
    alert("You will need a Mozilla based browser to install a search plugin.");
  }
}

function checkEmailField() {
error = false;
if ((trim(document.emailstarred.email.value) == "") ||
(document.emailstarred.email.value.indexOf('@',0) == -1) || 
(document.emailstarred.email.value.indexOf('.',0) == -1) ||
(document.emailstarred.email.value.indexOf(' ',0) != -1))
{
error = true;
document.emailstarred.email.focus();
}
else
{
//hideError("email_frame");
}

if (error)
{
show("formemailerror");
return false;
}
else
{
hide("formemailerror");
return true;
}
}

function checkEmailField2() {
error = false;
if ((trim(document.getElementById('your_email').value) == "") ||
(document.getElementById('your_email').value.indexOf('@',0) == -1) || 
(document.getElementById('your_email').value.indexOf('.',0) == -1) ||
(document.getElementById('your_email').value.indexOf(' ',0) != -1))
{
error = true;
document.getElementById('your_email').focus();
}
else
{
//hideError("email_frame");
}

if (error)
{
show("formemailerror");
return false;
}
else
{
hide("formemailerror");
 submit_data('modules.php?act=contact_email', 'email&your_email&message&casa_id', 'sendi', false, false);
return true;
}
}

var mensaje_busqueda = "Debes introducir un texto de m&aacute;s de 3 caracteres.";

function checkSearchField() {
var objetoSPAN = document.getElementById("msg_txt");
if (trim(document.busca.q.value).length <  3)
{
objetoSPAN.innerHTML = mensaje_busqueda;
        objetoSPAN.className = 'masde3';
return false;
}
else {
return true;
}
}


function recalculatePrices(str_venta, str_alquiler) {    
    id_tipo = document.contact.tipo[document.contact.tipo.selectedIndex].value; 
    str = "";
    if (id_tipo == 1) {
        str = str_venta; 
    }
    else {
        str = str_alquiler;
    }
    
if (str != "") {
myarray = str.split("|");
document.contact.precio.length = myarray.length;
for (i=0; i < myarray.length; i++) 
{ 
elements = myarray[i].split("#"); 
id = elements[0];
nombre = elements[1];
document.contact.precio.options[i].value = id;
document.contact.precio.options[i].text = nombre;
} 
}
}

function checkAlertFields(cad_error1) {
error = false;
var objetoSPAN = document.getElementById("error_txt");
if (trim(document.contact.que.value) == "")
{
error = true;
//showError("pwd_frame");
document.contact.que.focus();
}
else
{
//hideError("pwd_frame");
}
if ((trim(document.contact.email.value) == "") ||
(document.contact.email.value.indexOf('@',0) == -1) || 
(document.contact.email.value.indexOf('.',0) == -1) ||
(document.contact.email.value.indexOf(' ',0) != -1))
{
error = true;
objetoSPAN.innerHTML += "<br>-" + cad_error1;
//showError("email_frame");
document.contact.email.focus();
}
else
{
//hideError("email_frame");
}

if (error)
{
show("formerror");
}
else
{
hide("formerror");
document.contact.submit();
}
}