$(document).ready(function(){
	Alpha();
	TimedSlider();
	SlideGallery();
	ProductStage();
});

function Alpha(){
	var isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if (document.all && !window.opera && !window.XMLHttpRequest) {
		isIE6 = true;
	}
	if(!isIE6){
		$(".alpha").fadeTo(1, 0.75);
	}
}

function TimedSlider(){
	$('.TimedSlider').each(function(i){
		
		var _myTS = this;
		var _itemWidth = 973;
		var _noOfItems = $('.ts_contentlist',_myTS).children().length;
		var _slidedelay = 5000;
		var _currentSchema = $('.ts_contentlist>li:eq('+0+')>div',_myTS).attr('class');
		var _currentItem = 1;
		var _interval;
		$('.ts_contentlist',_myTS).width(_itemWidth*_noOfItems);
		$(_myTS).addClass(_currentSchema);

		// building paging-navigation
		$(_myTS).append('<ul class="ts_paging"></ul>');
		$('.ts_paging',_myTS).append('<li><div class="active">'+1+'</div></li>');
		for(var i=2;i<=_noOfItems;i++){
			$('.ts_paging',_myTS).append('<li><div>'+i+'</div></li>');
		}

		$('.ts_paging>li',_myTS).click(function() {
			slideTo(parseInt($(this).text()));			
		});		
		
		$(_myTS).mouseover(function() {
			clearInterval(_interval);
		});

		$(_myTS).mouseout(function() {
			_interval = setInterval(function(){
				slideTo(_currentItem+1);
			},_slidedelay);
		});
		$(_myTS).mouseout();
			
		function slideTo(clickValue){
			$('.ts_paging>li>div:eq('+(_currentItem-1)+')',_myTS).removeClass('active');
			$(_myTS).removeClass(_currentSchema);

			// alert(clickValue);
			switch (clickValue){
				case ">":
					nextItem = parseInt(_currentItem)+1;
					if(nextItem > _noOfItems){ nextItem = 1};
					break;
				case "<":
					nextItem = parseInt(_currentItem)-1;
					if(nextItem < 1){ nextItem = _noOfItems};
					break;
				default:
					if(clickValue > _noOfItems){
						clickValue = 1;
					}
					nextItem = clickValue;
					break;
			}
			$('.ts_contentlist',_myTS).animate({left:(nextItem-1)*-_itemWidth+"px"},400);
			$('.ts_paging>li>div:eq('+(nextItem-1)+')',_myTS).addClass('active');

			_currentSchema = $('.ts_contentlist>li:eq('+(nextItem-1)+')>div',_myTS).attr('class');
			$(_myTS).addClass(_currentSchema);
			_currentItem = nextItem;
		}
	});
}


function SlideGallery(){
	$('.SlideGallery').each(function(i){
		var _mySG = this;
		var _itemWidth = 582;
		var _noOfItems = $('.sg_con>ul',_mySG).children().length;
		var _currentItem = 1;
		$('.sg_con>ul',_mySG).width(_itemWidth*_noOfItems);

		$(_mySG).append('<div class="sg_nav"><ul></ul></div>');
		$('.sg_nav>ul',_mySG).append('<li class="prev"><div>&lt;</div></li>');
		$('.sg_nav>ul',_mySG).append('<li><div class="active">'+1+'</div></li>');
		for(var i=2;i<=_noOfItems;i++){
			$('.sg_nav>ul',_mySG).append('<li><div>'+i+'</div></li>');
		}
		$('.sg_nav>ul',_mySG).append('<li class="next"><div>&gt;</div></li>');

		$('.sg_nav>ul>li',_mySG).click(function() {
			slideTo($(this).text());
		});

		// img click
		$('.sg_con>ul>li>.sg_img',_mySG).click(function() {
			slideTo(">");
		});

		function slideTo(clickValue){
			$('.sg_nav>ul>li>div:eq('+_currentItem+')',_mySG).removeClass('active');
			switch (clickValue){
				case ">":
					nextItem = parseInt(_currentItem)+1;
					if(nextItem > _noOfItems){ nextItem = 1};
					break;
				case "<":
					nextItem = parseInt(_currentItem)-1;
					if(nextItem < 1){ nextItem = _noOfItems};
					break;
				default:
					nextItem = clickValue;
					break;
			}
			$('.sg_con>ul',_mySG).animate({left:(nextItem-1)*-_itemWidth+"px"},400);
			$('.sg_nav>ul>li>div:eq('+nextItem+')',_mySG).addClass('active');
			_currentItem = nextItem;
		}
	});

}

function ProductStage(){
	$('.ProductStage').each(function(i){
		var _myPS = this;
		var _itemWidth = 582;
		var _noOfItems = $('.ps_contentlist',_myPS).children().length;
		var _currentItem = 1;
		var _interval;
		var _slidedelay = 5000;

		// streching listwrapper
		$('.ps_contentlist',_myPS).width(_itemWidth*_noOfItems);

		// building paging-navigation
		$(_myPS).append('<ul class="ps_paging"></ul>');
		$('.ps_paging',_myPS).append('<li><div class="active">'+1+'</div></li>');
		for(var i=2;i<=_noOfItems;i++){
			$('.ps_paging',_myPS).append('<li><div>'+i+'</div></li>');
		}

		$('.ps_paging>li',_myPS).click(function() {
			slideTo(parseInt($(this).text()));			
		});		
		
		$(_myPS).mouseover(function() {
			clearInterval(_interval);
		});

		$(_myPS).mouseout(function() {
			_interval = setInterval(function(){
				slideTo(_currentItem+1);
			},_slidedelay);
		});
		$(_myPS).mouseout();

		function slideTo(clickValue){
			$('.ps_paging>li>div:eq('+(_currentItem-1)+')',_myPS).removeClass('active');

			switch (clickValue){
				case ">":
					nextItem = parseInt(_currentItem)+1;
					if(nextItem > _noOfItems){ nextItem = 1};
					break;
				case "<":
					nextItem = parseInt(_currentItem)-1;
					if(nextItem < 1){ nextItem = _noOfItems};
					break;
				default:
					if(clickValue > _noOfItems){
						clickValue = 1;
					}
					nextItem = clickValue;
					break;
			}
			$('.ps_contentlist',_myPS).animate({left:(nextItem-1)*-_itemWidth+"px"},400);
			$('.ps_paging>li>div:eq('+(nextItem-1)+')',_myPS).addClass('active');
			_currentItem = nextItem;
		}
	});
}

/* PRODUCT TEASER PAGING */
function switchProductTeaserPage(from, to) {
    // last/first page check
    if (from.length == 0 || to.length == 0) {
        return;
    }
    from.toggleClass('current');
    from.animate({
    	opacity: 0
    },
    300,
    function() {
    	from.toggleClass('invisible');
    	to.css('opacity', 0);
    	to.toggleClass('invisible');
    	to.animate({
    		opacity: 1
    	}, 
    	300,
    	function() {
    		to.toggleClass('current');
    	})
    });
}
