function getCookie(cookieName)
{
	var mycookies = String(document.cookie);
	var cookarray = mycookies.split(/;/g);
	for(each in cookarray){
		if(cookarray[each].indexOf(cookieName) > -1){
			// found the cookie, let's apply it
			return cookarray[each].substr(cookarray[each].indexOf("=") + 1);
		}
	}
	return null;
}


function contains(arr, value) {
	var lb = arr.split(/,/g);
	for(var i in lb) {
		if(lb[i] == value) {
			return true;
		}
	}
	return false;
}

//function isInLightbox(imageId) {
//	var lb = getCookie("lightbox");
//	return contains(lb, imageId);
//}

function isInLightbox(imageId) {
	//var lb = getCookie("lightbox");
	//return contains(lb, imageId);
	return false;
}

//now the lightbox stuff
function addToLightbox(imageId) {
	var status = document.forms.lightboxform["cb_" + imageId].checked;
	var exp = new Date();
	var plusOneYear = exp.getTime() + (365 * 24 * 60 *60 * 1000);
	exp.setTime(plusOneYear);
	if(!status) {
		removeFromLightbox(imageId, exp);
		document.cookie = "added=false";
	}
	else {
		var lb = getCookie("lightbox");
		if(lb == " lightbox") lb = "";
		if((lb == null) || (lb == "")) {
			document.cookie = "lightbox=" + imageId + ";expires=" + exp.toGMTString() + "; path=/";
		}
		else {
			if(!contains(lb, imageId)) {
				document.cookie = "lightbox=" + lb + "," + imageId + ";expires=" + exp.toGMTString() + "; path=/";
			}
		}
		var im = MM_findObj("sp_" + imageId);
		if(im) {
				im.src = "/img/lb_ar_over.gif";
		}
		else {
			im = MM_findObj("gall_" + imageId)
			if(im) {
				im.src = "/img/lb_gal_over.gif";
			}
		}
		document.cookie = "added=true";
	}


}


function addToLightboxSimple(imageId, clipid, g) {
	var exp = new Date();
	var plusOneYear = exp.getTime() + (365 * 24 * 60 *60 * 1000);
	exp.setTime(plusOneYear);
	
	var lb = getCookie("lightbox");
	if(lb == " lightbox") lb = "";
	if((lb == null) || (lb == "")) {
		//alert("lightbox is null or empty");
		document.cookie = "lightbox=" + imageId + ";expires=" + exp.toGMTString() + "; path=/";
	}
	else {
		if(!contains(lb, imageId)) {
			document.cookie = "lightbox=" + lb + "," + imageId + ";expires=" + exp.toGMTString() + "; path=/";
		}
		else {
			//alert("This image is already in lightbox, not adding it");
			
		}
		if(clipid) {
			var clip = MM_findObj(clipid);
			if(clip) {
				if(g) {
					clip.src = "/img/zf_clip_added_g.gif";
				} else {
					clip.src = "/img/zf_clip_added.gif";
				}
			}
		}
	}
	document.cookie = "added=true;path=/";
}

function addToFavorites(artistId, clipid, g, normalImage, addedImage) {

    var exp = new Date();
	var plusOneYear = exp.getTime() + (365 * 24 * 60 *60 * 1000);
	exp.setTime(plusOneYear);
	
	var lb = getCookie("favorites");
	if(lb == " favorites") lb = "";
	if((lb == null) || (lb == "")) {
		//alert("lightbox is null or empty");
	    document.cookie = "favorites=" + artistId + ";expires=" + exp.toGMTString() + "; path=/";
	}
	else {
	    if (!contains(lb, artistId)) {
		    document.cookie = "favorites=" + lb + "," + artistId + ";expires=" + exp.toGMTString() + "; path=/";
		}
		else {
			//alert("This artist is already in favorites, not adding it");
			
		}
		if(clipid) {
			var clip = MM_findObj(clipid);
			if(clip) {
				if(g) {
					clip.src = "/img/" + addedImage;
				} else {
					clip.src = "/img/" + normalImage;
				}
			}
		}
	}
	document.cookie = "favupdated=yes;path=/";
}

function removeFromFavorites(artistId) {
    var exp = new Date();
    var plusOneYear = exp.getTime() + (365 * 24 * 60 * 60 * 1000);
    exp.setTime(plusOneYear);

    var lb = getCookie("favorites");
    if ((lb == null || lb == "") || (lb == "favorites")) return;
    var images = lb.split(/,/g);
    var arr = "";
    for (var i = 0; i < images.length; i++) {
        if ((String(images[i]) != String(artistId)) && (images[i] != "favorites")) {
            if (arr != "") arr += ",";
            arr += images[i];
        }
    }
    document.cookie = "favorites=" + arr + ";expires=" + exp.toGMTString() + "; path=/";
}


function addToLightboxNew(imageId, clipid, g, normalImage, addedImage) {
	var exp = new Date();
	var plusOneYear = exp.getTime() + (365 * 24 * 60 *60 * 1000);
	exp.setTime(plusOneYear);
	
	var lb = getCookie("lightbox");
	if(lb == " lightbox") lb = "";
	if((lb == null) || (lb == "")) {
		//alert("lightbox is null or empty");
		document.cookie = "lightbox=" + imageId + ";expires=" + exp.toGMTString() + "; path=/";
	}
	else {
		if(!contains(lb, imageId)) {
			document.cookie = "lightbox=" + lb + "," + imageId + ";expires=" + exp.toGMTString() + "; path=/";
		}
		else {
			//alert("This image is already in lightbox, not adding it");
			
		}
		if(clipid) {
			var clip = MM_findObj(clipid);
			if(clip) {
				if(g) {
					clip.src = "/img/" + addedImage;
				} else {
					clip.src = "/img/" + normalImage;
				}
			}
		}
	}
	document.cookie = "lbupdated=yes;path=/";
	//document.cookie = "added=true;path=/";
}


function removeFromLightbox(imageId) {
	var exp = new Date();
	var plusOneYear = exp.getTime() + (365 * 24 * 60 *60 * 1000);
	exp.setTime(plusOneYear);
	
	var lb = getCookie("lightbox");
	if((lb == null || lb == "") || (lb == "lightbox")) return;
	var images = lb.split(/,/g);
	var arr = "";
	for(var i = 0; i < images.length; i++){
		if((String(images[i]) != String(imageId)) && (images[i] != "lightbox")) {
			if(arr != "") arr += ",";
			arr += images[i];
		}
	}
	document.cookie = "lightbox=" + arr + ";expires=" + exp.toGMTString() + "; path=/";
	document.cookie = "lbupdated=yes;path=/";
	window.location.reload();

}