jQuery.noConflict();

jQuery(document).ready(function(){
	sliderfx('player', 'vertical', 400, true, false, 8000);
});

function sliderfx(context, direction, speed, allowLoop, omitSteps, timer){
	if( jQuery('#'+context).length>0 ){
		// alert('do all');
		sliderfxHtmlSetup(context);
	
		var direction = (typeof(direction)!='undefined') ? direction : 'horizontal';
		direction = (direction=='vertical') ? 'vertical' : 'horizontal';
		var speed = (typeof(speed)!='undefined') ? speed : 400;
		var allowLoop = (typeof(allowLoop)!='undefined') ? allowLoop : true;
		var omitSteps = (typeof(omitSteps)!='undefined') ? omitSteps : false;
	
		if(timer){
			allowLoop = true;
		}
	
		if(omitSteps){
			jQuery('#'+context).addClass('omit-steps');
		} else {
			jQuery('#'+context).addClass('show-steps');
		}
	
		jQuery('#'+context).addClass(direction);
	
		if(jQuery('#'+context+' .sfx-group').length > 1){
			var currentSlide = jQuery('#'+context+' .sfx-thumb.selected').closest('.sfx-group').attr('id');
			currentSlide = (!currentSlide) ? context+'-0' : currentSlide;
			sliderfxScrollTo(context, '#'+currentSlide, 0);
			sliderfxDisableArrows(context, allowLoop);
		
			jQuery('#'+context+'-arrows .next').click(function(evt){
				evt.preventDefault();
				var target = sliderfxFindNext(context, allowLoop);
				sliderfxScrollTo(context, target, speed);
				sliderfxDisableArrows(context, allowLoop);

				sliderfxAutoClickStop();
				sliderfxAutoClickStart(timer, context, speed);
			});

			jQuery('#'+context+'-arrows .prev').click(function(evt){
				evt.preventDefault();
				var target = sliderfxFindPrev(context, allowLoop);
				sliderfxScrollTo(context, target, speed);
				sliderfxDisableArrows(context, allowLoop);

				sliderfxAutoClickStop();
				sliderfxAutoClickStart(timer, context, speed);
			});
		
			jQuery('#'+context+'-arrows .specific').click(function(evt){
				evt.preventDefault();
				var target = jQuery(this).attr('href');
				target = '#'+(target.split('#').pop());
				sliderfxScrollTo(context, target, speed);
				sliderfxDisableArrows(context, allowLoop);

				sliderfxAutoClickStop();
				sliderfxAutoClickStart(timer, context, speed);
			});

		} else {
			jQuery('#'+context+'-arrows .next, #'+context+'-arrows .prev, #'+context+'-arrows .specific').css('visibility', 'hidden');
		}
	
		sliderfxAutoClickStart(timer, context, speed);
	} else {
		// alert('do nothing');
	}
}

function sliderfxHtmlSetup(context){
	jQuery('#'+context).addClass('sfx-slider-wrap');
	
	var i=0;
	jQuery('#'+context).children().each(function(){
		jQuery(this).wrap('<div id="'+context+'-'+i+'" class="sfx-group"><div class="sfx-thumb"></div></div>');
		i++;
	});
	
	if(i>0){
		jQuery('<div id="'+context+'-arrows" class="sfx-slider-arrows"></div>').insertAfter(jQuery('#'+context));
		for(k=0;k<i;k++){
			jQuery('#'+context+'-arrows').append('<a href="#'+context+'-'+k+'" class="specific">'+(k+1)+'</a>');
		}
		jQuery('#'+context+'-arrows').append('<a href="#'+context+'" class="prev">prev</a>');
		jQuery('#'+context+'-arrows').append('<a href="#'+context+'" class="next">next</a>');
	}
	jQuery('#'+context).children().wrapAll('<div class="sfx-slider" />');
}

