var margins = [];
margins["layered_paper"] = [248, 230, 142, 230];
margins["parchment"] = [16, 10, 20, 10];
margins["cremebg"] = [0, 68, 0, 68];
margins["tractorfeed"] = [19, 21, 22, 21];

var frameTheseNodes = [ ".autoframed", 
                        ".framedBlockquotes blockquote", 
                        ".framedCode code"]; 

dojo.addOnLoad(
    function() {       
        autoframe();
        
        // Tag the code for google-code-prettify.
        dojo.query(".framedCode code").addClass("prettyprint");
        // Call google-code-prettify
        prettyPrint();
    }
)

function postPictureFrame() {
    dojo.query(".framedImage").forEach(function(image) {
        var width = dojo.attr(image, "width");
    
		var div = document.createElement("div");
		dojo.style(div, "margin", "0 auto");
		dojo.style(div, "textAlign", "center");
		if(width && width != 0) {
            dojo.style(div, "maxWidth", width+"px");
        }

        var float = dojo.style(image, "float");
        if(!float) {
            float = dojo.attr(image, "align");
	    }
        if(float == "right") {
           dojo.style(div, "float", "right");
           dojo.style(div, "margin", "0 0 0 10px");
           if(width) {
               dojo.style(div, "width", width+"px");
           }
        }    
        if(float == "left") {
           dojo.style(div, "float", "left");
           dojo.style(div, "margin", "0 10px 0 0");
           if(width) {
               dojo.style(div, "width", width+"px");
           }
        }    
        

		var tape = document.createElement("div");
		tape.className="pictureFrameTape";
		
		div.appendChild(tape);
		
		var tbl = document.createElement("table");
	    tbl.setAttribute("cellSpacing", "0");
	    tbl.setAttribute("cellPadding", "0");
	    tbl.setAttribute("border" ,"0");
		dojo.style(tbl, "marginLeft", "auto");
		dojo.style(tbl, "marginRight", "auto");
		dojo.style(tbl, "width", "100%");
//		dojo.style(tbl, "height", "auto");

		var tblBody = document.createElement("tbody");
		
		var row = document.createElement("tr");
		var imageCell = document.createElement("td");
		
		var newImage = document.createElement("img");
		dojo.style(newImage, "position", "relative");
		dojo.style(newImage, "display", "block");
		dojo.style(newImage, "height", "auto");
	var widthWasSet = false;  		
if(width && width != 0) {
            dojo.style(newImage, "maxWidth", width+"px");
		if(float == "left" || float == "right") {
		    dojo.style(newImage, "width", width+"px");
		    widthWasSet = true;
		}
        }
if(!widthWasSet) {
		    dojo.style(newImage, "width", "100%");
}

		newImage.src = image.src;
		imageCell.appendChild(newImage);

		row.appendChild(imageCell);
		    		
		tblBody.appendChild(row);
		
		var row = document.createElement("tr");
		
		var cell = document.createElement("td");
		
		var shadowDiv = document.createElement("div");
		shadowDiv.className="pictureFrameShadowLeft";
		cell.appendChild(shadowDiv);
		
		var shadowDiv = document.createElement("div");
		shadowDiv.className="pictureFrameShadowMiddle";
		cell.appendChild(shadowDiv);
		
		var shadowDiv = document.createElement("div");
		shadowDiv.className="pictureFrameShadowRight";
		cell.appendChild(shadowDiv);
		
		row.appendChild(cell);    

		tblBody.appendChild(row);
		
		tbl.appendChild(tblBody);

		var angle = Math.random() - 0.5;
		angle *= 2;
		//if(angle <= 0) {
		//	angle -= 0.5;
		//} else {
		//	angle += 0.5;
		//}
		
		tbl.style.WebkitTransform="rotate(" + angle +"deg)";

		div.appendChild(tbl);
		
		image.parentNode.replaceChild(div, image);
    });
}

