var linkCode = "<a id=\"pwyl_print_button\" href=\""+_pwyl_home+"\" onclick=\"javascript:(function(){window._pwyl_home='"+_pwyl_home+"';window._pwyl_print_button=document.createElement('script');window._pwyl_print_button.setAttribute('type','text/javascript');window._pwyl_print_button.setAttribute('src',window._pwyl_home+'js/print_button/${id}');window._pwyl_print_button.setAttribute('pwyl','true');document.getElementsByTagName('head')[0].appendChild(window._pwyl_print_button);document.body.style.cursor='progress';document.getElementById('pwyl_print_button').style.cursor='progress';})();return false;\" title=\"Print this page\" style=\"text-decoration: none;\">${button}</a>";
var buttons = 	{
					logo: 				"<img src='"+_pwyl_home+"button/print_button_icon2.png' alt='Print' border='0' />",
					logo_small:			"<img src='"+_pwyl_home+"button/print_button_icon_small2.png' alt='Print' border='0' />",
					icon: 				"<img src='"+_pwyl_home+"button/print_button_icon.png' alt='Print' border='0' />",
					icon_small:			"<img src='"+_pwyl_home+"button/print_button_icon_small.png' alt='Print' border='0' />",
					big_logo_text:		"<img src='"+_pwyl_home+"button/printer_icon2.png' border='0' alt='Print' />&nbsp;<span style='color: #719a11; font-size: 20px'>Print</span>",
					small_logo_text:	"<img src='"+_pwyl_home+"button/printer_icon_small2.png' border='0' alt='Print' />&nbsp;<span style='color: #719a11; font-size: 15px'>Print</span>",
					big_icon_text:		"<img src='"+_pwyl_home+"button/printer_icon.png' border='0' alt='Print' />&nbsp;<span style='color: #719a11; font-size: 20px'>Print</span>",
					small_icon_text:	"<img src='"+_pwyl_home+"button/printer_icon_small.png' border='0' alt='Print' />&nbsp;<span style='color: #719a11; font-size: 15px'>Print</span>"//,
//					text:				"<span style='text-decoration: none; color: #719a11;'>Print</span>"
				};

/*------------------ Util --------------*/
function setError(errorMessage, fieldId, anchorId) {
	var anchor = document.getElementById(anchorId);
	anchor.innerHTML = errorMessage;
	anchor.style.display = "inline";
	YAHOO.util.Dom.addClass(fieldId, "error");
}

function clearError(fieldId, anchorId) {
	document.getElementById(anchorId).style.display = "none";
	YAHOO.util.Dom.removeClass(fieldId, "error");
}

/*------------------ Name + Url --------------*/
function validateName() {
	var name = document.getElementById("printer_layout_name").value;
	if (name.length == 0) 
		setError("Please enter a name", "printer_layout_name", "printer_layout_name_error");
	else
		clearError("printer_layout_name", "printer_layout_name_error");
}

function validateUrl() {
	var url = document.getElementById("printer_layout_url").value;
	if (url.length == 0)
		setError("Please enter a url", "printer_layout_url", "printer_layout_url_error");
//	else if ()
//		setError("Please enter a valid url", "printer_layout_url", "printer_layout_url_error");
	else {
		clearError("printer_layout_url", "printer_layout_url_error");
	}
}

function step1IsValid() {
	var name = document.getElementById("printer_layout_name").value;
	var url = document.getElementById("printer_layout_url").value;
	return 	name.length > 0 && name != "(ex. PrintWhatYouLike Blog Articles)" && 
			url.length > 0 && url != "(ex. http://www.printwhatyoulike.com/blog/2009/02/16/promotional-video)";
}

function setHints() {
	var nameBtn = document.getElementById("printer_layout_name");
	var urlBtn = document.getElementById("printer_layout_url");
	var defaultName = "(ex. PrintWhatYouLike Blog Articles)";
	var defaultUrl = "(ex. http://www.printwhatyoulike.com/blog/2009/02/16/promotional-video)";
	if (nameBtn.value == "" || nameBtn.value == defaultName) {
		nameBtn.value = defaultName;
		nameBtn.className = "hint";
	}
	if (urlBtn.value == "" || urlBtn.value == defaultUrl) {
		urlBtn.value = defaultUrl;
		urlBtn.className = "hint";
	}
}

function clearHint(id) {
	var field = document.getElementById(id);
	if (field.className == "hint") {
		field.value = "";
		field.className = "";
	}
}

