var vimeoApiUrl = 'http://vimeo.com/api/v2/moonshinepark/albums.json?callback=initContent';
var workingUrl;
//http://vimeo.com/api/v2/album/album_id/request.output
var content, item, video, details, currentView;

function init(){
	$(document).ready(function($){
	    $.history.init(showContent);
    	$("a[@rel='history']").click(function(){
        	$.history.load(this.href.replace(/^.*#/, ''));
        	return false;
    	});
	});
}

function showContent(hash){
	content = document.getElementById('content');
	item = document.getElementById('item');
	video = document.getElementById('video');
	details = document.getElementById('details');
	if(hash){
		initSlideshow();
		var id = hash.substring(0, hash.indexOf("_"));
		var type = hash.substring(hash.indexOf("_")+1);
		switch(type){
			case "Album":
				$("#workNav").addClass("currentNav");
				$("#bioNav").removeClass("currentNav");
				$("#blogNav").removeClass("currentNav");
				workingUrl = 'http://vimeo.com/api/v2/album/'+id+'/videos.json?callback=initContent';
				getVimeoData(workingUrl);
				item.style.display = 'none';
				break;
			case "Video":
				$("#workNav").addClass("currentNav");
				$("#bioNav").removeClass("currentNav");
				$("#blogNav").removeClass("currentNav");
				workingUrl = 'http://vimeo.com/api/v2/video/'+id+'.json?callback=showVideo';
				getVimeoData(workingUrl);
				break;
			case "Page":
				$("#workNav").removeClass("currentNav");
				$("#bioNav").addClass("currentNav");
				$("#blogNav").removeClass("currentNav");
				loadPage(id+".html");
				item.style.display = 'none';
				break;
			default:

				break;
		}
	}else{
		$("#workNav").addClass("currentNav");
		$("#bioNav").removeClass("currentNav");
		$("#blogNav").removeClass("currentNav");
		currentView = 'all';
		item.style.display = 'none';
		initSlideshow();
		getVimeoData(vimeoApiUrl);
		showLoading(content);
	}
}

function initSlideshow(){
	document.getElementById('slideShow').style.backgroundImage = 'url("slide_show_0'+Math.ceil(Math.random()*2)+'.jpg")';
	//document.getElementById('slideShow').style.backgroundImage = 'url("slide_show_02.jpg")';
}

function loadPage(url){
    $.ajax({
        url: url,
        dataType: 'html',
        success: function(data){
        	content.innerHTML = (data);
		},
        error: function(error){
			alert("There was an error processing your request.");
        }
    });
}

function getVimeoData(url){
	var js = document.createElement('script');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', url);
	document.getElementsByTagName('head').item(0).appendChild(js);
}

function initContent(data){
	content.innerHTML = '';
	content.style.display = '';
	console.log(data);
	if(data.length > 1){
		for (var i = 0, il = data.length; i < il; i++) {
			//console.log(data[i]);
			var link = document.createElement('a');
			
			link.setAttribute("rel", "history");
			var thumb = document.createElement('img');
			thumb.setAttribute('src', data[i].thumbnail_medium);
			thumb.setAttribute('alt', data[i].title);
			thumb.setAttribute('title', data[i].title);
			thumb.setAttribute('id', data[i].id);
			if(data[i].total_videos){
				link.setAttribute("href", "#"+data[i].id+"_Album");
			}else{
				link.setAttribute("href", "#"+data[i].id+"_Video");
			}
			if(i % 4 == 3){
				thumb.setAttribute('class', 'videoThumb rowEnd');
			}else{
				thumb.setAttribute('class', 'videoThumb');
			}
			link.appendChild(thumb);
			content.appendChild(link);
		}
	}else{
		showVideo(data);
	}
}

function showVideo(data){
	item.style.display = '';
	item.style.marginTop = '20px';
	details.innerHTML = '<h1>'+data[0].title+'</h1><br/>'+data[0].description;
	video.innerHTML = '<iframe src="http://player.vimeo.com/video/'+data[0].id+'?title=0&amp;byline=0&amp;portrait=0" width="640" height="360" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
	if(content.childNodes.length < 2)content.style.display = 'none';
}

function showLoading(container){
	container.innerHTML = '';
	var loading = document.createElement('div');
	loading.setAttribute('id', 'loading');
	var loadingGif = document.createElement('img');
	loadingGif.setAttribute('src', 'loading.gif');
	loadingGif.setAttribute('alt', 'Loading...');
	loadingGif.setAttribute('title', 'Loading...');
	loading.appendChild(loadingGif);
	container.appendChild(loading);
}

