var $chk = function(obj){return !!(obj || obj === 0);};

window.addEvent('domready', function(){
	if(window.getSize()['y'] > 768) $('container').setStyle('margin-top', (window.getSize()['y'] - 768) / 2);
	if($chk($('home'))){
		$$('#header, #footer, #logo').setStyle('opacity', 0);
		$$('#header, #footer, #logo').setStyle('display', 'block');
		$('logo').set('tween', {
			onComplete: function(){
				$$('#header, #footer').fade(1);
			}
		});
		$('bg-foto').set('tween', {
			duration: 2000,
			onComplete: function(){
				$('logo').fade(1);
			}
		});
		var load = new Asset.images(["imagenes/bg-home.jpg"], {
			onComplete: function(){
				$('bg-foto').tween('left', 0);
			}
		});
	}else if($chk($('empresa'))){
		$$('.smenu')[0].setStyle('margin-left', $$('#menu .sel')[0].getPosition($('container'))['x']);
		$$('.smenu.fade, #cont, #texto, #cont h1').setStyle('opacity', 0);
		$$('.smenu.fade, #cont, #texto, #cont h1').setStyle('display', 'block');
		$('cont').set('tween', {
			onComplete: function(){
				if($chk($('foto'))){
					var load = new Asset.images([$$('#foto img')[0].get('src')], {
						onComplete: function(){
							$$('#foto img')[0].tween('margin-left', 0);
						}
					});
				}else{
					$$('#cont h1')[0].fade(1);
				}
			}
		});
		if($chk($$('#cont h1')[0])){
			$$('#cont h1')[0].set('tween', {
				onComplete: function(){
					stopped = true;
				}
			});
		}
		if($chk($('foto'))){
			$$('#foto img')[0].set('tween', {
				duration: 1000,
				onComplete: function(){
					$('texto').fade(1);
					$$('#cont h1')[0].fade(1);
				}
			});
		}
		$('cont').fade(1);
		$$('.smenu.fade').fade(1);
		if($chk($('prev'))){
			var curr = 0;
			var stopped = false;
			var arr_titles = $$('#cont h1');
			var arr_fotos = $$('#foto img');
			$$('#cont h1 img.hidden').setStyle('opacity', 0);
			$('next').addEvent('click', function(ev){
				ev.stop();
				if(arr_fotos[curr+1] != undefined && stopped){
					stopped = false;
					arr_fotos[curr].addClass('hidden');
					arr_titles[curr].addClass('hidden');
					arr_titles[curr].setStyle('opacity', 0);
					arr_fotos[curr].setStyle('margin-left', 665);
					arr_titles[curr+1].set('tween', {
						onComplete: function(){
							stopped = true;
						}
					});
					arr_fotos[curr+1].set('tween', {
						duration: 1000,
						onComplete: function(){
							arr_titles[curr].removeClass('hidden');
							arr_titles[curr].fade(1);
						}
					});
					arr_fotos[curr+1].removeClass('hidden');
					var load = new Asset.images([arr_fotos[curr+1].get('src')], {
						onComplete: function(){
							arr_fotos[curr+1].tween('margin-left', 0);
							curr++;
						}
					});
					
					$('lupa-pro').set('href', arr_fotos[curr+1].get('src').replace("-mini", ""));
				}
			});
			$('prev').addEvent('click', function(ev){
				ev.stop();
				if(curr > 0 && stopped){
					stopped = false;
					arr_fotos[curr].addClass('hidden');
					arr_titles[curr].addClass('hidden');
					arr_titles[curr].setStyle('opacity', 0);
					arr_fotos[curr].setStyle('margin-left', 665);
					arr_titles[curr-1].set('tween', {
						onComplete: function(){
							stopped = true;
						}
					});
					arr_fotos[curr-1].set('tween', {
						duration: 1000,
						onComplete: function(){
							arr_titles[curr].removeClass('hidden');
							arr_titles[curr].fade(1);
						}
					});
					arr_fotos[curr-1].removeClass('hidden');
					var load = new Asset.images([arr_fotos[curr-1].get('src')], {
						onComplete: function(){
							arr_fotos[curr-1].tween('margin-left', 0);
							curr--;
						}
					});
					$('lupa-pro').set('href', arr_fotos[curr-1].get('src').replace("-mini", ""));
				}
			});
		}
	}else{
		if($chk($$('.smenu')[0])) $$('.smenu')[0].setStyle('margin-left', $$('#menu .sel')[0].getPosition($('container'))['x']);
		$('bg-foto').setStyle('opacity', 0);
		$('bg-foto').setStyle('display', 'inline-block');
		if($('cont')){
			$('cont').setStyle('opacity', 0);
			$('cont').setStyle('display', 'block');
		}
		$$('.smenu.fade, #cont, #texto, #cont h1').setStyle('opacity', 0);
		$$('.smenu.fade, #cont, #texto, #cont h1').setStyle('display', 'block');
		if($('cont')){
			$('cont').set('tween', {
				onComplete: function(){
					if($chk($$('#cont h1')[0])){
						if($chk($('inner'))){
							$('inner').set('tween', {
								duration: 1000,
								onComplete: function(){
									$$('#cont h1')[0].fade(1);
									if($chk($('texto'))) $('texto').fade(1);
								}
							});
							$('inner').tween('margin-left', 0);
						}else{
							if($chk($('texto'))) $('texto').fade(1);
							$$('#cont h1')[0].fade(1);
						}
					}
				}
			});
		}
		$('bg-foto').set('tween', {
			onComplete: function(){
				if($('cont')) $('cont').fade(1);
				else $$('.smenu.fade').fade(1);
			}
		});
		var load = new Asset.images([$('bg-foto').get('src')], {
			onComplete: function(){
				$('bg-foto').fade(1);
			}
		});
		$$('.smenu.fade').fade(1);
	}
	if($chk($('cont-pro'))){
		$$('#cont-pro-text span').setStyle('opacity', 0);
		$$('#cont-pro-text span').setStyle('display', 'block');
		$$('#cont-pro img').each(function(el){
			el.set('tween', {
				duration: 200,
				onComplete: function(){
					if(el.getStyle('width').toInt() == 214){
						el.setStyle('z-index', 1);
					}else{
						$('text-'+el.get('id')).setStyle('opacity', 1);
					}
				}
			});
			el.addEvent('mouseenter', function(){
				this.setStyle('z-index', 2);
				$$('#cont-pro-text span').setStyle('opacity', 0);
				if(this.get('id') == "comunes") var left = 165;
				if(this.get('id') == "dormitorios") var left = 403;
				if(this.get('id') == "comunes" || this.get('id') == "dormitorios"){
					var fx = new Fx.Morph(this, {
						duration: 200,
						onComplete: function(){
							if(el.getStyle('width').toInt() == 214){
								el.setStyle('z-index', 1);
							}
							$('text-'+el.get('id')).setStyle('opacity', 1);
						}
					});
					fx.start({
						'width': 360,
						'left': left
					});
				}else{
					this.tween('width', 360);
				}
			});
			el.addEvent('mouseout', function(){
				if(this.get('id') == "comunes") var left = 238;
				if(this.get('id') == "dormitorios") var left = 476;
				if(this.get('id') == "comunes" || this.get('id') == "dormitorios"){
					var fx = new Fx.Morph(this, {
						duration: 200,
						onComplete: function(){
							if(el.getStyle('width').toInt() == 214){
								el.setStyle('z-index', 1);
							}
						}
					});
					fx.start({
						'width': 214,
						'left': left
					});
				}else{
					this.tween('width', 214);
				}
				$('text-'+this.get('id')).setStyle('opacity', 0);
			});
		});
	}
});
window.addEvent('resize', function(){
	if(window.getSize()['y'] > 768) $('container').setStyle('margin-top', (window.getSize()['y'] - 768) / 2);
});
