
jQuery.noConflict();

jQuery (function() {

    jQuery (".accordion").accordion(
		{active: false}
	);
	jQuery (".accordion a, .accordion h3").click(function() {
		jQuery (this).blur();
	});

	jQuery (".sliderText").eq(0).addClass("slider-selected");
	jQuery (".sliderContent").eq(0).addClass("sliderContent-selected");
	// set click-methods
	jQuery (".sliderArrow-left a").click(function(ev, ui) {
		this.blur();
		slideTo(jQuery ("#sliderTextbox .slider-selected").index()-1);
	});
	jQuery (".sliderArrow-right a").click(function(ev, ui) {
		this.blur();
		slideTo(jQuery ("#sliderTextbox .slider-selected").index()+1);
	});
	
	jQuery (".sliderText:first").addClass("sliderText-first");
	jQuery (".sliderText:last").addClass("sliderText-last");
		
	
	jQuery (".sliderText a").click(function(ev, ui) {
		this.blur();
		slideTo(jQuery (this).parent().index());
	});
	
	// set href to void(0)
	jQuery (".sliderArrow a, .sliderText a").attr("href", "javascript:void(0)");
	
	jQuery ("#sliderfront").draggable(
		{ 
			axis: 'x',
			containment: 'parent',
			drag: function(ev, ui) {
				jQuery ("#sliderElement").css('left', jQuery ("#sliderfront").css('left'));
				jQuery ("#sliderElement").data('droppedIndex', -1);
			},
			stop: function(ev, ui) {
				index = jQuery (this).index();
				droppedIndex = jQuery ("#sliderElement").data('droppedIndex');
				
				// move to selected
				selectedIndex = jQuery ("#sliderTextbox .slider-selected").index();
				offsetToParent = jQuery ("#sliderTextbox .slider-selected").position().left;
				
				offsetToParent += parseInt(jQuery (".slider-selected").css('margin-left'));
									
				//jQuery ("#debug").append('offset: '+offsetToParent);
				
				jQuery ("#sliderElement, #sliderfront").animate({
					left: offsetToParent
				  }, 500
				);
			}
		}
	);
	jQuery (".sliderText").droppable(
		{
			accept: "#sliderfront",
			hoverClass: 'slider-hover',
			drop: function(ev, ui) {
				index = jQuery (this).index();
				selectedIndex = jQuery ("#sliderTextbox .slider-selected").index();
				
				jQuery (".sliderText").removeClass('slider-selected');
				jQuery (this).addClass('slider-selected');
				jQuery ("#sliderElement").data('droppedIndex', index);
				
				if(index != selectedIndex)
				{
					jQuery (".sliderContent").fadeOut(500);
					jQuery (".sliderContent").eq(index).fadeIn(500);
				}
			}
		}
	);	
});

function slideTo(index)
{
	count = jQuery (".sliderText").length;
	
	if(index >= count)
		index = 0;
	else if(index < 0)
		index = count-1;
		
	slideSelect(index);
}

function slideSelect(index)
{
	jQuery (".sliderText").removeClass('slider-selected');
	jQuery (".sliderText").eq(index).addClass('slider-selected');
	jQuery ("#sliderElement").data('droppedIndex', index);

	// move to selected
	selectedIndex = jQuery ("#sliderTextbox .slider-selected").index();
	offsetToParent = jQuery ("#sliderTextbox .slider-selected").position().left;
	
	offsetToParent += parseInt(jQuery (".slider-selected").css('margin-left'));
						
	//jQuery ("#debug").append('offset: '+offsetToParent);
	
	jQuery ("#sliderElement, #sliderfront").animate({
		left: offsetToParent,
		specialEasing: {
		  left: 'easeOutBounce'
		}
	  }, 500
	);
	
	jQuery (".sliderContent").fadeOut(500);
	jQuery (".sliderContent").eq(index).fadeIn(500);
}

