$ez = jQuery.noConflict();
var moogaloop = false;
var endpoint = 'http://www.vimeo.com/api/oembed.json';
var callback = 'embedVideo';
var current	 = null;
var timeout_cleared = false;
var page = window.location.href;

// This function puts the video on the page
function embedVideo(video) {
	$ez("#moogaloop").show();
	$ez("#moogaloop").html(unescape(video.html));
	$ez("#moogaloop").append('<div style="clear"></div>');
	$ez('html, body').animate({scrollTop:0}, 'fast');
	
	/*
	$ez("#moogaloop").fadeTo("slow", 1, function() {
		$ez("#moogaloop").html(unescape(video.html));
		$ez("#moogaloop").append('<div style="clear"></div>');
		$ez('html, body').animate({scrollTop:0}, 'fast');
	});
	*/
}

// This function loads the data from Vimeo
function vimeo_init(id) {
	hide_home_items();
	var videoUrl = 'http://www.vimeo.com/'+id;
	var call_url = endpoint + '?url=' + (videoUrl) + '&callback=' + callback + '&width=731&height=411&autoplay=true&iframe=true&wmode=transparent';
	
	var js = document.createElement('script');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', call_url);
	js.setAttribute('id', id);
	document.getElementsByTagName('head').item(0).appendChild(js);
	
	$ez(".header").css("background-image", "");
	current = id;
}

function home_vimeo_still(url, id)
{
	alert(url + ", " + id);
}

// This function loads the data from Vimeo
function home_vimeo_init(id) {
	hide_home_items();
	
	if(window.console) window.console.log(id)
	var videoUrl = 'http://www.vimeo.com/'+id;
	var call_url = endpoint + '?url=' + (videoUrl) + '&callback=' + callback + '&width=731&height=411&autoplay=true&iframe=true&wmode=transparent';
	
	$ez('head').append('<script type="text/javascript" src="'+call_url+'"></script>');

}

function toot(id, duration) {
	alert(id + ", " + duration);
}

function request_image(id)
{
	hide_home_items();
	$ez("#home-image_"+id).fadeTo("slow", 1);
}

function request_twitter(id)
{
	hide_home_items();
	$ez("#home-twitter_"+id).fadeTo("slow", 1);
}

function hide_home_items()
{
	$ez("#moogaloop").hide();
	$ez(".home-images-item").hide();
	$ez(".home-twitter-item").hide();
}

function set_image(url, lmn)
{
	if(!lmn) lmn = "#header-img";
	$ez(lmn).fadeTo("fast", 0.1, function() {
		$ez(lmn).css("background-image", "url('" + url + "')");
		$ez(lmn).fadeTo("fast", 1);
		$ez('html, body').animate({scrollTop:0}, 'fast');
	});
}

$ez(document).ready(function()
{
	init_videogallery();
	init_bindings();
	init_hashtagcheck();
});

function init_hashtagcheck()
{
	var location = window.location.href;
	var hashtag  = '';
	var found	 = false;
	
	for(a in location) {
		if(found) hashtag += location[a];
		if(location[a]=='#') found = true;
	}
	
	if(found) {
		vimeo_init(hashtag);
		$ez(".header .text").hide();
		$ez('.motitle').hide();
		$ez('.motitle').removeClass("selected");
		$ez('#gallery-item_'+hashtag+' .motitle').addClass("selected");
		$ez('#gallery-item_'+hashtag+' .motitle').show();
		if(window.console) window.console.log('hashtag found: '+hashtag);
	}
}

function init_bindings()
{
	bind_mainmenu();
	
	if($ez(".home-menu-item").size() > 0) {
		bind_mediabox(); };
	
	$ez(".tooltipthis").live("mousemove", function(e) {
		id = $ez(this).attr("id");
		if(id) {
			html  = "";
			html += $ez("#"+id+" .tooltip").html();
			tooltip(html, e.pageY, e.pageX);
		}
	});
	
	$ez(".tooltipthis").live("mouseout", function(e) {
		$ez("#tooltip").hide();
	});
}

function bind_mediabox()
{
	$ez(".home-menu-item").live('mouseup', function() {
		$ez(".home-menu-item").removeClass("active");
		$ez(this).addClass("active");
	});
}

function hide_submenus(t)
{
	if(timeout_cleared != true) {
		clicked.removeClass('active');
		$ez('ul.subitems').each(function(){
			if($ez(this).data('paper')){
		      	$ez(this).data('paper', null);
			}
		})
		$ez('.scrollbarpaper-container').remove();

		$ez('ul.subitems').slideUp(100);
		timeout_cleared = false;
	}
}

