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

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();

		var sendData = {};
		sendData.email = $('idNewsletterInput').get("value");

		var params = {};
		params.component = "application.home";
		params.method = "newsletterAdd";

		newsletterOk = (appCore.language == 'en') ? "You have successfully subscribed to the newsletter" : "Te has suscrito correctamente a la newsletter";
		params.onComplete = function(result){ alert(newsletterOk); };
		params.isPublic = true;
		params.onError = function(result){ };
		params.variables = sendData;
		
		appCore.ajaxCalls.newCFComponentRequest(params);
		return;

	}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.newsPanel = new Class({
        Extends: bilboostCore.bilboost,
		
		setup: function(){
			$$(".lastNews li").addEvent("click",function(){
				location.href = this.getElements("a")[0].get("href");
				
			});
		}
        
});  

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();
			if(this.element.get("class").indexOf("design") > -1){
				this.fx1.start("top","-28px");
				this.fx2.start("left","-16px");
			}else{
				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();
			
			if(this.element.get("class").indexOf("design") > -1){
				this.fx1.start("top","-175px");
				this.fx2.start("left","25px");
			}else{
				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 thumbs = $$("#featuredPanel .thumb");
			
			for(var a = 0; a < thumbs.length; a++){
				var btn = buttons[a];
				var thumb = thumbs[a];
				this.setupThumb(thumb, 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);
			});
		},
		
		setupThumb : function(thumb, btn,index){
			var tClass = this;
			
			thumb.addEvent("click",function(e){
				e.preventDefault();
				tClass.gotoItem(btn,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");
		}
});

bilboostCore.rooms = new Class({
	Extends: bilboostCore.bilboost,
	rooms : null,
	nextRoom: 2,
	roomsInterval: null,
	time: 5000,
	clicked: false,
	
	setup: function(){
		var thisClass = this;
		thisClass.rooms = $$("#homeRoomsThumbs a");
		thisClass.interval();
		
		
		thisClass.rooms.addEvent('click', function(e){
		
			var index = parseInt(this.href.split("#").pop());
			
			
			if (thisClass.clicked == false) {
				thisClass.clicked = true;
				if ((thisClass.nextRoom - 1) != index) {
				
					clearInterval(thisClass.roomsInterval);
					$$("#homeRoomsThumbs li").removeClass("activo");
					this.getParent().addClass("activo");
					
					thisClass.showRoom(index);
					
					if ((index) < thisClass.rooms.length) {
						thisClass.nextRoom = index + 1;
					}
					else {
						thisClass.nextRoom = 1;
					}
					thisClass.interval();
					
				}
				thisClass.btnClicked.delay(1500, thisClass);
			}
			return false;		
		});
		
		
		thisClass.rooms.addEvent('mouseover', function(e){
			
			var index = parseInt(this.href.split("#").pop());
			this.getParent().addClass("activo");
		});
		thisClass.rooms.addEvent('mouseout', function(e){
			var index = parseInt(this.href.split("#").pop());
			if((thisClass.nextRoom - 1) != index) {
				this.getParent().removeClass("activo");
			}
		});		
		
	},
	
	showRoom: function(index){
		$$("#homeRoomsDescription dt").setStyle("display","none");
		$$("#homeRoomsDescription dd").setStyle("display","none");
		
		$$("#homeRoomsThumbs li").removeClass("activo");
		$$("#homeRoomsThumbs li")[index-1].addClass("activo");
		
		var dt = $$("#homeRoomsDescription dt")[index-1];
		var dd = $$("#homeRoomsDescription dd")[index-1];
	
		
		dt.setStyle("opacity",0);
		dd.setStyle("opacity",0);
		dt.setStyle("display","block");
		dd.setStyle("display","block");

		
		appCore.tween(dt,900,"opacity",1);
		appCore.tween(dd,900,"opacity",1);
	},
	
	btnClicked: function(){
		var thisClass = this;
		thisClass.clicked = false;
	},
	
	interval: function(){
		var thisClass = this;
		this.roomsInterval = (function() {
			thisClass.showRoom(thisClass.nextRoom)
			if (thisClass.nextRoom < thisClass.rooms.length) {
				thisClass.nextRoom++;
			}
			else{
				thisClass.nextRoom = 1;	
			}
  		}).periodical(thisClass.time);
	}
	
});

bilboostCore.latestMembers = new Class({
	Extends: bilboostCore.bilboost,
	
	setup: function(){
		var members = $$(".lastMembers li");
		var thisClass = this;
		
		members.addEvent('mouseover', function(e){
			
			//appCore.tween(this.getElement("span"), 600, 'opacity', .7);
			//appCore.tween(this.getElement("strong"), 600, 'opacity', .7);
			
			//this.getChildren()[1].setStyle("display","block");
			//this.getChildren()[2].setStyle("display","block");
		});
		
		members.addEvent('mouseout', function(e){
			//appCore.tween(this.getElement("span"), 600, 'opacity', 0);
			//appCore.tween(this.getElement("strong"), 600, 'opacity', 0);
		});
		
		
	}
});



bilboostCore.showLayer = new Class({
	Extends: bilboostCore.bilboost,
	
	layer : null,
	layerContent : null,
	layerBg : null,
	closerBtn : null,
	
	layerWidth:952,
	layerHeight:544,
	
	bgOpacity : 1,
	
	setup: function(){
		this.layer = $("homeLayer");
		this.layerContent = this.layer.getElement(".layerContent");
		this.layerBg = this.addBackground(this.layer);
		this.closerBtn = this.addCloser(this.layerContent);
		
		this.bgOpacity = this.layerBg.getStyle("opacity");
		this.layerBg.setStyle("opacity",0);
		this.layerContent.getElement("a").setStyle("opacity",0);
		
		this.layerContent.setStyle("margin-left","-"+String(this.layerWidth/2)+"px");
		this.layerContent.setStyle("margin-top","-"+this.layerHeight+"px");
		
		this.layerBg.addEvent('click', this.closeLayer.bind(this));
		this.closerBtn.addEvent('click', this.closeLayer.bind(this));
		
		this.openLayer();
	},
	
	addBackground : function(layer){
		var bg = new Element("div",{"class":"bg"});
		bg.inject(layer,"top");
		
		return bg;
	},
	
	addCloser : function(layerContent){
		var closer = new Element("div",{"class":"closer"});
		closer.inject(layerContent,"bottom");
		
		return closer;
	},
	
	openLayer: function(){
		this.layer.setStyle("display","block");
		
		appCore.tween(this.layerContent.getElement("a"),900,"opacity",1);
		appCore.tween(this.layerContent,700,"margin-top",0);
		appCore.tween(this.layerBg,700,"opacity",this.bgOpacity);
		
		
	},
		
	closeLayer: function(){
		appCore.tween(this.layerContent,500,"margin-top","-"+this.layerHeight+"px");
		appCore.tween(this.layerBg,500,"opacity",0);
		
		
		this.destroyLayer.delay(700,this);
	},
	
	destroyLayer : function(){
		this.layer.dispose();
	}
});




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(); });

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

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

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

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

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




