// JavaScript Document
var NS = (navigator.appName == "Netscape") ? true : false; 
var slidesPerRow = 4;
			
function mouseOverThumb(thumb) {
	thumb.style.cursor = (NS) ? "pointer" : "hand";
	thumb.style.border = "1px solid #000000";
}

function mouseOutThumb(thumb) {
	thumb.style.cursor = (NS) ? "pointer" : "hand";
	thumb.style.border = "1px solid #FFFFFF";
}

function mouseOverLightBox(lightBox) {
	lightBox.style.cursor = (NS) ? "pointer" : "hand";
}

function addCDCart(cdID, formatID) {
	if (cookieCheck(true) == false) return false;
	if (xmlHttp == null) return false;
	
	var url = "shop?cmd=addCD&cdID=" + cdID + "&cdFmt=" + formatID;
	
	xmlHttp.open("GET", url);
	xmlHttp.onreadystatechange = ajxAddCartReturn;
	xmlHttp.send(null);
}

function addCart(index, imageName) {
	if (cookieCheck(true) == false) return false;
	if (xmlHttp == null) return false;
		
	var comboFmt = document.getElementById("sel.cartOptions" + index);
	if (comboFmt.value == "0") {
		alert("Please select Image Size and Format");
		return;
		
	} else {
		var url = "shop?cmd=add&imageName=" + imageName + "&imageFmt=" + comboFmt.value;
		
		xmlHttp.open("GET", url);
		xmlHttp.onreadystatechange = ajxAddCartReturn;
		xmlHttp.send(null);
	}
}

function ajxAddCartReturn() {
	if (xmlHttp.readyState != 4 || xmlHttp.status != 200) return;
	
	var res = xmlHttp.responseText;
	
	// server will respond with the typ value in return.  If populated then refresh lightbox counters.
	if (res > "") {
		if (cookieCheck(true)) {
			alert("Item added to Shopping Cart.\nWhen ready, click on 'My Cart' to proceed to check out.");
			
			ajaxRefreshCart();	
		}
	}
}

function selectFormat(index, combo) {
	if (combo.value == "") combo.selectedIndex = 0;
}

function viewCD(imageName) {
	document.location.href = "cdcatalog?cmd=cd_for_image&image_name=" + imageName + "&hid.mode=cdimages";
}

function editKeywords(image_name, previewPath, dir) {
	openDialog(varWebRoot, "Image: " + image_name, "maintKeywords?cmd=change&preview=" + previewPath + "&image_name=" + image_name, "268px");
}

function showPreview(image, image_id, previewPath, dir) {
	open(dir + "/viewPreview.jsp?image_name=" + image.id + "&image_id=" + image_id + "&previewPath=" + escape(previewPath), "",
		"resizable=" + (NS ? "yes" : "no") + ", status=no, width=200, height=200");
}

function removeImgFromCD(cdID, imageID, imgName) {
	if (!confirm("Remove Image " + imgName + " from CD?")) return false;
	
	if (xmlHttp == null) return false;
	
	var url = "editCD?cmd=delcdimg&cd_id=" + cdID + "&image_id=" + imageID;
	
	xmlHttp.open("GET", url);
	xmlHttp.onreadystatechange = ajaxRefreshPage;
	xmlHttp.send(null);
}

function ajaxRefreshPage() {
	if (xmlHttp.readyState != 4 || xmlHttp.status != 200) return;
	
	// do not refresh after img remove
	// if (xmlHttp.responseText == "refresh") refreshPage();
}

function initPage() {
	// check to display searchHelp if on last page or on page 3 or greater
	var searchID = document.getElementById("hid.searchID");
	var totalPages = document.getElementById("hid.totalPages");
	var currPage = document.getElementById("hid.currPage");
	if (totalPages != null && currPage != null && searchID != null) {
		var searchHelp = document.getElementById("search-help");
		if (searchHelp != null && searchID.value > "")
			searchHelp.style.display = (parseInt(currPage.value) >= 3 || parseInt(currPage.value) == parseInt(totalPages.value) ? "block" : "none");
	}
}

function editCD(cdID) {
	var returnValue = openDialog(varWebRoot, "Edit CD", "editCD?cmd=change&cd_id=" + cdID, "100px");
	if (returnValue == "refresh") window.location.reload(true);
}

function addImgToCD(imageID) {
	openDialog(varWebRoot, "Add Image To CD", "editCD?cmd=addimg&image_id=" + imageID, "100px");
}

function imageDetail(url, imageName, lightbox_item_id) {
	document.location.href =  varWebRoot + "/stock-photo" + url + "/" + imageName + ".html" + (lightbox_item_id == null ? "" : "?lightbox_item_id=" + lightbox_item_id);
}

function deleteCD(cd_id) {
	if (!confirm("Are you sure you want to delete this CD?\n\nThis action cannot be undone.")) return;
	
	document.location.href =  varWebRoot + "/editCD?cmd=delcd&cd_id=" + cd_id;
}

function setActiveKeyword() {
	var keyword = openDialog(varWebRoot, "Set Active Keyword", "maintActiveKeyword.jsp", "100px");
	
	if (keyword != null && keyword > "") refreshPage(keyword);
}

function remActiveKeyword() {
	refreshPage(null, 1);
}

function addKeyword(image_id, keyword) {
	if (xmlHttp == null) return false;
	
	var url = "maintKeywords?add_keyword=" + keyword + "&image_id=" + image_id;
	
	xmlHttp.open("GET", url);
	xmlHttp.onreadystatechange = ajaxKeywordReturn;
	xmlHttp.send(null);
}

function delKeyword(image_id, keyword) {
	if (xmlHttp == null) return false;
	
	var url = "maintKeywords?rem_keyword=" + keyword + "&image_id=" + image_id;
	
	xmlHttp.open("GET", url);
	xmlHttp.onreadystatechange = ajaxKeywordReturn;
	xmlHttp.send(null);
}

function ajaxKeywordReturn() {
	if (xmlHttp.readyState != 4 || xmlHttp.status != 200) return;
}

function localLightboxAction(cdID) {
	var img = document.getElementById("lightbox_icon");
	lightboxAction(img, cdID, 'lightbox', 'lightboxcd');
}