// -----------------------------------------------------------------------------
// This file is part of [nice] frame
// (c) 2007 Dynamic Media eLearning GmbH
// All rights reserved.
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// $Id: skin.js 1476 2008-01-21 11:54:49Z dynamicmedia\Arnold $
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// Imports
// -----------------------------------------------------------------------------

NICE.include("util.Browser");
NICE.include("util.DOMQuery");
NICE.include("ui.SplitPane");
NICE.include("ui.ProgressBar");

// -----------------------------------------------------------------------------
// General
// -----------------------------------------------------------------------------

Player.skin.id      = "imc.standard";
Player.skin.name    = "POWERTRAINER Standard";
Player.skin.version = "1.1.0";

// -----------------------------------------------------------------------------
// Settings
// -----------------------------------------------------------------------------

Player.skin.settings = {
	minimumSize:        [995,580],
	// The initial width of the TOC
	tocInitialWidth:    300,
	// The maximum width of the TOC
	tocMaximumWidth:    350,
	// The margin between the top of the browser's client area and the TOC
	tocMarginTop:       39,
	// The height of the minimize host flash
	hostFlashMinHeight: 26,
	// The height of the maximized host flash
	hostFlashMaxHeight: 200,
	// The current height of the host flash
	hostFlashHeight:    200,
	// Show the host flash
	showHostFlash:      true,
	// The onshow effect for the language selection
	langSelShowFx:      { fx: "fade", start: 0, end: 100, duration: 0.4 },
	// The onhide effect for the language selection
	langSelHideFx:      { fx: "fade", start: 100, end: 0, duration: 0.2 }
};

// -----------------------------------------------------------------------------
// Initialize skin specific settings
// -----------------------------------------------------------------------------

Player.skin.initialize = function() {
	
	var settings = Player.skin.settings;
	
	// Re-arrange options ------------------------------------------------------
	var options = [
		"option-exit",
		"option-help",
		"option-status",
		"option-print",
		"option-text",
		"option-audio",
		"option-glossary",
		"option-profiles",
		"option-home"
	];
	var addoptions = $$("nice-add-options");
	for(var i = 0; i < options.length; i++) {
		addoptions.appendChild($$(options[i]));
	}
	
	// Set up a split pane -----------------------------------------------------
	this.splitPane = new SplitPane("nice-container", {
		identifier:      "nice-splitpane",
		initialwidth:     settings.tocInitialWidth,
		maximumwidth:     settings.tocMaximumWidth,
		// For Firefox & Co. it is necessary to hide the content iframe on
		// drag operations of the split pane. Otherwise it would prevent
		// mouseevents from reaching the parent window.
		begindraghandler: function() {
			Player.contentFrame.hide();
			Player.events.emit("toclock", true);
		},
		enddraghandler:   function(width) {
			Player.contentFrame.show();
			// Store the current position as user preference
			Player.skin.setUserPref("toc.width", width);
			Player.events.emit("toclock", false);
			_ie6cssHack(true);
		}
	});
	
	// Set up the toggle button ------------------------------------------------
	var toggle = document.createElement("div");
	toggle.id = "toggle-toc";
	this.toggleButton = new NICE.ui.Button(toggle, {
	    identifier:   "b-toggle-toc",
	    clickhandler:  function() {
	        Player.skin.splitPane.toggleViewState();
			_ie6cssHack();
	    }
	});
	$$("nice-content").appendChild(toggle);
	
	this.splitPane.insertIntoFirst(
		"nice-toc",
		"nice-hostflash");
	this.splitPane.insertIntoSecond("nice-content");
	
	// Hide the play button ----------------------------------------------------
	Player.buttons.play.hide();
	
	// Set the effects for the language selection ------------------------------
	Player.langSelect.setOnShowEffect(settings.langSelShowFx);
	Player.langSelect.setOnHideEffect(settings.langSelHideFx);
	
}

// -----------------------------------------------------------------------------
// Size handler
// -----------------------------------------------------------------------------

