
function ponerPoblacion(source, target, calle){

	var srcSelect = $(source);
	var provincia_id = parseInt(srcSelect.value);

	borrarSelect(calle);

	if(!provincia_id){
		borrarSelect(target);
		return;
	}

	var url = Orange.baseUrl + 'tienda/poblacion_json/'+provincia_id;
	
	Connect.asyncRequest(
			'GET',  url,
			{success: recibirPoblaciones,
			 argument: [target, calle]}
	);

	$('icono-cargando').style.display = "";

}

function recibirPoblaciones(o){
	var poblaciones;
	var target = $(o.argument[0]);

	borrarSelect(target);

	eval("poblaciones = " + o.responseText);

	for(var i=0; i < poblaciones['poblaciones'].length; i++){
		var option = document.createElement('option');
		option.text = poblaciones['poblaciones'][i].nombre;
		option.value = poblaciones['poblaciones'][i].poblacion_id;
		target.add(option, null);
	}

	$('icono-cargando').style.display = "none";

}

function borrarSelect(target){
	var tarSel = $(target);
	while(tarSel.options.length > 1){
		tarSel.remove(tarSel.options.length-1);
	}
}

function ponerCalle(source, target){

	var srcSelect = $(source);
	var poblacion_id = parseInt(srcSelect.value);
	
	if(!poblacion_id){
		borrarSelect(target);
		return;
	}

	var url = Orange.baseUrl + 'tienda/calle_json/'+poblacion_id;
	
	Connect.asyncRequest(
			'GET',  url,
			{success: recibirCalles,
			 argument: [target]}
	);

	$('icono-cargando-2').style.display = "";

}

function recibirCalles(o){
	var calles;
	var target = $(o.argument[0]);
	
	borrarSelect(target);

	eval("calles = " + o.responseText);

	for(var i=0; i < calles['calles'].length; i++){
		var option = document.createElement('option');
		option.text = calles['calles'][i].direccion_calle;
		option.value = calles['calles'][i].direccion_calle;
		target.add(option, null);
	}

	$('icono-cargando-2').style.display = "none";

}

var Scroll = function(el, percent, direction, size){
	this.init(el, percent, direction, size);
}
Scroll.HORIZONTAL = 0;
Scroll.VERTICAL = 1;

Scroll.prototype = {
	dd: null,
	el: null,
	initXY:0,

	init: function(el, percent, direction, size){
		
		this.el = el;

		var slider = document.createElement('div');
		slider.className = 'slider';
		slider.id = "slider";
		el.appendChild(slider);

		if(direction == Scroll.HORIZONTAL){
			slider.style.width = size*percent + "px";
		}
		else{
			slider.style.height = size*percent + "px";
		}
		
		this.dd = new YAHOO.util.DD(slider);
		this.initConstraints(direction);
		
		
	},

	initConstraints: function(dir){

		//Get the top, right, bottom and left positions
		var region = Dom.getRegion(this.el);

		//Get the element we are working on
		var el = this.dd.getEl();

		//Get the xy position of it
		var xy = Dom.getXY(el);

		//Get the width and height
		var width = parseInt(Dom.getStyle(el, 'width'), 10);
		var height = parseInt(Dom.getStyle(el, 'height'), 10);

		//Set left to x minus left
		var left = xy[0] - region.left;

		//Set right to right minus x minus width
		var right = region.right - xy[0] - width;

		//Set top to y minus top
		var top = xy[1] - region.top;

		//Set bottom to bottom minus y minus height
		var bottom = region.bottom - xy[1] - height;

		if(dir == Scroll.HORIZONTAL){
			//Set the constraints based on the above calculations
			this.dd.setXConstraint(left, right);
			this.dd.setYConstraint(0, 0);
		}
		else{
			//Set the constraints based on the above calculations
			this.dd.setXConstraint(0, 0);
			this.dd.setYConstraint(top, bottom);
		}

		this.initXY = xy;

	}


}

Event.addListener(window, 'load', function(){
	new Pasarela('pasarela');
	new CajaTexto('texto-quienes-somos');
});

var Pasarela = function(id){
	this.init(id);
}

Pasarela.ITEM_WIDTH = 162;


