window.addEvent('domready', function(){

	tagHighlightSetup();
	screenshotSetup();
	//videoSetup();
	
});


/*
*
* 	Mootools dynamic tag highlighting
* 	(cc)2008, Sembiki Interactive, http://sembiki.com/
* 
* 	This code is licenced under a Creative Commons Licence, which means you are free to use and modify it as long as you keep this header.
* 	http://creativecommons.org/licenses/by/3.0/us/
*
*/


function tagHighlightSetup(){

	// 	Replace all tags with anchors with mouseovers to highlight all other instances
	var taggroups = $$(".tags");
	
	taggroups.each( function(taggroup, i) {
	
		var tags = taggroup.getText().split(",");
		
		// Clear the div
		taggroup.empty();
		
		// Add each mouseover-enabled tag back to the div
		tags.each( function(tag, i) {
		
			var tagtext = tag.clean().toLowerCase();
		
			var newtag = new Element('a', {
				'class': 'tag',
				'href': '#/tag/' + tagtext,
				'title': 'highlight other "' + tagtext + '" items',
				'onclick': 'return false',
				'events': {
					'mouseover': function(){
						highlightTag(tagtext);
					},
					'mouseout': function(){
						$$(".tag").removeClass('selected');
					},
					'click': function(){
						lockTag(tagtext);
					}
				}
			});
			
			newtag.setText( tagtext );
						
			// Don't put a comma before the first item
			taggroup.appendText( ( taggroup.getText() == "" ? "" : ", " ) );

			taggroup.adopt(newtag);
		});
		
	});

}

// 	Highlight all other instances of the same tag
function highlightTag(selectedtag){

	var alltags = $$(".tag");
	
	alltags.each( function(tag, i) {
		if (tag.getText() == selectedtag) {
			tag.addClass('selected');
		} else {
			tag.removeClass('selected');
		}

	});

}

// 	Lock/unlock a given tag
function lockTag(selectedtag){

	var alltags = $$(".tag");
	
	alltags.each( function(tag, i) {
		if (tag.getText() == selectedtag) {
			if (tag.hasClass('locked')) {
				tag.removeClass('locked');
				tag.removeClass('selected');
			} else {
				tag.addClass('locked');
			}
		}
	});

}


/*
*
* 	Mootools dynamic screenshots
* 	(cc)2008, Sembiki Interactive, http://sembiki.com/
* 
* 	This code is licenced under a Creative Commons Licence, which means you are free to use and modify it as long as you keep this header.
* 	http://creativecommons.org/licenses/by/3.0/us/
*
*/


function screenshotSetup(){

	// 	Hide screenshots
	$$("li").setStyles({
		clear: 'none'
	});
	
	$$(".ss").setStyles({
		display: 'none'
	});

	// Add empty container for screenshots
	$$(".items").each( function(item, i) {
	
		var sectionSs = new Element('div', {
			'class': 'ss',
			'id': item.id + '-ss'
		});

		sectionSs.injectBefore(item);

	});
	
	// Show screenshot on mouseover
	$$(".items li").addEvent('mouseover', function(){
	
		// Empty the container and clone and adopt the hidden screenshot
		$(this.getParent().id + "-ss").empty().adopt(this.getFirst().getFirst().clone());

	});
	
	// Show the first screenshot
	$$(".items").getFirst().fireEvent('mouseover');

}


/*
*
* 	Mootools video embedding (in progress)
* 	(cc)2008, Sembiki Interactive, http://sembiki.com/
* 
* 	This code is licenced under a Creative Commons Licence, which means you are free to use and modify it as long as you keep this header.
* 	http://creativecommons.org/licenses/by/3.0/us/
*
*/


function videoSetup(){
	
	// Show screenshot on mouseover
	$$("a.vimeo").addEvent('click', function(){

		$$(".embeddedvideo").setStyles({
			display: 'none'
		});
		
		var video = new Element('div', {
			'class': 'embeddedvideo',
			'html': '<object width="500" height="377"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+videoid+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+videoid+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="500" height="377"></embed></object>'
		});
	
		// Empty the container and clone and adopt the hidden screenshot
		this.getParent().adopt(video);
		
		return false;
	});
	
}
