
function stich(eventName, jsController, htmlElement, htmlElement_id){
    return function(ev){
        ev = ev || window.event
        jsController[eventName](ev, htmlElement, htmlElement_id)
    }
}

function PanelDeslizante(htmlElement_id, posicionInicial, posicionFinal, mode){
    this.that = this
    var that = this
    this.posicionInicial = posicionInicial
    this.posicionFinal = posicionFinal
    this.mode = mode || "absolute"
    this.moving = false
    this.dentro = true
    this.stopMoving = function(){
        that.moving = false
    }
    this.end_saliendo = function(){
        that.moving = false
        that.dentro = false
    }
    this.end_entrando = function(){
        that.moving = false
        that.dentro = true
    }
    this.panelOculto = null
    htmlElement = $(htmlElement_id)
    htmlElement.onmouseover = stich("onmouseover", this, htmlElement, htmlElement_id)
    htmlElement.onmouseout  = stich("onmouseout", this, htmlElement)
	htmlElement.onclick     = stich("onclick", this, htmlElement)
}


PanelDeslizante.prototype.onmouseover = function(event, htmlElement, htmlElement_id){
    /*
	var that = this
    var src = (window.event) ? (event.srcElement) : (event.target)
    
    if (!this.moving && event.clientX > 1000 && this.dentro) {
        this.moving = true
        new Effect.Move(htmlElement, {
            x: this.posicionInicial.x,
            y: this.posicionInicial.y,
            mode: this.mode,
            afterFinish: that.end_saliendo
        })
        this.panelOculto = $('pastillaVista') || $('plantillaFicha') || $('plantillaLista')
        this.panelOculto && this.panelOculto.fade();
        this.dentro = false
    }
    */
}

PanelDeslizante.prototype.onclick = function(event, htmlElement, htmlElement_id){
    var that = this
    var src = (window.event) ? (event.srcElement) : (event.target)
    
    if (this.dentro) {
		this.moving = true
		new Effect.Move(htmlElement, {
			x: this.posicionInicial.x,
			y: this.posicionInicial.y,
			mode: this.mode,
			afterFinish: that.end_saliendo
		})
		this.panelOculto = $('pastillaVista') || $('plantillaFicha') || $('plantillaLista')
		this.panelOculto && this.panelOculto.fade();
		$('containerInicio') && $('containerInicio').fade()
		this.dentro = false
	}
}


PanelDeslizante.prototype.onmouseout = function(event, htmlElement, htmlElement_id){
    var that = this
    var src = (window.event) ? (event.srcElement) : (event.target)
    var left = htmlElement.style.left
    left = left.replace(/px/, "")
    if (!this.dentro && !this.moving &&
    (event.clientX < parseInt(totalX($('carritoDesp1'))) ||
    event.clientY < parseInt(totalY($('carritoDesp1'))) ||
    ((event.clientX < parseInt(totalX($('carritoDesp1'))) + $('carritoDesp1').offsetWidth) && 
	 (event.clientY > parseInt(totalY($('carritoDesp3'))) + $('carritoDesp1').offsetHeight))) &&
    (left * 1 < 300)) {
    
        new Effect.Move(htmlElement, {
            x: this.posicionFinal.x,
            y: this.posicionFinal.y,
            mode: this.mode,
            afterFinish: that.end_entrando
        })
        this.panelOculto && this.panelOculto.appear();
		$('containerInicio') && $('containerInicio').appear()
        this.dentro = true
        this.moving = true
    }
    
}

function Pos(x, x0){
    this.x = x || 0;
    this.time = new Date()
    this.x0 = x0 || this.x
    this.time0 = this.time - 1000
    this.x1 = this.x0
    this.time1 = this.time0 - 1000
}

Pos.prototype.setX = function(x){
    // Roll, baby, roll. All night long
    this.x1 = this.x0
    this.time1 = this.time0
    this.x0 = this.x
    this.time0 = this.time
    this.x = x
    this.time = new Date()
}

function getXTotal(elementoHTML){
    var xt = 100
    var elemento = elementoHTML
    do {
        xt += elemento.offsetLeft
    }
    while (elemento = elemento.offsetParent);
    return xt
}

function FlechaMenu(elementoHTML, xDest, limites, longitudContenedor, compensacionImagen, k, m, c){
    this.k = k || 500
    this.m = m || 7
    this.c = c || 0.00000001
    this.elementoHTML = $(elementoHTML)
    this.compensacionRaton = getXTotal(this.elementoHTML)
    this.posicionFija = xDest - (compensacionImagen + 100 || 0)
	if (elementoHTML == 'flecha1') {
		marcador = document.getElementById('marcadorMenu').parentNode
		this.posicionFija += marcador.offsetLeft + (marcador.offsetWidth / 2) - 27
	}
	if (elementoHTML == 'flecha2') {
		marcador = document.getElementById('marcadorSubmenu').parentNode
		this.posicionFija += marcador.offsetLeft  + (marcador.offsetWidth / 2) - 47
	}
    this.limites = limites
    this.longitudContenedor = longitudContenedor
    this.destino = this.posicionFija
    this.pos = new Pos(xDest)
    this._moverFlecha = _moverFlecha(this)
	
    this.activate()
}

