// This script sets OSName variable as follows:
// "Windows"    for all versions of Windows
// "MacOS"      for all versions of Macintosh OS
// "Linux"      for all versions of Linux
// "UNIX"       for all other UNIX flavors 
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";


portfolio_action = 0;


    

function getDynInfo(subMenuId, divId, gallery_id, project_id) {
	
	var url = 'query.php';
	
	var params = "subMenuId=" + subMenuId + "&divId=" + divId + "&gallery_id=" + gallery_id + "&project_id=" + project_id;
	//alert(params);
	var ajax = new Ajax.Updater(
		{success: divId},
		url,
		{method: 'get', parameters: params, onFailure: reportError});
		
}


function reportError(request) {
	alert('Error');
}


// build intial left side menu based on gallery_id
function intMenuConstruct (gallery_id, project_id) {
	
	intMenu = new Array();

	// max number of projects in gallery and listed in left menu = 15
	var maxProjects = 16;
	
	for (i=0;i<maxProjects;i++) {
		intMenu[i] = new Object();
		intMenu[i].id = i;
		intMenu[i].name = "intMenu" + i;
		//intMenu[i].action = i;
		intMenu[i].state = 0;
		
		// 0 = inactive; 1 = active; 2 = current; 3 = visited
		
		intMenu[i].count = 0;
		intMenu[i].t = 0;
	
		// wait for a second; then trigger the top menu item for intro copy
		intMenu[i].active = function (initPrjMenu, project_id) {
			theInitPrjMenu = initPrjMenu;
			theProjectId = project_id;
			theIntId = this.id;
			this.t = setTimeout("intMenu[theIntId].active(theInitPrjMenu, theProjectId)",50);
			
			// use timeout to flash left side menu item
			if (this.count % 2 == 0) {
   				document.getElementById(this.name).style.background = "#ccc";	
			} else {
	
				this.state = 1;
		
				document.getElementById(this.name).style.background = "#ccc";
				document.getElementById(this.name).style.backgroundImage = "url(media/images/gallery_nav_over.gif)";
				document.getElementById(this.name).style.backgroundPosition = "top left";
				document.getElementById(this.name).style.backgroundRepeat = "no-repeat";	
				
			}			
			
			if (this.count < 3) {
				this.count = this.count + 1;
			} else {
				clearTimeout(this.t);
				
				// loop thru objects, any button that is current gets set to visited (3)
				for (i=0;i<maxProjects;i++) {
					if (intMenu[i].state == 2) {
						intMenu[i].state = 3;
						document.getElementById(intMenu[i].name).style.background = "#ccc";
						document.getElementById(intMenu[i].name).style.backgroundImage = "url(media/images/gallery_nav_visited.gif)";
						document.getElementById(intMenu[i].name).style.backgroundPosition = "top left";
						document.getElementById(intMenu[i].name).style.backgroundRepeat = "no-repeat";
					}
				}
				
				this.state = 2;
				this.count = 0;
		
				document.getElementById(this.name).style.background = "#ccc";
				document.getElementById(this.name).style.backgroundImage = "url(media/images/gallery_nav_over.gif)";
				document.getElementById(this.name).style.backgroundPosition = "top left";
				document.getElementById(this.name).style.backgroundRepeat = "no-repeat";
				
				// call function to load content in 'gallerySample' div
				getDynInfo(0, 'gallerySample', gallery_id, theProjectId);
				
				
				switch(initPrjMenu) {

					case 0:
					break;
					
					case 1:
						prjMenuConstruct(gallery_id, theProjectId);
					break;
					
				}
			}
		}
	}
	
	intMenu[0].active(0,project_id);
}



