jQuery(document).ready(function($) {
	
// WIDGETS

	// Setup Photo & Video Share Buttons
	$(".embed-btn").each(function(){
		var id = this.id.replace("embed-btn-",""),
			type = this.rel,
			widget = $("#widget-"+id),
			embedBtn = $(this),
			codebox = $("#codebox-"+id);
		
		// widget mouse events
		widget.mouseenter( function(){ 
			widgetOv(id,type); 
		});
		widget.mouseleave( function(){
			widgetOut(id,type);
		});
		
		$("#embed-btn-"+id).click( function() {
			share(id,this.rel);
		});
		$("#close-btn-"+id).click( function() {
			shareClose(id);
		});
		if(type=='video') {
			$("#embed-"+id).click( function() {
				share(id,type);
			});
		}
		
		// mouse enter
		function widgetOv(id,type) {
			embedBtn.fadeIn("fast");
			if(type=='video') {
			} else {
				$("#gal-"+id+"-controls").fadeIn("fast");
				$("#gal-"+id+"-caption").fadeIn("fast");			
				$("#gal-"+id+"-thumbs").fadeIn("fast");			
			}
		};
		// mouse leave		
		function widgetOut(id,type) {
			embedBtn.fadeOut("fast");
			codebox.fadeOut("fast");
			if(type=='video') {
			} else {
				$("#gal-"+id+"-controls").fadeOut("fast");				
				$("#gal-"+id+"-caption").fadeOut("fast");
				$("#gal-"+id+"-thumbs").fadeOut("fast");
			}
		};
		// share toggle
		function share(id,type){
			codebox.fadeIn("fast");
			embedBtn.fadeOut("fast");
			get_embed(id,type);
			return false;
		};
		// share - close click
		function shareClose(id) {
			codebox.fadeOut("fast");
			embedBtn.fadeIn("fast");
			return false;
		}
	});
	
// SLIDERS

	$('.slider').tinycarousel({display:3,pager: true});
	$('.removeslider').click( function() {
		var slider = $("#slider-"+this.rel),
			list = $("#list-"+this.rel);
		
		list.append('<li class="listend">&nbsp;</li>');
		list.attr("style","margin-left:50px;");
		list.appendTo(slider.parent());
		slider.remove();
		$(this).remove();
	});
	
// WIDGETS - VIDEO PLAYERS
	
	var vimeos = document.querySelectorAll('iframe.e-vimeo'), player;	
	for (var i = 0, length = vimeos.length; i < length; i++) {
		player = vimeos[i];
		$f(player).addEvent('ready', ready);
	}
	function addEvent(element, eventName, callback) {
		if (element.addEventListener) element.addEventListener(eventName, callback, false);
		else element.attachEvent(eventName, callback);
	}
	function ready(player_id) {
		var video = document.getElementById(player_id).parentNode;
			id = video.id.replace("widget-","");
		var froogaloop = $f(player_id);
		
		var playing = false,
			screenshot = $("#screenshot-"+id),
			playBtn = $("#play-"+id),
			embedBtn= $("#embed-btn-"+id),
			codebox = $("#codebox-"+id);
			
		function vimeoPlay() {
			playing = true;
						
			embedBtn.fadeOut("fast");
			codebox.fadeOut("fast");
			
			// overlay
			if(screenshot.length) {
				froogaloop.api('play');
				screenshot.fadeOut("fast");
				playBtn.fadeOut("fast");
			}
			
			// events
			froogaloop.addEvent('finish', vimeoFinish);	
		}
		function vimeoFinish() {
			playing = false;
			
			embedBtn.fadeIn("fast");
			codebox.fadeIn("fast");
			get_embed(id,'video');
			
			// overlay
			if(screenshot.length) {
				screenshot.fadeIn("fast");
				playBtn.fadeIn("fast");
			} else froogaloop.seekTo(0);
			
			// events
			froogaloop.removeEvent('finish');
		}

		if(screenshot.length) {
			addEvent(document.querySelector("#play-"+id), 'click', vimeoPlay, false);
			addEvent(document.querySelector("#screenshot-"+id), 'click', vimeoPlay, false);
			playBtn.fadeIn("fast");
		} else froogaloop.addEvent('play', vimeoPlay);
		
		embedBtn.fadeIn("fast");
		froogaloop.removeEvent('ready');
	}
});

// Lightbox
function galbox(a) {
	Shadowbox.open({
	content:    a.href,
	player:     "img",
	title:		a.title,
	});	
}
