window.onload = function() {
	if(document.getElementById('HomeBannerSideBar')) {
		speaker_init('home');
	}
	if(document.getElementById('SubPageBannerSideBar')) {
		speaker_init('sub');
	}
	if(document.getElementById('videoFilmstrip')) {
		createVideoFilmstrip();
	}
	/*if(document.getElementById('videoPlayer')) {
		renderVideoFlash(videoURL);
	}*/
}

var speaker_height = 150;
var speaker_num = 0;
var speaker_speed = 46;
var speaker_interval;
var speaker_total = 0;

function speaker_init(page) {
	if(page == "home") {
		speaker_height = 370;
		speaker_speed = 46;
	}

	var speakers = document.getElementById('speakers');

	//var ulCopy = document.createElement("div");
	//ulCopy.id = "speakerCopy";
	//ulCopy.innerHTML = speakers.innerHTML;
	//speakers.appendChild(ulCopy);

	var lis = speakers.getElementsByTagName('li');
	speaker_total = lis.length / 2;
	for (var x = 0; x < lis.length; x++) {
		lis[x].id = "speaker_"+x;
	}
	
	speaker_interval = setInterval('speaker_scroll()', speaker_speed);

}

function speaker_scroll() {
	speaker_num = speaker_num + 1;
	document.getElementById('speakers').style.top = -speaker_num + "px";
	
	if(speaker_num > speaker_total * 36) {
		speaker_num = 0;
	}
}

function speaker_add() {
        var name = document.getElementById('ctl00_BodyContent_NameField').value;
        var email = document.getElementById('ctl00_BodyContent_EmailField').value;
        var re = new RegExp(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/gi);
        if(email.match(re)) {
        
        var xmlhttp =  new XMLHttpRequest();
        var content = "/Join.asmx/AddMember?name="+name+"&email="+email;
        //alert(content);
        xmlhttp.open("GET", content, true);
        xmlhttp.setRequestHeader("Content-Type", "charset=UTF-8");
      
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    var value = xmlhttp.responseText.replace(/[^1-3]/gi,"");
                    //alert(value);
                    document.getElementById('load').className = "hide";
                    if(value == '11') {
                        document.getElementById('success').className = "SuccessMessage";
                        speaker_position(name);
                    }
                    if(value == '12') {
                        document.getElementById('join').className = "hide";
                        document.getElementById('already').className = "ErrorMessage";
                        speaker_position(name);
                    }
                    if(value == '13') {
                        document.getElementById('join').className = "hide";
                        document.getElementById('fail').className = "ErrorMessage";
                    }
                    //alert(xmlhttp.responseText);
                }
            }
        }
        xmlhttp.send('');
        document.getElementById('join').className = "hide";
        document.getElementById('load').className = "";
        }
}

function speaker_position(name) {
    var position = Math.ceil((speaker_num + speaker_height) / 36);
    var newLi = document.createElement("li");
    newLi.innerHTML = name;
    var copyLi = document.createElement("li");
    copyLi.innerHTML = name;
	
    if(position < speaker_total) {
	    document.getElementById('speakers').getElementsByTagName('ul')[0].insertBefore(newLi, document.getElementById('speaker_'+position));
	    speaker_total++;
	    document.getElementById('speakerCopy').getElementsByTagName('ul')[0].insertBefore(copyLi, document.getElementById('speaker_'+(position+speaker_total)));
    }
    else {
	    document.getElementById('speakers').getElementsByTagName('ul')[0].insertBefore(newLi, document.getElementById('speaker_'+(position - speaker_total)));
	    speaker_num = speaker_num + 36;
	    document.getElementById('speakers').style.top = -speaker_num + "px";
	    document.getElementById('speakerCopy').getElementsByTagName('ul')[0].insertBefore(copyLi, document.getElementById('speaker_'+position));
	    speaker_total++;
    }
}

function input(object, text) {
	if(object.value == text) {
		object.value = "";
	}
}
function output(object, text) {
	if(object.value.length < 1) {
		object.value = text;
	}
}

function SendFriend(url) {
    var friendWindow = window.open(url,'sfwin','width=520,height=650,status=1,toolbar=0,menubar=1');
    friendWindow.focus();
}

function customValidationStyle(oElem, isValid) {
    if ("INPUT" == oElem.tagName || "SELECT" == oElem.tagName || "TEXTAREA" == oElem.tagName) {
        if (isValid) {
            oElem.style.background = "none";
        }
        else {
            oElem.style.background = "#EACCBA";
        }
    }
    else {
        design_validationStyle(oElem, isValid);
    }
}

function createVideoFilmstrip() {
	var vf = new filmstrip();
	vf.frame = document.getElementById('videoFilmstrip');
	vf.previous = document.getElementById('videosLeft');
	vf.next = document.getElementById('videosRight');
	vf.settings.totalVisible = 3;
	vf.settings.trimEnding = true;
	vf.settings.scrollWheel = true;
	vf.events.onAfterInit = function() {
		if(vf.count < 4) {
			vf.previous.style.visibility = "hidden";
			vf.next.style.visibility = "hidden";
		}
	};
	vf.init();
	
	$('#videoFilmstrip li').hover(function() {
		$(this).addClass('hover');
	},
	function() {
		$(this).removeClass('hover');
	}).click(function() {
		$('#videoFilmstrip li').removeClass('active');
		$(this).addClass('active');
		var furl = $(this).find('div.url').html();
		playVideo(furl);
	});
}


