﻿var banner_animation = false;
var active_index;
var banner_count = 0;
var banner_play;
var banner_time = 10000;

$(document).ready(function()
{

	banner_play= $.cookie('banner_play');
	if(banner_play == null)
	{
		$.cookie('banner_play', 1, {path: "/"})
		banner_play = 1;
	}



	banner_count = $('#rotation .rotation_image').length;
	if(banner_count > 0)
	{
		ShowImage(0);
	}
	
	
	$('#rotation .rotation_page').click(function()
	{
		ShowImage($(this).attr('index'));
	});
	
	if(banner_play == 1)
	{
		setTimeout(PlayBanner, banner_time);
		$('#rotation #playpause img').attr('src', '/images/pause.jpg');
	}
	else
	{
		$('#rotation #playpause img').attr('src', '/images/play.jpg');

	}
	
	
	$('#rotation #playpause').click(function()
	{
		if(banner_play == 1)
		{
			banner_play = 0;
			$.cookie('banner_play', banner_play, {path: "/"})

			$('#rotation #playpause img').attr('src', '/images/play.jpg');
		}
		else
		{
			banner_play = 1;
			$.cookie('banner_play', banner_play, {path: "/"})

			$('#rotation #playpause img').attr('src', '/images/pause.jpg');
			setTimeout(PlayBanner, banner_time);


		}
	});
});

function PlayBanner()
{
	if(banner_play == true)
	{
		
		var new_index = parseInt(active_index) + 1;
		
		if(new_index == banner_count)
		{
			new_index = 0;
		}
		ShowImage(new_index);
		setTimeout(PlayBanner, banner_time);
	}
}


function ShowImage(index)
{
	if(active_index != index && banner_animation == false)
	{
		banner_animation = true;
		active_index = index;
			
		var image= $('#rotation .rotation_image')[index];
		var old = $('#rotation .rotation_image:visible');
		var page = $('#rotation .rotation_page[index='+index+']')
					
		$('#rotation .rotation_page.active').removeClass('active');
		$(page).addClass('active');
		
		$(image).show();
		$(image).css('z-index', 0);
		old.fadeOut(1000, function()
		{
			$(image).css('z-index', 99);
			banner_animation =false;
		});
		if(old.length == 0)
		{
			$(image).css('z-index', 99);
			banner_animation = false;
		}
		
	}
}