Player.skin.setSize = function() {
	
	var settings = Player.skin.settings;
	var toc = $$("nice-toc");
	var sz = Browser.elementSize(window);
	var container = $$("nice-container");

	// Ensure minimum size -----------------------------------------------------
	if(sz[0] < settings.minimumSize[0]) {
		sz[0] = settings.minimumSize[0];
	}
	container.style.width = sz[0] + "px";

	if(sz[1] < settings.minimumSize[1]) {
		sz[1] = settings.minimumSize[1];
	}
	container.style.height = sz[1] + "px";

	var tocheight = sz[1] - settings.tocMarginTop - settings.hostFlashHeight;
	$$("nice-hostflash").style.top = tocheight + "px";

	// Set size of TOC and content ---------------------------------------------
	toc.style.height = tocheight + "px";
	$$("nice-content").style.height = (sz[1] - settings.tocMarginTop) + "px";
	$$("nice-splitpane-first").style.height =
		(sz[1] - settings.tocMarginTop) + "px";
	
	// Show the split pane -----------------------------------------------------
	if(!this.splitPane.isVisible()) this.splitPane.show();
	if(this.isIE6) Player.main.height = tocheight;	
}

// -----------------------------------------------------------------------------
// Host flash API
// -----------------------------------------------------------------------------

Player.skin.showHostFlash = function(mode) {
	
	var size = 0;
	var settings = Player.skin.settings;
	if(settings.showHostFlash) {
		var sz = Browser.elementSize(window);
		var hostflash = $$("nice-hostflash");
		var toc = $$("nice-toc");
		var tocheight = sz[1] - settings.tocMarginTop;
		
		switch(mode) {
			case "hide":
				size = 0;
				hostflash.hide();
				settings.showHostFlash = false;
				break;
			case "maximize":
				size = settings.hostFlashMaxHeight;
				break;
			case "minimize":
				size = settings.hostFlashMinHeight;
				break;
		}
		tocheight -= size;
		settings.hostFlashHeight = size;
		toc.style.height = tocheight + "px";
		
		if(this.isIE6) Player.main.height = tocheight;
	}
	
	return true;
	
}

// -----------------------------------------------------------------------------
// Hooks
// -----------------------------------------------------------------------------

Player.events.registerHook("pause", function() {
	
	if(Player.settings.pause) {
		Player.buttons.play.show();
		Player.buttons.pause.hide();
		Player.buttons.play.blink(500, 0);
	}
	
});

Player.events.registerHook("play", function() {
	
	if(Player.settings.pause) {
		Player.buttons.pause.show();
		Player.buttons.play.hide();
	}
	
});

// -----------------------------------------------------------------------------
// Load settings and user preferences
// -----------------------------------------------------------------------------

Player.events.registerHook("initialized", function() {
	
	var skin = Player.skin;
	if(Player.main.GetCourseSetting("toc") == "false") {
		skin.splitPane.toggleViewState();
		$$("nice-splitpane-bar").hide();
		$$("b-toggle-toc").hide();
	} else {
		var pref = skin.getUserPref("toc.width");
		skin.settings.tocInitialwidth =
			parseInt(Player.main.GetCourseSetting("tocInitialWidth"));
		
		// Split pane width ----------------------------------------------------
		skin.splitPane.setPaneWidth(pref || skin.settings.tocInitialWidth);
		
		if(Player.main.GetCourseSetting("tocInitialState") === "closed") {
			skin.splitPane.toggleViewState();
		}
	}

});

// -----------------------------------------------------------------------------
// Hacks
// -----------------------------------------------------------------------------

// Check for IE6 ---------------------------------------------------------------
Player.skin.isIE6 = false;

/*@if(@_win32)

var version = parseInt(/MSIE\s+(\d)/.exec(navigator.userAgent)[1]);
if(version < 7) Player.skin.isIE6 = true;

@end @*/

function _ie6cssHack() {
	// Ugly hack for IE6 CSS bug
	/*@if(@_win32)
	var version = parseInt(/MSIE\s+(\d)/.exec(navigator.userAgent)[1]);
	if(version < 7) {
		if(arguments[0] == true) Player.skin.splitPane._open = true;
		if(Player.skin.splitPane._open) {
			$$("b-toggle-toc").style.marginLeft = "0px";
		} else {
			$$("b-toggle-toc").style.marginLeft = "3px";
		}
	}
	Player.skin.splitPane._open = !Player.skin.splitPane._open;
	@end @*/
}
