// Settings
var image_start = 1;
var image_end = 0;
var image_current = 1;
var anim_speed = 1000;
var anim_speed_hide_overlay = 150;
var anim_speed_show_overlay = 250;
var timeout = 5000;
var slideshow_timeout = false;

function slideshow_demo_labels(){
	$('span.slideshow_label').each(function(index, label){
		label = $(label);
		
		var settings = $(label.children('span.settings')[0])
		
		/*
		var font_weight			=	$(settings.children('span.font_weight')[0]).html();
		var font_size			=	$(settings.children('span.font_size')[0]).html();
		var font_family			=	$(settings.children('span.font_family')[0]).html();
		var font_color			=	$(settings.children('span.font_color')[0]).html();
		*/
		var background_color	=	$(settings.children('span.background_color')[0]).html();
		var background_image	=	$(settings.children('span.background_image')[0]).html();
		var border_size			=	$(settings.children('span.border_size')[0]).html();
		var border_color		=	$(settings.children('span.border_color')[0]).html();
		var width				=	$(settings.children('span.width')[0]).html();
		var height				=	$(settings.children('span.height')[0]).html();
		var position_y			=	$(settings.children('span.position_y')[0]).html();
		var position_x			=	$(settings.children('span.position_x')[0]).html();
		
		if (width != '' && width > 0){
			label.css('width', width+'px');
		}
		if (height != '' && height > 0){
			label.css('height', height+'px');
		}
		
		/*
		label.css('font-weight', font_weight);
		label.css('font-size', font_size);
		label.css('font-family', font_family);
		label.css('color', '#'+font_color);
		*/
		
		if (background_color != ''){
			label.css('background-color', '#'+background_color);
		}
		if (background_image != ''){
			label.css('background-image', 'url('+background_image+')');
			label.css('background-position', 'top left');
			label.css('background-repeat', 'no-repeat');
		}
		if (border_color != ''){
			label.css('border-style', 'solid');
			label.css('border-width', border_size+'px');
			label.css('border-color', '#'+border_color);
		}
	});
}

function slideshow_setup_labels(){
	$('div.slideshow_image_labels').each(function(index, container){
		container = $(container);
		container.css('display', 'block');
		
		var text = $('#'+(container.attr('id').replace('slideshow_image_labels_', 'slideshow_image_text_')));
		if (text.length){
			text.css('display', 'block');
			container.css('height', (230 - text.height())+'px');
			text.css('display', 'none');
		}
		
		if (image_end > 1){
			container.css('left', '16px');
			container.css('width', (960 - 32)+'px');
		}
		
		container.children('span.slideshow_label').each(function(index, label){
			label = $(label);
			
			var settings = $(label.children('span.settings')[0])
			
			var font_weight			=	$(settings.children('span.font_weight')[0]).html();
			var font_size			=	$(settings.children('span.font_size')[0]).html();
			var font_family			=	$(settings.children('span.font_family')[0]).html();
			var font_color			=	$(settings.children('span.font_color')[0]).html();
			var background_color	=	$(settings.children('span.background_color')[0]).html();
			var background_image	=	$(settings.children('span.background_image')[0]).html();
			var border_size			=	$(settings.children('span.border_size')[0]).html();
			var border_color		=	$(settings.children('span.border_color')[0]).html();
			var width				=	$(settings.children('span.width')[0]).html();
			var height				=	$(settings.children('span.height')[0]).html();
			var position_y			=	$(settings.children('span.position_y')[0]).html();
			var position_x			=	$(settings.children('span.position_x')[0]).html();
			
			if (width != '' && width > 0){
				label.css('width', width+'px');
			}
			if (height != '' && height > 0){
				label.css('height', height+'px');
			}
			
			label.css('font-weight', font_weight);
			label.css('font-size', font_size);
			label.css('font-family', font_family);
			
			label.css('color', '#'+font_color);
			if (background_color != ''){
				label.css('background-color', '#'+background_color);
			}
			if (background_image != ''){
				label.css('background-image', 'url('+background_image+')');
				label.css('background-position', 'top left');
				label.css('background-repeat', 'no-repeat');
			}
			if (border_color != ''){
				label.css('border-style', 'solid');
				label.css('border-width', border_size+'px');
				label.css('border-color', '#'+border_color);
			}
			
			var label_width, label_height;
				label_width = label.outerWidth();
				label_height = label.outerHeight();
			
			var top, left, bottom, right;
			switch (position_y){
				case 'top':
					top = 5;
				break;
				case 'middle':
					top = (230 / 2) - (label_height / 2);
				break;
				case 'bottom':
					bottom = 5;
				break;
			}
			switch (position_x){
				case 'left':
					left = 5;
				break;
				case 'center':
					left = (960 / 2) - (label_width / 2);
				break;
				case 'right':
					right = 5;
				break;
			}
			
			label.css('position', 'absolute');
			if (top){
				label.css('top', top+'px');
			}
			if (left){
				label.css('left', left+'px');
			}
			if (bottom){
				label.css('bottom', bottom+'px');
			}
			if (right){
				label.css('right', right+'px');
			}
		});
		
		container.css('display', 'none');
	});
}

