$(document).ready(function(){

	$("input[type=text], textarea").simpleWaterMark('watermark');
	
	// PNG Fix
	$(document).pngFix();
	
	if (top.location != location) {
		top.location.href = document.location.href;
	} 

});

function confirmBox(message,location_link) {
	var confirm_box = confirm(message);
	if (confirm_box == true) {
		window.location = location_link;
	}
}

function over_star(star){
	var stars = $("#rating-star-container div").length;
	$("#rating-star-container div").removeClass("active");
	for (var x = 0; x <= stars; x++) {
		if (x <= star) {
			$("#rating-star-container div:eq("+x+")").addClass("active");
		}
	}
}

function leave_star(star, rating){
	if (rating > 0) {
		for (var x = 0; x <= rating; x++) {
			$("#rating-star-container div:eq("+x+")").addClass("active");
		}
	}
	else {
		$("#rating-star-container div").removeClass("active");
	}
}

function update_event(i){
	
	if (typeof timer_event != 'undefined') {
		clearTimeout(timer_event);
	}
	
	if(i == $('#tab-info-container ul').children().size()){
		i = 0;
	}
	
	for (x in events) {
		if(x == i){
			$("#tab-info-container li:eq("+x+")").addClass("selected");
			$("#tab-info-container li:eq("+x+")").html("<span class='white-box-selected-left'></span><div class='white-box-selected-mid'><a onclick='return update_event("+x+");' href=''>"+events[x].category+"</a></div><span class='white-box-selected-right'/>");
		}
		else {
			$("#tab-info-container li:eq("+x+")").removeClass("selected");
			$("#tab-info-container li:eq("+x+")").html("<a onclick='return update_event("+x+");' href=''>"+events[x].category+"</a>");
		}
	}
	$("#tab-info-container h1").fadeOut("fast", function(){
		$("#tab-info-container h1").html(events[i].title).fadeIn("fast");
	});
	$("#tab-info-container p").fadeOut("fast", function(){
		$("#tab-info-container p").html(events[i].content).fadeIn("fast");
	});
	$("#tab-info-container img").fadeOut("fast", function(){
		$("#tab-info-container img").attr("src", "/uploads/article/"+events[i].thumbnail).fadeIn("fast");
	});
	$("#tab-info-container .learn-more-medium-middle a").attr("href", events[i].slug);
	$("#tab-info-container img").attr("title", events[i].title);
	$("#tab-info-container img").attr("alt", events[i].title);
	
	timer_event = setTimeout("update_event("+(i+1)+")", 4000);
	
	return false;
}

function update_speaker(i){
	
	if (typeof timer_speaker != 'undefined') {
		clearTimeout(timer_speaker);
	}
	
	if(i == $('#speaker-banner-container ul').children().size()){
		i = 0;
	}
	
	for (x in speakers) {
		if(x == i){
			$("#speaker-banner-container li:eq("+x+") img").addClass("selected");
		}
		else {
			$("#speaker-banner-container li:eq("+x+") img").removeClass("selected");
		}
	}
	$("#speaker-banner-container h1").fadeOut("fast", function(){
		$("#speaker-banner-container h1").html('<a href="'+speakers[i].slug+'">'+speakers[i].title+'</a>').fadeIn("fast");
	});
	$("#speaker-banner-container p").fadeOut("fast", function(){
		$("#speaker-banner-container p").html('<a href="'+speakers[i].slug+'">'+speakers[i].sub_title+'</a>').fadeIn("fast");
	});
	$("#speakerImg").fadeOut("fast", function(){
		$("#speakerImg").attr("src", "/uploads/article/"+speakers[i].banner).fadeIn("fast");
	});
	$("#speaker-banner-container .learn-more-medium-middle a").attr("href", speakers[i].slug);
	
	timer_speaker = setTimeout("update_speaker("+(i+1)+")", 4000);
	
	return false;
}

