// www.ghostlifemedia.com
// Beta version
// operations.js
// This script houses general operational functions
//
// Change History
// Date      User  Description
// -----------------------------------------
// 07/09/10  KWA   Added Image Gallery construction code

// replace all instances of the specified character in the string
function replaceChar (inStr, char1, replacementChar){
	if (char1 == replacementChar) return inStr;
	newStr = inStr;
	while (newStr.indexOf(char1) != -1){
		var temp = newStr.replace(char1, replacementChar);
		newStr = temp;
	}
	//alert("Returning " + newStr);
	return newStr;
}

// change the background color of the specified object
function setBgColor (id, color) {
    if (document.layers)
	document[id].bgColor = color == 'transparent' ? null : color;
    else if (document.all)
	document.all[id].style.backgroundColor = color;
    else if (document.getElementById)
	document.getElementById(id).style.backgroundColor = color;
}

// change the background image of the specified object
function setBackgroundImage (id, image) {
    if (document.layers)
	document[id].background.src = image == 'none' ? null : image;
    else if (document.all)
	document.all[id].style.backgroundImage = image == 'none' ? 'none' : 'url(' + image + ')';
    else if (document.getElementById)
	document.getElementById(id).style.backgroundImage = image == 'none' ? 'none' : 'url(' + image + ')';
}

// Preload an image and return the image object
function preloadImage( imageURL ){
	imageObj = new Image();
	imageObj.src = imageURL;
	return imageObj;

}

// Change an image (IE only for now)
function changeImage(id, newImg){
	document[id].src = newImg;
	
}

illegalPlaylistCharacters = new Array ('<', '>', '{', '}', '[', ']', '%', '$', '|', '\\');

//----- MILES BOX Utilities -----------------------------

function checkPlaylistFileName(obj, fullCheck){
		var illegalCharFound = false;
		for (j=0; j < illegalPlaylistCharacters.length; j++){
			// check for this illegal char inside of the object's text 
			if (obj.value.indexOf(illegalPlaylistCharacters[j]) != -1) illegalCharFound = true;
		}
		// attempt to change the box's color for easy identification
		if (illegalCharFound){
			//if (flagTheError) alertOfIllegals();
			obj.style.color="red";
			//obj.style.background=\"red\";
			if (! fullCheck) return true;
		} else {
			obj.style.color="black";
			if (! fullCheck) return false;
		}
		if (illegalCharFound){
			str = "The following characters are not permitted in the playlist URL text box: \n";
			for (k=0; k < illegalPlaylistCharacters.length; k++){
				str += " " + illegalPlaylistCharacters[k];
			}
			str += "\nSorry for the inconvenience.";
				alert(str);
				return false;
		} else {
			if ((obj.value == "") || (obj.value == " ")){
				alert("Please enter the URL path to your playlist file");
				return false;
			}
			return true;
		}
}

function highlightTextBoxCode(obj){
		obj.focus();
		obj.select();
}
function generateMilesBoxCode(){
		face = "";
		if (document.forms.milesBoxForm.color.value != "aqua"){
			face = "&face=" + document.forms.milesBoxForm.color.value;
		}
		if (checkPlaylistFileName(document.forms.milesBoxForm.playlistFile, true)){
			milesBoxStr = "<object type='application/x-shockwave-flash' allowScriptAccess='never' allowNetworking='internal' height='150' width='400' id='milesbox_2' data='http://www.ghostlifemedia.com/miles407/milesbox_2.swf?xmlFile=" + document.forms.milesBoxForm.playlistFile.value + face + "'>";
			//milesBoxStr += "<param name='allowScriptAccess' value='never' />";
			//milesBoxStr += "<param name='allowNetworking' value='internal' />";
			milesBoxStr += "<param name='movie' value='http://www.ghostlifemedia.com/miles407/milesbox_2.swf?xmlFile=" + document.forms.milesBoxForm.playlistFile.value + face + "' />";
			milesBoxStr += "</object>";
			document.forms.milesBoxForm.codeWindow.value = milesBoxStr;
		}
}

milesBoxWindowPoppedUp = false;
function previewMilesBoxCode(code, width, height){

	if (milesBoxWindowPoppedUp) newWindow.close();
	options = "width=" + width + ", height=" + height + ", scrollbars=no";
	newWindow = window.open("", "card_window", options);
	//newWindow = window.open(href, "card_window", 'width=200, height=340, scrollbars=no');
	code = "<html><title>MilesBox Preview</title><body bgcolor='yellow'><center><p>" + code + "</body></body>";
	newWindow.document.write(code);

	milesBoxWindowPoppedUp = true;
}
//-------------------------------------------------------------
//----- Image Gallery 2010 ------------------------------------
//-------------------------------------------------------------

//----- IMAGE GALLERY SETUP VARIABLES -----------------------------//
// these variables define the look and feel of the image gallery.
// NOTE: Values specified here could be overridden by values from the
//       image gallery configuration files

