/**
 * Utilities for LAN Panic!
 * by Mark Woodman
 * Copyright (c) 2005
 */
 

Utils = new Object();

Utils.nextLevel = 0;

/**
 * Debugging can be nice sometimes.
 */
var DEBUG = false;
var debugCount = 1;

function debug(msg)
{
    if(DEBUG==true) 
    {
        var fullMsg = "<br>" + (debugCount++) + ": " + msg;
        var console = document.getElementById("console");
        if(!console)
        {
            console = document.createElement("div");
            console.setAttribute("id", "console");
            console.setAttribute("style", "background-color:#cccccc; color:black; clear:both");
            document.getElementsByTagName("body")[0].appendChild(console);
        }
        console.innerHTML = fullMsg + console.innerHTML;
    }
}

function debugClear()
{
    var console = document.getElementById("console");
    console.innerHTML = "";
}
 
 
Utils.clearLinks = function()
{
    navNode = document.getElementById("navigation");
    if(navNode!=null)
    {
    	while(navNode.hasChildNodes()) navNode.removeChild(navNode.firstChild);
    }
}

Utils.showResetLink = function()
{
    // Create link
    linkNode = document.createElement("A");
    linkNode.appendChild(document.createTextNode("Reset Puzzle"));
    linkNode.setAttribute("href", "#");
    linkNode.className = "linkButton";
    linkNode.onclick = function() { Grid.instance.reset(); return false; };
    
    // Show link
    navNode = document.getElementById("navigation");
    if(navNode) navNode.appendChild(linkNode);
}

Utils.showNextPuzzleLink = function(label, index)
{
  if(Levels[index])
  {
		// Create link
	  linkNode = document.createElement("A");
	  linkNode.appendChild(document.createTextNode(label));
	  linkNode.setAttribute("href", "#");
	  linkNode.className = "linkButton";
	  linkNode.onclick = function() { Utils.loadLevel(index); return false; };
	  navNode = document.getElementById("navigation");
	  if(navNode) navNode.appendChild(linkNode);
	}
}

Utils.showCustomPuzzlesLink = function()
{
	linkNode = document.createElement("A");
	linkNode.appendChild(document.createTextNode("Custom Puzzles"));
	linkNode.setAttribute("href", "fame.htm");
	linkNode.className = "linkButton";
	
    navNode = document.getElementById("navigation");
	navNode.appendChild(linkNode);
}

Utils.showNavigation = function()
{
	// Remove previous nav
    Utils.clearLinks();
    
    // Show reset button
    Utils.showResetLink();
    
    // Only show Editor button if test mode
    if(Grid.instance.testMode)
    {
        Utils.showEditorLink(true);
        return; 
    }
    
    // Show level buttons
    if(Grid.instance.hasCustomPuzzle==true)
    {
    		Utils.showCustomPuzzlesLink();
	    	Utils.showNextPuzzleLink("Official Puzzles", 0);	
    }
    else
  	{
  		  // Show level buttons if official puzzle solved
	    	if(Grid.instance.solved==true)
	    	{
	    			Utils.nextLevel++;
	    			if(Levels[Utils.nextLevel])
		  			{
		  				Utils.showNextPuzzleLink("Next Puzzle", Utils.nextLevel);
				    }
				    else
				    { 
		  				Utils.showEditorLink(false);
		  				alert("You solved all of our puzzles!\nTry some of the Hall of Fame puzzles, or\nmake your own with the Puzzle Editor!");
		  			} 		
	  		} 
  	}
}

/**
 * Load a level based on index.
 */

Utils.loadLevel = function(index)
{
    Grid.instance.currentPuzzle = Levels[index];
    Grid.instance.hasCustomPuzzle = false;
    Grid.instance.reset()
}


Utils.showEditorLink = function(editPuzzle)
{
    var label;
    var href;
    if(editPuzzle==true)
    {
        label = "Return to Editor";
        href = new String(document.location).split("index.htm").join("editor.htm");
    }
    else
    {
        label = "Puzzle Editor";
        href = "editor.htm";
    }
    
    // Create link
    linkNode = document.createElement("A");
    linkNode.appendChild(document.createTextNode(label));
    linkNode.setAttribute("href", href);
    linkNode.className = "linkButton";
    
    // Show link
    navNode = document.getElementById("navigation");
    if(navNode) navNode.appendChild(linkNode);
}

Utils.launchEmail = function(subject)
{
    if(!subject) subject = "LAN Panic!";
    window.location="mailto:mark@markwoodman.com?subject=" + subject;   
}

/**
 * This loads an array with every hardware
 * image used in the game.  Used for preloading
 * as the page loads, this ensures all rotations
 * of the hardware are already cached in the browser.
 */
Utils.loadImages = function()
{
    Utils.preload = new Array();
    for(x in Hardware.FILE_NAMES)
    {
        for(y in Hardware.ORIENTATIONS)
        {
            for(z in Hardware.STATES)
            {
                var img = new Image(41,41);
                img.src = Hardware.IMAGE_DIR 
                          + Hardware.FILE_NAMES[x] 
                          + "_" + Hardware.ORIENTATIONS[y] 
                          + "_" + Hardware.STATES[z] 
                          + ".png";
                Utils.preload.push(img);
            }
        }
    }
}