function frame(node) 
{
    var background = dojo.style(node, "backgroundImage");
    var backgroundBase = null;
    var backgroundExtension = null;
    var backgroundName = null;
    if(background) {
        matches = /url\("?(.*\/frames\/(.*))\/.*\.(...)"?\)/i.exec(background);
     	backgroundBase = matches[1];
        backgroundName= matches[2];
        backgroundExtension= matches[3];
    }

    if(backgroundName && backgroundBase && backgroundExtension) {
    	var paddingTop = margins[backgroundName][0];
        var paddingRight = margins[backgroundName][1];
     	var paddingBottom = margins[backgroundName][2];
     	var paddingLeft = margins[backgroundName][3];
     	        
        // creates a <table> element and a <tnode> element
        var tbl = document.createElement("table");
        tbl.setAttribute("cellSpacing", "0");
        tbl.setAttribute("cellPadding", "0");
        tbl.setAttribute("border" ,"0");
        
        var tblBody = document.createElement("tbody");
        var middlecell = null;
    
        // creating all cells
        for (var j = 0; j < 3; j++) {
     		if(j == 0 && paddingTop == 0) {
     			continue;
     		}
     		if(j == 2 && paddingBottom == 0) {
     			continue; 
     		}
            // creates a table row
            var row = document.createElement("tr");
            
            for (var i = 0; i < 3; i++) {
               	if(i == 0 && (!paddingLeft || paddingLeft == 0 || paddingLeft == "none")) {
     				continue;
     			}
     			if(i == 2 && (!paddingRight || paddingRight == 0 || paddingRight == "none")) {
     				continue;
     			}
            
                // Create a <td> element and a text node, make the text
                // node the contents of the <td>, and put the <td> at
                // the end of the table row
                var cell = document.createElement("td");
                
                if(i == 1 && j == 1) {
                    dojo.style(cell, "backgroundImage", background);
                    middleCell = cell;
                } else if(i == 1 || ((i == 0 && j == 0) || (i == 2 && j == 0) || (i == 0 && j == 0) || (i == 2 && j == 2))) {
                    var image = document.createElement("img");
                    dojo.style(image, "display", "block");
                    image.src = backgroundBase + "/" + (j+1) + "-" + (i+1) + "." + backgroundExtension;
                    if(i == 1) {
    					dojo.style(image, "width", "100%");
    					dojo.style(image, "height", j == 0 ? paddingTop + "px" : paddingBottom  + "px");
                    } else {
                   	 	cell.setAttribute("height", j == 0 ? paddingTop : paddingBottom + "px");
                  	  	cell.setAttribute("width", i == 0 ? paddingLeft : paddingRight + "px");
                    }
                    dojo.style(image, "minWidth", 3);
                    cell.appendChild(image);
                } else {
                    dojo.style(cell, "backgroundImage", "url(" + backgroundBase + "/" + (j+1) + "-" + (i+1) + "." + backgroundExtension + ")");
                    dojo.style(cell, "width", i == 0 ? paddingLeft + "px" : paddingRight  + "px");
                }
                row.appendChild(cell);
            }
            
            // add the row to the end of the table body
            tblBody.appendChild(row);
        }
        
        // put the <tbody> in the <table>
        tbl.appendChild(tblBody);
                    
        dojo.style(node, "background", "transparent");
        dojo.style(tbl, "position", "relative");
     
        var marginCorrector = document.createElement("div");
        dojo.style(marginCorrector, "margin", "-" + paddingTop + "px -" + paddingRight + "px -" + paddingBottom + "px -" + paddingLeft + "px");
        
        
        var widthCorrector = document.createElement("div");
        dojo.style(widthCorrector, "marginTop", dojo.style(node, "marginTop") + "px");
        dojo.style(node, "marginTop", "0");
        dojo.style(widthCorrector, "marginRight", dojo.style(node, "marginRight") + "px");
        dojo.style(node, "marginRight", "0");
        dojo.style(widthCorrector, "marginBottom", dojo.style(node, "marginBottom") + "px");
        dojo.style(node, "marginBottom", "0");
        dojo.style(widthCorrector, "marginLeft", dojo.style(node, "marginLeft") + "px");
        dojo.style(node, "marginLeft", "0");
        
        dojo.style(tbl, "width", "100%");
        
        middleCell.appendChild(marginCorrector);
        widthCorrector.appendChild(tbl);
        
        marginCorrector.appendChild(node.parentNode.replaceChild(widthCorrector, node))
                dojo.style(node, "zoom", "1");

    }
}

function autoframe()
{
    postPictureFrame();
    // get the reference for the body
    dojo.forEach(frameTheseNodes, 
        function(selector) {
            dojo.query(selector).forEach(frame);
        });
}
