/* Bilboost.com */
/* homescripts.js */
/* Created Oct, 2009 */
/* Modified Oct, 2009
--------------------------------------- */

var appCore = new bilboostCore.appCore();
window.addEvent("domready", appCore.replaceFonts);
window.addEvent("load", appCore.replaceFonts);


var mainForm;

setupForm = function(){
	mainForm = new bilboostCore.formValidator("form_newsletter");	
	mainForm.addField("email",	{obligatorio: true,	tipo: "email"},{});
	mainForm.limpiaTodo();	
}

sendForm = function(){
	var ok = mainForm.compruebaTodo();
	
	if (ok == true){
		document.form_newsletter.action = window.location;
		document.form_newsletter.submit();
	}else{
		return;
	}	
}

addBannerVideo = function(){
	var flashVideo = new Swiff('/resources/swf/jcplayer.swf', {
	    id: 'videoContainer1',
		container:"videoContainer",
	    width: 320,
	    height: 240 + 25,
	    params: {
	        bgcolor: '#FF0000'
	    },
	    vars: {
	        videoURL:"/resources/flv/home/banner/artandfashion.flv",
			startPhotoSource:"/resources/flv/home/banner/artandfashion.jpg",
			autoPlay:false,
			scaleMode:"maintainAspectRatio",
			loop:false,
			backgroundColor1:0x000000,
			backgroundColor2:0x444444,
			movieBackgroundColor:0x000000,
			inkColor:0xffffff,
			highlightColor:0xff0000,
			playButoon:true,
			seekBar:true,
			timeBar:false,
			volumeButton:true,
			fullScreenButton:false,
			autoHide:false,
			autoHideFullScreen:true,
			volume:70,
			bufferTime:1,
			smoothing:false
	    }
	});
}

bilboostCore.newsPanel = new Class({
        Extends: bilboostCore.bilboost,
		
		fx1 : null,
		fx2: null,
		locked : false,
		active : null,
		
		initialize: function(){},
		
		setup : function(){
			var tClass = this;
			
			this.fx1 = new Fx.Tween($("news1"), {duration: 600, transition: Fx.Transitions.Quad.easeInOut});
			this.fx2 = new Fx.Tween($("news2"), {duration: 600, transition: Fx.Transitions.Quad.easeInOut});
			
			$("newsBoxes").setStyle("min-height",Math.min($("news1").scrollHeight, $("news2").scrollHeight) + "px");
			
			if(this.ie7 == true){
				$("news2").setStyle("height",String($("news2").scrollHeight + 10) + "px");
				$("news2").setStyle("opacity",0);
				$("news1").setStyle("position","absolute");
				$("news2").setStyle("position","absolute");				
				$("newsBoxes").setStyle("height",Math.max($("news1").scrollHeight+10, $("news2").scrollHeight+10) + "px");			
			}
			
			$("tab1").addEvent("click",function(){
				if(tClass.locked || tClass.active == "tab1")
					return;
				tClass.active = "tab1";
				tClass.locked = true;
				setTimeout(function(){tClass.locked = false;},1500);
				setTimeout(function(){tClass.setActive("tab1","news1",tClass.fx1);},700);
				tClass.setInactive("tab2","news2",tClass.fx2);
			});
			$("tab2").addEvent("click",function(){
				if(tClass.locked || tClass.active == "tab2")
					return;
				tClass.active = "tab2";
				tClass.locked = true;
				setTimeout(function(){tClass.locked = false;},1500);
				setTimeout(function(){tClass.setActive("tab2","news2",tClass.fx2);},700);
				tClass.setInactive("tab1","news1",tClass.fx1);
			});
			
			$("tab1").addClass("active");		
		},
		
		setActive : function(tab,news,fx){
			var tab = $(tab);
			var news = $(news);

			tab.addClass("active");
			news.setStyle("display","block");
			
			news.setStyle('opacity',"0");
			fx.start('opacity',"0","1");
			
			if(this.ie7 == true)
				return;
				
			if(this.ie < 0)
				news.setStyle('height',String(news.scrollHeight) + "px");
			else
				news.setStyle('height',String(news.scrollHeight + 10) + "px");

		},
		
		setInactive : function(tab,news,fx){
			var tab = $(tab);
			var news = $(news);
			
			tab.removeClass("active");			
			
			if(this.ie7 == true){
				fx.start('opacity',"1","0");
				return;
			}
			
			fx.start('height',"0px");

		}		
});

bilboostCore.usersPanel = new Class({
	Extends: bilboostCore.bilboost,
				
		initialize: function(){},
		
		setup: function(){
			var items = $$("#usersPanel .user");
			for(var a = 0; a < items.length; a++){
				var item = items[a];
				new bilboostCore.rollOverUser(item)
			}
		}
});

