//
// Copyright (c) 2008 Drew Wilson (http://www.drewwilson.com/)
//


$(document).ready(function() {
	url_token = document.location.toString().replace(/.*#/, '');
	if (document.location.toString().indexOf("#") != '-1' && url_token != ''){
		fireBox('pop', url_token, '666px', '541px', 'true');
	}
});

frSuccess = function(){
	$.getJSON("http://www.rootedpictures.com/api/data/video-covers", function(data){
		var arr = {};
		for(var i=0;i < data.count;i++){
			var num = data.items[i]['media-id'];
			arr[num] = data.items[i].image;
		}
		$("ul.gallery li").each(function(){
			var data = $(this).data("data");
			$(this).find("div.thumb").html($('<img />').attr("src", "http://www.rootedpictures.com/upload/data/1/"+arr[data.id]));
			$(this).find("a").attr("href", "#"+data.id).click(function(){
				$("#popup-body div.video").html(data.embed);
				$("#popup-body h4").html(data.name);
				$("#popup-body p").html(data.media_caption);
				fireBox('pop', data.id, '666px', '541px', 'true');
				return false;
			});
		});
		$("ul.gallery li:nth-child(3n)").addClass("end");
	});
}

function fireBox(filler, pdata, width, height, auto_height) {
	
	function pageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		return [xScroll, yScroll]; 
	}

	function pageViewport() {
		var scroll = pageScroll();
		return [$(window).width(), $(window).height(), scroll[0], scroll[1]];
	}

	var viewport = pageViewport();
	if (width == ''){ width = '400px';}
	if (height == ''){ height = '400px';}
	var loadingTimer;
	loadingFrame = 1;

	function showLoading() {
		clearInterval(loadingTimer);
		var pos = pageViewport();
		$("#fs-loading").css({'left': ((pos[0] - 40) / 2 + pos[2]), 'top': ((pos[1] - 40) / 2 + pos[3])}).show();
		loadingTimer = setInterval(animateLoading, 66);
	}

	function animateLoading(el, o) {
		if (!$("#fs-loading").is(':visible')){
			clearInterval(loadingTimer);
			return;
		}
		$("#fs-loading > div").css('top', (loadingFrame * -40) + 'px');
		loadingFrame = (loadingFrame + 1) % 12;
	}

	
	if(height.search('px') != '-1') { 
		splits = height.split('px');
		intheight = splits[0];
		var scroll = pageScroll();
		var newtop = Math.round(parseInt(viewport[1] - intheight) / 2) + scroll[1];
		var heightmargin = 'margin-top:' + newtop + 'px';
	} else {
		splits = height.split('%');
		intheight = splits[0];
		var scroll = pageScroll();	
		mathheight = eval(scroll[1] * (intheight / 100));
		var newtop = Math.round(parseInt(viewport[1] - intheight) / 2) + scroll[1];
		var heightmargin = 'margin-top:' + newtop + 'px';
	}

	var fs_overlay = document.createElement("div");
	fs_overlay.setAttribute('id','fs-overlay');
	fs_overlay.setAttribute('style', 'display:none;');
	document.body.appendChild(fs_overlay);

	var fs_holder = document.createElement("div");
	fs_holder.setAttribute('id','fs-holder');
	fs_holder.setAttribute('style', 'display:none;');
	document.body.appendChild(fs_holder);

	var fs_close = document.createElement("div");
	fs_close.setAttribute('id','fs-close');
	fs_close.setAttribute('onclick', 'fireBoxClose()');
	fs_holder.appendChild(fs_close);

	var fs_body = document.createElement("div");
	fs_body.setAttribute('id','fs-body');
	if(auto_height == 'true'){
		fs_body.setAttribute('style', 'width:'+width+';'+heightmargin+';');
	} else {
		fs_body.setAttribute('style', 'width:'+width+';height:'+height+';'+heightmargin+';');
	}
	fs_holder.appendChild(fs_body);

	var fs_loading = document.createElement("div");
	fs_loading.setAttribute('id','fs-loading');
	fs_body.appendChild(fs_loading);
	var fs_loaddiv = document.createElement("div");
	fs_loading.appendChild(fs_loaddiv);
	
	var fs_content = document.createElement("div");
	fs_content.setAttribute('id','fs-content');
	if(filler.indexOf(".") == '-1'){
		fs_content.innerHTML = $("#"+filler).html();
	}
	fs_body.appendChild(fs_content);
	
	$("#fs-overlay").fadeIn(300);	
	showLoading();
	
	if(filler.indexOf(".") != '-1'){
		$("#fs-content").load(filler, {video:escape(pdata)}, function(){
			$("#fs-loading").remove();
			$("#fs-holder").fadeIn(300);
		});
	} else if(filler.indexOf(".") == '-1'){
		$("#fs-loading").remove();
		$("#fs-holder").fadeIn(300);
	}
	
	return false;
}

function fireBoxClose(){
	$("#fs-holder").fadeOut(300, function() {
		$("#fs-holder").remove();
	});
	$("#fs-overlay").fadeOut(400, function() {
		$("#fs-overlay").remove();
	});
	
	return false;
}
