/////////////////////////////////////////////
/////////////////////////////////////////////
// HIGHLIGHT ////////////////////////////////
/////////////////////////////////////////////
var time_to_change=5000;
var time_to_fade=2000;
/////////////////////////////////////////////


/////////////////////////////////////////////
// SCROLL ///////////////////////////////////
/////////////////////////////////////////////
var scroll_hover=true;
var scroll_time_events=300;
var scroll_time_actions=300;
var scroll_delay_events=100;
var scroll_delay_actions=100;
var events_displayed=4;
var actions_displayed=2;
/////////////////////////////////////////////


/////////////////////////////////////////////
// START ////////////////////////////////////
/////////////////////////////////////////////

function start() {
	// scrolling
	if (scroll_hover) {
		$("#up").mouseenter(
			function() {
				is_mouse_ove_button_up=true;
				scroll_up();
			}
		);
		$("#down").mouseenter(
			function() {
				is_mouse_ove_button_down=true;
				scroll_down();
			}
		);
		$("#left").mouseenter(
			function() {
				is_mouse_ove_button_left=true;
				scroll_left();
			}
		);
		$("#right").mouseenter(
			function() {
				is_mouse_ove_button_right=true;
				scroll_right();
			}
		);
		$("#up").mouseleave(
			function() {
				is_mouse_ove_button_up=false;
			}
		);
		$("#down").mouseleave(
			function() {
				is_mouse_ove_button_down=false;
			}
		);
		$("#left").mouseleave(
			function() {
				is_mouse_ove_button_left=false;
			}
		);
		$("#right").mouseleave(
			function() {
				is_mouse_ove_button_right=false;
			}
		);
	} else {
		$("#up").click( function() { scroll_up(); } );
		$("#down").click( function() { scroll_down(); } );
		$("#left").click( function() { scroll_left(); } );
		$("#right").click( function() { scroll_right(); } );
	}
	$("#next-events-all").bind("mousewheel", function(event, delta) { return scroll_mouse_events(event, delta); } );
	$("#all_actions").bind("mousewheel", function(event, delta) { return scroll_mouse_actions(event, delta); } );
	
	//hide all highlights
	$(".highlight").hide(0);
	
	// remove all buttons
	$("#buttons").html("");
	
	// add buttons
	for ( i=0; i<$(".highlight").length; i++ ) {
		$("#buttons").append("<div class=\"button\"></div>");
	}
	
	// display first highlight
	var object_highlight=$(".highlight")[0];
	$(object_highlight).show(0);
	
	// select the first button
	var object_button=$(".button")[0];
	$(object_button).css("background-position", "bottom left");
	
	// add onclick events
	highlight_add_onclick_events($(".button")[0]);
	
	// add mouse events for highlight
	$("#highlight_container").mouseover( function() { is_countdown_paused=true; } );
	$("#highlight_container").mouseout( function() { is_countdown_paused=false; } );
	
	// start slideshow
	is_countdown_started=true;
	countdown();
}


/////////////////////////////////////////////
// HIGHLIGHT ////////////////////////////////
/////////////////////////////////////////////

function highlight_add_onclick_events(selected_button) {
	// add onclick event on buttons
	$(".button").css("cursor", "pointer");
	$(".button").click( 
		function() {
			highlight_set(this);
		}
	);
	
	// remove onclick event on selected button
	$(selected_button).css("cursor", "auto");
	$(selected_button).unbind("click");
}

function highlight_set(the_button) {
	is_countdown_started=false;
	
	//remove onclick event on buttons
	$(".button").css("cursor", "auto");
	$(".button").unbind("click");
	
	// find array id of div to highlight
	var object_highlight_id=-1;
	for ( i=0; i<$(".highlight").length; i++ ) {
		if ( $(".button")[i]==the_button ) {
			object_highlight_id=i;
		}
	}
	
	// buttons_temp
	var buttons_html="";
	for ( i=0; i<$(".highlight").length; i++ ) {
		buttons_html+="<div class=\"button\"></div>";
	}
	buttons_html="<div id=\"buttons_temp\">"+buttons_html+"</div>";
	$("#highlight_container").append(buttons_html);
	$($("#buttons_temp div.button")[object_highlight_id]).css("background-position", "bottom left");
	$(".button").css("cursor", "auto");
	
	// fadeOut
	$(".highlight").fadeOut(time_to_fade);
	$("#buttons").fadeOut(time_to_fade);
	
	// fadeIn
	$($(".highlight")[object_highlight_id]).fadeIn(time_to_fade);
	$("#buttons_temp").fadeIn(time_to_fade);
	
	// after fade...
	setTimeout( 
		function() {
			// replace old buttons with new
			$("#buttons").remove();
			$("#buttons_temp").attr("id", "buttons");
			
			// add onclick events
			highlight_add_onclick_events($($("#buttons_temp div.button")[object_highlight_id]));
			
			// countdown
			my_count=0;
			is_countdown_started=true;
		}, time_to_fade
	);
}