function sliderfxScrollTo(context, target, speed){
	var total = jQuery('#'+context+' .sfx-group').length;
	var selected_id = sliderfxFindSelectedGroup(context);
	var selected_index = parseFloat(selected_id.split('-').pop());
	var destination_index = parseFloat(target.split('-').pop());
	var index_variation = selected_index-destination_index;
	var axis = (jQuery('#'+context).hasClass('vertical')) ? 'top' : 'left';
	
	
	if(jQuery('#'+context).hasClass('show-steps')){
		// show all steps
		
		if(axis=='left'){
			jQuery('#'+context+' .sfx-slider').animate({ left: (-100*destination_index)+'%' }, speed);
		} else {
			jQuery('#'+context+' .sfx-slider').animate({ top: (-100*destination_index)+'%' }, speed);
		}

	} else {
		// omit steps		
		
		jQuery('#'+context+' .sfx-group').hide();
		jQuery('#'+context+'-'+selected_index).show();
		jQuery('#'+context+'-'+destination_index).show();
		index_variation = (index_variation>0) ? 1 : (index_variation<0) ? -1 : 0;

		if(index_variation<0){
			if(axis=='left'){
				jQuery('#'+context+' .sfx-slider').animate({ left: '-100%' }, speed, function(){
					jQuery('#'+context+'-'+selected_index).hide();
					jQuery('#'+context+' .sfx-slider').css('left', 0);
				});
			} else {
				jQuery('#'+context+' .sfx-slider').animate({ top: '-100%' }, speed, function(){
					jQuery('#'+context+'-'+selected_index).hide();
					jQuery('#'+context+' .sfx-slider').css('top', 0);
				});
			}
		}

		if(index_variation>0){
			if(axis=='left'){
				jQuery('#'+context+' .sfx-slider').css('left', '-100%');
				jQuery('#'+context+' .sfx-slider').animate({ left: '-0%' }, speed, function(){
					jQuery('#'+context+'-'+selected_index).hide();
				});
			} else {
				jQuery('#'+context+' .sfx-slider').css('top', '-100%');
				jQuery('#'+context+' .sfx-slider').animate({ top: '-0%' }, speed, function(){
					jQuery('#'+context+'-'+selected_index).hide();
				});
			}
		}
	}

	sliderfxMarkSelectedGroup(context, target);
}

function sliderfxMarkSelectedGroup(context, target){
	jQuery('#'+context+' .sfx-group').removeClass('selected');
	jQuery(target).addClass('selected');
	sliderfxMarkSliderMenu(context, target);
}

function sliderfxMarkSliderMenu(context, target){
	jQuery('#'+context+'-arrows a').removeClass('selected');
	jQuery('#'+context+'-arrows a[href$='+target+']').addClass('selected');
}

function sliderfxFindSelectedGroup(context){
	if(!jQuery('#'+context+' .sfx-group').hasClass('selected')){
		jQuery('#'+context+' .sfx-slider .sfx-group:first-child').addClass('selected');
	}
	return(jQuery('#'+context+' .sfx-group.selected').attr('id'));
}

function sliderfxFindNext(context, allowLoop){
	var total = jQuery('#'+context+' .sfx-group').length;
	var selected_id = sliderfxFindSelectedGroup(context);
	var selected_index = parseFloat(selected_id.split('-').pop());
	if(allowLoop){
		var target = (selected_index == total-1) ? '#'+context+'-0' : '#'+context+'-'+(selected_index+1);
	} else {
		var target = (selected_index == total-1) ? '#'+context+'-'+selected_index : '#'+context+'-'+(selected_index+1);
	}
	return target;
}

function sliderfxFindPrev(context, allowLoop){
	var total = jQuery('#'+context+' .sfx-group').length;
	var selected_id = sliderfxFindSelectedGroup(context);
	var selected_index = parseFloat(selected_id.split('-').pop());
	if(allowLoop){
		var target = (selected_index == 0) ? '#'+context+'-'+(total-1) : '#'+context+'-'+(selected_index-1);
	} else {
		var target = (selected_index == 0) ? '#'+context+'-'+selected_index : '#'+context+'-'+(selected_index-1);
	}
	return target;
}

function sliderfxDisableArrows(context, allowLoop){
	var total = jQuery('#'+context+' .sfx-group').length;
	var selected_id = sliderfxFindSelectedGroup(context);
	var selected_index = parseFloat(selected_id.split('-').pop());
	if(!allowLoop){
		if(selected_index == 0){
			jQuery('#'+context+'-arrows .prev').addClass('disabled');
			jQuery('#'+context+'-arrows .next').removeClass('disabled');
		} else if(selected_index == total-1){
			jQuery('#'+context+'-arrows .prev').removeClass('disabled');
			jQuery('#'+context+'-arrows .next').addClass('disabled');
		} else {
			jQuery('#'+context+'-arrows .prev').removeClass('disabled');
			jQuery('#'+context+'-arrows .next').removeClass('disabled');
		}
	}
}

function sliderfxAutoClickStart(timer, context, speed){
	if(timer){
		sliderfxAutoClickRepeat = setInterval( function() { sliderfxAutoClick(context, speed); }, timer );
	}
}

function sliderfxAutoClickStop(){
	if(typeof(sliderfxAutoClickRepeat)!='undefined'){
		clearInterval( sliderfxAutoClickRepeat );
	}
}

function sliderfxAutoClick(context, speed){
	var target = sliderfxFindNext(context, true);
	sliderfxScrollTo(context, target, speed);
}
