// Requires "ajax_getData.js"

document.onkeydown=keyShortcut;
window.popupActive = false;
window.popupZoom = false;
window.popupComment = false;

// Keyboard Shortcuts (for popup)
function keyShortcut(e){
	var evtobj = window.event ? event : e; //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var unicode = evtobj.charCode ? evtobj.charCode : evtobj.keyCode;
	var actualkey = String.fromCharCode(unicode);
	
	/* File Comment Popup */
	if (window.popupActive) {
		switch(unicode) {
			case 27: // esc
				if(document.popupMaximize) zoomFile(0);
				hideAllPopups();
				document.location = '#';
				break;
			case 37: // left arrow
				//page_loadingUI(1);
				ajax_data2Div('content/popups/popup_fileComment.php?prev', 'popup_panel_generic' , 0);
				// page_loadingUI(0); This is run at the end of the loaded page
				break;
			case 39: // right arrow
				//page_loadingUI(1);
				ajax_data2Div('content/popups/popup_fileComment.php?next', 'popup_panel_generic' , 0);
				// page_loadingUI(0); This is run at the end of the loaded page
				break;
			case 38: // up arrow
				toggle_maximize();
				//zoomFile(1);
				break;
			case 40: // down arrow
				unmaximize();
				break;
			
			default:
				break;
		}
	}
}


function page_loadingUI(on) {
	var elem = document.getElementById('page_loading');	
	switch(on) {
		case 0://Off
			doOpacityFade(elem,100,0,0,25,25,4);
			document.body.style.cursor = 'default';
			break;
		case 1://On
    		doOpacityFade(elem,0,100,100,1,1,1);
    		document.body.style.cursor = 'wait';
			break;
		default:
			break;
	}
}
function toggle_maximize() {
	if (!document.popupMaximize) {
		document.popupMaximize = 1;
		maximize();
	}
	else if (document.popupMaximize) {
		document.popupMaximize = 0;
		unmaximize();
	}
}

function returnImgOrigDimensions( imgUrl ) {
	var img = document.createElement('img');
	img.src = imgUrl;
	img.onload = function () { 
		//alert(img.width + ' x ' + img.height);
		var sizeArray = new Array(img.width , img.height)
		return sizeArray
	};
}



function maximize() {
	var popup_contentDiv = document.getElementById('popup_contentDiv');
	var popup_panel = document.getElementById('popup_panel_generic');
	var panel_commentSection = document.getElementById('popup_commentSection');
	var panel_floatBreak = document.getElementById('floatBreak');
	var panel_img = document.getElementById('file_img');
	var navSpacer = document.getElementById('navSpacer');

	var max_width = ( parseInt(document.getElementById('popup_widthContainer').style.width) )
	
	// Embed Info	
	if( document.embeds["file_img"] ) { 
		var panel_imgW = document.embeds["file_img"].width;
		var panel_imgH = document.embeds["file_img"].height;
	}else {
	// Image Info		
		var panel_imgW = panel_img.alt.split("x")[0];
		var panel_imgH = panel_img.alt.split("x")[1];
	}

	var max_imgW = maxDim(panel_imgW, panel_imgH, max_width, (window.innerHeight - 100))[0];
	var max_imgH = maxDim(panel_imgW, panel_imgH, max_width, (window.innerHeight - 100))[1];

	// Resize Embed
	if(document.embeds["file_img"]) { 
		document.embeds["file_img"].width = max_imgW + "";
		document.embeds["file_img"].height = max_imgH + "";
	} else {
	// Resize Image
		panel_img.style.width = (max_imgW)+ "px";
		panel_img.style.height = (max_imgH)+ "px";
		panel_img.style.cursor = "-moz-zoom-out";
		popup_contentDiv.style.styleFloat = "none";
		popup_contentDiv.style.cssFloat = "none";
	}
		
	var scrollArray = getScrollXY();
	popup_panel.style.top  =  scrollArray[1] + "px";
			    
	panel_commentSection.style.display = 'none';
	panel_floatBreak.style.display = 'none';
}