var colCount = 4;
galleryThumbnailImageWidth = 100;
var galleryImageWidth = 500;
var galleryImageHeight = 300;
var galleryBorderWidth = 3;
var galleryBorderColor = "#000000";
var imageViewerBg = "http://www.ghostlifemedia.com/images/image_viewer_bg.jpg";


var picSet = new Array();
var z1 = 0;

//----- Image Gallery Functions ---------------------------//
// print the specified text to the page
function print(msg){
	document.write(msg);
}
function pictureClass(){
	this.index = 0;
	this.width = 0;
	this.height = 0;
	this.thumbnailWidth = 0;
	this.title = "The Thinker";
	this.imagePath = "images/black.gif";
	this.description = "";
	this.enlargedImageAvailable = true;

}
//--------------------------------------
// launch the image window
var imageCloseUpWindow=null;
function launchImageWindow(id){
	if (imageCloseUpWindow != null){
		imageCloseUpWindow.close();
	}
	// gather up the image data from the array
	imgWinWidth  = picSet[id].width + 40;
	imgWinHeight = picSet[id].height + 100;
	imgGuardWidth  = imgWinWidth;  //+ 400;
	imgGuardHeight = imgWinHeight; //+ 400;
	title = "Image Viewer";
	imageCloseUpWindow = window.open('', 'imgCloseUpWin', 'toolbar=no,status=no,menubar=no,resizable=yes,width=' + imgWinWidth + ',height=' + imgWinHeight + ',scrollbars=yes');
	code = '<html><title>' + title + '</title><body background=' + imageViewerBg + '><center>';
	code += '<div style=\"position:absolute;top:0;left:0;z-index:3\"><img src=images/clear.gif width=' + imgGuardWidth + ' height=' + imgGuardHeight + '></div>';
	code += '<img src=' + picSet[id].imagePath + ' width=' + picSet[id].width + ' height=' + picSet[id].height + '><p>'; 
	code += '<b>' + picSet[id].title + '</b><br>';
	code += '<small>' + picSet[id].description + '</small>';
	code += '<br></body></html>';
	imageCloseUpWindow.document.write(code);
}
// launch a small info window because the enlarged image is not available
function enlargedImageUnavailable(id){
	if (imageCloseUpWindow != null){
		imageCloseUpWindow.close();
	}
	alert("Image Title: " + picSet[id].title + "\n" + picSet[id].description);
}
// wait a little bit before updating the window.status with the specified string
function picStatusDelayed (id, waitTime){
	safeTitle = replaceChar (picSet[id].title, "'", "");
	window.setTimeout("window.status = safeTitle", waitTime);
}

function initializeImageGallery2010(){
	var col=0;
	var preLoadedImg;

	// preload images
	var totalImages = picSet.length;
	window.status = "Preloading Images......";
	for (x=0; x < picSet.length; x++){
		imgCount = x + 1;
		window.status = "Preloading: " + picSet[x].title + "(" + imgCount + " of " + totalImages + ")";		
		preLoadedImg = preloadImage(picSet[x].imagePath);
	}
	window.status = "Image Preload Complete";

	// begin drawing the table
	print('<table cellspacing=' + galleryBorderWidth + ' cellpadding=0 bgcolor=' + galleryBorderColor + '<tr>');
	for (x=0; x < picSet.length; x++){
		// set image sizes... if necessary
		picSet[x].index = x;
		safeTitle = replaceChar (picSet[x].title, "'", "");
		if (picSet[x].thumbnailWidth == 0){
			picSet[x].thumbnailWidth = galleryThumbnailImageWidth;	
		}
		if (picSet[x].width == 0){
			picSet[x].width = galleryImageWidth;	
		}
		if (picSet[x].height == 0){
			picSet[x].height = galleryImageHeight;	
		}
		cellStr = '<td bgcolor=white>';
		// setup the thumbnail and enlarged image link
		if (picSet[x].enlargedImageAvailable){
			cellStr += '<a href=\'Javascript:void[0];\' onMouseOver=\'picStatusDelayed(' + x + ',50);\' onClick=\'launchImageWindow(' + x + ')\'>';
		} else {
			cellStr += '<a href=\'Javascript:void[0];\' onMouseOver=\'picStatusDelayed(' + x + ',50);\' onClick=\'enlargedImageUnavailable(' + x + ')\'>';	
		}
		cellStr += '<img border=0 src=\'' + picSet[x].imagePath + '\' width=' + picSet[x].thumbnailWidth + ' alt=\'**** ' + picSet[x].title + ' ****\'>';
		cellStr += '</a>';
		cellStr += '</td>';
		print(cellStr);
		col++;
		if (col >= colCount){
			print('</tr><tr>');
			col = 0;
		}
	}
	print('</tr></table>');

	window.status = "Image gallery load complete";
	//alert("Image gallery loaded...");

}


//alert("Good");