var rItem = false;
var currentItem = 0;
var rotateTime = 5000;

function rotateItems(){
	currentItem++;
	if(currentItem == $('#rotator > li').length){ currentItem = 0; $('#rotator > li:last-child').fadeOut('fast',function(){ $('#rotator > li:eq('+currentItem+')').fadeIn('fast'); });  }
	else{ $('#rotator > li:eq('+(currentItem-1)+')').fadeOut('fast',function(){ $('#rotator > li:eq('+currentItem+')').fadeIn('fast'); }); }
	$('#rotatorControls > li').removeClass('current');
	$('#rotatorControls > li:eq('+currentItem+')').addClass('current');
	rItem = setTimeout(rotateItems,rotateTime);
}

function interruptRotation(newIndex){
	clearTimeout(rItem);
	rItem = false;
	if(!$('#rotatorControls > li.playpause').hasClass('paused')){ $('#rotatorControls > li.playpause').addClass('paused'); }
	if(currentItem != newIndex){
		$('#rotator > li:eq('+currentItem+')').fadeOut('fast',function(){ $('#rotator > li:eq('+newIndex+')').fadeIn('fast'); });
		$('#rotatorControls > li').removeClass('current');
		$('#rotatorControls > li:eq('+newIndex+')').addClass('current');
		currentItem = newIndex;
	}
}

function toggleRotation(){
	if(!rItem){ rotateItems(); $('#rotatorControls > li.playpause').removeClass('paused') }
	else{ clearTimeout(rItem); rItem = false; $('#rotatorControls > li.playpause').addClass('paused'); }
}

$(function(){
	if($('#client_login').length){ $('#client_login').click(function(){ $('#frmlogin').addClass('active'); return false; }); }
	var showSpeed = (navigator.vendor && navigator.vendor == 'Apple Computer, Inc.') ? 0 : 500;
	if($('#featureCompare').length){ $('#featureCompare').click(function(){ $('.lightboxTableWrap:hidden').show(showSpeed); return false; }); }
	if($('.lightboxTableWrap').length){
		$('.lightboxTableWrap .close').click(function(){ $(this).parent().hide(showSpeed); return false; });
		$('.lightboxTableWrap').draggable({cancel:'.innerWrapper',cursor:'move'});
	}
	if($('.itfull .itapps').length){
		$('.itapps').click(function(){
			$('.lightboxTableWrap .innerWrapper').html($('#'+$(this).attr('rel')).html());
			if($(this).children('img:first-child[src*="_logo"]').length){ $('.lightboxTableWrap .innerWrapper .column2').prepend('<img src="'+(($(this).children('img:first-child').attr('src')).replace(/64\.gif/,'.gif')).replace(/\/apps\//,'/apps/large/')+'" class="paddedBorder" /><br />'); }
			else{ $('.lightboxTableWrap .innerWrapper .column2').prepend($(this).children('img:first-child').clone().addClass('floatLeftPadded')); }
			$('.lightboxTableWrap:hidden').show(showSpeed);
			return false;
		});
	}
	$('#rotator > li:gt(0)').hide().removeClass('hidden');
	if(!rItem && $('#rotator').length){
		lc = 1;
		mh = 0;
		listr = '';
		$('#rotator > li').each(function(){
			mh = ($(this).height() > mh) ? $(this).height() : mh;
			listr += '<li>'+lc+'</li>';
			lc++;
		});
		if($('#rotator').parent().is('#header')){ $('#header').append('<ul id="rotatorControls">'+listr+'<li class="playpause"></li></ul>'); }
		else{
			$('#rotator').css({'height':mh+'px'});
			$('<ul id="rotatorControls">'+listr+'<li class="playpause">&nbsp;</li></ul>').insertBefore('#rotator');
		}
		$('#rotatorControls > li:eq(0)').addClass('current');
		$('#rotatorControls > li[class!="playpause"]').each(function(ind){ $(this).click(function(){ interruptRotation(ind); }); });
		$('#rotatorControls > li.playpause').click(function(){ toggleRotation(); });
		rItem = setTimeout(rotateItems,rotateTime);
	}
	if(!$('#frmlogin').hasClass('sessionFound')){
		$('#client_login').click(function(){ $('#frmlogin').addClass('active'); return false; });
	}
});
