KEYCODE_ENTER = 13;
KEYCODE_ESC = 27;
KEYCODE_LEFT = 37;
KEYCODE_UP = 38;
KEYCODE_RIGHT = 39;
KEYCODE_DOWN = 40;
hoverMax = 500;
shoutBoxLoaded = false;
toolTab = "blank";
refEmpty = true;
galleryVisible = false;
uploaderVisible = false;
imageModVisible = false;
galleryContent = false;
conEmpty = true;
memEmpty = true;
chartEmpty = true;
logEmpty = true;

function galleryCanvas() {
	var gC = $(document.createElement('div')).attr('id','galleryWindow');
	gC.addClass('gWindow abs');
	gC.appendTo('#btLive');
	var gClose = $(document.createElement('div')).attr('id','galleryClose');
	gClose.addClass('gClose abs hand');
	gClose.appendTo('#galleryWindow');
	var uP = $(document.createElement('div')).attr('id','imgUplPanel');
        uP.addClass('base abs shadow rad3 wordWrap');
        uPcss = { 'padding' : '5px' , 'left' : viewportwidth/2-400 , 'top' : '80px' , 'width' : '800px' , 'height' : viewportheight-160 , 'z-index' : '9998' , 'font-size' : '14px' };
        uP.css(uPcss);
	uP.html("Image Uploader\n\nThis is a basic image uploader which will gain additional functionality as time goes on.  After clicking the select button, you are prompted to select one or more files to be uploaded.  After selecting files, the upload queue is processed immediately.  For each file to be upladed, a script checks the quarantine location to ensure that the filename is unique.  A secondary check ensures there are no duplicate images on the server.");
	uP.appendTo('#galleryWindow');
/*	var uplForm = $(document.createElement('form')).attr('id','uploadForm');
	uplForm.appendTo('#imgUplPanel');
	var uplDiv = $(document.createElement('div')).attr('id','uploader');
	uplDiv.appendTo('#uploadForm');  */
	$('#imgUplPanel').hide();
	var imgA = $(document.createElement('div')).attr('id','imageModerator');
	imgA.addClass('base abs shadow rad3 imageMod');
	imgAcss = { 'left' : viewportwidth/2-400 , 'top' : '50px' , 'z-index' : '9999' };
	imgA.css(imgAcss);
	imgA.appendTo('#galleryWindow');
	var imgB = $(document.createElement('div')).attr('id','verticalScroller');
	imgB.addClass('imageScroller abs');
	imgB.appendTo('#imageModerator');
	var imgH = $(document.createElement('p')).attr('id','modLabel');
	imgH.html("Image Upload Moderator");
	imgH.addClass('imgModLabel abs');
	imgH.appendTo('#imageModerator');
	$('#imageModerator').hide();
	$('#galleryWindow').hide();
}
function hoverTools() {
	var hoverX = viewportwidth - 320;
        var hPanel = $(document.createElement('div')).attr('id','hTools');
	hPanel.addClass('base abs shadow rad3 hTools');
	var hCss = { 'right' : '20px' };
	hPanel.css(hCss);
	hPanel.appendTo('#btLive');
        var hovToggle = $(document.createElement('div')).attr('id','hToggle');
        hovToggle.addClass('hToggle abs hand');
        hovToggle.appendTo('#hTools');
	var hcb = $(document.createElement('div')).attr('id','hoverCanvasBody');
	hcb.addClass('hcbContainer abs rad3');
	var hcbCss = { 'height' : hoverMax+'px' };
	hcb.css(hcbCss);
	hcb.appendTo('#hTools');
	var toolIcons = new Array(["Music","ipod"],["Gallery","asp"]);
	for (var i=0; i<toolIcons.length; i++) {
		var hI = $(document.createElement('img')).attr('id','hiddenTool'+i);
		hI.attr('src','assets/images/icons/16/gray/' + toolIcons[i][1] + '.png');
		hI.attr('title',toolIcons[i][0]);
		hIcss = { 'position':'absolute' , 'top':'2px' , 'right':24*i+32 };
		hI.css(hIcss);
		hI.appendTo('#hTools');
		var hS = $(document.createElement('img')).attr('id','showTool'+i);
		hS.attr('src','assets/images/icons/16/' + toolIcons[i][1] + '.png');
		hS.attr('title',toolIcons[i][0]);
		hScss = { 'position':'absolute' , 'top':'2px' , 'right':24*i+32 , 'cursor':'pointer' };
		hS.css(hScss);
		hS.appendTo('#hTools');
		$('#showTool'+i).hide();
	}
	$('#showTool1').show();
	$('#showTool12').show();
	hoverEmpty = true;
}

