var ydom = YAHOO.util.Dom;
var yven = YAHOO.util.Event;


function emailFocus(box){
	if(box.value == "email address"){
		box.value="";
	}
	ydom.removeClass(box, "emailDefault");
}

function emailBlur(box){
	console.log(box.value);
	if(box.value==""){
		box.value="email address";
		ydom.addClass(box, "emailDefault");
	}
}

function mmcDoEmail(){
	var add = document.getElementById("emailSignup").value;
	var goUrl = "/ajax/newsletter.php?add="+encodeURI(add);
	if(add == "email address"){
		alert("Please enter a valid email address.");
	}
	else {
		YAHOO.util.Connect.asyncRequest('POST', goUrl, mmcEmailCallback);
	}
}

mmcEmailCallback = {
	success: function(o){
		alert(o.responseText);
	},
	failure: function(o){
		alert("Something went wrong.  Please try again.");
	}
}

function mmcChangeVid(id, e){
	yven.preventDefault(e);
	var container = document.getElementById("vidPlayer");
	var embed = '<object width="853" height="505"><param name="movie" value="http://www.youtube.com/v/'+id+'&hl=en_US&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+id+'&hl=en_US&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed></object>';
	container.innerHTML = embed;
}

function mmc () {
	//Constructor!!
}

