// JS Document

var inter;
var totWidth = 0;
var curid = 0;
var dm_wi = dm_he = 0;
var tog = 1;
var tTime = .2;
var delay = 5;
var inmotion = false;
var arrow = false;
var auto = true;
var tabs = false;
var num_photos = 0;

$(document).ready(function() {
	
	if ($('#dm-slideshow-tabs ul li').size() > 0) {
		tabs = true;
	}
	
	var str = '<div class="dm_arrow"><div><div id="larrow"></div></div></div><div class="dm_arrow"><div><div id="rarrow"></div></div></div><div id="dm_mover"><div id="imger1" class="dm_left"></div></div>';
	var num = 0;
	$('#dm-slideshow img').each(function() {
		dm_wi = Number($(this).attr('width'));
		dm_he = Number($(this).attr('height'));
		$(this).addClass('dm-hide');
		totWidth += $(this).width();
		num++
	});
	
	num_photos = num;
	
	$('#dm-slideshow').append(str);
	
	$('#dm-slideshow').width(dm_wi);
	if (tabs) {
		$('#dm-slideshow-tabs').width(dm_wi);
		$('#dm-slideshow-tabs ul li').filter(function(index) {
			return index == 0;
		}).addClass('active');
		$('#dm-slideshow-tabs ul li').each(function() {
			$(this).click(function(e) { 
				gotoTab($(this).index());
			});
		});
	} else {
		$('#dm-slideshow-tabs').hide();
	}
	$('#dm-slideshow').height(dm_he);
	
	if (num > 1) {
		setupArrows();
	}
	
	$('#imger1').width(dm_wi).height(dm_he).css('background', 'url('+getImageInfo(curid)[0]+') no-repeat');
	$('#imger1').click(function() {
		if (!inmotion) { switchImage(); }
	});
	$('#imger1').click(function(e) {
	    window.location.href = getImageInfo(curid)[3];
	});
	
	if (num > 1) {
		interval();
	}
	
});

function gotoTab(id) {
	if (curid < id) {
		switchImage(1,id);
	} else {
		switchImage(2,id);
	}
	interval();
}

function interval() {
	if ((auto) && (num_photos > 1)) {
		clearInterval(inter);
		inter = setInterval('switchImage()', (delay*1000));
	}
}

function autoIt(de,tt,au) {
	delay = de;
	tTime = tt;
	auto = false;
	if (au == 1) {
		auto = true;
	}
	interval();
}

function arrows(ar) {
	if (ar == 1) {
		arrow = true;
	}
	setupArrows();
}

function switchImage(dir,setid) {
	
	if (setid == undefined) {
		setid = null;
	}
	
	if (dir == 2) {
		
		var str = '<div id="imger1" class="dm_left"></div><div id="imger2" class="dm_left"></div>';
		$('#dm_mover').html(str);
		$('#dm_mover').width((dm_wi*2));
		var before = curid;
		if (setid != null) {
			curid = setid;
		} else if (curid == 0) {
			curid = ($("#dm-slideshow img").length-1);
		} else {
			curid--;
		}
		
		$('#imger1').width(dm_wi).height(dm_he).css('background', 'url('+getImageInfo(curid)[0]+') no-repeat');
		$('#imger1').click(function(e) {
		    window.location.href = getImageInfo(curid)[3];
		});
		$('#imger2').width(dm_wi).height(dm_he).css('background', 'url('+getImageInfo(before)[0]+') no-repeat');
		$('#dm_mover').offset({top : $('#dm_mover').offset().top, left : ($('#dm_mover').offset().left-dm_wi)});
		inmotion = true;
		$('#dm_mover').animate( { left: '0px' }, 
		(tTime*1000), 
		function(e) {
    		$('#imger2').remove();
    		tog = 1;
    		inmotion = false;
  		});
  		
	} else {
		
		var str = '<div id="imger2" class="dm_left"></div><div id="imger1" class="dm_left"></div>';
		$('#dm_mover').html(str);
		$('#dm_mover').width((dm_wi*2));
		var before = curid;
		if (setid != null) {
			curid = setid;
		} else if (curid == ($("#dm-slideshow img").length-1)) {
			curid = 0;
		} else {
			curid++;
		}
		
		$('#imger2').width(dm_wi).height(dm_he).css('background', 'url('+getImageInfo(before)[0]+') no-repeat');
		$('#imger1').width(dm_wi).height(dm_he).css('background', 'url('+getImageInfo(curid)[0]+') no-repeat');
		$('#imger1').click(function(e) {
		    window.location.href = getImageInfo(curid)[3];
		});
		inmotion = true;
		$('#dm_mover').animate( { left: '-'+dm_wi+'px' }, 
		(tTime*1000), 
		function(e) {
    		$('#imger2').remove();
    		$('#dm_mover').offset({top : $('#dm_mover').offset().top, left : ($('#dm_mover').offset().left+dm_wi)});
    		inmotion = false;
  		});
    
    }
    
    if (tabs) {
    	$('#dm-slideshow-tabs ul li').each(function() {
			$(this).removeClass('active');
		});
		$('#dm-slideshow-tabs ul li').filter(function(index) {
			return index == curid;
		}).addClass('active');
	}
    
}

function getImageInfo(id) {

	var src = $('#dm-slideshow img').filter(function(index) {
		return index == id;
	}).attr('src');
	var w = $('#dm-slideshow img').filter(function(index) {
		return index == id;
	}).attr('width');
	var h = $('#dm-slideshow img').filter(function(index) {
		return index == id;
	}).attr('height');
	var url = $('#dm-slideshow img').filter(function(index) {
		return index == id;
	}).attr('url');
	
	return new Array(src,w,h,url)
}

function setupArrows() {
	
	if (arrow) {
		$('.dm_arrow').height(dm_he);
		$('#rarrow').parent().parent().css('margin-left' , (dm_wi-$('#rarrow').width())+'px');
		$('#larrow').parent().css('padding', Math.ceil((dm_he/2)-($('#larrow').height()/2))+'px 0 0 0');
		$('#rarrow').parent().css('padding', Math.ceil((dm_he/2)-($('#rarrow').height()/2))+'px 0 0 0');
		
		$('#larrow').click(function(e) {
			switchImage(1);
			interval();
		});
		$('#rarrow').click(function(e) {
			switchImage(2);
			interval();
		});
	} else {
		$('#larrow').parent().addClass('dm-hide');
		$('#rarrow').parent().addClass('dm-hide');
	}
	
}