function slideshow_set_timer(){
	slideshow_clear_timer();
	if (image_end > 1){

		slideshow_timeout = setTimeout("slideshow_next();", timeout);
	}
}

function slideshow_clear_timer(){
	clearTimeout(slideshow_timeout);
	slideshow_timeout = false;
}

function slideshow_next(){
	slideshow_clear_timer();
	if (image_current == image_end){
		slideshow_slide_right(image_current, image_start);
		image_current = image_start;
	}
	else{
		slideshow_slide_right(image_current, (image_current + 1));
		image_current = (image_current + 1);
	}
	slideshow_set_timer();
}

function slideshow_prev(){
	slideshow_clear_timer();
	if (image_current == 1){
		slideshow_slide_left(image_current, image_end);
		image_current = image_end;
	}
	else{
		slideshow_slide_left(image_current, (image_current - 1));
		image_current = (image_current - 1);
	}
	slideshow_set_timer();
}

function slideshow_slide_right(current_id, next_id){
	var current = $('#slideshow_image_'+current_id);
	var next = $('#slideshow_image_'+next_id);
	
	slideshow_hide_overlays(current_id);
	
	current.css('left', 0);
	current.animate({ left : -960 }, anim_speed);
	
	next.css('left', 960);
	next.animate({ left : 0 }, anim_speed, function(){ slideshow_show_overlays(next_id); });
}

function slideshow_slide_left(current_id, next_id){
	var current = $('#slideshow_image_'+current_id);
	var next = $('#slideshow_image_'+next_id);
	
	slideshow_hide_overlays(current_id);
	
	current.css('left', 0);
	current.animate({ left : 960 }, anim_speed);
	
	next.css('left', -960);
	next.animate({ left : 0 }, anim_speed, function(){ slideshow_show_overlays(next_id); });
}

function slideshow_hide_overlays(id){
	var prev = $('#slideshow_prev');
	var next = $('#slideshow_next');
	var text = $('#slideshow_image_text_'+id);
	var link = $('#slideshow_image_link_'+id);
	var labels = $('#slideshow_image_labels_'+id);
	
	prev.animate({ opacity : 0.0 }, anim_speed_hide_overlay, function(){ prev.css('display', 'none'); });
	next.animate({ opacity : 0.0 }, anim_speed_hide_overlay, function(){ next.css('display', 'none'); });
	
	if (text.length){
		text.animate({ opacity : 0.0 }, anim_speed_hide_overlay, function(){ text.css('display', 'none'); });
	}
	if (link.length){
		link.hide();
	}
	if (labels.length){
		//labels.animate({ opacity : 0.0 }, anim_speed_hide_overlay, function(){ text.css('display', 'none'); });
		labels.hide();
	}
}

function slideshow_show_overlays(id){
	var prev = $('#slideshow_prev');
	var next = $('#slideshow_next');
	var text = $('#slideshow_image_text_'+id);
	var link = $('#slideshow_image_link_'+id);
	var labels = $('#slideshow_image_labels_'+id);
	
	if (image_end > 1){
		if (text.length){
			text.css('left', '16px');
			text.css('width', (960 - 32)+'px');
		}
		if (link.length){
			link.css('left', '16px');
			link.css('width', (960 - 32)+'px');
		}
		
		prev.css('opacity', '0');
		prev.css('display', 'block');
		prev.animate({ opacity : 0.5 }, anim_speed_show_overlay);
		
		next.css('opacity', '0');
		next.css('display', 'block');
		next.animate({ opacity : 0.5 }, anim_speed_show_overlay);
	}
	
	if (text.length){
		text.css('opacity', '0');
		text.css('display', 'block');
		text.animate({ opacity : 0.5 }, anim_speed_show_overlay);
	}
	if (link.length){
		link.show();
	}
	if (labels.length){
		//labels.css('opacity', '0');
		//labels.css('display', 'block');
		//labels.animate({ opacity : 1.0 }, anim_speed_show_overlay);
		labels.show();
	}
}