function bind_mainmenu()
{
	$ez(".video-gallery:not(.novideo) .item").live('mouseup', function() {
		$ez("#lightbox-menu").fadeOut("fast", function() {
			$ez("#lightbox-menu").fadeIn("fast");
		});
	});
	
	$ez("ul#main-menu li").live('mouseup', function() {
		clicked = $ez(this).find('span');
		$ez(this).find('span').addClass('active');
		$ez(this).find('ul.subitems').slideDown(100, function(){
			if(window.fancyscrollbars){
				$ez('#subitems_7').scrollbarPaper();
			}
		});
		
		
		$ez("ul.subitems, li span.active, .scrollbarpaper-container").live('mouseleave', function() {
			setTimeout('hide_submenus()', 100);
			timeout_cleared = false;
		});
	});
	
	
	$ez('li span.active, .subitems, .scrollbarpaper-container').live('mouseenter', function() {
		timeout_cleared = true;
	});
	
	$ez("li.menu-item-hover").hover(function() {
		$ez(this).addClass("active");
	}, function() {
		$ez(this).removeClass("active");
	});
	
	$ez("#add-to-lightbox").die().live('mouseup', function() {
		if(current) {
			$ez.ajax({
			type: "POST",
			url: 'lightbox', 
			data: ({
				type: 'add', 
				value: current
				}),
			success: function(data) {
				lightbox_count();
				if(window.console) console.log(data);
				},
			});
		}
	});
	
	$ez("#remove-from-lightbox").die().live('mouseup', function() {
		if(current) {
			$ez.ajax({
			type: "POST",
			url: 'lightbox', 
			data: ({
				type: 'remove', 
				value: current
				}),
			success: function(data) {
				lightbox_count();
				$ez("#gallery-item_"+current).fadeOut("slow");
				if(window.console) console.log(data);
				$ez("#moogaloop").slideUp("slow", function() {
					$ez('script#'+current).remove();
					$ez('iframe').attr('src', 'about:blank'); 
					});
				},
			});
		}
	});
	
/*
	$ez("#main-menu div.parent span.parent").hover(function() {
			
		var id = ($ez(this).attr("id")).replace("_span", "");
		if(id) var subitems = $ez("#"+id+" .subitems").size();
		
		$ez("#main-menu .subitems").hide();
		$ez("#main-menu li.parent").toggleClass("active");
		$ez("#main-menu #"+id).toggleClass("active");
		
		if(subitems > 0)
			$ez("#"+id+" .subitems").show();
		
		$ez("#"+id+" .subitems").live('mouseout', function() {
			$ez("#main-menu .subitems").hide();
		});
		
		$ez("#"+id+" .subitems").live('mouseover', function() {
			$ez("#"+id+" .subitems").show();
		});
	}, function(){
		
	);
*/
	
	$ez("#content-left").live('mouseover', function() {
		$ez(".menu .subitems").hide();
	});
}

function lightbox_count()
{
	$ez.ajax({
		type: "POST",
		url: 'lightbox', 
		data: ({
			type: 'count'
		}),
		success: function(data) {
			$ez("#mmi_span_14").fadeOut("fast", function() {
				$ez("#mmi_span_14 a").text("Lightbox (" + data + ")");
				$ez("#mmi_span_14").fadeIn("fast");
			});
		},
	});
}	


function set_hashtag(tag)
{
	var href = '';
	var found = false;
	
	for(a in page) {
		if(page[a]=='#') found=true;
		if(!found) href += page[a];
	}
	
	href += '#'+tag;
	window.location.href = href;
}