function unmaximize() {
	var popup_contentDiv = document.getElementById('popup_contentDiv');
	var popup_panel = document.getElementById('popup_panel_generic');
	var panel_commentSection = document.getElementById('popup_commentSection');
	var panel_floatBreak = document.getElementById('floatBreak');
	var panel_img = document.getElementById('file_img');
	var navSpacer = document.getElementById('navSpacer');

	var min_width = ( parseInt(document.getElementById('popup_widthContainer').style.width) - parseInt(document.getElementById('popup_commentSection').style.width) - 16)
		
	// Embed Info	
	if( document.embeds["file_img"] ) { 
		var panel_imgW = document.embeds["file_img"].width;
		var panel_imgH = document.embeds["file_img"].height;
	}else {
	// Image Info		
		var panel_imgW = panel_img.alt.split("x")[0];
		var panel_imgH = panel_img.alt.split("x")[1];
	}
	
	var max_imgW = maxDim(panel_imgW, panel_imgH, min_width , (window.innerHeight - 100))[0];
	var max_imgH = maxDim(panel_imgW, panel_imgH, min_width , (window.innerHeight - 100))[1];

	// Resize Embed
	if(document.embeds["file_img"]) { 
		document.embeds["file_img"].width = max_imgW + "";
		document.embeds["file_img"].height = max_imgH + "";
	} else {
	// Resize Image
		panel_img.style.width = (max_imgW)+ "px";
		panel_img.style.height = (max_imgH)+ "px";
		panel_img.style.cursor = "-moz-zoom-out";
		popup_contentDiv.style.styleFloat = "left";
		popup_contentDiv.style.cssFloat = "left";
	}
	
	var scrollArray = getScrollXY();
	popup_panel.style.top  =  scrollArray[1] + "px";
			    
	panel_commentSection.style.display = 'block';
	panel_floatBreak.style.display = 'block';
}


function maxDim(w, h, wMax, hMax){
	// Find New Image Size
    var ratio = h / w;
    var maxRatio = parseInt(hMax) / parseInt(wMax);
    
    //LANDSCAPE IMAGE - LANDSCAPE WINDOW
    if( ratio < maxRatio ){
    	var newW = wMax;
    	var newH = wMax * ratio;
    }
    
    //PORTRAIT & SQUARE IMAGE - LANDSCAPE WINDOW
    if( ratio >= maxRatio ){
    	var newW = hMax / ratio;
    	var newH = hMax;
    }
    return [newW,newH];
}