function toggleHoverPanel(src) {
	if($('#hTools').height()==20) {
		switch(toolTab) {
			case "shout" :	$('#hTools').animate({height: hoverMax+'px','width':'600px'}, 500);	break;
			case "blank" :	$('#hTools').animate({height: hoverMax+'px'}, 500);	break;
		}
	}else{
		$('#hTools').animate({height: '20px','width':'350px'}, 500);
	}
	
}
function closeGallery() {
	$('#galleryWindow').fadeOut('slow');
        galleryVisible = false;
	$('#uploadCanvas').remove();
}
$(document).ready(function() {
	$("#shoutImage").click(function() {
		toolTab = "shout";
		if($('#hTools').height()==20) { toggleHoverPanel(); }
		if(shoutBoxLoaded==false) { loadShoutBox(); }
        });
	$('#hToggle').click(function() {
		aaa = "Not working.";
          		$.getScript('assets/scripts/test.js', function() {
               			
          		});
		toggleHoverPanel();
        });
	$('#showTool1').click(function() {
		addJplayer();
	});
	$('#showTool12').click(function() {
		$('#galleryWindow').fadeIn('slow');
		galleryVisible = true;
		$.getScript('assets/scripts/gallery/galleryScripts.js', function() {
			if(galleryContent==false) {setupGallery() }
		});
	});
	$('#galleryClose').click(function() {
		closeGallery();
	});
	$('#galleryClose').hover(
		function() {
			$(this).stop().animate({ opacity: 1.0 }, 500);
		},
		function() {
			$(this).stop().animate({ opacity: 0.3 }, 500);
		
	});

	$('#sideLabel0').click(function() {
		if($('#sideP0').width()==8) {
			if($('#sideP1').width()!=8) { constructClose(); referenceDelay(); }
			if($('#sideP2').width()!=8) { membersClose(); referenceDelay(); }
			if($('#sideP3').width()!=8) { chartsClose(); referenceDelay(); }
			if($('#sideP4').width()!=8) { logsClose(); referenceDelay(); }
			referenceOpen();
		}else{
			referenceClose();
		}
        });
	$('#sideLabel1').click(function() {
		if($('#sideP1').width()==8) {
			if($('#sideP0').width()!=8) { referenceClose(); constructDelay(); }
                        if($('#sideP2').width()!=8) { membersClose(); constructDelay(); }
                        if($('#sideP3').width()!=8) { chartsClose(); constructDelay(); }
                        if($('#sideP4').width()!=8) { logsClose(); constructDelay(); }
                        constructOpen();
		}else{
			constructClose();
		}
	});
	$('#sideLabel2').click(function() {
                if($('#sideP2').width()==8) {
                        if($('#sideP0').width()!=8) { referenceClose(); memberDelay(); }
                        if($('#sideP1').width()!=8) { constructClose(); memberDelay(); }
                        if($('#sideP3').width()!=8) { chartsClose(); memberDelay(); }
                        if($('#sideP4').width()!=8) { logsClose(); memberDelay(); }
                        membersOpen();
                }else{
                        membersClose();
                }
        });
	$("#sideLabel3").click(function() {
                if($('#sideP3').width()==8) {
                        if($('#sideP0').width()!=8) { referenceClose(); chartDelay(); }
                        if($('#sideP2').width()!=8) { membersClose(); chartDelay(); }
                        if($('#sideP1').width()!=8) { constructClose(); chartDelay(); }
                        if($('#sideP4').width()!=8) { logsClose(); chartDelay(); }
                        chartsOpen();
                }else{
                        chartsClose();
                }
        });
	$("#sideLabel4").click(function() {
                if($('#sideP4').width()==8) {
                        if($('#sideP0').width()!=8) { referenceClose(); logDelay(); }
                        if($('#sideP2').width()!=8) { membersClose(); logDelay(); }
                        if($('#sideP1').width()!=8) { constructClose(); logDelay(); }
                        if($('#sideP3').width()!=8) { chartsClose(); logDelay(); }
                        logsOpen();
                }else{
                        logsClose();
                }
        });
	$("span.file", "#documentTree li").click(function() {
                currentChapter = 0;
                getBookContents($(this).text());
        });
	$(document).keydown(function(e){
		if($('#newsFloater').is(":visible")) {
			if (e.keyCode == KEYCODE_ESC) {
				$('#newsFloater').fadeOut('slow');
			}
		}
		if(galleryVisible==true) {
			if (e.keyCode == KEYCODE_LEFT) {
				selectPreviousImage(); 
				return false;
			}
			if (e.keyCode == KEYCODE_RIGHT) {
                                selectNextImage();
                                return false;
                        }
			if (e.keyCode == KEYCODE_ESC) {
				if(uploaderVisible==true) {
					closeUploader();
				}else { if(imageModVisible==true) {
					closeModerator();
					}else{	
						closeGallery();
					}
				}
                                return false;
                        }
			if (e.keyCode == 85 && uploaderVisible == false) {
                                openUploader();
                                return false;
                        }
                        if (e.keyCode == 77 && imageModVisible == false) {
                        	openModerator();
                        	return false;
                        }
		}
	});
});
function referenceOpen() {
        $("#sideP0").animate({ left: '30px'}, 200);
        $("#sideP0").animate({ width: frameWidth}, 300);
        $("#sideP0").animate({ height: frameHeight}, 400);
	if(refEmpty==true) {
		buildDocTree();
	}
}
function referenceClose() {
        $("#sideP0").animate({ height: '108px'}, 400);
        $("#sideP0").animate({ width: '8px'}, 300);
        $("#sideP0").animate({ left: '8px'}, 200);
}
function constructOpen() {
        $("#sideP1").animate({ left: '30px'}, 200);
        $("#sideP1").animate({ width: frameWidth}, 300);
        $("#sideP1").animate({ height: frameHeight, top: '36px'}, 400);
	if(conEmpty==true) {
		constructionFramework();
	}
}
function constructClose() {
        $("#sideP1").animate({ height: '100px', top: '155px'}, 400);
        $("#sideP1").animate({ width: '8px'}, 300);
        $("#sideP1").animate({ left: '8px'}, 200);
}
function membersOpen() {
        $("#sideP2").animate({ left: '30px'}, 200);
        $("#sideP2").animate({ width: frameWidth}, 300);
        $("#sideP2").animate({ height: frameHeight, top: '36px'}, 400);
	if(memEmpty==true) {
		
	}
}
function membersClose() {
        $("#sideP2").animate({ height: '90px', top: '266px'}, 400);
        $("#sideP2").animate({ width: '8px'}, 300);
        $("#sideP2").animate({ left: '8px'}, 200);
}
function chartsOpen() {
        $("#sideP3").animate({ left: '30px'}, 200);
        $("#sideP3").animate({ width: frameWidth}, 300);
        $("#sideP3").animate({ height: frameHeight, top: '36px'}, 400);
	if(chartEmpty==true) {
		
	}
}
function chartsClose() {
        $("#sideP3").animate({ height: '95px', top: '367px'}, 400);
        $("#sideP3").animate({ width: '8px'}, 300);
        $("#sideP3").animate({ left: '8px'}, 200);
}
function logsOpen() {
	$("#sideP4").animate({ left: '30px'}, 200);
        $("#sideP4").animate({ width: frameWidth}, 300);
        $("#sideP4").animate({ height: frameHeight, top: '36px'}, 400);
	if(logEmpty==true) {
		
	}
}
function logsClose() {
        $("#sideP4").animate({ height: '23px', top: '474px'}, 400);
        $("#sideP4").animate({ width: '8px'}, 300);
        $("#sideP4").animate({ left: '8px'}, 200);
}
function referenceDelay() { $('#sideP0').delay(900); }
function constructDelay() { $('#sideP1').delay(900); }
function memberDelay() { $('#sideP2').delay(900); }
function chartDelay() { $('#sideP3').delay(900); }
function logDelay() { $('#sideP4').delay(900); }