// build project menu based on project_id
function prjMenuConstruct (gallery_id, project_id) {
	
	prjMenu = new Array();

	// max number of samples in gallery and listed in left menu = 10
	var maxSamples = 11;

	for (i=0;i<maxSamples;i++) {
		prjMenu[i] = new Object();
		prjMenu[i].id = i;
		prjMenu[i].name = "prjMenu" + i;
		//prjMenu[i].action = theAction;
		prjMenu[i].state = 0;
		
		// 0 = inactive; 1 = active; 2 = current; 3 = visited
		
		prjMenu[i].count = 0;
		prjMenu[i].t = 0;
	
		// wait for a second; then trigger the top menu item for first samples
		prjMenu[i].active = function (project_id, submenu_id) {
			
			clearTimeout(initTimeout);
			
			theProjectId = project_id;
			theSubMenuId = submenu_id;
						
			thePrjId = this.id;
			this.t = setTimeout("prjMenu[thePrjId].active(theProjectId, theSubMenuId)",50);
			
			if (this.count % 2 == 0) {
   				document.getElementById(this.name).style.borderColor = "#000000";
			} else {
				this.state = 1;
				document.getElementById(this.name).style.borderColor = "#ffffff";
			}			
			
			if (this.count < 3) {
				this.count = this.count + 1;
			} else {
				clearTimeout(this.t);
				
				// loop thru objects, any button that is current gets set to visited (3)
				for (i=0;i<maxSamples;i++) {
					if (prjMenu[i].state == 2) {
						prjMenu[i].state = 3;
						document.getElementById(prjMenu[i].name).style.borderColor = "#ffcccc";
						document.getElementById(prjMenu[i].name).style.opacity = .5;
					}
				}
				
				this.state = 2;
				this.count = 0;
		
				document.getElementById(this.name).style.borderColor = "#000000";
				document.getElementById(this.name).style.opacity = 1.0;
				
				getDynInfo(theSubMenuId, 'prj_sub', gallery_id, project_id);
			
			}						
		}
	}
	
	theProjectId = project_id;
	initTimeout = setTimeout("prjMenu[0].active(theProjectId,1)",1000);
	
}


function checkContactForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.contact.message.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide a message.\r\r";
	}
	
	if (document.contact.from.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide a reply-to email addresss.\r\r";
	} else {
	
		if (document.contact.from.value.indexOf('@') == -1) {
			checkFlag++; 
			alertMessage = alertMessage + "It appears you are missing a '@' character in your reply-to email address.\r\r";
		}

		if (document.contact.from.value.indexOf('.') == -1) {
			checkFlag++; 
			alertMessage = alertMessage + "It appears you are missing a '.' character in your reply-to email address.\r\r";
		}
	
	}
	
	if (checkFlag == 0) {
		document.contact.submit();
	} else {
		alert(alertMessage);
	}
	
}

function checkAddForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.form_add.item_name.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide a name.\r\r";
	}
	
	if (checkFlag == 0) {
		document.form_add.submit();
	} else {
		alert(alertMessage);
	}

}

function checkRenameForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.form_rename.item_name.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide a name.\r\r";
	}
	
	if (checkFlag == 0) {
		document.form_rename.submit();
	} else {
		alert(alertMessage);
	}

}


function checkAddSampleForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.form_addsample.ext_html.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide your HTML code.\r\r";
	}
	
	if (checkFlag == 0) {
		document.form_addsample.submit();
	} else {
		alert(alertMessage);
	}

}

function checkAddEtsyForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.form_addsample.etsy_link.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide a link to your Etsy item.\r\r";
	}
	
	if (document.form_addsample.etsy_photo.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide the link to you Etsy item's photo.\r\r";
	}
		
	if (checkFlag == 0) {
		document.form_addsample.submit();
	} else {
		alert(alertMessage);
	}

}

function checkUrlForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.form_url.item_url.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide a valid URL.\r\r";
	}
	
	if (checkFlag == 0) {
		document.form_url.submit();
	} else {
		alert(alertMessage);
	}

}

function checkMetaForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.form_meta.item_meta.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide a valid meta tag.\r\r";
	}
	
	if (checkFlag == 0) {
		document.form_meta.submit();
	} else {
		alert(alertMessage);
	}

}

function checkAddTextSlideForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.form_addtextslide.caption.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide some text.\r\r";
	}
	
	if (checkFlag == 0) {
		document.form_addtextslide.submit();
	} else {
		alert(alertMessage);
	}

}

function checkIpMaskForm() {
	
	checkFlag = 0;
	alertMessage = "Please fix the following errors:\r\r";
	
	if (document.form_ip_to_mask.ip_to_mask.value == "") {
		checkFlag++; 
		alertMessage = alertMessage + "You must provide an IP address.\r\r";
	}
	
	if (checkFlag == 0) {
		document.form_ip_to_mask.submit();
	} else {
		alert(alertMessage);
	}

}