var main_page_number = 0;
var speaker_page_number = 0;
var cwf_timer = 0;



jQuery(document).ready(function($) {
	
	// Dropdown menu action!
	$('#header ul li').hover (
		function () {
			$(this).children(".children").fadeIn(50);
			$(this).children(".children").children().animate({marginTop: '2px'}, 100);
		},
		function () {
			$(this).children(".children").fadeOut(50);
			$(this).children(".children").children().animate({marginTop: '-40px'}, 100);
	});
	
	$('.youtube_video').fancybox({
        overlayShow: true,
        frameWidth:640,
        frameHeight:360
    });
	
	
	// If it's the front page.
	if ($('#slider').length) {
		$('#slider').nivoSlider({
			effect:'fade',
			pauseTime: 8000
		});
		
		$('#recent_content .youtube_video').fancybox({
            overlayShow: true,
            frameWidth:640,
            frameHeight:360
        });

		$('#recent_content .youtube_video').click(function(e){
			e.preventDefault();
		});
	}
	
	if ($('#twitterSearch').length) {
		$('#twitterSearch').liveTwitter('#cwf2010', {limit: 5, rate: 5000});
	}
	
	
	/*if ($(".gce-list").length) {
		$(".gce-list").accordion({ autoHeight: false, active: false, collapsible: true });
	}*/
	$('.gce-feed-5').has('.gce-list-desc').find('.gce-list-start').after('<div class="gce-toggle"><div class="icon"></div></div>');
	$('.gce-list-event, .gce-list-start, .gce-toggle').click(function() {
		$(this).siblings('.gce-list-desc').toggle('slow');
		$(this).parent().find('.gce-toggle').toggleClass('closed');
	});

	$('#show_menu').click(function(e) {
		e.preventDefault();
		// Showing the menu.

		if ($(this).html() == "↓ SHOW TOP MENU") {
			$(this).html("↑ HIDE TOP MENU");
			
			$("#logo img").animate({height: "130px"}, 300);
			
			$("#header ul").animate({height:"65px"}, 300, function(){
				$("#header ul").css({display: 'block'});
				$("#header ul ul").css({display: 'none'});
			});
			
		// Hiding the menu.
		} else if ($(this).html() == "↑ HIDE TOP MENU") {
			$(this).html("↓ SHOW TOP MENU");
			$("#header ul").animate({height:"0px"}, 300, function(){
				$("#header ul").css({display: 'none'});
			});
			
			$("#logo img").animate({height: "65px"}, 400);
		}
	});

	// If it's the CWF page.
	if ($('#cwf').length) {
		
		startAutoScroll();
		setUpSpeakerList();
		
		// When the Previous Button is clicked.
		$('#cwf_main .prev_button').click(function(e){
			e.preventDefault();
			$(this).stop();
			scrollMainLeft(main_page_number);
		});

		// When the Next Button is clicked.
		$('#cwf_main .next_button').click(function(e){
			e.preventDefault();
			$(this).stop();
			scrollMainRight(main_page_number);
		});
		
		// The main button with the video.
		$('#cwf_main_button').fancybox({
            overlayShow: true,
            frameWidth:640,
            frameHeight:360
        });

		$(".prev_button").hover(
		  function () {
			if (main_page_number != 0) {
				$(this).stop();
				$(this).fadeTo("fast", 0.5);
			}
		  },
		  function () {
			if (main_page_number != 0) {
				$(this).stop();
				$(this).fadeTo("fast", 1);
			}
		  }
		);
		
		$(".next_button").hover(
		  function () {
			if (main_page_number != 3) {
				$(this).stop();
				$(this).fadeTo("fast", 0.5);
			}
		  },
		  function () {
			if (main_page_number != 3) {
				$(this).stop();
				$(this).fadeTo("fast", 1);
			}
		  }
		);

		// Makes sure if you hover over the cwf_main div the rotation stops.
		$("#cwf_main").hover(
		  function () {
		    stopAutoScroll();
		  },
		  function () {
		    startAutoScroll();
		  }
		);
	
		// Speaker List Stuffs
		$("#speaker_list_container .prev_button").click(function(e){
			e.preventDefault();
			$(this).stop();
			
			if (speaker_page_number != 0) {
				speaker_page_number--;
				$('#speaker_list_container div').animate({scrollLeft:(speaker_page_number * 750)}, 300, 'swing');			
			}
		});
		
		$("#speaker_list_container .next_button").click(function(e){
			e.preventDefault();
			$(this).stop();
			
			if (speaker_page_number <= 3) {
				speaker_page_number++;
				$('#speaker_list_container div').animate({scrollLeft:(speaker_page_number * 750)}, 300, 'swing');
			}
		});
		
		$("ul#speaker_list img").hover(
		  function () {
			$(this).stop();
			$(this).fadeTo("fast", 0.8);
		  },
		  function () {
			$(this).stop();
			$(this).fadeTo("fast", 1);
		  }
		);
	}
	
});

function startAutoScroll() {
	cwf_timer = setTimeout(autoScroll, 4500);
}

function stopAutoScroll() {
	clearTimeout(cwf_timer);
}

function autoScroll() {
	if (main_page_number == 3) {
		main_page_number = 1;
		scrollMainLeft();
		
		cwf_timer = setTimeout(autoScroll, 7000);
	} else {
		scrollMainRight();
		cwf_timer = setTimeout(autoScroll, 7000);
	}
}

function scrollMainLeft() {
	if (main_page_number == 1) {
		$('#cwf_main').animate({backgroundPosition:"0px 0px"}, 1000, function(e){
			$('#cwf_main_button').fadeIn("fast");
		});
		$('#cwf_main .next_button').fadeIn("fast");
		$('#cwf_main .prev_button').fadeOut("fast");
	} else if (main_page_number == 2) {
		$('#cwf_main').animate({backgroundPosition:"-1900px 0"}, {duration:1000});
	} else if (main_page_number == 3) {
		$('#cwf_main').animate({backgroundPosition:"-3800px 0"}, {duration:1000});
		$('#cwf_main .next_button').fadeIn("fast");
	}
	
	main_page_number--;
}

function scrollMainRight() {
	if (main_page_number == 0) {
		$('#cwf_main').animate({backgroundPosition:"-1900px 0"}, {duration:1000});
		$('#cwf_main .prev_button').fadeIn("fast");
		$('#cwf_main_button').fadeOut("fast");
	} else if (main_page_number == 1) {
		$('#cwf_main').animate({backgroundPosition:"-3800px 0"}, {duration:1000});
	} else if (main_page_number == 2) {
		$('#cwf_main').animate({backgroundPosition:"-5700px 0"}, {duration:1000});
		$('#cwf_main .next_button').fadeOut("fast");
	} else if (main_page_number == 3) {
		$('#cwf_main').animate({backgroundPosition:"0px 0px"}, {duration:1000});
	}
	
	main_page_number++;
}

function setUpSpeakerList() {
	var div = $('#speaker_list_container div');
    var ul = $('ul#speaker_list');

    div.css({overflow: 'hidden'}); // Remove Scrollbars

	var divWidth = div.width();

	//div.scrollMainLeft();

}

function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en'
  }, 'google_translate_element');
}
