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_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.className.test(f, "i")){
				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"
	for(var i=0; i < gallery_src.length; i++){
		visible_gallery_items.push(i);
		visible_gallery_funcs.push($('gallery_item_'+i).getFirst().href);
	}
	//deeplink to a particular video or photo
	if(getParameter('image')) {
		showImg(getParameter('image'));
	} else if(getParameter('video')) {
		showVideo(getParameter('video'));
	} else {
		eval($( $('gallery_thumbs_container').getFirst() ).getFirst().href );
	}
	
	//deeplink to particular gallery
	if(getParameter('filter')) {
		$('filter').value = getParameter('filter');
		$('filter').onchange();
		cur_section = getParameter('filter');
	}
	
	$('gallery_thumbs_container').setStyle('visibility', 'visible');
	
}
function showImg(argPos){
	$('video_player').setStyle('visibility', 'hidden');
	$('video_player').setHTML('');
	highlightThumb(argPos);
	$('enlarged_photo').setHTML('<img src="'+gallery_src[argPos]+'" /><span class="caption">'+gallery_titles[argPos]+'</span>');
	$('enlarged_photo').setStyle('display', 'block');
	setControls(argPos);
	if (window.wallpapersOBJ) setWallpaper(argPos);
	pageTrack('Gallery Item ' + argPos, 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]+"' />");
	showFlash("/shared/flash/stand_alone_player.swf", "video_player", '', true); // last argument overrides the low bandwidth user pref
	embedLoadedPos = argPos;
	pageTrack('Gallery Item ' + argPos, gallery_titles[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');
	}else{
		scroll_container.setStyle('visibility', 'hidden');
		scroll_contents.setStyle('top', Math.round(0 - scroll_height)+'px');
	}
	
}
function setWallpaper(argPos) {
	
	var wallOBJ = wallpapersOBJ[argPos];
	if (!wallOBJ) return false;
	if (wallOBJ.length != 0) {
		
		var list = new Element('ul');
		
		wallOBJ.each( function(obj) {
			var source = "#";
			var title = obj['title'];
			var width = obj['width'];
			var height = obj['height'];
			var count = wallOBJ.indexOf(obj);
			
			var item = new Element('li');
			var anch = new Element('a', {
					'events' : {
						'click' : function() {
							linkTrack('wallpaper_menu', width + 'x' + height);
							animateWallpaper(list);
							flexWin('/en/wallpaper/?pos=' + argPos + '&count=' + count, '', width, height, 'wallpaper', '', '', '', '');
						}
					},
					
					'href' : '#',
					'name' : '&lpos=wallpaper_menu&lid=' + width + 'x' + height
			});
			
				anch.setText(title);
				anch.injectInside(item);
				item.injectInside(list);
		});
		new Element('li', {'class' : 'last_item' }).injectInside(list);
		buildWallpaper(list);
	}
}
function buildWallpaper(list) {
	var gallery = $('enlarged_photo');
	var image = gallery.getElement('img');
	var caption = gallery.getElement('span.caption');
	
	var container = new Element('div', { 'class' : 'container' });	
	var wall_info = new Element('div', { 'id' : 'wallpaper_info' });	
	var wall_anch = new Element('a', {
			'events' : {
				'click' : function() {
					linkTrack('wallpaper_menu', 'wallpaper_menu_btn');
					animateWallpaper(list);
				}
			},
			
			'class' : 'wallpaper_label',
			'href' : '#',
			'name' : '&lid=wallpaper_menu_btn&lpos=wallpaper_menu'
	});
	
	wall_anch.injectInside(wall_info);
	list.injectTop(wall_info);
	
		wall_info.injectInside(container);
		image.injectInside(container);
		container.injectBefore(caption);	
}
function animateWallpaper(list) {
	var size = list.getSize().size.y;
	var height = 0;
	var children = list.getChildren();
	var slide = list.effect('height', {
			transition: Fx.Transitions.Circ.easeOut
	});
	
	children.each( function(child) {
			height = height + child.getSize().size.y;
	});
		
		var max = height;
		height = (list > 0) ? 0 : height;
	
		if (size == 0) slide.start(height);
		if (size == max) slide.start(0);
}
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);
}
onload_register('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));
}
*/