mmc.prototype = {
	
	suffix: "o=a",
	
	cCont: "mmcContent",
	
	cNav: {},
	
	cPage: "speakers",
	
	inMotion: false,
	
	targNum: 0,
	
	scrollerWidth: 0,
	
	perScroll: 4,
	
	init: function(){
		var navs = ydom.getElementsByClassName("mmcNavItem");
		yven.addListener(navs, "click", this.clicker, this, true);
		var loc = window.location.hash;
		var loc = loc.split("#")[1];
		if(loc===undefined){
			var curLink = document.getElementById("mmcNav_"+this.cPage);
			this.clicker({},{},curLink);
		}
		else{
			var curLink = document.getElementById("mmcNav_"+loc);
			this.clicker({},{},curLink);
		}
	},
	
	arInit: function(){
		var arrows = ydom.getElementsByClassName("scrollArrow");
		yven.addListener(arrows, "click", this.scrollMe, this, true);
		
		var scrollers = ydom.getElementsByClassName("scrollItem")
		var sTot = scrollers.length;
		var scReg = ydom.getRegion(scrollers[0]);
		this.scrollerWidth = scReg.right-scReg.left;
		
		var sWid = sTot*this.scrollerWidth;
		ydom.setStyle("scrollList", "width", sWid+"px");
		
		var clickables = ydom.getElementsByClassName("clickable");
		yven.addListener(clickables, "click", this.clickProf, this, true);
		
		yven.addListener("bioCloser", "click", this.closeBio, this, true);
	},
	
	clicker: function(e, obj, targer){
		var targ;
		targer?targ=targer:targ=yven.getTarget(e);
		var selNav = ydom.getElementsByClassName("selected");
		ydom.removeClass(selNav, "selected");
		if(targ.tagName.toUpperCase() == "DIV"){
			this.cNav = targ;
			targ = targ.getElementsByTagName("A")[0];
		}
		else {
			var cNav = targ;
			while(cNav.tagName != "DIV"){
				cNav = cNav.parentNode;
			}
			this.cNav = cNav;
		}
		var page = targ.href.split("pages/");
		var page = page[1].split(".php");
		this.cPage = page[0];
		this.doAjax(targ.href);
		yven.preventDefault(e);
	},
	
	doAjax: function(targ){
		YAHOO.util.Connect.asyncRequest('POST', targ, {success:this.handleSuccess,failure:this.handleFailure,scope:this}, this.suffix);
	},
	
	callback: {
		success:this.handleSuccess,
		failure:this.handleFailure
	},
	
	handleSuccess: function(o){
		var container = document.getElementById(this.cCont);
		container.innerHTML = o.responseText;
		this.arInit();
		ydom.addClass(this.cNav, "selected");
		window.location = "#"+this.cPage;
	},
	
	handleFailure: function(o){
		console.log(o);
	},
	
	// Scroller stuff
	
	scrollMe: function(e) {
		if(this.inMotion){return false;}
		var targ = yven.getTarget(e);
		var scroller = document.getElementById("scrollList");
		var sReg = ydom.getRegion(scroller);
		var sTot = (sReg.right-sReg.left)/this.scrollerWidth;
		var sLeft = ydom.getStyle(scroller, "left").slice(0,-2);
		sLeft = sLeft*1;
		if(targ.id == "leftArrow"){
			if(sLeft<0){
				nLeft = sLeft+(this.perScroll*this.scrollerWidth);
				nLeft>0?nLeft=0:nLeft=nLeft;
			}
			else {
				nLeft = sLeft-((this.scrollerWidth)*(sTot-this.perScroll));
				nLeft>0?nLeft=0:nLeft=nLeft;
			}
		}
		else if(targ.id == "rightArrow") {
			var sMov = (Math.abs(sLeft/this.scrollerWidth))+this.perScroll;
			//console.log("tot:"+sTot+",mov:"+sMov+",per:"+this.perScroll);
			if(sTot-sMov>this.perScroll){
				nLeft = sLeft-(this.perScroll*this.scrollerWidth);
				nLeft>0?nLeft=0:nLeft=nLeft;
			}
			else if (sTot-sMov>0){
				nLeft = sLeft-((sTot-sMov)*this.scrollerWidth);
			}
			else{
				nLeft = 0;
			}
		}
		this.moveScroller(sLeft, nLeft);
	},
	
	moveScroller: function(sLeft, nLeft){
		this.inMotion = true;
		//console.log(sLeft+" to "+nLeft);
		var attributes = {
		   left: { from: sLeft, to:nLeft }
		};
		
		var myAnim = new YAHOO.util.Anim('scrollList', attributes);
		myAnim.duration = 1.0;
		myAnim.method = YAHOO.util.Easing.easeBoth;
		myAnim.onComplete.subscribe(function(){
			this.inMotion = false;
		}, this, true);
		myAnim.animate();
	},
	
	// Clickable Profiles
	
	clickProf: function(e){
		var targ = yven.getTarget(e);
		this.targNum = targ.id.split("_")[1];
		var profBG = document.getElementById("scrollBioBG");
		var profBio = document.getElementById("scrollBioText_"+this.targNum);
		
		ydom.setStyle(profBG, "opacity", 0);
		ydom.setStyle(profBG, "left", "0px");
		var myAnim = new YAHOO.util.Anim(profBG);
		myAnim.attributes = {opacity:{from:0,to:.75}};
		myAnim.duration = 0.5;
		myAnim.animate();
		
		ydom.setStyle(profBio, "opacity", 0);
		ydom.setStyle(profBio, "left", "20px");
		var myBioAnim = new YAHOO.util.Anim(profBio);
		myBioAnim.attributes = {opacity:{from:0,to:1}};
		myBioAnim.duration = 0.5;
		myBioAnim.animate();
	},
	
	closeBio: function(e){
		var profBG = document.getElementById("scrollBioBG");
		var profBio = document.getElementById("scrollBioText_"+this.targNum);
		
		var myAnim = new YAHOO.util.Anim(profBG);
		myAnim.attributes = {opacity:{from:0.75,to:0}};
		myAnim.duration = 0.5;
		myAnim.onComplete.subscribe(function(){
			ydom.setStyle(profBG, "left", "-10000px");
		}, this, true);
		myAnim.animate();
		
		ydom.setStyle(profBio, "left", "20px");
		var myBioAnim = new YAHOO.util.Anim(profBio);
		myBioAnim.attributes = {opacity:{from:1,to:0}};
		myBioAnim.duration = 0.5;
		myAnim.onComplete.subscribe(function(){
			ydom.setStyle(profBio, "left", "-10000px");
		}, this, true);
		myBioAnim.animate();
	}
	
}

yven.onDOMReady(function(){
	var mmcStart = new mmc();
		mmcStart.init();
	}
);