var mode = '';
var parent_width = '';
var parent_height = '';
var duration = '';


var slideshowTimer = 0;

 


function manageSlideshow(slide_duration, transition_mode) {
	mode = transition_mode;
	duration = slide_duration;
	
	// getting dimension for trans mode
	parent_height = $('#slideshow_container').height();
	parent_width = $('#slideshow_container').width();
	slideshowTimer = setInterval ("slideSwitch('transition_mode', parent_height, parent_width)", duration);
}
function slideRevSwitch(trans_mode, cont_height, cont_width) {
	var $active = $('#slideshow_container div.slideshow_active');


	if ($active.length == 0)
		$active = $('#slideshow_container div:last');

	var $next = $active.prev().length ? $active.prev()
			: $('#slideshow_container div:last');

	$active.addClass('last-active');

	var double_width = 2 * cont_width;
	
	if (mode == 'push_slide') {
		$next.css({
			height: cont_height, 
			width: cont_width,
			left: cont_width,
		}).addClass('slideshow_active').animate({
			height: cont_height, 
			width: cont_width,
			left: 0
		}, 500, function() {
			$active.removeClass('slideshow_active last-active');
		});
		$active.css({
			height: cont_height, 
			width: cont_width,
			left: 0
		}).animate({
			height: cont_height, 
			width: cont_width,
			left: -cont_width
		}, 500, function() {
		});
	} else if (mode == 'edge_wipe') {
		$next.css({
			height: cont_height, 
			width: "0px",
			left: cont_width
		}).addClass('slideshow_active').animate({
			height: cont_height, 
			width: cont_width,
			left: "0px"
		}, 1000, function() {
			$active.removeClass('slideshow_active last-active');
		});
	} else if (mode == 'cross_dissolve') {
		$next.css({
			opacity: 0.0
		}).addClass('slideshow_active').animate({
			opacity: 1.0
		}, 1000, function() {
			$active.removeClass('slideshow_active last-active');
		});
	}
	
	
 

}

function slideSwitchExt() {
	clearInterval(slideshowTimer);
	slideSwitch('mode', parent_height, parent_width);
}
function slideRevSwitchExt() {
	clearInterval(slideshowTimer);
	slideRevSwitch('mode', parent_height, parent_width);
}

function dotSwitch(sw_element) {
	clearInterval(slideshowTimer);
	var $id = $(sw_element).attr('alt');
	var $active = $('#slideshow_container div.slideshow_active');
	var $active_id = $active.attr('id').substring(10);
	console.log('active id:' + $active_id);
	console.log('id:' + $id);
	
	if ($active.length == 0)
		$active = $('#slideshow_container div:last');
	//$active.addClass('last-active');
	
	var $next = $active.next().length ? $active.next()
			: $('#slideshow_container div:first');
	
	if (mode == 'push_slide') {
	  if (parseInt($active_id) != parseInt($id)) {

			$('#slideshow_' + $id).css({
				height: parent_height, 
				width: parent_width,
				left: -parent_width
			}).addClass('slideshow_active').animate({
				height: parent_height, 
				width: parent_width,
				left: 0
			}, 500, function() {
			}); 

			$active.css({
				height: parent_height, 
				width: parent_width,
				left: 0
			}).animate({
				height: parent_height, 
				width: parent_width,
				left: parent_width
			}, 500, function() {
			});
			$active.addClass('last-active').removeClass('slideshow_active');
			//$active.removeClass('last-active');
		  
	  }
	} else if (mode == 'edge_wipe') {
		$('#slideshow_' + $id).css({
			height: parent_height, 
			width: "0px"
		}).addClass('slideshow_active').animate({
			height: parent_height, 
			width: parent_width
		}, 1000, function() {
			$active.removeClass('slideshow_active last-active');
		});
	} else if (mode == 'cross_dissolve') {
		$('#slideshow_' + $id).css({
			opacity : 0.0
		}).addClass('slideshow_active').animate({
			opacity : 1.0
		}, 1000, function() {
			$active.removeClass('slideshow_active last-active');
		});
	}
}

function slideSwitch(trans_mode, cont_height, cont_width) {
	var $active = $('#slideshow_container div.slideshow_active');

	if ($active.length == 0)
		$active = $('#slideshow_container div:last');

	var $next = $active.next().length ? $active.next()
			: $('#slideshow_container div:first');

	$active.addClass('last-active');

	if (mode == 'push_slide') {
		$next.css({
			height: cont_height, 
			width: cont_width,
			left: -cont_width,
		}).addClass('slideshow_active').animate({
			height: cont_height, 
			width: cont_width,
			left: 0
		}, 500, function() {
			$active.removeClass('slideshow_active last-active');
		});
		$active.css({
			height: cont_height, 
			width: cont_width,
			left: 0
		}).animate({
			height: cont_height, 
			width: cont_width,
			left: cont_width
		}, 500, function() {
		});
	} else if (mode == 'edge_wipe') {
		$next.css({
			height: cont_height, 
			width: "0px"
		}).addClass('slideshow_active').animate({
			height: cont_height, 
			width: cont_width
			}, 1000, function() {
			$active.removeClass('slideshow_active last-active');
		});
	} else if (mode == 'cross_dissolve') {
		$next.css({
			opacity: 0.0
		}).addClass('slideshow_active').animate({
			opacity: 1.0
			}, 1000, function() {
			$active.removeClass('slideshow_active last-active');
		});
	}

}


$(document).ready(function() {

	// switching slides
	$('.slideshow_dot').click(function () {
		dotSwitch(this);
	});

	 
	$('#right_arrow').click(function() {
		slideSwitchExt();
	});
	$('#left_arrow').click(function() {
		slideRevSwitchExt();
	});
	/*
	 if(window.location.hash) {
	 handleLocationHash(window.location.hash);
	 }
	 */

});

