// JavaScript Document
var currentPart;
var autohandler;
var autoplay = false;

$(document).ready(function(){
	//this fixes the backbutton no event effect
	jQuery(window).bind("unload", function(){})
	$(document).select(function(){return false})


	$('.fadeIn').fadeIn('slow',function(){
		var count = 0;
		$(this).find('div:hidden,li:hidden,p:hidden').each(function(){
			var o = $(this);
			if( !o.attr('id') ){
				var d = new Date
				o.attr('id', 'x'+d.getTime()+'_'+Math.ceil(Math.random()*1000));
			}
			setTimeout('$("#'+o.attr('id')+'").fadeIn()',count * 50)
			count++;
		})
		interact()
	})

	if( $('#SCROLL_INNER').children().length > 1 ){
		autoplay = true
		$('#SCROLL_INNER').children().click(function(){scrollToTop(this)})

		$('#SCROLL_INNER').draggable({
			axis: 'x',
			delay: 50,
			distance: 20,
			start:function(event, ui){
				$('#SCROLL_INNER').stop().children().unbind('click');
				clearSelection( )
				interact()
			},
			stop:function(event, ui){
				interact()
				clearSelection( )
				setTimeout("$('#SCROLL_INNER').children().click(function(){scrollToTop(this)})",200);
				//scroll back first
				if( scrollBack( ) )return

				this.smallestValue = 100000;
				$(this).children().each( function (){
					var cont = $('#SCROLL_INNER');
					var left = cont.position().left;
					var cleft = $(this).position().left;
					var value = Math.abs(cleft+left);
					if( value < cont.attr('smallestValue') ){
						cont.attr('smallestValue', value);
						scrollToTop( this )
					}
				})
			}
		});
	}else{
		$('#SCROLL_BUTTONS').remove()
	}

	$('a').each(function(){
		if( $(this).attr('href').substr(0,7) != 'http://' ){
			$(this).attr('href','javascript:goto(\''+$(this).attr('href')+'\')')
		}
	})

	scrollToTop( $('#SCROLL_INNER').children().first() );
	
})

function interact(){
	if( autohandler ){
		clearTimeout( autohandler );
	}
	autohandler = setTimeout( 'autoShuffle()',11000)
}

function autoShuffle(){
	if( !autoplay )return;
	interact()
	if( !$(currentPart).next().length ){
		scrollToTop( $('#SCROLL_INNER').children().first() );
		return;
	}
	scrollToTop( $(currentPart).next() )
}

function clearSelection( ){
	if ( $.browser.msie ){
		document.selection.clear()
	}else{
		window.getSelection().removeAllRanges()
	}
}

function scrollBack( ){
	var cont = $('#SCROLL_INNER');
	//scroll back last
	var widthAll = cont.children().length * cont.children().first().width()
	var widthAll = cont.children().last().position().left + cont.children().last().outerWidth()
	var deltaAll = (widthAll + cont.position().left) - cont.parent().width()
	if( widthAll < cont.parent().width() || cont.position().left > 0  ){
		cont.stop().animate({'left':'0px'});
		return true;
	}
	if( deltaAll < 0 ){
		var s = Math.ceil(widthAll - cont.parent().width())
		cont.animate({'left':'-'+s+'px'},1000);
		return true
	}
	return false;
}

function goto( url ){
	$('#BODY').fadeOut('fast',function(){ 
		document.location = url
	});
}

function scrollToTop( obj ){
	interact()
	currentPart = obj;
	$('#SCROLL_INNER').stop().animate({'left':'-'+$(obj).position().left+'px'},1000,null,function(){
		scrollBack( )
	});
}

function scroll_left(){
	interact()
	var prev = $(currentPart).prev();
	if( prev.length ){
		scrollToTop( prev );
	}
}

function scroll_right(){
	interact()
	var next = $(currentPart).next();
	if( next.length ){
		scrollToTop( next );
	}
}


