var last_gallery_item;
var visible_gallery_items = new Array();
var visible_gallery_funcs = new Array();
var scroll;
var scroll_area;
var scroll_contents;
var scroll_container;
var scroll_height;
var scroll_thumb_height;
var scroll_thumb;
var cur_section = "photos_videos";
var embedLoaded = false;
var embedLoadedPos = '';
var gallery_video_disclaimer = new Array();
function galleryFunctions(){
	scroll = new Fx.Scroll($('gallery_thumbs_container'));
	scroll_container = $('gallery_thumbs_scrollarea');
	scroll_contents = $('gallery_thumbs_container');
	scroll_height = scroll_container.getSize().size.y;
	scroll_thumb = $('gallery_thumbs_scrollthumb');
	scroll_thumb_height = scroll_thumb.getSize().size.y;
	scroll_thumb.makeDraggable(
		{
			limit: {x: [0, 0], y: [0, scroll_height - scroll_thumb.getSize().size.y]},
			onDrag:function(){
				var percent_scrolled = (scroll_thumb.getTop() - scroll_container.getTop()) / (scroll_height - scroll_thumb_height);
				scroll_contents.setStyle('top', Math.round(0 - scroll_height - ((scroll_contents.getSize().size.y - scroll_height) * percent_scrolled)) +'px');
			}			
		}
	);
	scroll_thumb.setStyle('top',0);
	scroll_thumb.setStyle('left',0);
	$('filter').onchange = function(){
		//empty the visilbe_gallery
		while(visible_gallery_items.length != 0){
			visible_gallery_items.pop();
			visible_gallery_funcs.pop();
		}
		$(last_gallery_item).removeClass('at');
		var f = $('filter').value;
		var count = 0;
		$('gallery_thumbs_container').getChildren().each(function(t){
			if(f == "photo_video"){
				t.setStyle('display', 'block');
				visible_gallery_items.push(count);
				visible_gallery_funcs.push($(t).getFirst().href);
			}
			else if(t.hasClass(f)){
				t.setStyle('display', 'block');
				visible_gallery_items.push(count);
				visible_gallery_funcs.push($(t).getFirst().href);
			}else{
				t.setStyle('display', 'none');
			}
			count++;
		});
		linkTrack('gallery filters', f);
		eval($("gallery_item_"+visible_gallery_items[0]).getFirst().href);
	}	
	//set the initial "visible_gallery" update
	for(var i=0; i <= gallery_src.length-1; i++){
		visible_gallery_items.push(i);
		visible_gallery_funcs.push($('gallery_item_'+i).getFirst().href);
	}
	//deeplink to first photo/video
	eval($( $('gallery_thumbs_container').getFirst() ).getFirst().href );
}
function showImg(argPos){
	$('video_player').setStyle('visibility', 'hidden');
	$('video_player').setHTML('');
	$('enlarged_photo').setHTML('<img src="'+gallery_src[argPos]+'" /><span class="disclaimer opaque">'+gallery_video_disclaimer[argPos]+'</span><span class="caption">'+gallery_titles[argPos]+'</span>');
	$('enlarged_photo').setStyle('display', 'block');
	setControls(argPos);
	//pageTrack('Gallery Item ' + argPos, argPos);
	highlightThumb(argPos);
}
function showVideo(argPos){
	highlightThumb(argPos);
	$('video_player').setStyle('visibility', 'hidden');
	$('enlarged_photo').setStyle('display', 'none');
	setControls(argPos);
	$('video_player').setHTML("<img src='"+gallery_poster[argPos]+"' alt='"+gallery_titles[argPos]+"' />");
	showFlashBox("/flash/content/stand_alone_player.swf", "video_player", '', true); // last argument overrides the low bandwidth user pref
	embedLoadedPos = argPos;
	//pageTrack('Gallery Item ' + argPos, argPos);
}
function highlightThumb(argPos){
	if($(last_gallery_item))last_gallery_item.removeClass('at');
	$('gallery_item_'+argPos).addClass('at');
	last_gallery_item = $('gallery_item_'+argPos);
	var temp_y = $('gallery_item_'+argPos).getPosition().y - $('gallery_thumbs_container').getPosition().y - 1;
	//scroll.scrollTo(0, temp_y);
	var dest_y = (temp_y > scroll_contents.getSize().size.y - scroll_height) ? scroll_contents.getSize().size.y - scroll_height : temp_y;
	var percent_y = (temp_y > scroll_contents.getSize().size.y - scroll_height) ? 1 : temp_y / (scroll_contents.getSize().size.y - scroll_height) ;
	if(scroll_contents.getSize().size.y >= scroll_height){
		scroll_thumb.setStyle('top', Math.round((scroll_height - scroll_thumb.getSize().size.y) * percent_y) +'px');
		scroll_contents.setStyle('top', Math.round(0 - scroll_height - dest_y) +'px');
		scroll_container.setStyle('visibility', 'visible');
		scroll_thumb.setStyle('visibility', 'visible');
		//$('gallery_menu').setStyle('width', 200);
	}else{
		//$('gallery_menu').setStyle('width', 182);
		//scroll_container.setStyle('visibility', 'hidden');
		scroll_thumb.setStyle('visibility', 'hidden');
		scroll_contents.setStyle('top', Math.round(0 - scroll_height)+'px');
	}
	
}
function setControls(argPos){
	for(var s = 0; s < visible_gallery_items.length; s++){
		if(visible_gallery_items[s] == argPos){
			argPos = s;
		}
	}
	var next_item;
	var next_func;
	var previous_item;
	var previous_func;
	if(argPos != 0){
		previous_item = argPos - 1;
	}else{previous_item = visible_gallery_items.length - 1;}
	if(argPos < visible_gallery_items.length - 1){
		next_item = argPos + 1;
	}else{next_item = 0;}
	
	$('gallery_previous').href = visible_gallery_funcs[previous_item];
	$('gallery_next').href = visible_gallery_funcs[next_item];
	$('gallery_number').setHTML((argPos+1) + " / " + visible_gallery_items.length);
}
window.addEvent('domready', function() {
	galleryFunctions();
});
function setEmbedLoaded(){
	embedLoaded = true;
	setJSON(embedLoadedPos);
}

function setJSON(argPos){
	window.document["video_player_flash"].SetVariable('title',gallery_titles[argPos]);
	window.document["video_player_flash"].SetVariable('poster',gallery_poster[argPos]);
	window.document["video_player_flash"].SetVariable('flv',gallery_src[argPos]);
	window.document["video_player_flash"].SetVariable('disclaimer',gallery_video_disclaimer[argPos]);
	window.document["video_player_flash"].change();
}

/*
function hasFlashMin(){
	var debug_html = getParameter('flash');
	var flash_version;
	if(debug_html == "false" && debug_html !=  ""){
		flash_version = 10;
	}else{
		flash_version = 8;
	}
	return(UFO.hasFlashVersion(flash_version,0));
}
*/