$ms(document).ready(function(){
	//alert($ms('.featureshow').attr('class'));
	$ms('#Feature').msSlide();
/*var slide_count = 0;
$ms('.featureshow').addClass('js');
$ms('.featureshow .section:eq('+slide_count+')').addClass('current');

function nextSlide() {
	
}

*/
	$ms('#GiftCards, #SkinInfo, #ScreenProtectors, #Wallpapers').click(function(){
		window.location = $ms(this).find('a:eq(0)').attr('href');
	});

});

(function($ms){


	$ms.fn.msSlide = function(){
		this.addClass('js');
		var element = this;
		var slide_count = $ms(this).children('.feature').length;
		//alert('Slide Count = ' + slide_count);
		var current = 0;
		
		
		goToSlide(current);
		addNav(current);
		//setInterval('$ms.fn.cseSlide.goToSlide('+ (current++) +');',3000);
		//$ms.delay(2000).goToSlide(1);
		
		
		function goToSlide(n, wait) {
			removeNavInteraction();
			$ms(element).find('ul.nav li.current').removeClass('current');
			
			$ms(document).stopTime("transition");
			if(n < slide_count) {
				current = n;
			} else {
				current = 0;
			}
			$ms(element).find('ul.nav li:eq('+current+')').addClass('current');
			//removeNav();
			$ms(element).children('.feature.current').fadeOut(1, function() {
				
				$ms(element).children('.feature.current').removeClass('current');
				
			});
			
			$ms(element).children('.feature:eq('+current+')').fadeIn(1, function() {
				$ms(element).children('.feature:eq('+current+')').addClass('current');
				
				addNavInteraction();
			});
			
			
			if(wait) {
				$ms(document).oneTime(7000, "transition", function(){
					goToSlide(++current);
				});
			} else {
				$ms(document).oneTime(7000, "transition", function(){
					goToSlide(++current);
				});
			}
			
			
			
		
		}
		
		function pause() {
			$ms(document).stopTime("transition");
		}
		
		function play() {
			$ms(document).oneTime(7000, "transition", function(){
					goToSlide(++current);
			});
			
		}
		
		function addNav(start) {
			var navigation = '<ul class="nav">';
			for(var i = 0; i < slide_count; i++) {
				if(i == current) {
					navigation += '<li class="current">' + (i + 1) + '</li>';
				} else {
					navigation += '<li>' + (i + 1) + '</li>';
				}
			}
			navigation += '<li class="control">||</li>';
			navigation += '</ul>';
			$ms(element).append(navigation);
			$ms('.control').toggle(
				function(){
					pause();
					$ms(this).text('>');
					//alert('pause');
				},
				function(){
					play();
					$ms(this).text('||');
					
					//alert('play');
				}
				
			);
				
		}
		
		
		function removeNav() {
			$ms(element).children('.feature:eq('+current+')').children('.text').children('.nav').remove();
		}
		
		function addNavInteraction() {
			$ms(element).children('ul.nav li:not(:eq('+current+'), .control)').click(function(){
				goToSlide($ms(this).index(), true);
				$ms('.control').text('||');
			});
			
		}
		
		function removeNavInteraction(){
			$ms(element).children('ul.nav li:not(:eq('+current+'), .control)').unbind('click');
			
		}
		
	}
	
		

})(jQuery);