FlechaMenu.prototype.activate = function(){
    this.posicionar()
    this.moverFlecha()
}

FlechaMenu.prototype.restaurarDestino = function(event){
    var src = (window.event) ? (event.srcElement) : (event.target)
    var retornar = false;
    for (var i = 0; i < this.limites.length; i++) 
        if (src.id == this.limites[i]) 
            retornar = true
    if (retornar) 
        this.destino = this.posicionFija
}
FlechaMenu.prototype.posicionar = function(){
    this.elementoHTML.style.left = (this.pos.x - this.longitudContenedor / 2) + "px"
}

FlechaMenu.prototype.setDestino = function(event){
    event = event || window.event
    this.destino = event.clientX - this.compensacionRaton
    this.moverFlecha()
}

function _moverFlecha(object){
    return function(){
        object.moverFlecha();
    }
}

FlechaMenu.prototype.moverFlecha = function(){
    var dt = (this.pos.time - this.pos.time0) / 1000
    if (dt == 0) 
        dt = 0.01
    var muelle = this.k * (this.destino - this.pos.x)
    var c = this.c * (this.pos.x0 - this.pos.x1) * (this.pos.x0 - this.pos.x1) / dt / dt
    if (Math.abs(c) > Math.abs(muelle)) 
        if (c * muelle > 0) 
            c = muelle
        else 
            c = -muelle
    
    var a = (muelle + (muelle * c < 0 ? 1 : -1) * c) / this.m
    var v = a * dt + (this.pos.x0 - this.pos.x1) * dt
    this.pos.setX(this.pos.x + v * dt)
    this.posicionar()
    setTimeout(this._moverFlecha, 100)
}

function setXFlechaMenu1(evento){
    evento = evento || window.event
    xMenu1 = evento.clientX
}

function changeColor(element){
    var name = element.className
	var tam = name.substr(16,2)
    if (name.substr(18) == "pendiente"){
		element.className = "ParticipacionCol" + tam + "reservada"
		element.style.backgroundColor = '#b8b8b8'
	}else{
		element.className = "ParticipacionCol" + tam + "pendiente"
		element.style.backgroundColor = '#686868'
	}
}


function cortarMouseout(e){
    if (e && e.stopPropagation) 
        e.stopPropagation()
    else 
        window.event.cancelBubble = true
    src = e.target || e.srcElement
}

function checkcolateral(event){
    event = event || window.event
    var src = (window.event) ? (event.srcElement) : (event.target)
    if (!panelDerecho.dentro && (src.id == "carritoDesp1" || src.id == "carritoDesp2" || src.id == "carritoDesp3")) 
        if (event.stopPropagation) 
            event.stopPropagation()
        else 
            window.event.cancelBubble = true
}

function totalX(elemento){
    if (!elemento.offsetParent) 
        return 0;
    return elemento.offsetLeft + totalX(elemento.offsetParent)
}

function totalY(elemento){
    if (!elemento.offsetParent) 
        return 0;
    return elemento.offsetTop + totalY(elemento.offsetParent)
}

function getNumber(cadena){
    return 1 * cadena.replace(/px/, "")
}

function Ventana(htmlElement_id){
    var that = this
    this.dragging = false
    
    htmlElement = $(htmlElement_id)
    this.x = getNumber(htmlElement.style.left)
    this.y = getNumber(htmlElement.style.top)
    htmlElement.onmousedown = stich("onmousedown", this, htmlElement, htmlElement_id)
    htmlElement.onmousemove = stich("onmousemove", this, htmlElement, htmlElement_id)
    htmlElement.onmouseup = stich("onmouseup", this, htmlElement, htmlElement_id)
}

Ventana.prototype.onmousedown = function(event, htmlElement, htmlElement_id){
    this.dragging = true
    this.x = getNumber(htmlElement.style.left)
    this.y = getNumber(htmlElement.style.top)
    this.ratonX = event.screenX
    this.ratonY = event.screenY
}

Ventana.prototype.onmousemove = function(event, htmlElement, htmlElement_id){
    if (this.dragging) {
        htmlElement.style.left = (event.screenX - this.ratonX + this.x) + "px"
        htmlElement.style.top = (event.screenY - this.ratonY + this.y) + "px"
    }
}

Ventana.prototype.onmouseup = function(event, htmlElement, htmlElement_id){
    this.dragging = false
}


function enviarPosicion(ventana, cajaTop, cajaLeft, formulario){
	$(cajaTop).value  = ventana.y
	$(cajaLeft).value = ventana.x
	$(formulario).onsubmit()
}

function logueado(){
	$('containerLogin').style.display = "none"
	
}

function reloguear(){

}
