var center_td_max_right;
var opening_countdown_interval;

var now;
var ms_difference;
var printed_days;
var printed_hours;
var printed_minutes;
var printed_seconds;


function opening_countdown()
{
   now = new Date();
   
   ms_difference = end_time.getTime() - now.getTime();
   
   if(ms_difference <= 0)
   {
      return;
   }
   printed_days = Math.floor(ms_difference / (1000*60*60*24));
   ms_difference -= printed_days*(1000*60*60*24);
   printed_hours = Math.floor(ms_difference / (1000*60*60));
   ms_difference -= printed_hours*(1000*60*60);
   printed_minutes = Math.floor(ms_difference / (1000*60));
   ms_difference -= printed_minutes*(1000*60);
   printed_seconds = Math.floor(ms_difference / 1000);
   
   if(printed_days < 10)
   {
      printed_days = "0"+printed_days;
   }
   if(printed_hours < 10)
   {
      printed_hours = "0"+printed_hours;
   }
   if(printed_minutes < 10)
   {
      printed_minutes = "0"+printed_minutes;
   }
   if(printed_seconds < 10)
   {
      printed_seconds = "0"+printed_seconds;
   }
   
   $("#days .big-text").text(printed_days);
   $("#hours .big-text").text(printed_hours);
   $("#minutes .big-text").text(printed_minutes);
   $("#secondes .big-text").text(printed_seconds);
}

function open_sub(menu)
{
	var sub_menu = $(".sub_menu_container",menu)
	sub_menu.show();
	if(sub_menu.data("left_set") == false)
	{
		sub_menu.width(sub_menu.width()+"px");
		sub_menu.data("left_set",true);
		var sub_menu_right = sub_menu.offset().left+sub_menu.outerWidth(true);
		if(sub_menu_right > center_td_max_right)
		{
			var new_left = center_td_max_right - sub_menu_right;
			sub_menu.css({
				left : new_left+"px"
			});
		}
	}
	sub_menu.hide();
	sub_menu.stop(true,true).slideDown(400);
	$((".top_menu_anchor"),menu).css({
		color : "#0C608E"
	});
}

function close_sub(menu)
{
	$(".sub_menu_container",menu).stop(true,true).slideUp(400,function(){
		$((".top_menu_anchor"),menu).css({
			color : "#1887C3"
		});
	});
}

var thumb_anim_speed = 400;

function show_thumb(index)
{
	var thumb = $("#photo_gallery_thumbs .thumb").eq(index);
	thumb.animate(
		{
			top : -(thumb.outerHeight(true))+"px"
		},
		{
			duration : thumb_anim_speed,
			easing : "easeOutQuad",
			queue : false
		}
	);
}

function hide_thumb(index)
{
	var thumb = $("#photo_gallery_thumbs .thumb").eq(index);
	thumb.animate(
		{
			top : 0
		},
		{
			duration : thumb_anim_speed,
			easing : "easeInQuad",
			queue : false
		}
	);
}

var picture_fade_speed = 800;
var text_show_speed = 1500;

function change_picture(index,from_interval)
{
	hide_active_picture();
	var pic_to_show = $("#photo_gallery_pictures .picture").eq(index);
	if(from_interval == true)
	{
		reset_top_gallery_animation(pic_to_show);
	}
	pic_to_show.fadeIn(picture_fade_speed,function(){
		if($(".white_bar",$(this)).length > 0)
		{
			$(".white_bar_text",$(this)).show();
			var new_right = -($(".white_bar_text",$(this)).outerWidth());
			$(".white_bar_text",$(this)).css({
				right : new_right+"px"
			});
			$(".white_bar_text",$(this)).animate(
				{
					right : 0
				},
				{
					duration : text_show_speed,
					queue : false
				}
			);
		}
	},"easeOutQuad");
	$("#photo_gallery_controls_numbers .control").eq(index).addClass("active");
}

function hide_active_picture()
{
	var index = $("#photo_gallery_controls_numbers .active").index();
	$(".white_bar_text",$("#photo_gallery_pictures .picture").eq(index)).hide();
	$("#photo_gallery_controls_numbers .active").removeClass("active");
	$("#photo_gallery_pictures .picture").eq(index).fadeOut(picture_fade_speed,"easeOutQuad");
}

var playing = false;
var gallery_interval;
var default_gallery_interval_speed = 5000;

function photo_gallery_controls_play_pause_click(btn)
{
	if(btn.hasClass("pause"))
	{
		pause_top_gallery_animation();
	}
	else
	{
		resume_top_gallery_animation();
	}
}

function pause_top_gallery_animation()
{
	playing = false;
	$("#photo_gallery_controls_play_pause").removeClass("pause");
	clearInterval(gallery_interval);
}

function resume_top_gallery_animation()
{
	$("#photo_gallery_controls_play_pause").addClass("pause");
	if(playing == false)
	{
		playing = true;
		clearInterval(gallery_interval);
		gallery_interval = setInterval("next_picture_from_interval()",default_gallery_interval_speed);
	}
}

function start_top_gallery_animation()
{
	$("#photo_gallery_controls_play_pause").addClass("pause");
	playing = true;
	reset_top_gallery_animation($("#photo_gallery_pictures .picture").eq(0));
}

function reset_top_gallery_animation(obj)
{
	var speed = default_gallery_interval_speed;
	if($(".timer",obj).length > 0)
	{
		speed = $.trim($(".timer",obj).text());
		speed *= 1000;
	}
	clearInterval(gallery_interval);
	gallery_interval = setInterval("next_picture_from_interval()",speed);
}

function next_picture_from_interval()
{
	next_picture(true);
}

function next_picture(from_interval)
{
	from_interval = (from_interval == true) ? true : false;
	var active_pic_index = $("#photo_gallery_controls_numbers .active").index("#photo_gallery_controls_numbers .control");
	if(active_pic_index < $("#photo_gallery_controls_numbers .control").length-1)
	{
		change_picture(active_pic_index+1,from_interval);
	}
	else
	{
		change_picture(0,from_interval);
	}
}

function prev_picture()
{
	var active_pic_index = $("#photo_gallery_controls_numbers .active").index("#photo_gallery_controls_numbers .control");
	if(active_pic_index != 0)
	{
		change_picture(active_pic_index-1);
	}
	else
	{
		change_picture($("#photo_gallery_controls_numbers .control").length-1);
	}
}

function setThumbsContainerLeft()
{
	$("#photo_gallery_thumbs").css({
		left : $("#photo_gallery_controls_numbers").position().left+"px"
	});
	$("#photo_gallery_thumbs .thumb").each(function(){
		var index = $(this).index("#photo_gallery_thumbs .thumb");
		var new_left = $("#photo_gallery_controls_numbers .control").eq(index).position().left;
		$(this).css({
			position : "absolute",
			top : 0,
			left : (new_left-20)+"px"
		});
	});
}

function to_console(text)
{
	if(window.console != null)
	{
		window.console.log(text);
	}
}

