var desktopVisible = false;
var desktopReload = false;

$(document).ready(function() {
	$("#all").css('width','10000px');
	$("#all").css('height','10000px');
	
	if($(location).attr("hash") == '#overview') {
		showBoxes(0);
		$('#menu-center').css('display', 'block');
		$('#menu-center').css('cursor', 'pointer');
		$('#menu-center').click(function(){
			menuCenter(400);
		});
		desktopVisible = true;
		desktopReload = true;
	}

});

function menuCenter(speed) {
	cx = ($(window).width() - $('#main').width()) / (-2);
	cy = ($(window).height() - $('#main').height()) / (-2);
	$("body").scrollTo('#main', speed, {offset:{top:cy, left:cx}});
}

var totalBoxes;
var loadedBoxes = Array();
var limitBoxes = 3;

function toggleDesktop()
{
  if (desktopVisible) {
    hideBoxes();
    loadedBoxes = Array();
    $('#menu-center').css('display', 'none');
	$('#menu-center').css('cursor', 'default');
    desktopVisible = false;
	desktopReload = false;
  }
  else {
    showBoxes(0);
    $('#menu-center').css('display', 'block');
    $('#menu-center').css('cursor', 'pointer');
    $('#menu-center').click(function(){
		menuCenter(400);
	});
    desktopVisible = true;
	desktopReload = false;
  }
}

Array.prototype.inArray = function(o) {
	for(var i = 0; i < this.length; i++)
		if(this[i] === o)
			return true;
	return false;
}

function showBoxes(sb)
{
	$.ajax({
		type: "GET",
		url: "index.php",
		data: "usr_action=loadboxes&limit="+limitBoxes+"&start="+sb+"&response_format=JSON",
		success: function(msg) {
			eval("var msgjson ="+msg);
			if (msgjson.objects) {
				for (i=0; i<msgjson.objects.length; i++) {
					if(!loadedBoxes.inArray(msgjson.objects[i].oid)) {
						viewBox(msgjson.objects[i].oid,msgjson.objects[i].values['1']);
						loadedBoxes.push(msgjson.objects[i].oid);
					}
				}
			}
			if (loadedBoxes.length < msgjson.totalCount) {
				showBoxes(loadedBoxes.length);
			}
		}
	});
}

var savemenux = 0;
var savemenuy = 0;
var saveboxes = Array();

function setMenu(oid,item) {

	savemenux = $("#main").position().left;
	savemenuy = $("#main").position().top;
	
	// 1px offset because of 1px border in backend
	item.x++;
	item.y++;
	
	$("#main").css('left',item.x+'px');
	$("#main").css('top',item.y+'px');
	$("#main").css('margin','0');
	
	$("#n00 a").addClass('active');
	
	if(!desktopReload) {						
		$l = item.x - savemenux + 300 + $(window).scrollLeft();
		$t = item.y - savemenuy + 250 + $(window).scrollTop();
		
		
		$("body").scrollTo({top:$t, left:$l}, 0);
	} else {
		menuCenter(0);
	}
	
    document.location.hash = '#overview';
    
   	DragScroll.allowdrag = true;
    $("body").css('cursor','move');
}

function resetMenu() {

	savemenux = $("#main").position().left;
	savemenuy = $("#main").position().top;

	$("#main").css('top','50%');
	$("#main").css('left','50%');
	$("#main").css('margin','-250px 0 0 -300px');
	
	$("#n00 a").removeClass('active');
	
	//$("#all").css('width','1px');
	//$("#all").css('height','1px');

	$l = $(window).scrollLeft() - savemenux + $("#main").position().left - 300;
	$t = $(window).scrollTop() - savemenuy + $("#main").position().top - 250;

	$("body").scrollTo({top:$t, left:$l}, 0);
	//window.scrollTo(100, 100);
	
	document.location.hash = '#';
	
	DragScroll.allowdrag = false;
    $("body").css('cursor','default');
}

function viewBox(oid,item) {
	if (item.ismenubox == 1) {			
		setMenu(oid,item);
	} else {
	
		
		$box = $('<div id="'+oid+'" class="box" style="z-index:'+item.z+'"><\/div>').appendTo('body');
		$box.css('display','none');
		
		saveboxes.push($box);
		
		if (item.video != "" && item.video != undefined) {
		
			if (item.imagewidth == "" || item.imagewidth == undefined) { item.imagewidth = 200; };
			if (item.imageheight == "" || item.imageheight == undefined) { item.imageheight = 150; };

			$box2 = $('<div id="videoPlayer'+oid+'" name="videoPlayer'+oid+'" class="video">To watch this video, you need the latest<\/div>').appendTo($box);

			var flashvars = {};
			flashvars.mediaURL = item.video;
			flashvars.teaserURL = item.image;
			flashvars.allowSmoothing = "true";
			flashvars.autoPlay = "false";
			flashvars.buffer = "6";
			flashvars.showTimecode = "true";
			flashvars.loop = "false";
			flashvars.controlColor = "0xffffff";
			flashvars.controlBackColor = "0x000000";
			flashvars.scaleIfFullScreen = "true";
			flashvars.showScalingButton = "true";
			flashvars.defaultVolume = "100";
			
			var params = {};
			params.menu = "false";
			params.allowFullScreen = "true";
			params.allowScriptAccess = "always";

			var attributes = {};
			attributes.id = "nonverblaster";
			attributes.name = "nonverblaster";
			attributes.bgcolor = "#ffffff";
			attributes.wmode = "transparent";
			
			swfobject.embedSWF("nonverblaster/NonverBlaster.swf", "videoPlayer"+oid, item.imagewidth, item.imageheight, "9", "#ffffff", flashvars, params, attributes);

			registerForJavaScriptCommunication("nonverblaster");
			
		} else {
			$box2 = $('<div style="width:'+item.imagewidth+'px; height:'+item.imageheight+'px;background:url('+item.image+') no-repeat;">&nbsp;<\/div>').appendTo($box);
		}
		if (item.customerid != "" && item.customerid != undefined) {
		
			if(jQuery.browser.msie) {
				w =  ' style="width:'+(item.imagewidth-20)+'px;"';
			} else {
				w = '';
			}
			$box2 = $('<h1'+w+'>'+item.customername+'<\/h1>').appendTo($box);
			$box2 = $('<p'+w+'><a href="customer/'+item.customerid+'#'+item.projectid+'">'+item.projectname+'<\/a><\/p>').appendTo($box);
			
		}
		$box.css('left',item.x+'px');
		$box.css('top',item.y+'px');
		$box.css('border','1px solid #413233');
		
		
		$box.fadeIn(400);
	}
}

function hideBoxes() {
	
	// delete boxes
	jQuery.each(saveboxes, function() {
		//$(this).fadeOut(400, function() {
			$(this).remove();
		//});
	});

	if($("#main").css('left') != '50%') {
		resetMenu();
	}
}
