$(document).ready(function(){
	SC.Slider.init();
});
SC.Slider = {
	previousVal: 0,
	slideEnabled: 1,
	handle: {
		range: null,
		center: null,
		width: null
	},
	labels: [null],
	init: function (val) {
		SC.Slider.getLabels();
		SC.Slider.setSlider();
		SC.Slider.getHandleInfo();
	},
	getLabels: function () {
		$('#slider-bar > span').each(function () {
			var i = SC.Slider.labels.length;
			var left = parseInt($(this).css('left'), 10) - 111;
			var width = $(this).width();

			SC.Slider.labels[i] = {
				el: $(this),
				center: (left + parseInt(width / 2, 10)),
				width: width
			};
		});
	},
	getHandleInfo: function () {
		var barWidth = $('#slider-bar').width();
		var handleWidth = $('#slider-handle').width();
		var handleLeft = parseInt($('#slider-handle').css('left'), 10);
		SC.Slider.handle.width = handleWidth;
		SC.Slider.handle.center = (handleLeft + parseInt(handleWidth / 2, 10));
		SC.Slider.handle.range = barWidth - handleWidth;
	},
	setSlider: function () {
		$("#slider-bar").slider({
		    handle: '#slider-handle',
			animate: true,
			min: 0,
			max: 100,
			slide: function(event, ui) {
				SC.Slider.prepChange(ui.value)
				SC.Slider.slidePosition(ui.value);
				SC.Slider.slideLabelOpacity();
			},
			change: function(event, ui) {
				SC.Slider.prepChange(ui.value)
				SC.Slider.changePosition();
				SC.Slider.changeLabelOpacity();
			}
		});
	},
	enableSlide: function () {
		SC.Slider.slideEnabled = 1;
	},
	prepChange: function (val) {
		SC.Slider.val = val;
		SC.Slider.difference = ((val - SC.Slider.previousVal) < 0) ? (SC.Slider.previousVal - val) : (val - SC.Slider.previousVal);
		SC.Slider.duration = 32 * SC.Slider.difference;
		SC.Slider.previousVal = val;
	},
	slidePosition: function (val) {
		if(SC.Slider.slideEnabled) {
			SC.Slider.previousVal = val;
			var imgWidth = $('#image').attr('offsetWidth');
			var sldWidth = $('#slider-container').attr('offsetWidth');
	
			var factor = imgWidth / sldWidth - 2.17;
			val = val * -factor;
			$('#image').css('left', val + '%');
		}
	},
	changePosition: function () {
		SC.Slider.slideEnabled = 0;
		var imgWidth = $('#image').attr('offsetWidth');
		var sldWidth = $('#slider-container').attr('offsetWidth');
		var factor = imgWidth / sldWidth - 2.17;
	
		var val = parseInt((SC.Slider.previousVal * -factor),10);
		
		$("#image").animate(
			{left: val + '%'},
			{duration: SC.Slider.duration,
			easing: null,
			complete: SC.Slider.enableSlide,
			queue: false});
	},
	slideLabelOpacity: function () {
		if(SC.Slider.slideEnabled) {
			var position = (SC.Slider.val / 100) * SC.Slider.handle.range;
			var labels = SC.Slider.labels;
			var duration = SC.Slider.duration * .8;
			var el, center, width, gap;
		
			for(var i = 1; i < labels.length; i++) {
				el = labels[i].el;
				center = labels[i].center;
				width = (labels[i].width > SC.Slider.handle.width) ? labels[i].width : SC.Slider.handle.width;
				width = width / 2;
				gap = ((position - center) < 0) ? (center - position) : (position - center);
				if(gap < width) {
					if(el.css('color') != 'white') {
						el.css({
							'color': 'white'
						}, {
							duration: duration,
							easing: null,
							queue: false
						});
					}
				}
				else {
					if(el.css('color') != '#8299AD') {
						el.css({
							'color': '#8299AD'
						});
					}
				}
			}
		}
	},
	changeLabelOpacity: function () {
		var position = (SC.Slider.val / 100) * SC.Slider.handle.range;
		var labels = SC.Slider.labels;
		var el, center, width, gap;
		
		for(var i = 1; i < labels.length; i++) {
			el = labels[i].el;
			center = labels[i].center;
			width = (labels[i].width > SC.Slider.handle.width) ? labels[i].width : SC.Slider.handle.width;
			width = width / 2;
			gap = ((position - center) < 0) ? (center - position) : (position - center);
			if(gap < width) {
				if(el.css('color') != 'white') {
					el.animate({
						color: 'white'
					}, {
						duration: SC.Slider.duration,	
						easing: null,
						queue: false
					});
				}
			}
			else {
				if(el.css('color') != '#8299AD') {
					el.animate({
						color: '#8299AD'
					}, {
						duration: SC.Slider.duration,
						easing: null,
						queue: false
					});
				}
			}
		}
	}
}