bilboostCore.rollOverUser = new Class({
	Extends: bilboostCore.bilboost,
		
		element : null,
		layer : null,
		fx1 : null,
		fx2 : null,
		
		initialize: function(elem){
			var tClass = this;
			
			this.element = elem;
			this.layer = this.element.getElement(".infoLayer");
			this.element.addEvent("mouseover",function(){ tClass.showLayer(); });
			this.element.addEvent("mouseout",function(e){ tClass.hideLayer(e); });
			
			this.fx1 = new Fx.Tween(this.layer, {duration: 400, transition: Fx.Transitions.Quad.easeOut});
			this.fx2 = new Fx.Tween(this.layer, {duration: 400, transition: Fx.Transitions.Quad.easeOut});
		},
		
		showLayer : function(){
			this.fx1.cancel();
			this.fx2.cancel();
			this.fx1.start("top","-20px");
			this.fx2.start("left","-35px");
		},
		
		hideLayer : function(e){
			if(e.target.getPrevious != undefined && e.target.getPrevious("a") == this.element.getElement("a"))
				return;
					
			this.fx1.cancel();
			this.fx2.cancel();
			this.fx1.start("top","-150px");
			this.fx2.start("left","33px");
		}
});

bilboostCore.featuredPanel = new Class({
	Extends: bilboostCore.bilboost,
		
		scrollingItem : null,
		totalItems : null,
		totalWidth : null,
		fx: null,
		
		activeIndex : null,
		activeBtn : null,
		
		isOnPlay : false,
		nextTimeOut : null,
		viewTime :6000,
		
		initialize: function(){},
		
		setup: function(){
		
			
			this.scrollingItem = $("featured_bannersHolder");
			this.totalItems = $$("#featured_bannersHolder .destacado").length;
			
			this.scrollingItem.setStyle("width",this.scrollingItem.scrollWidth * this.totalItems)
			
			this.fx = new Fx.Tween(this.scrollingItem, {duration: 1000, transition: Fx.Transitions.Quad.easeOut});
			
			var buttons = $$("#featured_navigator .featured_navigator_button");
			
			for(var a = 0; a < buttons.length; a++){
				var btn = buttons[a];
				this.setupButton(btn,a+1);
			}
			
			var tClass = this;
			$("featured_play").addEvent("click",function(){tClass.play(true);});
			$("featured_pause").addEvent("click",function(){tClass.pause();});
			
			this.gotoItem(buttons[0],1);
			
			this.play();
		},
		
		setupButton : function(btn,index){
			var tClass = this;
			
			btn.addEvent("click",function(e){
				tClass.gotoItem(e.target,index);
			});
			
			btn.addEvent("mouseover",function(e){
				tClass.overBtn(e.target,index);
			});
			
			btn.addEvent("mouseout",function(e){
				tClass.outBtn(e.target,index);
			});
		},
		
		play : function(doNow){
			if(this.isOnPlay == true)
				return;
			
			this.isOnPlay = true;
			
			if(doNow == true)
				this.next();
				
			var tClass = this;
			this.nextTimeOut = setTimeout(function(){tClass.next();},this.viewTime);
		},
		
		pause : function(){
			if(this.isOnPlay == false)
				return;
				
			this.isOnPlay = false;
			
			clearTimeout(this.nextTimeOut);
		},
		
		next : function(){
			var index = this.activeIndex + 1;
			if(index > this.totalItems)
				index = 1;
			
			var buttons = $$("#featured_navigator .featured_navigator_button");
			var btn = buttons[index-1]
			
			this.gotoItem(btn,index);
		},
		
		gotoItem : function(btn,index){
			if(this.activeIndex == index)
				return;
			if(this.activeBtn != null)
				$(this.activeBtn).removeClass("active");
			
			this.activeIndex = index;
			this.activeBtn = btn;
				
			var totalWidth = this.scrollingItem.clientWidth; 
			var destino = (totalWidth / this.totalItems) * (index - 1);
			var posActual = this.scrollingItem.offsetLeft;
			var distancia = Math.abs(-destino - posActual);
			
			if(distancia > 920)
				var duracion = Math.round((distancia * 500) / 460);
			else
				var duracion = 1000;
						
			$(btn).addClass("active");
			this.fx.cancel();
			this.fx = null;
			delete this.fx;
			this.fx = new Fx.Tween(this.scrollingItem, {duration: duracion, transition: Fx.Transitions.Quart.easeInOut});
			this.fx.start("left",-destino + "px");
			
			if(this.isOnPlay == false)
				return;
			
			var tClass = this;
			clearTimeout(this.nextTimeOut);
			this.nextTimeOut = setTimeout(function(){tClass.next();},this.viewTime);
		},
		
		overBtn : function(btn,index){
			if(this.activeIndex == index)
				return;
				
			$(btn).addClass("active");
		},
		
		outBtn : function(btn,index){
			if(this.activeIndex == index)
				return;
				
			$(btn).removeClass("active");
		}
});



var newsPanel = new bilboostCore.newsPanel();
window.addEvent("load", function(){	newsPanel.setup(); });

var usersPanel = new bilboostCore.usersPanel();
window.addEvent("load", function(){	usersPanel.setup(); });

var featuredPanel = new bilboostCore.featuredPanel();
window.addEvent("load", function(){	featuredPanel.setup(); });

//window.addEvent("load", function(){	addBannerVideo(); });

window.addEvent("load", function(){	setupForm(); });



