// fade the slides
function com_slideshow_fadeToNext(slideshowID){
	
	var interval = 	parseInt(com_slideshow[slideshowID]['interval']);
	var duration = 	parseInt(com_slideshow[slideshowID]['duration']);
	var last = 		parseInt(com_slideshow[slideshowID]['last']);
	var previous = last;

	if(com_slideshow[slideshowID]['current'] == (last - 1)){
		previous = (last - 1)
		com_slideshow[slideshowID]['current'] = 0;
	}else{
		previous = com_slideshow[slideshowID]['current'];
		com_slideshow[slideshowID]['current'] ++;
	}
	
	var current  =  parseInt(com_slideshow[slideshowID]['current']);

	//var imgs = $$('#com_slideshow_'+slideshowID+' li.slideshowSlideContainer');
	var imgs = '';
	if(imgs == ''){
		imgs = $$('#com_slideshow_'+slideshowID+' li.slideshowSlideContainerEmpty');
	}
	var slide = new Fx.Elements(imgs, {wait: false, duration: duration, transition: Fx.Transitions.Cubic.easeOut});
	
	var o = {};
	//foreach of the images hide the others and show the current
	imgs.each(function(image, i){
		if(current == i){
			o[i] = {'opacity': [0,1]};
		}else if(previous == i){
			o[i] = {'opacity': [1,0]};
		}else{
			image.set('opacity',0);
		}
	});

	slide.start(o);
	
	setTimeout("com_slideshow_fadeToNext('"+slideshowID+"')",interval);
}


function com_slideshow_next(slideshowID){
	com_slideshow_slideToNext(slideshowID);	
}

function com_slideshow_previous(slideshowID){
	var direction = com_slideshow[slideshowID]['direction'];
	var last = parseInt(com_slideshow[slideshowID]['last']);

	//alert(com_slideshow[slideshowID]['current']+"\n"+last);

	switch(direction){
		case 'SLIDE_LEFT':
		case 'SLIDE_UP':
			if(com_slideshow[slideshowID]['current'] == 0){
				com_slideshow[slideshowID]['current'] = last - 2;
			}else{
				com_slideshow[slideshowID]['current'] = com_slideshow[slideshowID]['current'] - 2;
			}
			break;
		case 'SLIDE_RIGHT':
		case 'SLIDE_DOWN':
			if(com_slideshow[slideshowID]['current'] == last - 1){
				com_slideshow[slideshowID]['current'] = 1;
			}else{
				com_slideshow[slideshowID]['current'] = com_slideshow[slideshowID]['current'] + 2;
			}
			break;
	}

	com_slideshow_slideToNext(slideshowID);
}


//slide the slides
function com_slideshow_slideToNext(slideshowID){

	var interval = 	parseInt(com_slideshow[slideshowID]['interval']);
	var duration = 	parseInt(com_slideshow[slideshowID]['duration']);
	var last = 		parseInt(com_slideshow[slideshowID]['last']);
	var width = 	parseInt(com_slideshow[slideshowID]['width']);
	var height = 	parseInt(com_slideshow[slideshowID]['height']);
	var controls = 	parseInt(com_slideshow[slideshowID]['controls']);
	var direction = com_slideshow[slideshowID]['direction'];

	var scroll = new Fx.Scroll('com_slideshow_wrap_'+slideshowID, {
		wait: false,
		duration: duration,
		transition: Fx.Transitions.Cubic.easeOut
	});
	
	switch(direction){
		case 'SLIDE_LEFT':
		case 'SLIDE_UP':
			if(com_slideshow[slideshowID]['current'] == (last - 1)){
				com_slideshow[slideshowID]['current'] = 0;
			}else{
				com_slideshow[slideshowID]['current'] ++;
			}
			break;
		case 'SLIDE_RIGHT':
		case 'SLIDE_DOWN':
			if(com_slideshow[slideshowID]['current'] == 0){
				com_slideshow[slideshowID]['current'] = (last - 1);
			}else{
				com_slideshow[slideshowID]['current'] --;
			}
			break;
	}
	
	//scroll.toElement("com_slideshow_"+slideshowID+"_slide_"+com_slideshow[slideshowID]['current']);
	switch(direction){
		case 'SLIDE_LEFT':
		case 'SLIDE_RIGHT':
			scroll.start(width * com_slideshow[slideshowID]['current'],'0');
			break;
		case 'SLIDE_UP':
		case 'SLIDE_DOWN':
			scroll.start('0', height * com_slideshow[slideshowID]['current']);
			break;
	}
	
	if(controls == 0){
		setTimeout("com_slideshow_slideToNext('"+slideshowID+"')",interval);
	}
}

window.addEvent('domready', function(){
 
 	var posOut = 150, posIn = 0;
 
	var slides = $$(".slideshowSlideContainer");
	var info = $$(".slideshowSlideDetails");
	var fx = new Fx.Elements(info, {wait: false, duration: 350, transition: Fx.Transitions.Cubic.easeOut});
	
	slides.each(function(slide, i){
		slide.addEvent("mouseenter", function(event){
			var o = {};
			info.each(function(inf, j){
				switch(inf.title){
					case 'TOP' :
						posOut = 0; posIn = -100;
						break;
					case 'BOTTOM' :
						posOut = 0; posIn = 100;
						break;
					case 'LEFT' :
						posOut = 0; posIn = -150;
						break;
					case 'RIGHT' :
						posOut = 0; posIn = 150;
						break;
				}
				if(inf.title == 'BOTTOM' || inf.title == 'TOP'){
					if(i == j){
						o[j] = {marginTop: [inf.getStyle("marginTop").toInt(), posOut]}
					}else{
						o[j] = {marginTop: [inf.getStyle("marginTop").toInt(), posIn]}
					}
				}else{
					if(i == j){
						o[j] = {marginLeft: [inf.getStyle("marginLeft").toInt(), posOut]}
					}else{
						o[j] = {marginLeft: [inf.getStyle("marginLeft").toInt(), posIn]}
					}
				}
				
			});
			fx.start(o);
		});
	});

	$$(".slideshowSlideContainer").addEvent("mouseleave", function(event){
		var o = {};
		info.each(function(inf, i){
			switch(inf.title){
				case 'TOP' :
					posOut = 0; posIn = -100;
					break;
				case 'BOTTOM' :
					posOut = 0; posIn = 100;
					break;
				case 'LEFT' :
					posOut = 0; posIn = -150;
					break;
				case 'RIGHT' :
					posOut = 0; posIn = 150;
					break;
			}
			if(inf.title == 'BOTTOM' || inf.title == 'TOP'){
				o[i] = {marginTop: [inf.getStyle("marginTop").toInt(), posIn]}
			}else{
				o[i] = {marginLeft: [inf.getStyle("marginLeft").toInt(), posIn]}
			}
			
		});
		fx.start(o);
	});

});













