YAHOO.util.Event.onDOMReady(function () {
	$('qsmenu').setStyle({display: 'block'});
	$('serviciosmenu').setStyle({display: 'block'});
	
	var oMenu = new YAHOO.widget.Menu("navbar", {  
		position: "static",
		preventcontextoverlap: false,
		submenuhidedelay: 750,
		autosubmenudisplay: true,  
		lazyload: false,
		submenualignment: ['tl','bl']
	});
	
	oMenu.render();
   
   if (YAHOO.env.ua.gecko && YAHOO.env.ua.gecko < 1.9 && YAHOO.widget.Module.prototype.platform == "mac") 
   {
   		YAHOO.util.Dom.addClass(oMenuBar.element, "hide-submenu-shadow");
   }
}); 

var Productos = Class.create({
	initialize: function(mainsrc) {
		this.mainsrc = mainsrc;

		this.isEspecVisible = false;
		this.isGaleriaVisible = false;
		this.isBtnEnabled = true;
		
		this.espec = $('especificaciones');
		this.galeria = $('galeria');
		this.barra = $('barra');
		this.menu = $('menu');
		this.contenido = $('contenido');

		this._int = null;
		this._int_galeria = null;
		this._galHandEffect = null;
		this._barHandEffect = null;
		
		this.isMenuHidden = false;
		this.lastBg = null;
		
		this.setup();
	},
	setup: function() {
		this.menu.show();
		this.espec.hide();
		this.galeria.hide();
		$('especificaciones_btn').observe('click', function(ev) {ev.stop();});
		$('galeria_btn').observe('click', function(ev) {ev.stop();});
		this.enableBtns();

		this.sp = new SlidePanels('galeria_slider', {pager: 'galeria_pager'});
		//this.refreshPosition();
		//Event.observe(document.onresize ? document : window, 'resize', this.refreshPosition.bind(this));
		$('left_btn').observe('click', function(ev) { ev.stop(); this.sp.previous(); this._galeria_remove_handler(); }.bind(this));
		$('right_btn').observe('click', function(ev) { ev.stop(); this.sp.next(); this._galeria_remove_handler(); }.bind(this));
		
		$$('.thumb').each(function(e,k) {
			e.observe('click', function(ev) {
				this.setBg(ev.findElement().alt);
			}.bind(this));	
		}.bind(this));
		
		if (this.mainsrc != '') {
			var _int_check = setInterval(function() {
				if (this.setBg(this.mainsrc)) {
					clearInterval(_int_check);
				}
			}.bind(this),'1000');
		}
		
		this._int_menu = null;
		$('productos_btn').observe('mouseover', function(ev) {
			$('productos_menu').show();
			setSIFR();
		});
		$('qs_btn').observe('mouseover', function(ev) {
			$('productos_menu').hide();
		});
		$('postventa_btn').observe('mouseover', function(ev) {
			$('productos_menu').hide();
		});
		$('cerrar_btn').observe('click', function(ev) {
			ev.stop();
			$('productos_menu').hide();
//			$('productos_menu').observe('mouseout', function(ev) {
//				$('productos_menu').hide();
//			});
		});
	},
	refreshPosition: function(ev) {
		this.barra.setStyle({top: (document.viewport.getHeight()-140)+'px'});
	},
	setBg: function(src) {
		if (this.lastBg == src || typeof $('galeria_bg').loadImage != 'function') {
			return;
		}
		this.lastBg = src;
		return $('galeria_bg').loadImage(unescape(src));
	},
	showEspec: function(ev) {
		if (this.isEspecVisible || this.isGaleriaAnim) { return; }
		this.isEspecVisible = true;
		this.disableBtns();
		
		this.setBg(this.mainsrc);
		
		new Effect.Parallel([
			new Effect.Appear(this.espec, {transition: Effect.Transitions.spring}),
			new Effect.Tween(this.espec, 130, 120, {transition: Effect.Transitions.spring}, function(p){this.espec.setStyle({top: p+'px'});}.bind(this))
		], {afterFinish: function(ev){
			this.enableBtns();
			setSIFR();
		}.bind(this)});
		//this.espec.show();
		
		$('ver_productos').stopObserving('mousemove');
		$('ver_productos').stopObserving('click');
		this._galeria_remove_handler();

		if (this.isGaleriaVisible) {
			this.galeria.hide();
			this.isGaleriaVisible = false;
		}
	},
	showGaleria: function(ev) {
		if (this.isGaleriaVisible || this.isEspecAnim) { return; }
		this.isGaleriaVisible = true;
		this.disableBtns();

		$('ver_productos').observe('mousemove', this._galeria_move_handler.bind(this));
		$('ver_productos').observe('click', this._galeria_move_handler.bind(this));
		
		new Effect.Parallel([
			new Effect.Appear(this.galeria, {transition: Effect.Transitions.spring}),
			new Effect.Tween(this.galeria, 400, 390, {transition: Effect.Transitions.spring}, function(p){this.espec.setStyle({top: p+'px'});}.bind(this))
		], {afterFinish: function(ev){this.enableBtns();}.bind(this)});
		
		if (this.isEspecVisible) {			
			this.espec.hide();
			this.isEspecVisible = false;
		}
	},
	_galeria_move_handler: function(ev) { 
		clearInterval(this._int_galeria);
		this.menu.show();
		this.menu.setOpacity(1);
		this.isMenuHidden = false;

		this._int_galeria = setTimeout(function() {
			this.isMenuHidden = true;
			new Effect.Fade(this.menu, {duration: 0.2});
		}.bind(this), 1500);
	},
	_galeria_remove_handler: function(ev) {
		clearInterval(this._int_galeria);
		this.isMenuHidden = false;
		this.menu.setOpacity(1);
		this.menu.show();
	},
	disableBtns: function() {
		$('especificaciones_btn').stopObserving('click', this.showEspec.bind(this));
		$('galeria_btn').stopObserving('click', this.showGaleria.bind(this));
	},
	enableBtns: function() {
		$('especificaciones_btn').observe('click', this.showEspec.bind(this));
		$('galeria_btn').observe('click', this.showGaleria.bind(this));
	},
	getPanelsDimensions: function() {
		var dims = {width:0,height:0};
		var d = {};
		this.panels.childElements().each(function(e) {
			d = e.down().getDimensions();
			dims.width += d.width;
		});
		dims.height = d.height;
		return dims;
	},
	doSliding: function(ev, dir) {
		ev.stop();
		var e = ev.findElement();
		if (this._int) {
			clearInterval(this._int);
		}
		var dim = this.getPanelsDimensions();
		var rLimit = dim.width;
		this._int = setInterval(function() {
			var pos = this.panels.positionedOffset();

			if ((dir == 1 && pos[0] >= 0) || (dir == -1 && pos[0] <= -(dim.width-1154))) {
				return false;
			}
			var x = (pos[0]) + (4 * dir);
			this.panels.setStyle({left: x+'px'});
		}.bind(this), 20);
	},
	stopSliding: function(ev) {
		ev.stop();
		clearInterval(this._int);
	}
});
var SelectorMarcas = Class.create ({
	initialize: function() {
		this.el = $('intro_marcas');
		this.panels = $('panels');
		this._int = null;
		this.setup();
	}, 
	setup: function() {
		$$('.c').each(function(e,k) {
			e.observe('mouseover', function(ev) {
				this.down('.over').setStyle({left: 0+'px'});
				this.down('.up').setStyle({'background-position': 'bottom center'});
			});
			e.observe('mouseout', function(ev) {
				this.down('.over').setStyle({left: 190+'px'});
				this.down('.up').setStyle({'background-position': 'center center'});
			});
		});
		
		this.sp = new SlidePanels('productos_slider');
		$('left_btn').observe('click', function(ev) { 
			ev.stop(); this.sp.previous(); 
		}.bind(this));
		$('right_btn').observe('click', function(ev) { 
			ev.stop(); this.sp.next(); 
		}.bind(this));
		
		setSIFR();
		
		Shadowbox.init();
		
		//$('left_btn').observe('click', this.doSliding.bindAsEventListener(this,1));
		//$('right_btn').observe('click', this.doSliding.bindAsEventListener(this,-1));
		//$('left_btn').observe('mouseover', this.doSliding.bindAsEventListener(this,1));
		//$('right_btn').observe('mouseover', this.doSliding.bindAsEventListener(this,-1));
		//$('left_btn').observe('mouseup', this.stopSliding.bind(this));
		//$('right_btn').observe('mouseup', this.stopSliding.bind(this));
		//$('left_btn').observe('mouseout', this.stopSliding.bind(this));
		//$('right_btn').observe('mouseout', this.stopSliding.bind(this));
	},
	getPanelsDimensions: function() {
		var dims = {width:0,height:0};
		var d = {};
		this.panels.childElements().each(function(e) {
			d = e.down().getDimensions();
			dims.width += d.width;
		});
		dims.height = d.height;
		return dims;
	},
	doSliding: function(ev, dir) {
		ev.stop();
		var e = ev.findElement();
		if (this._int) {
			clearInterval(this._int);
		}
		var dim = this.getPanelsDimensions();
		var rLimit = dim.width;
		this._int = setInterval(function() {
			var pos = this.panels.positionedOffset();

			if ((dir == 1 && pos[0] >= 0) || (dir == -1 && pos[0] <= -(dim.width-1154))) {
				return false;
			}
			var x = (pos[0]) + (4 * dir);

			this.panels.setStyle({left: x+'px'});
		}.bind(this), 20);

	},
	stopSliding: function(ev) {
		ev.stop();
		clearInterval(this._int);
	}
});