/////////////////////////////////////////////
// COUNTDOWN ////////////////////////////////
/////////////////////////////////////////////

var is_countdown_started=false;
var is_countdown_paused=false;
var my_count = 0

function countdown() {
	if ( (is_countdown_started) && (!is_countdown_paused) ) {
		my_count=my_count+100;
	}
	
	if ( my_count>=time_to_change) {
		// find highlighted div
		var object_highlight=NaN;
		for ( i=0; i<$(".highlight").length; i++ ) {
			var obj = $(".highlight")[i];
			if ( $(obj).css("display")!="none" ) {
				// find  next
				var j=i+1;
				if (j==$(".highlight").length) j=0;
				
				// click next
				var button_to_click =$(".button")[j];
				$(button_to_click).click();
			}
		}
	}
	
	setTimeout(
		function() {
			countdown();
		}, 100
	);
}


/////////////////////////////////////////////
// SCROLLING ////////////////////////////////
/////////////////////////////////////////////

var is_mouse_ove_button_up=false;
var is_mouse_ove_button_down=false;
var is_mouse_ove_button_left=false;
var is_mouse_ove_button_right=false;

function scroll_down() {
	if ( $("#next-events-all:animated").length>0 ) {
		return;
	}
	
	var events_num=$(".event").length;
	var scroll_by=0
	scroll_by = scroll_by + $(".event").height().toString().replace("px","")*1;
	/* scroll_by = scroll_by + $(".action").css("margin-top").toString().replace("px","")*1;
	scroll_by = scroll_by + $(".action").css("margin-bottom").toString().replace("px","")*1;
	*/
	var margin_top=$("#next-events-all").css("margin-top");
	margin_top=margin_top.toString().replace("px","")*1;
	
	if (margin_top==(events_num-events_displayed)*scroll_by*-1) {
		return;
	}
	
	// animate
	if (margin_top<(events_num-events_displayed)*scroll_by*-1) {
		$("#next-events-all").animate({marginTop: ((events_num-events_displayed)*scroll_by*-1).toString()+"px"}, scroll_time_events);
	} else if (margin_top>0) {
		$("#next-events-all").animate({marginTop: "0px"}, scroll_time_events);
	} else {
		$("#next-events-all").animate({marginTop: "-="+scroll_by+"px"}, scroll_time_events);
	}
	
	// bind
	setTimeout( 
		function() {
			if (scroll_hover) {
				if (is_mouse_ove_button_down) {
					scroll_down();
				}
			}
			$("#next-events-all").bind("mousewheel", function(event, delta) { return scroll_mouse_events(event, delta); } );
		}, scroll_time_events+scroll_delay_events
	);
}

function scroll_up() {
	if ( $("#next-events-all:animated").length>0 ) {
		return;
	}
	
	var events_num=$(".event").length;
	var scroll_by=0
	scroll_by = scroll_by + $(".event").height().toString().replace("px","")*1;
/*	scroll_by = scroll_by + $(".action").css("margin-top").toString().replace("px","")*1;
	scroll_by = scroll_by + $(".action").css("margin-bottom").toString().replace("px","")*1;
    */	
	var margin_top=$("#next-events-all").css("margin-top");
	margin_top=margin_top.toString().replace("px","")*1;
	
	if (margin_top==0) {
		return;
	}
	
	// animate
	if (margin_top>0) {
		$("#next-events-all").animate( { marginTop: "0px" }, scroll_time_events );
	} else if (margin_top<(events_num-events_displayed)*scroll_by*-1) {
		$("#next-events-all").animate( { marginTop: ((events_num-events_displayed)*scroll_by*-1).toString()+"px" }, scroll_time_events );
	} else {
		$("#next-events-all").animate( { marginTop: "+="+scroll_by+"px" }, scroll_time_events );
	}
	
	// bind
	setTimeout( 
		function() {
			if (scroll_hover) {
				if (is_mouse_ove_button_up) {
					scroll_up();
				}
			}
			$("#next-events-all").bind("mousewheel", function(event, delta) { return scroll_mouse_events(event, delta); } );
		}, scroll_time_events+scroll_delay_events
	);
}

