$(document).ready(function() {
	/*
	//  ADD MOUSEOVER TO FLASH SCOLL RIGHT
	$('#flash-scroll-right').hover(
		function () {
			$(this).addClass('flash-scroll-right-on');
			$(this).removeClass('flash-scroll-right-off');
		},
		function () {
			$(this).addClass('flash-scroll-right-off');
			$(this).removeClass('flash-scroll-right-on');
		}
	);
	
	//  ADD onCLICK {next} TO FLASH SCOLL RIGHT
	$('#flash-scroll-right').click(function() {
			galleryNext();
	});

	//  ADD MOUSEOVER TO FLASH SCOLL LEFT
	$('#flash-scroll-left').hover(
		function () {
			$(this).addClass('flash-scroll-left-on');
			$(this).removeClass('flash-scroll-left-off');
		},
		function () {
			$(this).addClass('flash-scroll-left-off');
			$(this).removeClass('flash-scroll-left-on');
		}
	);
	
	//  ADD onCLICK {prev} TO FLASH SCOLL LEFT
	$('#flash-scroll-left').click(function() {
				galleryPrev();
	});
	*/
	//  ADD MOUSEOVER TO FLASH DOT
	$('#flash-dots span').hover(
		function () {
			if (!$(this).hasClass('flash-dot-current')) {
				$(this).addClass('flash-dot-on');
				$(this).removeClass('flash-dot-off');
			}
		},
		function () {
			if (!$(this).hasClass('flash-dot-current')) {
				$(this).addClass('flash-dot-off');
				$(this).removeClass('flash-dot-on');
			}
		}
	);
	
	//  ADD onCLICK {prev} TO FLASH DOTS
	$('#flash-dots span').click(function() {
				gallerySelect(this.id);
	});
	
	//Execute the slideShow, set 4 seconds for each images
	slideShow(4000);
	

});

function slideShow(speed) {


	//append a LI item to the UL list for displaying caption
	//$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1.0});
	
	$('ul.slideshow li:first').addClass('show');
	
	var t = $("ul.slideshow li:first").attr("id");
	var tArr = t.split("-");
	t=tArr[1];
	$("#dot-" + t).addClass('flash-dot-current');
	$("#dot-" + t).removeClass('flash-dot-off');
	
	//Get the caption of the first image from REL attribute and display it
	//$('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title'));
	//$('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));
		
	//Display the caption
	//$('#flash-back').css({opacity: 0.4, bottom:-40});
	//$('#flash-scroll').css({bottom:-40,opacity: 1.0});
	$('#flash-controls').css({opacity: 1.0});
	
	//Call the gallery function to run the slideshow	
	var timerFlash = setInterval('gallery()',speed);
	
	//pause the slideshow on mouse over - show flash scroll
	$('ul.slideshow').hover(
		function () {
			//alert("clear");
			clearInterval(timerFlash);
			//$('#flash-scroll').css({bottom:0});
			//$('#flash-back').css({bottom:0});
		}, 	
		function () {
			//$('#flash-scroll').css({bottom:-40});
			//$('#flash-back').css({bottom:-40});
			timerFlash = setInterval('gallery()',speed);			
		}
	);
}

function gallery() {

	var current = ($('ul.slideshow li.show') ?  $('ul.slideshow li.show') : $('ul.slideshow li:first'));

	var next = ((current.next().length) ? ((current.next().attr('class') == 'no-rotate') ? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
		
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	
	var t = next.attr("id");
	var tArr = t.split("-");
	t=tArr[1];
	$("#dot-" + t).removeClass('flash-dot-off');
	$("#dot-" + t).addClass('flash-dot-current');
	
	var u = current.attr("id");
	var uArr = u.split("-");
	u=uArr[1];

	$("#dot-" + u).removeClass('flash-dot-current');
	$("#dot-" + u).addClass('flash-dot-off');
	
	current.animate({opacity: 0.0}, 1000).removeClass('show');

}


function galleryNext() {
	var current = ($('ul.slideshow li.show') ?  $('ul.slideshow li.show') : $('ul.slideshow li:first'));
	var next = ((current.next().length) ? ((current.next().attr('class') == 'no-rotate') ? $('ul.slideshow li:first') : current.next()) : $('ul.slideshow li:first'));
	changeImage(next, current);
	changeDots(next, current);
}

function galleryPrev() {
	var current = ($('ul.slideshow li.show') ?  $('ul.slideshow li.show') : $('ul.slideshow li:first'));
	var next = ((current.prev().length) ? ((current.prev().attr('class') == 'no-rotate') ? $('ul.slideshow li:last').prev() :current.prev()) : $('ul.slideshow li:last').prev());
	changeImage(next, current);
	changeDots(next, current);
}

function gallerySelect(id) {
	var t = id;
	var tArr = t.split("-");
	t=tArr[1];
	var current = ($('ul.slideshow li.show') ?  $('ul.slideshow li.show') : $('ul.slideshow li:first'));
	var next = $('#flash-' + t);	
	changeImage(next, current);
	changeDots(next, current);
}


function changeDots(next, current) {
	var t = next.attr("id");
	var tArr = t.split("-");
	t=tArr[1];
	$("#dot-" + t).removeClass('flash-dot-off');
	$("#dot-" + t).addClass('flash-dot-current');
	$("#dot-" + t).removeClass('flash-dot-on');
	var u = current.attr("id");
	var uArr = u.split("-");
	u=uArr[1];
	$("#dot-" + u).removeClass('flash-dot-current');
	$("#dot-" + u).addClass('flash-dot-off');
}

function changeImage(next, current) {
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 300);
	current.animate({opacity: 0.0}, 300).removeClass('show');
}


