function get_scroll(){
	if($('body.root').size()){
		var v = $('html').scrollTop();	//Safari reacts to 'body', other browsers to 'html'
		if( $('html').scrollTop() ) return v;
		else return $('body').scrollTop();
	}else{
		return $('body').css('background-position').match(/(\d+)\D+$/)[1];	
	}
}


function set_scroll(y){
	if($('body.root').size()){	
		$('html,body').scrollTop(y);
	}else{
		 $('body').css('background-position','0px -'+y+'px');
	}
}

var total_swf = 0;

//called by MusicPlayer.swf
function MusicPlayer_started(id){
	//stop all other players
	var i;
	for(i=0;i<total_swf;i++){
		var ido = 'swf'+i;
		if(ido != id) document.getElementById(ido).action_pause();
	}
}


$(document).ready(function(){
	
	$('a.scroll_to').click(function(){
		var ofs = $('#callout_' + (this.href.match(/(\w+)\.html$/))[1]).offset(); 
		var newTop = ofs.top - 100;
		var dist = Math.abs(get_scroll() - newTop);	//distance to travel
		if(dist > 0){
			var time = dist / conf.scroll_speed ;	//time to travel based on a fixed 
			$('#menu').stop().animate({opacity:0}, 300,function(){
				$('html, body').stop().animate({scrollTop:newTop}, time,function(){
					$('#menu').stop().animate({opacity:1}, 300);	 
				});		
			});
		}
		$(this).addClass('active').siblings().removeClass('active');
		return false;
	});
	
	total_swf = $('.nt a').each(function(n){
		var id = "swf" + n;
		var title = $(this).text();
		var url = this.href;
		var regex = /\.mp3$/i;
		if(regex.test(url)){
			$(this).wrap('<div id="'+id+'" class="swf" />');
			swfobject.embedSWF('/site/MusicPlayer.swf?no_cache=' + MusicPlayer_mod 
				+ '&url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&id=' + encodeURIComponent(id)
				,id, 652, 29, "9.0.115", "/lib/swfobject/expressInstall.swf",{},{wmode:"transparent"});		
		}
	}).size();
	
	
	$('a.scroll_saved').click(function(){
		var href = this.href;
		set_cookie('scroll_saved',get_scroll());	
		if($(this).parents('#popup_nav').size()){			
			$('#popup_content').fadeOut(300,function(){location.href=href;});
			return false;		
		}else{									   
			return true;
		}
	});	
	
	
	var $slide = $('#slideshow>img:first');
	
	function slide_move(delta){
		if(delta>0){
			$slide_new = $slide.next('img');					
			if(!$slide_new.size()) $slide_new = $('#slideshow>img:first');
		}else{
			$slide_new = $slide.prev('img');					
			if(!$slide_new.size()) $slide_new = $('#slideshow>img:last');
		}
		$slide.fadeOut(1000);
		$slide_new.fadeIn(1000);
		$slide = $slide_new;
	}
	
	var slide_interval = setInterval(function(){ slide_move(1);	}, 5000);

	$('#slide_prev').click(function(){clearInterval(slide_interval);slide_move(-1);return false;});
	$('#slide_next').click(function(){clearInterval(slide_interval);slide_move(1);return false;});	
	
	
	if($('body.root').size()){	//home page animations
		for(var k in anim){
			var a =  anim[k];
			var $d = $('<div class="anim"><img src="' + a.file + '" /></div>').prependTo('body');
			$d.css({top:a.top});
			if('left_to_right' == a.motion  || 'right_to_left' == a.motion){
				$img = $d.find('img').attr('k',k);
				$d.css({width:'100%',overflow:'hidden'});
				anim_loop(k,true);
			}else{
				$d.css({left:a.left});	
			}
		}
	}
	
	function anim_loop(k,first){
		var a =  anim[k];
		var $img = 	$('.anim img[k="'+ k +'"]');
		var m = $('body').width();
		var t = m / a.speed;		
		var w = $img.width();
		
		if('left_to_right' == a.motion){
			$img.css('marginLeft',first ? a.left : -w );
		}else{
			$img.css('marginLeft',first  ?  (m - a.right)  : m + w);
			m = -w;			
		}
		$img.stop().animate({marginLeft:m},t,'linear',function(){
			var k = $(this).attr('k');																						
			setTimeout(function(){anim_loop(k,false);},a.delay)
		});		
	}

	
});