/*------------------ Icons --------------*/
function displayIcons() {
	
	var table = document.getElementById("printer_layout_icons_wrapper");
	var i = 0;
	var str = "<table id=\"printer_layout_icons\"><tr>";
	for (var key in buttons) {
		str += "<td>\
					<input type='radio' name='printer_layout_icons' id='printer_layout_"+key+"' value='"+key+"' tabindex='2"+i+"' onclick='chooseIcon(this.value)' "+(i==0 ? 'checked' : '')+">\
					<label for='printer_layout_"+key+"'>"+buttons[key]+"</label>\
				</td>";
		i++;
		if ((i % 4) == 0) str += "</tr><tr>";
	}
	str += "</tr></table>";
	table.innerHTML = str;
}

function chooseIcon(iconKey) {
	buttonIcon = iconKey;
	
	//has the code already been generated?
	if (document.getElementById("printer_layouts_code").disabled == false) {
		setCode();
	}
}

function getButtonIcon() {
	var icons = document.getElementById("printer_layout_icons").getElementsByTagName("input");
	for (var i=0; i<icons.length; i++) {
		if (icons[i].checked) return icons[i].value
	}
}

/*------------------ Edit Template --------------*/
function editTemplate() {
	if (!step1IsValid()) {
		alert("Please enter a valid name and url.");
		return false;
	}
	
	var url = "/print_button/edit" + 
				"?id=" + document.getElementById("printer_layout_id").value +
				"&url=" + encodeURIComponent(document.getElementById("printer_layout_url").value);
	window.templatePanel = new IFramePanel(url, "ppw_new_template_bd", "ppw_new_template");
	window.scroll(0,0); //if user is scrolled down, window will appear scrolled off the top
	window.templatePanel.show();
}

function _pwyl_saveChangeSetToSever() {
	var ifrwin = window.templatePanel.ifr.contentWindow;
	
	var params = {
					id: document.getElementById("printer_layout_id").value,
					name: document.getElementById("printer_layout_name").value,
					key: ifrwin.ppw.server.id,
					url: ifrwin.pwylProperUrl
				};
	params = ifrwin.ppw.server.serializeCommandObjects(params);
	ifrwin.ppw.server.ajaxCall('/print_button/saveOrUpdate', params, _pwyl_continueEditingChangeSet);
}

function _pwyl_continueEditingChangeSet(response) {
	var ifrppw = window.templatePanel.ifr.contentWindow.ppw;
	
	var changesList = "";
	for (var i=0; i<ifrppw.changeset.doStack.length; i++) {
		changesList += "<li>" + ifrppw.changeset.doStack[i].name + "</li>";
	}
	
	updateEditTemplate(response, changesList);	//update list of changes
	templatePanel.hide();	//close iframe
}

function updateEditTemplate(changeId, changesMsg) {
	document.getElementById("changes_list").innerHTML = changesMsg;
	updateChangesListStyle();
	document.getElementById("printer_layout_id").value = changeId;	//set id so clicking add changes again will prepopulate changes
	setCode();
}

function updateChangesListStyle() {
	var changesList = document.getElementById("changes_list");
	if (changesList.getElementsByTagName("li").length > 0) {
		changesList.style.marginTop = "10px";
		var changesAdd = document.getElementById("changes_add");
		changesAdd.style.paddingTop = "10px";
		changesAdd.style.paddingBottom = "10px";
		document.getElementById("changes_add_button").value = "Edit";
	}
}

/*------------------ Button Code --------------*/
function setCode() {
	if (_pwyl_no_layout)
	{
		var csId = '';
	}
	else
	{
		var csId = document.getElementById("printer_layout_id").value;
	}
	if (_pwyl_wordpress_layout)
	{
		var inputField = document.getElementById("button_id");
		inputField.disabled = false;
		inputField.value = csId;
		inputField.select();
	}
	else
	{
		var buttonCode = buttons[getButtonIcon()];
		var textarea = document.getElementById("printer_layouts_code");
		textarea.disabled = false;
		textarea.value = linkCode.replace("${id}", csId).replace("${button}", buttonCode);
		textarea.select();
	}
}

/*------------------ Start --------------*/
displayIcons();
if (_pwyl_no_layout)
{
	setCode();
}
else
{
	updateChangesListStyle();
	if (document.getElementById("changes_list").getElementsByTagName("li").length > 0) setCode();
	setHints();
}