// Filmstrip (coda slider):
//		var slider = new filmstrip();
//		slider.frame = document.getElementById('slider');
//		slider.settings.scrollWheel = true;
//		slider.init();

function filmstrip(frame, next, previous) {
	var self = this;
	this.current = 1;
	this.count = 1;
	this.settings = {direction:0, totalVisible:1, prefix:"filmstrip", ease:"sineInOut", trimEnding:false, disableWrap:false, setRotate:0, scrollWheel:false, pageSlide:false, debug:false};
	this.events = {onBeforeMove:null, onMoveComplete:null, onAfterInit:null};
	this.frame = frame;
	this.next = next;
	this.previous = previous;
	this.anim = {time:0, begin:0, change:0.0, timer:null, duration:50, speed:15, rotate:null};
	this.init = function() {
		if(this.frame.id.length > 0 && this.settings.prefix == "filmstrip") {
			this.settings.prefix = this.frame.id + '_';
		}
		
		var slides = this.frame.getElementsByTagName('li');
		this.count = slides.length;
		for(var x=0;x<this.count;x++) {
			slides[x].id = this.settings.prefix + (x+1);
		}
		
		var wrapper = this.frame.getElementsByTagName('ul')[0];
		if(this.settings.direction == 0) {
			wrapper.style.width = slides[0].offsetWidth * (slides.length + this.settings.totalVisible) + "px";
			//wrapper.style.paddingRight = this.frame.offsetWidth + "px"
		}
		else {
			//wrapper.style.height = slides[0].offsetHeight * (slides.length + this.settings.totalVisible) + "px";
			wrapper.style.paddingBottom = this.frame.offsetHeight + "px"
		}
		
		if(this.settings.scrollWheel) {
			this.initWheel();
		}
		
		this.go(1);
		//if(this.settings.setRotate > 0) {
		//	this.anim.rotate = setTimeout(function() { self.moveNext(); }, this.settings.setRotate);
		//}
		
		if(this.next) {
			this.next.onclick = function() {
				self.moveNext();
				return false;
			};
		}
		if(this.previous) {
			this.previous.onclick = function() {
				self.movePrevious();
				return false;
			};
		}
		
		if(this.events.onAfterInit) {
			this.events.onAfterInit();
		}
		
		if(this.settings.debug) {
			if(this.settings.trimEnding && this.settings.pageSlide) alert('Warning:\nsettings.trimEnding and settings.pageSlide are both set to "true".');
		}
	};
	this.go = function(num) {
		clearTimeout(this.anim.rotate);
		
		if(this.events.onBeforeMove) {
			this.events.onBeforeMove(num);
		}
		
		this.current = num;
		
		var position = c2.findElementPos(document.getElementById(this.settings.prefix + this.current));
		var offsetPos = c2.findElementPos(document.getElementById(this.settings.prefix + 1));
		position[this.settings.direction] = position[this.settings.direction] - offsetPos[this.settings.direction];
		
		var scrollOffset = 0;
		if(this.settings.direction == 0) {
			scrollOffset = this.frame.scrollLeft;
		}
		else if(this.settings.direction == 1) {
			scrollOffset = this.frame.scrollTop;
		}
		
		if (this.anim.timer != null) {
			clearInterval(this.anim.timer);
			this.anim.timer = null;
		}
		this.anim.time = 0;
		this.anim.begin = scrollOffset;
		this.anim.change = position[this.settings.direction] - scrollOffset;
		
		this.anim.timer = setInterval(function() { self.scroll(); }, this.anim.speed);
	};
	this.scroll = function() {
		if (this.anim.time > this.anim.duration) {
			clearInterval(this.anim.timer);
			this.anim.timer = null;
			
			if(this.settings.setRotate > 0) {
				this.anim.rotate = setTimeout(function() { self.moveNext(); }, this.settings.setRotate);
			}
			
			if(this.events.onMoveComplete) {
				this.events.onMoveComplete();
			}
		}
		else {
			var move = c2.ease[this.settings.ease](this.anim.time, this.anim.begin, this.anim.change, this.anim.duration);
			if(this.settings.direction == 0) {
				this.frame.scrollLeft = move;
			}
			else if(this.settings.direction == 1) {
				this.frame.scrollTop = move;
			}
			this.anim.time++;
		}
	};
	this.moveNext = function() {
		var num;
		if(this.settings.pageSlide) {
			num = this.current + this.settings.totalVisible;
		}
		else {
			num = this.current + 1;
		}
		if((this.settings.trimEnding && num > this.count - this.settings.totalVisible + 1) || (!this.settings.trimEnding && num > this.count)) {
			if(this.settings.disableWrap) {
				return false;
			}
			else {
				num = 1;
			}
		}
		this.go(num);
	};
	this.movePrevious = function() {
		var num;
		if(this.settings.pageSlide) {
			num = this.current - this.settings.totalVisible;
		}
		else {
			num = this.current - 1;
		}
		if(num < 1) {
			if(this.settings.disableWrap) {
				return false;
			}
			else {
				if(this.settings.trimEnding) {
					num = this.count - this.settings.totalVisible + 1;
				}
				else {
					if(this.settings.pageSlide) {
						if(this.count % this.settings.totalVisible != 0) {
							num = this.count - (this.count % this.settings.totalVisible) + 1;
						}
						else {
							num = this.count - this.settings.totalVisible + 1;
						}
					}
					else {
						num = this.count;
					}
				}
			}
		}
		this.go(num);
	};
	this.initWheel = function() {
		if (this.frame.addEventListener) {
			/** DOMMouseScroll is for mozilla. */
			this.frame.addEventListener('DOMMouseScroll', this.slideEventWheel, false);
		}
		/** IE/Opera. */
		this.frame.onmousewheel = this.slideEventWheel;
	};
	this.slideEventWheel = function(event) {
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
        } else if (event.detail) { /** Mozilla case. */
                delta = -event.detail/3;
        }
        if (delta) {
			if(delta < 0) {
				self.moveNext();
			}
			else {
				self.movePrevious();
			}
		}
        if (event.preventDefault)
			event.preventDefault();
		event.returnValue = false;
	};
}