var SlidePanels = Class.create({
	initialize: function(el, options) {
		if (options == undefined) {
			options = {};
		}

		this.pager = null;
		this.defaultPanel = 0;
		this.numerate = false;

		if (options.pager != undefined)
			this.pager = options.pager;
		if (options.effect != undefined) {
			this.setEffect(options.effect);
		} else {
			this.setEffect('slide');
		}
		if (options.defaultPanel != undefined)
			this.defaultPanel = options.defaultPanel;
		if (options.numerate != undefined)
			this.numerate = options.numerate;
			
		this.el = $(el);

		this.i = 0;
		this.nPanels = null;
		this.panel = null;
		this.oldPanel = null;
		this.panels = null;

		this._locked = false;
		this.setup();
	},
	setup: function() {
		this.panels = this.el.down().childElements().each(function(e,k) {
			e.index = k;
		});
		this.nPanels = this.panels.length;
		this.panel = this.panels[this.i = 0];
		
		this.drawPager();
	},
	setEffect: function(effect) {
		if (effect instanceof Function) {
			this.effect = effect.bind(this);
		} else {
			switch(effect) {
				case 'fade':
					this.effect = this.fadeEffect;
					break;
				case 'slide':
				default:
					this.effect = this.slideEffect;
					break;
				
			}
		}
		return true;
	},
	drawPager: function() {
		if ($(this.pager) == undefined || this.nPanels <= 1) {
			return;
		}

		var txt = null;
		var el = new Element('ul');
		for (var i = 0; i < this.nPanels; i++) {
			if (this.numerate) {
				txt = i+1;
			}
			el.insert(new Element('li').insert(new Element('a', {
				href: '#'
			}).observe('click',
				this.goToPanelHandler.bindAsEventListener(this, i)
			).update(txt)));

		}
		$(this.pager).insert(el);
		this._updatePager();
	},
	_update: function(i, go, useEffect) {
		this._updatePager();
		if (useEffect == true || useEffect == undefined) { 
			this.effect(this.panel, this.oldPanel, i, go);
		}
	},
	_updatePager: function() {
		if ($(this.pager) == undefined || $(this.pager).down('ul').down('li',this.i) == undefined) {
			return;
		}
		$(this.pager).down('ul').childElements().each(function(e,k) {
			e.className = '';
		});
		$(this.pager).down('ul').down('li',this.i).className = 'sel';
	},
	_setPanel: function(i, go, useEffect) {
		if (this.nPanels <= 1) {
			this.i = 0;
			return;
		}
	
		if (this.panel == null) {
			this.panel = this.panels[0];
		}
		this.oldPanel = this.panel;
		
		if (!this._locked) {
			/*
			if (i >= this.nPanels || i < 0) {
				this.i = 0;
			} else {
				this.i = i;
			}*/
			if (i < 0) i = this.nPanels - 1;
			this.i = i % this.nPanels;
		}
		this.panel = this.panels[this.i];
		
		this._update(i, go, true);
	},
	goToPanelHandler: function(ev, i) {
		ev.stop();
		if (this._locked == false) {
			this.goToPanel(i);
		}
	},
	goToPanel: function(p, useEffect) {
		this._setPanel(p, 'goto', true);
	},
	next: function() {
		this._setPanel(this.i+1, 'next', true);
	},
	previous: function() {
		this._setPanel(this.i-1, 'previous', true);
	},
	first: function() {
		this._setPanel(0, 'first', true);
	},
	last: function() {
		this._setPanel(this.nPanels-1, 'last', true);
	},
	isLastPanel: function() {
		return ((this.i+1) === this.nPanels);
	},
	isFirstPanel: function() {
		return (this.i === 0);
	},
	timer: function(time, funcName) {
		if (this[funcName] == undefined || this.nPanels <= 1) {
			return false;
		}
		var func = null;
		switch(funcName) {
			case 'next':
			case 'previous':
			case 'first':
			case 'last':
				func = this[funcName];
				break;
		}
		this._interval = setInterval(func.bind(this), time);
		return true;
	},
	stopTimer: function() {
		clearInterval(this._interval);
	},
	fadeEffect: function(panel, oldPanel, index, go) {
		for(var i = 0; i < this.nPanels; i++) {
			this.panels[i].hide();	
		}
		
		if (go != 'goto') {
			new Effect.Fade(oldPanel.show(), {
				queue: 'start',
				beforeStart: function() {this._locked = true;}.bind(this)
			});
			new Effect.Appear(panel.hide(), {
				queue: 'end',
				afterFinish: function() {this._locked = false;}.bind(this)
			});
		} else {
			oldPanel.hide();
			panel.show();
		}
	},
	slideEffect: function(panel, oldPanel, index, go) {
		/*
		for(var i = 0; i < this.nPanels; i++) {
			this.panels[i].hide();	
		}*/
		
		if (panel == undefined) {
			return;
		}
		
		var oldIdx = oldPanel.index;
		var newIdx = panel.index;
		if (oldIdx == newIdx) {
			return;
		}
		var pos = this.panel.positionedOffset();		
		var slideX = 0;
		var slideY = 0;
		if (oldIdx < newIdx) {
			var startI = oldIdx;
			var endI = newIdx;
			var dir = -1;
		} else {
			var startI = newIdx;
			var endI = oldIdx;
			var dir = 1;
		}
		for(var i = startI; i < endI; i++) {
			slideX += this.panels[i].getWidth();
		}
		slideX *= dir;
		var finalPos = pos[0] + slideX;
		this._locked = true;
		new Effect.Move(this.el.down(), {x: slideX, y: slideY, queue: 'start',
			afterFinish: function() {
				this._locked = false;
				//this.el.down().setStyle({left: slideX+'px'});
			}.bind(this)
		});
	}
});


