
var IS_DEVEL_SITE = (window.location.host != "www.myfonts.com");

function MF_addToAlbum(linkID)
{
	//return false on success

	if (!Sarissa && IS_DEVEL_SITE) 
	{
		alert("You should include /js/sarissa.js before fontalbums.js");
		return true;
	}

	if (!Sarissa.IS_ENABLED_XMLHTTP)
		return true;
	
	var a = document.getElementById(linkID);
	
	if (typeof a == 'undefined' || a == null)
	{
		if (IS_DEVEL_SITE)
			alert("Badly defined link: " . linkID);
		return true;
	}

	var xmlhttp = new XMLHttpRequest;
	xmlhttp.open("GET",a.href + "&xmlmode=1",false);
	xmlhttp.send('');

	if (!xmlhttp.responseXML || !xmlhttp.responseXML.getElementsByTagName('success').length)
	{
		if (IS_DEVEL_SITE)
			alert(xmlhttp.responseText);
			
		return true;  //fail!
	}

	var aid;
	if (aid = xmlhttp.responseXML.getElementsByTagName('success')[0].getAttribute('addedto'))
		a.href="/FontAlbums/AlbumDetail?returnto=" + encodeURIComponent(window.location.href) + "&aid=" + aid;
	else
		a.href="/FontAlbums/AlbumList?returnto=" + encodeURIComponent(window.location.href);
		
	a.title = "Go to Album";
	a.onclick = null;

	var img = document.getElementById(linkID + "Img");
	
	if (!img) return false; //return success even though image couldn't be swapped
	
	img.src="/images/icons/steve/album-2006.gif";
	//img.style.width="34px";
	//img.style.height="43px";
	img.alt="Album Icon";
	
	return false;
}


function albumDetailOnLoad()
{
	var kw = document.getElementById('albumkeywords');
	var ml = document.getElementById('morelike');

	if (!kw || !ml) return;

	dragObj.init('albumkeyworddrag','albumkeywords',0,0,-1000,1000,-1000,1000);
	dragObj.init('morelikedrag','morelike',0,0,-1000,1000,-1000,1000);

	kw.on_drag_end = saveWindowCoords;
	ml.on_drag_end = saveWindowCoords;
}

function saveWindowCoords(left,top)
{
	var kw = document.getElementById('albumkeywords');
	var ml = document.getElementById('morelike');

	if (!kw || !ml) return;

	document.cookie="albumkeywordcoords=" + parseInt(kw.style.left) + "," + parseInt(kw.style.top) + "," + parseInt(kw.style.zIndex) + "," + parseInt(ml.style.left) + "," + parseInt(ml.style.top) + "," + parseInt(ml.style.zIndex) + ";path=/FontAlbums";
}


var prevTitle;
var xmlhttp;

var checkboxes = new Array();

function setTitle(field)
{
	if (!Sarissa.IS_ENABLED_XMLHTTP)
		return true;

	var theDiv = document.getElementById(field);
	
	var theTitle = document.getElementById(field + 'Field').value;

	xmlhttp = new XMLHttpRequest();

	xmlhttp.open("POST","/FontAlbums/xmlalbumupdate.php",false);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	var post = "aid=" + aid + "&";
	if (field=='titleDiv')
		post += encodeURIComponent("newinfo[AlbumName]") + "=" + encodeURIComponent(theTitle);
	else if (field=='descDiv')
		post += encodeURIComponent("newinfo[AlbumDescription]") + "=" + encodeURIComponent(theTitle);

	xmlhttp.send(post);

	if (xmlhttp.status != 200
		|| !xmlhttp.responseXML
		|| xmlhttp.responseXML.getElementsByTagName("error").length)
	{
		theDiv.innerHTML = prevTitle.replace(/\n/g,"<br>\n");

		alert("Oh no! " + xmlhttp.responseXML.getElementsByTagName("error")[0].firstChild.nodeValue);
		return false;
	}

	theDiv.innerHTML = theTitle;
	return false;
}

function editTitle(field)
{
	var theDiv = document.getElementById(field);

	if (theDiv.innerHTML.match(/<input /i)) return false;

	prevTitle = theDiv.innerHTML;

	if (field=='titleDiv')
	{
		var newForm = "<input type='hidden' name='aid' value='" + aid + "'>";
		newForm += "<input id='titleDivField' type='text' size='24' style='width:400px;font-size:14pt;' name='newtitle' value=\"" + prevTitle.replace(/"/g,'&quot;') + "\">";
	}
	else if (field=="descDiv")
	{
		var newForm = "<input type='hidden' name='aid' value='" + aid + "'>";
		newForm += "<textarea id='descDivField' rows='4' cols='48' wrap='soft' name='newdesc'>\n";
		newForm += prevTitle;
		newForm += "</textarea><br>";
	}

	newForm += "<input type='button' name='action' onclick=\"document.getElementById('" + field + "').innerHTML=prevTitle; event.cancelBubble=true;\" value='Cancel'> <input type='submit' name='action' value='Save'>";

	theDiv.innerHTML = newForm;
	document.getElementById(field + 'Field').focus();
}

function toggleDefault()
{
	if (!Sarissa.IS_ENABLED_XMLHTTP)
		return true;

	var a = document.getElementById('defaultLink');
	var re = /default=(-?[0-9]+)/;
	var newDef = re.exec(a.href)[1];
		
	xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET",a.href,false);
	xmlhttp.send("");
		
	//if (xmlhttp.status < 300)
	//	return true;


	var img = document.getElementById('defaultCheckImg');
	
	if (newDef <= 0)
	{
		img.src="/images/small/greycheckcircle.gif";
		img.style.width = img.style.height = "15px";
		img.alt = "Not Default";

		a.title = "Make Default";
		a.href = a.href.replace("default=-1","default=" + aid);
	}
	else
	{
		img.src="/images/small/greencheckcircle.gif";
		img.style.width = img.style.height = "20px";
		img.alt = "Default Album";

		a.title = "Disable Default";
		a.href = a.href.replace("default=" + aid,"default=-1");
	}

	return false;
}