function scroll_right() {
	if ( $("#all_actions:animated").length>0 ) {
		return;
	}
	
	var actions_num=$(".action").length;
	var scroll_by=0;
	scroll_by = scroll_by + $(".action").width().toString().replace("px","")*1;
	scroll_by = scroll_by + $(".action").css("margin-left").toString().replace("px","")*1;
	scroll_by = scroll_by + $(".action").css("margin-right").toString().replace("px","")*1;
	
	var margin_left=$("#all_actions").css("margin-left");
	margin_left=margin_left.toString().replace("px","")*1;
	
	if (margin_left==(actions_num-actions_displayed)*scroll_by*-1) {
		return;
	}
	
	// animate
	if (margin_left<(actions_num-actions_displayed)*scroll_by*-1) {
		$("#all_actions").animate({marginLeft: ((actions_num-actions_displayed)*scroll_by*-1).toString()+"px"}, scroll_time_actions);
	} else if (margin_left>0) {
		$("#all_actions").animate({marginLeft: "0px"}, scroll_time_actions);
	} else {
		$("#all_actions").animate({marginLeft: "-="+scroll_by+"px"}, scroll_time_actions);
	}
	
	// bind
	setTimeout( 
		function() {
			if (scroll_hover) {
				if (is_mouse_ove_button_right) {
					scroll_right();
				}
			}
			$("#all_actions").bind("mousewheel", function(event, delta) { return scroll_mouse_actions(event, delta); } );
		}, scroll_time_actions+scroll_delay_actions
	);
}

function scroll_left() {
	if ( $("#all_actions:animated").length>0 ) {
		return;
	}
	
	var actions_num=$(".action").length;
	var scroll_by=0;
	scroll_by = scroll_by + $(".action").width().toString().replace("px","")*1;
	scroll_by = scroll_by + $(".action").css("margin-left").toString().replace("px","")*1;
	scroll_by = scroll_by + $(".action").css("margin-right").toString().replace("px","")*1;
	
	var margin_left=$("#all_actions").css("margin-left");
	margin_left=margin_left.toString().replace("px","")*1;
	
	if (margin_left==0) {
		return;
	}
	
	// animate
	if (margin_left>0) {
		$("#all_actions").animate({marginLeft: "0px"}, scroll_time_actions);
	} else if (margin_left<(actions_num-actions_displayed)*scroll_by*-1) {
		$("#all_actions").animate({marginLeft: ((actions_num-actions_displayed)*scroll_by*-1).toString()+"px" }, scroll_time_actions);
	} else {
		$("#all_actions").animate({marginLeft: "+="+scroll_by+"px"}, scroll_time_actions);
	}
	
	// bind
	setTimeout( 
		function() {
			if (scroll_hover) {
				if (is_mouse_ove_button_left) {
					scroll_left();
				}
			}
			$("#all_actions").bind("mousewheel", function(event, delta) { return scroll_mouse_actions(event, delta); } );
		}, scroll_time_actions+scroll_delay_actions
	);
}

function scroll_mouse_disabled(event, delta) {
	return false;
}

function scroll_mouse_events(event, delta) {
	if ( $("#next-events-all:animated").length>0 ) {
		return false;
	}
	
	if (delta>0) scroll_up();
	if (delta<0) scroll_down();
	
	return false;
}

function scroll_mouse_actions(event, delta) {
	if ( $("#all_actions:animated").length>0 ) {
		return false;
	}
	
	if (delta>0) scroll_left();
	if (delta<0) scroll_right();
	
	return false;
}