function setSIFR() {
	//if(typeof sIFR == "function") {
		/*
		sIFR.replace(gillsansmt, {
		  selector: 'h1.sifr',
		  css: '.sIFR-root { color: #000000; }',
		  wmode: 'transparent'
		});*/
		sIFR.replace(gillsansmt, {
		  selector: 'h1.sifr',
		  css: '.sIFR-root { color: #000000; font-weight: bold }',
		  wmode: 'transparent'
		});

		sIFR.replace(gillsans, {
		  selector: 'h2.sifr',
		  css: '.sIFR-root { color: #000000; font-weight: bold }',
		  wmode: 'transparent'
		});
		sIFR.replace(gillsans, {
		  selector: 'h2.sifr-light',
		  css: '.sIFR-root { color: #FFD300; font-weight: bold }',
		  wmode: 'transparent'
		});
		sIFR.replace(gillsans, {
		  selector: 'h3.sifr',
		  css: '.sIFR-root { font-size: 13pt; color: #000000; font-weight: bold; text-align: center; }',
		  wmode: 'transparent'
		});
		sIFR.replace(gillsans, {
		  selector: 'h3.sifr-light',
		  css: '.sIFR-root { color: #FFFFFF; font-weight: bold }',
		  wmode: 'transparent'
		});
		sIFR.replace(gillsans, {
		  selector: 'h3.sifr-light2',
		  css: '.sIFR-root { color: #F8FF00; font-weight: bold }',
		  wmode: 'transparent'
		});

	//};
}

