// constants
var dX = -107;


// elements
var links0 = jQuery('#digexgallery_thumbs0 a');
// ui elements 
var next0 = jQuery('#digexgallery_next0');
var back0 = jQuery('#digexgallery_back0');
var playlist0 = jQuery('#digexgallery_list0');
var playlistWrap0 = jQuery('#digexgallery_listWrap0');
var container0 = jQuery("#digexgallery_player0");
container0.addClass('jsenabled');

// status vars
var current0 = null;
var pos0 = 0;
var index0 = 0;
var total0 = links0.length;

// shuffle the media before adding event handlers
playlist0.shuffle();

// bind our show_handler function to the click event of each thumbnail
for(var i=0;links0[i];i++){
	jQuery(links0[i]).bind("click", show_handler(jQuery(links0[i]).attr('id')));
}

/**
* this function returns an anonymous handler function to display a specific media item in the player
* @param i the identifier of the slide to show, is hardcoded into the returned function as slides0[i]
* @return an anonymous event handler to show a specific item (show(i))
**/
function show_handler(i){
	return function(e){
		if (current0 != null) current0.removeClass("current");
		current0 = jQuery(this);
		container0.html(slides0[i]);
		current0.addClass("current");
		return false;
	};
}
// setup all the buttons and stuff
function init_ui(){
	back0.hide();
	next0.click(function(e){
		move0(4);
	})
	back0.click(function(e){
		move0(-4);
	})

	// little animations to pull out the nav buttons
	jQuery("#digexgallery0 .digexgallery_nav").hover(function(e){
		console.log("hover");
		jQuery(this).animate({ width:"16px" }, 100);
	}, function(e){
		_this = jQuery(this);
		// if the button dissapears during the hover, dont animate
		if (_this.is(":visible")) _this.animate({ width:"6px" }, 100); 
		else _this.width('6px');
	})
	
	if (total0<=4) next0.fadeOut(500);
}

function move0(x){
	//console.log("pos0: "+pos0+" index0: "+index0+" total0: "+total0);
	if (index0+4+x>total0){
		move0(total0-(index0+4));
	} else if (index0+x<0){
		move0(0-index0);
	} else {
		playlistWrap0.animate({ marginLeft:(pos0+dX*x)+"px"}, 1500);		
		pos0+=dX*x;
		index0+=x;
		if (x>0){
			if ((total0-index0)<=4){ next0.fadeOut(500); }
			if (index0>0){ back0.fadeIn(500); }
		} else if (x<0){
			if (index0==0){ back0.fadeOut(500); }
			if (index0<total0){ next0.fadeIn(500);}
		}
		console.log("moved! new index0: "+index0 +"total0: "+total0+"test: ");	
	}
}

init_ui();
jQuery('#digexgallery_thumbs0 a:first').click();
