/**
 * Scripts for simulator
 * @author Nikola Topalovic <mailto:nikola.topalovic@euroicc.com>
 */

/**
 * Globals
 */

var simulator   = null;
var iframe      = null;
var startScreen = null;
var iPhoneBack  = null;
var appIcon = null;
var helpOutlet = null;

var simulatorWidth  = 320;
var simulatorHeight = 480;


/**
 * Simple event handling
 */
function addEvent(obj, evt, fn) {
    if (obj.addEventListener)
	obj.addEventListener(evt, fn, false);
    else if (obj.attachEvent)
	obj.attachEvent('on'+evt, fn);
}


/**
 * Simple event handling
 */
function removeEvent(obj, evt, fn) {
    if (obj.removeEventListener)
	obj.removeEventListener(evt, fn, false);
    else if (obj.detachEvent)
	obj.detachEvent('on'+evt, fn);
}


/**
 * Launches application by injecting iframe containing relevant widget
 */
function appLaunch(pathToWidget) {
    iframe.setAttribute("src", pathToWidget);
    simulator.appendChild(iframe);
}

/**
 * Removes iframe
 */
function appClose() {
    if (iframe.parentNode)
	iframe.parentNode.removeChild(iframe);
    updateHelp('Intro');
}

/**
 *
 */
function updateHelp(chapter) {
    if (manual[chapter]) {
	helpOutlet.innerHTML = manual[chapter];
    }
}

/**
 * Initializes globals and attaches events
 */
function init() {
    simulator = document.getElementById("simulator");
    startScreen = document.getElementById("startScreen");
    helpOutlet = document.getElementById("outlet");
    
    iframe = document.createElement("iframe");
    iframe.setAttribute("name", "frame");
    iframe.setAttribute("id", "frame");
    iframe.setAttribute("width", simulatorWidth);
    iframe.setAttribute("height", simulatorHeight);

    iPhoneBack = document.getElementById("iPhoneBack");
    if (iPhoneBack != null) {
	iPhoneBack.removeAttribute("href");
	addEvent(iPhoneBack, 'click', appClose);
    }

    appIcon = document.getElementById("appIcon");
    if (appIcon != null) {
	pathToWidget = appIcon.getAttributeNode("rel").value;
	appIcon.removeAttribute("href");
	addEvent(appIcon, 'click', function() { appLaunch(pathToWidget); } );
    }
    updateHelp('Intro');
}

/**
 * Application entry point
 */
addEvent(window, 'load', init);


