//-------------- aardvark_load () ---------------------
// load the aardvark code callbacks if necessary

function aardvark_load () {
    if (window.aardvark == null) {
	var path = "http://www.printfriendly.com/javascripts/";
	// this is where we actually create the aardvark object and initialize a few members
	window.aardvark = {isBookmarklet:true, resourcePrefix:path, userCallbacks:[]};	
	// list of javascript files we need to load
	var files = [path+'aardvarkStrings.js', path+'aardvarkUtils.js', path+'aardvarkDBox.js', path+'aardvarkCommands.js', path+'aardvarkMain.js'];
	YAHOO.util.Get.script(files);
    }
}

function validSelection() {
    var limitElem = document.getElementById('pf-print-area');
    return YAHOO.util.Dom.isAncestor(limitElem, aardvark.selectedElem) && (aardvark.selectedElem.parentNode != limitElem);
}

function selectedActionHandler(e) {
    aardvark.selectedAction(aardvark.getElemFromEvent(e));
}

function onUndo() {
    aardvark.undo();
    return false;
}

function removeImages(e) {
    var images = document.getElementById('pf-print-area').getElementsByTagName('img');
    if (e.checked) { 
	for (var i = 0; i < images.length; ++i) {
	    images[i].style.display = 'none';			
	}
    } else {
	for (var j = 0; j < images.length; ++j) {
	    images[j].style.display = '';			
	}
    }

    return false;
}

function borderClick(e, elem) {
    aardvark.selectedAction(elem);
    
    return false;
}

function getPdf() {       
    window.aardvark.suspend();
    window.aardvark.clearBox();
    var pdfElem = document.getElementById('pnpdfform');
    if (pdfElem != null) {
	pdfElem.parentNode.removeChild(pdfElem);
    }    	
    var pdfId = document.getElementById('pfprintid').value;

    var i = document.createElement('input');
    i.name = 'code';
    i.id = 'pdfcode';
    i.type = 'hidden';
    i.value = window.document.documentElement.innerHTML;

    var j = document.createElement('input');    
    j.name = 'printid';
    j.id = 'pdfid';
    j.type = 'hidden';
    j.value = pdfId;			   

    var f = document.createElement('form');       
    f.id = 'pnpdfform';			   
    f.method = 'POST';
    f.action = '/print/genpdf';
    f.appendChild(i);
    f.appendChild(j);

    document.getElementById('pf-print-popup').style.display = 'none';
    document.getElementById('pf-pdf-popup').style.display = 'block';
    document.body.appendChild(f);
    f.submit();
}

function closePrintBox() {
    document.getElementById('pf-print-popup').style.display = 'none';
    window.aardvark.resume();
    return false;
}

function closePdfBox() {
    document.getElementById('pf-pdf-popup').style.display = 'none';
    window.aardvark.resume();
    return false;
}

function printIt() { 
    var printId = document.getElementById('pfprintid').value;
    var postData = "printid="+printId;
    var sUrl = "/print/mark";
    var callback = null;
  
    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);			   

    window.aardvark.suspend();
    window.aardvark.clearBox();
    document.getElementById('pf-print-popup').style.display = 'block';
    window.focus(); window.print();

    return false;
}