function update_banner_event(i){
	
	if (typeof timer != 'undefined') {
		clearTimeout(timer);
	}
	
	if(i == $('#banner-picker-container').children().size()){
		i = 0;
	}
	
	for (x in events) {
		if(x == i){
			$("#banner-picker-container li:eq("+x+")").addClass("active");
		}
		else {
			$("#banner-picker-container li:eq("+x+")").removeClass("active");
		}
	}
	$("#hub-banner-header").fadeOut("fast", function(){
		$("#hub-banner-header").html(events[i].title).fadeIn("fast");
	});
	$("#hub-banner-sub-header").fadeOut("fast", function(){
		$("#hub-banner-sub-header").html(events[i].sub_title).fadeIn("fast");
	});
	$("#hub-banner-date").fadeOut("fast", function(){
		$("#hub-banner-date").html(events[i].date).fadeIn("fast");
	});
	$("#topic-banner-img").fadeOut("fast", function(){
		$("#topic-banner-img").attr("src", events[i].banner).fadeIn("fast");
	});
	$("#banner-link").attr("href", events[i].slug);
	
	timer = setTimeout("update_banner_event("+(i+1)+")", 4000);
	
	return false;
}

function show_download(){
	$('#download-wrapper').slideToggle('slow');
	$('#quick-links-content li:eq(2) a').toggleClass('selected');
	return false;
}

function update_speaker_homepage(direction){
	
	if (typeof timer_speaker != 'undefined') {
		clearTimeout(timer_speaker);
	}
	
	if(direction == 'up'){
		currentSpeaker++;
		if(currentSpeaker == speakers.length){
			currentSpeaker = 0;
		}
	}
	else{
		currentSpeaker--;
		if(currentSpeaker == 0){
			currentSpeaker = speakers.length-1;
		}
	}
	
	//Fixed random problem where by links with forward slashes get replaced with ascii.
	while(speakers[currentSpeaker].slug.indexOf('%2F')!=-1)speakers[currentSpeaker].slug=speakers[currentSpeaker].slug.replace("%2F","/");
	
	$(".speakers-container .black-box-small-middle h1").fadeOut("fast", function(){
		$(".speakers-container .black-box-small-middle h1").html(speakers[currentSpeaker].title).fadeIn("fast");
	});
	$(".speakers-container p").fadeOut("fast", function(){
		$(".speakers-container p").html(speakers[currentSpeaker].content).fadeIn("fast");
	});
	$(".speakers-container img").fadeOut("fast", function(){
		$(".speakers-container img").attr("src", "/uploads/article/"+speakers[currentSpeaker].thumbnail).fadeIn("fast");
	});
	$(".speakers-container .black-box-small-middle a").attr("href", speakers[currentSpeaker].slug);
	$(".speakers-container img").attr("title", speakers[currentSpeaker].title);
	$(".speakers-container img").attr("alt", speakers[currentSpeaker].title);
	
	timer_speaker = setTimeout("update_speaker_homepage('up')", 4000);
	
	return false;
}

function updatesSubmit(){
	
	$.post("/updates/signup?name="+$("#tickets-name-input").val()+"&email="+$("#tickets-email-input").val(), function(data){
		if(data=='sent'){
			//For the subpages
			$("#tickets-middle form").fadeOut('slow');
			$("#tickets-middle").slideUp('slow', function(){
				$("#tickets-middle").html('Thankyou').slideDown('slow');
				setTimeout("$('.tickets-container').fadeOut('slow');", 3000);
			});
			//For the homepage
			$("#tickets-form").fadeOut('slow');
			$(".dark-box-small-middle span").fadeOut('slow', function(){
				$(".dark-box-small-middle span").html('You are signed up to receive the latest updates').fadeIn('slow');
			});
		}
	}); 
	
	return false;
}

function update_promo(i){
	
	if(i == $('#homepage-video-container ul').children().size()){
		i = 0;
	}
	
	for (x in promo) {
		if(x == i){
			$("#homepage-video-container ul li:eq("+x+")").addClass("selected");
			$("#homepage-video-container ul li:eq("+x+")").html("<span class='white-box-selected-left'></span><div class='white-box-selected-mid'><a onclick='return update_promo("+x+");' href=''>"+promo[x]+"</a></div><span class='white-box-selected-right'/>");
		}
		else {
			$("#homepage-video-container ul li:eq("+x+")").removeClass("selected");
			$("#homepage-video-container ul li:eq("+x+")").html("<a onclick='return update_promo("+x+");' href=''>"+promo[x]+"</a>");
		}
	}
	
	$("#homepage-promo-mid .switch").fadeOut("fast");
	
	 setTimeout("$('#homepage-promo-mid .switch:eq("+i+")').fadeIn('medium')",500);

	return false;
}