function init_videogallery()
{
	hide_home_items();
	
	$ez(".video-gallery:not(.novideo) .item").live('mouseup', function(e) {
		var element_id = $ez(this).attr("id");
		var id 		= $ez("#"+element_id+" .details .id").html();
		var title 	= $ez("#"+element_id+" .details .title").html();
		var descr 	= $ez("#"+element_id+" .details .descr").html();
		
		vimeo_init(id);
		
		set_hashtag(id);
		
		$ez(".header .text").hide();
	});
	
	$ez(".video-gallery:not(.novideo) .item").live('mouseup', function() {
		$ez('.motitle').hide();
		$ez('.motitle').removeClass("selected");
		$ez(this).find('.motitle').addClass("selected");
		$ez(this).find('.motitle').show();
	});
	
	$ez(".video-gallery .item").live('mousemove', function(e) {
		tooltip($ez(this).find('.details .descr').html(), e.pageY, e.pageX);
		});
	
	$ez(".video-gallery .item").live('mouseout', function(e) {
		hide_tooltip();
		});
	
	$ez(".video-gallery .item").hover( function(e) {
		$ez(this).find('.motitle').show();
		
/*
		var element_id = $ez(this).attr("id");
		var id 		= $ez("#"+element_id+" .details .id").html();
		var title 	= $ez("#"+element_id+" .details .title").html();
		var descr 	= $ez("#"+element_id+" .details .descr").html();
		var top 	= $ez(this).offset().top;
		var left 	= $ez(this).offset().left;
		var html	= "";
		
		html += title;
		html += "<br /><br />";
		html += descr;
		$ez(this).append('<div class="text" style="position:relative;top:10px;left:10px;" id="__tooltip"><p>'+html+'</p></div>')
*/
		//tooltip(html, top, left);
	}, function(){
		//$ez("#__tooltip").hide();
		if($ez(this).find('.motitle').hasClass("selected") != true)
			$ez(this).find('.motitle').hide();
	});
	
}

function tooltip(text, top, left)
{
	if(text) {
		$ez("#tooltip").hide();
		$ez("#tooltip").html(text);
		$ez("#tooltip").show();

		top  += 20;
		left += 5;

		$ez("#tooltip").css("top", top);
		$ez("#tooltip").css("left", left);
	}
}

/*
function console(txt)
{
	$ez("#console").append("<br />"+txt);
}
*/

function hide_tooltip(text)
{
	$ez("#tooltip").hide();
}

function vimeo_player_loaded(swf_id) {
	moogaloop = document.getElementById(swf_id);
	document.getElementById('controls').style.display = '';
	
	moogaloop.api_addEventListener('onProgress', 'vimeo_on_progress');
	moogaloop.api_addEventListener('onLoading',  'vimeo_on_loading');
	moogaloop.api_addEventListener('onFinish',   'vimeo_on_finish');
	moogaloop.api_addEventListener('onPlay',     'vimeo_on_play');
	moogaloop.api_addEventListener('onPause',    'vimeo_on_pause');
	moogaloop.api_addEventListener('onSeek',     'vimeo_on_seek');
	
	document.getElementById('vimeo_duration').innerHTML = moogaloop.api_getDuration();
}

function vimeo_on_play(swf_id) {
	document.getElementById('state').innerHTML = 'Playing';
}

function vimeo_on_pause(swf_id) {
	document.getElementById('state').innerHTML = 'Paused';
}

function vimeo_on_seek(time, swf_id) {
	document.getElementById('state').innerHTML = 'Seeking to ' + time;
}

function vimeo_on_progress(time, swf_id) {
	document.getElementById('vimeo_cur_time').innerHTML = time + 's';
}

function vimeo_on_loading(data, swf_id) {
	document.getElementById('vimeo_bytes_loaded').innerHTML = data.bytesLoaded;
	document.getElementById('vimeo_bytes_total').innerHTML = data.bytesTotal;
	document.getElementById('vimeo_decimal_loaded').innerHTML = data.decimal;
	document.getElementById('vimeo_percent_loaded').innerHTML = data.percent + '%';
}

function vimeo_on_finish(swf_id) {
	document.getElementById('state').innerHTML = 'Finished';
}

function toggle_loop(el) {
	if (el.value == 'Loop is off') {
		el.value = 'Loop is on';
		moogaloop.api_setLoop(true);
	}
	else {
		el.value = 'Loop is off';
		moogaloop.api_setLoop(false);
	}
}

function init_player(id)
{
	var video_id 	= id;
	
	// Run the javascript when the page is ready
	var swf_id = 'moogaloop';
	
	var flashvars = {
		clip_id: video_id,
		show_portrait: 0,
		show_byline: 0,
		show_title: 0, 
		autoplay: 1, 
		fullscreen: 1, 
		js_api: 1, // required in order to use the Javascript API
		js_onLoad: 'vimeo_player_loaded', // moogaloop will call this JS function when it's done loading (optional)
		js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
	};
	
	var params = {
		allowscriptaccess: 'always',
		allowfullscreen: 'true'
	};
	var attributes = {};
		
	// For more SWFObject documentation visit: http://code.google.com/p/swfobject/wiki/documentation
	swfobject.embedSWF("http://vimeo.com/moogaloop.swf", swf_id, "735", "413", "9.0.0","expressInstall.swf", flashvars, params, attributes);
}

