$(document).ready(function()
{
	setTimeout('move_up()', 1);
	setupMenu();
	setupThumbs();
	
});	

$(document).unload(function(){
	if (interval) clearInterval(interval);
})

//
//  Cookie Functions
//

function move_up() 
{
	var move = getCookie('move');
	if (move == '') return false;
	
	var menu = document.getElementById('menu');
	menu.scrollTop = move;
	
	// should i delete the cookie here
	// or reset it to zero
}


function do_click()
{
	moved = document.getElementById('menu');
	moved = moved.scrollTop;
	
	// record the cookie
	setCookie('move', moved, 1);
}

function getCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(c_name + "=")

		if (c_start != -1)
		{ 
			c_start = c_start + c_name.length + 1 
			c_end = document.cookie.indexOf(";", c_start)
			
			if (c_end == -1) c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start, c_end))
		} 
	}
	return 0;
}

function setCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) +
	((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + '; path=/';
}
	
//
// End Cookie Functions
//
	function setupMenu(){
	
	$("#menu li").each(function(){		
		$(this).hover(function (){
			if($(this).is('.active')) return;
			$(this).animate({top:"0"},350);
		},	function  (){
				if($(this).is('.active')) return;
				$(this).animate({top:"-22"},350);
		});		
		$(this).click(function(){
			$('.active').removeClass('active');
			$(this).addClass("active");
			$('#content').hide();
		});
	});
}

function setupThumbs(){
		$('.thumb').click(function(event){
			var args = $(this).attr('rel').split(":");
			display($(this).attr('href'), args);			
			return false;
		});
	
	}

	function display(src, args) {
		bob = false;
			var mov;
			src.match(/\.jpg|\.gif|\.jpeg|\.png$/) ? mov=false : mov=true;
			//mov = true;
			var w = parseInt(args[0]) || 640;
			var h = parseInt(args[1]) || 480;
			var title = args[2] || '';
			var title_w = parseInt(args[3]) || 0;
			var caption = args[4] || '';
			var group = args[5] || '';
			
			adjustHeight();
						
			
			$('#modal_content').width(w);
			
			if(mov){
				if($.browser.msie){		
					$('#modal_content').append('<div id="mov"></div>');	
					$('#mov').media({ src:src,width: w, height: h+15, autoplay: 'true',kioskmode:'true' });						
				}else{
					$('#modal_content').append('<div id="mov" ></div>');
					$('#modal_content').append('<div id="controller" class="ui-slider-1"><div class="ui-slider-handle"></div></div>');
					$('#modal_content').append('<div id="play" class="play"></div>');						    	
					$('#mov').media({ src:src,width: w, height: h, autoplay: 'true', params:{ controller: 'false'}});			
					movie = $('#mov embed').get(0);				
					attach = setInterval(attachController, 100);					
				}
				
				$('#modal_content').append('<a id="down" href="/download.php?file='+src+'"><span>Download</span></a>');
				
					 
			}else{
				$('#modal_content').append('<img id="mov" src="'+src+'") width="'+w+'" height="'+h+'" alt="'+title+' - '+caption+'"/>');
				$('#mov').width(w).height(h);
				$('#mov').show();
							
			}
			
						
			$('#modal_content').append('<div id="modal_title"><h2>'+title+'</h2></div>');
			$('#modal_content').append('<div id="close" ></div>');
			$('#modal_content').append('<p id="caption">'+caption+'</p><br/>');			
			$('#modal_title').css({backgroundImage:'url(/_nav/titles/'+escape(title)+'_modal.gif)',width:title_w});	
			$('#close').click(function(){xModal()});
			if( $.browser.msie )
				z = document.documentElement.scrollTop;
			else z = window.pageYOffset;
			$('#modal_content').css({top:( z + 80) + 'px'});
			adjustHeight();
			$('#modal_window').show();
			$('#modal_content').fadeIn('slow');
		}
	
		function attachController(){
			
			if(movie.GetPluginStatus() == 'Playable'){
							controller();
							clearInterval(attach);
						}
		}
	
	
		function controller(){	
		   ctl = 0;
		   var timescale = movie.GetTimeScale();
		   duration = movie.GetDuration();
		   $('#controller').slider({
		   		   	maxValue:duration,
		   		   	minValue:0,
		   		   	startValue:0,
		   		   	change:function(e, ui){
		   		   	 	movie.SetTime(ui.value);
		   		   	 	ctl = 0;
		   		   	},
		   		   	slide:function(e, ui){
		   		   		movie.SetTime(ui.value);
		   		   	 	ctl = 1;
		   		   	 	
		   		   	 },
		   		   	 stop:function(){
		   		   	 	movie.SetTime(ui.value);
						movie.Stop();
		   				ctl = 0;
		   		   	 },
		   		   	 start:function(){
		   		   	 	ctl = 1
		   		   	 	
		   		   	 },
		   		   	 click:function(){
		   		   	 	alert('hello?');
		   		   	 
		   		   	 }
		   		   });
		
		    w = $('#controller').width();
		
			
		    $('#play').click(function(){
		    		
		    		$(this).toggleClass('pause');
		    		if($(this).hasClass('pause')){
		    			movie.Stop();
		    		}else{
		    			movie.Play();
		    		}
		    
		    });
		    interval = setInterval(monitorMovie,timescale*0.01);
		}
		
		function monitorMovie(){				
			try{
				var state = movie.GetRate();
				var time = movie.GetTime();
			}catch(x){
				var state = 0;
				var time = 0;
			}
			
			if(movie.GetPluginStatus()!='Complete'){
				$('#controller').width(movie.GetMaxTimeLoaded()/duration*w);
			}else{
				$('#controller').width(w);
			}
			
			if(ctl!=1) {$('#controller').sliderMoveTo(time);}
			
			if(state==0){
				$('#play').addClass('pause');
			}else{
				$('#play').removeClass('pause');	
			}	
					
		}
			

	function adjustHeight() {
		var y;
		if( $.browser.msie ) { // stupid msie stupid
			y = document.documentElement.scrollHeight;
			if( document.documentElement.clientHeight > y ) // if the page is shorter than the window, use window height
				y = document.documentElement.clientHeight;
			}
		else {
			dimensions = Array( document.body.scrollHeight, 
								document.body.offsetHeight, 
								( window.innerHeight + window.scrollMaxY ) );
			dimensions.sort( function(a,b) { return b - a; } ); // reverse sort
			y = dimensions[0]; // largest value
			}
		y = y + 100;
		$('#modal_background').height(y);
		}

	function xModal() {	
			
		$('object, embed').each(function() {
		try { this.Stop(); } catch(e) {}
		
		if($.browser.msie){		
		}else{
			if(interval!=null){clearInterval(interval)}	
			$('#controller').sliderDestroy();	
		 }
		});
			$('#modal_window').hide(); 
			$('#modal_content').empty();
			
			

		}