// Needs to be relocated...
function zoomFile(on) {
	var popup_panel = document.getElementById('popup_panel_generic');
	var panel_titleBar = document.getElementById('popup_titleBar');
	var panel_titleBarSmall = document.getElementById('popup_titleBarSmall');
	var popup_contentDiv = document.getElementById('popup_contentDiv');

	var panel_img = document.getElementById('file_img');
	var panel_commentSection = document.getElementById('popup_commentSection');
/* 	var panel_list_position_row = document.getElementById('list_position_row'); */

	//Toggle, w/o "on" being set
	if (!on && !document.popupMaximize) on = 1;
	if (!on && document.popupMaximize) on = 0;

    switch(on) {
		case 0: //Zoom Out
			if( document.popupMaximize ) {
			    /* Restore Image Size */
			    panel_img.style.height = document.imgHeight;
				panel_img.style.width = document.imgWidth;
				panel_img.style.cursor = "-moz-zoom-in";
				if(document.embeds["file_img"]) { 
					document.embeds["file_img"].width = document.embedWidth;
					document.embeds["file_img"].height = document.embedHeight;
				}
	
			    /* show the popup panel */
			    popup_panel.style.height = document.popupHeight;
			    popup_panel.style.width = document.popupWidth;
			    popup_panel.style.left = document.popupLeft;
			    popup_panel.style.top  = document.popupTop;

				// Hide Grey Background
				popup_contentDiv.style.backgroundColor = document.popup_contentDiv_bgColor;

			    // show titleBar and comment area
			    //panel_titleBar.style.display = document.popupTitleBarDisplay;
			    panel_commentSection.style.display = document.popupCommentSectionDisplay;
/* 			    panel_list_position_row.style.display = document.panel_list_position_rowDisplay; */
			    
			    // hide titleBarSmall
			    //panel_titleBarSmall.style.display = "none";
			    
				document.popupMaximize = 0;
			}
		    break;
		case 1://Zoom Maximize
			if( !document.popupMaximize ) {
				// Record original size data
				if( document.embeds["file_img"] ) { 
					document.embedWidth = document.embeds["file_img"].width;
					document.embedHeight = document.embeds["file_img"].height;
				}
				document.imgHeight = panel_img.style.height;
				document.imgWidth = panel_img.style.width;
				document.popupHeight = popup_panel.style.height;
				document.popupWidth = popup_panel.style.width;
				document.popupLeft = popup_panel.style.left;
				document.popupTop = popup_panel.style.top;
				document.popupTitleBarDisplay = panel_titleBar.style.display;
				document.popupCommentSectionDisplay = panel_commentSection.style.display;
/* 				document.panel_list_position_rowDisplay = panel_list_position_row.style.display; */
				document.popup_contentDiv_bgColor = popup_contentDiv.style.backgroundColor;

				// Hide Grey Background
				popup_contentDiv.style.backgroundColor = '';
				
				// Find New Image Size
			    var embedRatio = parseInt(document.embedHeight) / parseInt(document.embedWidth);
			    var imgRatio = parseInt(document.imgHeight) / parseInt(document.imgWidth);
			    // Img or Embed?
			    if(document.embeds["file_img"]) imgRatio = embedRatio;
			    var windowRatio = parseInt(window.innerHeight) / parseInt(window.innerWidth);
			    //LANDSCAPE IMAGE - LANDSCAPE WINDOW
			    if( imgRatio < windowRatio ){
			    	var zoomW = window.innerWidth;
			    	var zoomH = window.innerWidth * imgRatio;
			    }
			    //PORTRAIT & SQUARE IMAGE - LANDSCAPE WINDOW
			    if( imgRatio >= windowRatio ){
			    	var zoomW = window.innerHeight / imgRatio;
			    	var zoomH = window.innerHeight;
			    }
			    // Expand Image Size
			    var zoomW = zoomW * .95;
			    var zoomH = zoomH * .95;
	
				// Expand the popup panel
			    popup_panel.style.height = "100%";
			    popup_panel.style.width = "1%";		    
			    //popup_panel.style.left = "0px";
			    popup_panel.style.left = ((window.innerWidth - zoomW)/2) + "px";
			    
			    var scrollArray = getScrollXY();
			    popup_panel.style.top  =  scrollArray[1] + "px";
			    			    
			    // Hide titleBar, comment area , list_position_row
			   // panel_titleBar.style.display = 'none';
			    panel_commentSection.style.display = 'none';
/* 			    panel_list_position_row.style.display = 'none'; */
			    
			    // Show titleBarSmall
		   		//panel_titleBarSmall.style.display = "block";

				// Resize IMG
				panel_img.style.width = zoomW + "px";
		    	panel_img.style.height = zoomH + "px";
		    	panel_img.style.cursor = "-moz-zoom-out";

				// Resize EMBED
				if(document.embeds["file_img"]) { 
			    	document.embeds["file_img"].width = zoomW + "";
			    	document.embeds["file_img"].height = zoomH + "";
				}

			    document.popupMaximize = 1;
			}
		    break;
	}
}


/*
function maxSize(origW, origH, maxW, maxH ) {
	var origHV = (origW > origH)? "horz": "vert";
	var origAspect = origW / origH;
	var maxHV =  (maxW > maxH)? "horz": "vert";
	var maxAspect = maxW / maxH;
}
*/

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}