Pasarela.prototype = {

	scroll: null,
	ul:null,
	percent: 0,
	count:0,

	init: function(id){
		var pasarela = document.getElementById(id);
		
		if(!pasarela){
			return;
		}

		var scrollEl = Selector.query('.desplazamiento-horizontal')[0];

		this.count = Selector.query('#'+id+' LI').length;
		if(this.count > 4){
			this.percent = 4/this.count;
		}
		else{
			scrollEl.style.display = "none";
		}
		
		this.scroll = new Scroll(scrollEl, this.percent, Scroll.HORIZONTAL, Pasarela.ITEM_WIDTH*4);
		this.scroll.dd.on('dragEvent',this.onDrag, this, true);
		
		this.ul = Selector.query('UL', id, true);

	},
	
	onDrag: function(e, ul){
		
		var xy = Dom.getXY(this.scroll.dd.getEl());
		var left = xy[0] - this.scroll.initXY[0];
		var totalWidth = this.scroll.el.offsetWidth;
		var maxLeft = totalWidth*(1-this.percent);
		var percent = left/maxLeft;
		var hiddenPart =  Pasarela.ITEM_WIDTH*(this.count - 4)+this.count/2;
		var offset = -hiddenPart*percent ;

		this.ul.style.left = offset + "px";

	}

}


var CajaTexto = function(id){
	this.init(id);
}

CajaTexto.HEIGHT = 245;

CajaTexto.prototype = {

	scroll: null,
	box:null,
	percent: 0,
	height:0,

	init: function(id){
		var caja = document.getElementById('texto-scroll');

		if(!caja){
			return;
		}

		var scrollEl = Selector.query('.desplazamiento-vertical')[0];

		this.height = caja.offsetHeight;
		
		if(this.height > CajaTexto.HEIGHT ){
			this.percent = CajaTexto.HEIGHT/this.height;
		}
		else{
			scrollEl.style.display = "none";
		}

		this.scroll = new Scroll(scrollEl, this.percent, Scroll.VERTICAL, CajaTexto.HEIGHT);
		this.scroll.dd.on('dragEvent',this.onDrag, this, true);

		this.box = document.getElementById('texto-scroll');

	},

	onDrag: function(e, ul){

		var xy = Dom.getXY(this.scroll.dd.getEl());
		var top = xy[1] - this.scroll.initXY[1];
		var totalHeight = this.scroll.el.offsetHeight;
		
		var maxTop = totalHeight*(1-this.percent);
		var percent = top/maxTop;
		
		var hiddenPart =  this.height - CajaTexto.HEIGHT;
		
		var offset = -hiddenPart*percent;

		this.box.style.top = offset + "px";

	}

}

Orange.Validator.Error.prototype =  {
	show: function(inputName, message, formId){
		var element = document.getElementById("error_message_"+inputName);
		if(!element)return;
		element.innerHTML = message;
		element.style.display = "";
		var form = document.getElementById(formId);

		var input = form[inputName];
		var xy = Dom.getXY(input);
		
		if(Orange.Array.is(xy[0])){
			input = $('form_input_'+inputName);
			xy = Dom.getXY(input);
		}

		if(input.getAttribute('type') == 'checkbox'){
			xy[0] +=290;
		}
		
		xy[0] += input.offsetWidth + 2;
		xy[1] += 1;
		Dom.setXY(element, xy);
	},

	hide: function(inputName){
		var element = document.getElementById("error_message_"+inputName);
		if(!element)return;
		element.style.display = "none";
	}
}


function mostrarFotoTienda(el){

	var idx = $(el).getAttribute('data-index');
	slideshow.changePicture(idx);
	
}

function mostrarCampoBoletin(){
	$('mailing-form').style.display = "block";
}

function vaciarCampoBoletin(campo){
	campo.value = "";
	campo.onclick = null;
}



function mostrarShadowbox(index){
	
	var link = Selector.query('A.paso_'+index, 'galerias', true);
	
	Shadowbox.open(
		link
	);

}

function mostrarCondiciones(e){
	Shadowbox.open({
        content:    $('condiciones').innerHTML,
        player:     "html",
        title:      "Declaración de derechos y responsabilidades",
        height:     400,
        width:      600
    });

}