var c2 = {
	findElementPos: function(elemFind) {
		var elemX = 0;
		var elemY = 0;
		do {
			elemX += elemFind.offsetLeft;
			elemY += elemFind.offsetTop;
		} while ( elemFind = elemFind.offsetParent )
		return Array(elemX, elemY);
	},
	randomInt: function(min, max) {
		return Math.floor(Math.random()*(max+1-min)) + min;
	},
	getQuery: function(param) {
		var p = escape(unescape(param));
		var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
		var match = regex.exec(window.location.search);
		var value = null;
		if( match != null ){
			value = match[1];
		}
		return value;
	},
	getKey: function(e) {
		if(!e) e = window.event;
		var key = e.which;
		if(!key) key = e.keyCode;
		return key;
	},
	ease: {
		quadIn: function (t, b, c, d) {
			return c*(t/=d)*t + b;
		},
		quadOut: function (t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		},
		quadInOut: function (t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t + b;
			return -c/2 * ((--t)*(t-2) - 1) + b;
		},
		cubicIn: function (t, b, c, d) {
			return c*(t/=d)*t*t + b;
		},
		cubicOut: function (t, b, c, d) {
			return c*((t=t/d-1)*t*t + 1) + b;
		},
		cubicInOut: function (t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t + b;
			return c/2*((t-=2)*t*t + 2) + b;
		},
		quartIn: function (t, b, c, d) {
			return c*(t/=d)*t*t*t + b;
		},
		quartOut: function (t, b, c, d) {
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		},
		quartInOut: function (t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
			return -c/2 * ((t-=2)*t*t*t - 2) + b;
		},
		quintIn: function (t, b, c, d) {
			return c*(t/=d)*t*t*t*t + b;
		},
		quintOut: function (t, b, c, d) {
			return c*((t=t/d-1)*t*t*t*t + 1) + b;
		},
		quintInOut: function (t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
			return c/2*((t-=2)*t*t*t*t + 2) + b;
		},
		sineIn: function (t, b, c, d) {
			return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
		},
		sineOut: function (t, b, c, d) {
			return c * Math.sin(t/d * (Math.PI/2)) + b;
		},
		sineInOut: function (t, b, c, d) {
			return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
		},
		expoIn: function (t, b, c, d) {
			return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
		},
		expoOut: function (t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		},
		expoInOut: function (t, b, c, d) {
			if (t==0) return b;
			if (t==d) return b+c;
			if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
			return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
		},
		circIn: function (t, b, c, d) {
			return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
		},
		circOut: function (t, b, c, d) {
			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
		},
		circInOut: function (t, b, c, d) {
			if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
			return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
		},
		elasticIn: function (t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		},
		elasticOut: function (t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		},
		elasticInOut: function (t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
			return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
		},
		backIn: function (t, b, c, d, s) {
			if (s == undefined) s = 1.70158;
			return c*(t/=d)*t*((s+1)*t - s) + b;
		},
		backOut: function (t, b, c, d, s) {
			if (s == undefined) s = 1.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		},
		backInOut: function (t, b, c, d, s) {
			if (s == undefined) s = 1.70158; 
			if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
			return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
		},
		bounceIn: function (t, b, c, d) {
			return c - this.bounceOut (d-t, 0, c, d) + b;
		},
		bounceOut: function (t, b, c, d) {
			if ((t/=d) < (1/2.75)) {
				return c*(7.5625*t*t) + b;
			} else if (t < (2/2.75)) {
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			} else if (t < (2.5/2.75)) {
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			} else {
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		},
		bounceInOut: function (t, b, c, d) {
			if (t < d/2) return this.bounceIn (t*2, 0, c, d) * .5 + b;
			return this.bounceOut (t*2-d, 0, c, d) * .5 + c*.5 + b;
		}
	}
};
