﻿////////////////////////////////////////////////////////////////////////
// spv v2.0.3.255
// 2007-01-05 10:42:26
// SiC
////////////////////////////////////////////////////////////////////////
var spv = {};


//**********************************************************
// Global Functions for Options Page
//**********************************************************

//----------------------------------------------------------
// Initialization (onload)
//----------------------------------------------------------
spv.init = function(isReload){

	// Initialize Config Cache
	spvConfig.init();

	// Replace page title
	document.title = $lang("china_spv");

	if(!isReload){

		// Read Last Opened Section ID from INI
		var targetPage = $cookies('get', 'activePage');

		// Check if page is set in url
		var params = $parseQueryString(String(document.location));
		if(params["page"]){
			if(spv.pages[params["page"]]){
				targetPage = params["page"];
			}
		}

		if(spv.pages[targetPage]) spv.activePage = targetPage;

		// Output Page Links
		spv.ui.buildPageLinks();

	}

	// Activate target page
	spv.action.activatePage(spv.activePage);

}


//----------------------------------------------------------
// Destroy Objects - onunload
//----------------------------------------------------------
spv.destroy = function (){

	spvConfig.cancel();

}


//**********************************************************
// Operation Functions
//**********************************************************
spv.action = {};

//----------------------------------------------------------
// Show Specific Page by ID
//----------------------------------------------------------
spv.action.activatePage = function(targetPage){

	// Change Active Page Link
	var obj = document.getElementById("pageLink_" + spv.activePage);
	if(obj){
		obj.className = '';
	}
	obj = document.getElementById("pageLink_" + targetPage);
	if(obj){
		obj.className = 'active';
	}

	// Build Page
	spv.ui.setPageHeader(targetPage);
	spv.ui.buildPage(targetPage);

	// Destroy Page - if defined destroy() method
	try{
		eval("spv.pages." + spv.activePage + ".destroy()");
	}catch(e){
		if(!((e.number & 0xFFFF) == 438)){
			alert(
				"spv.pages." + targetPage + ".destroy()\n" +
				(e.number & 0xFFFF) + " : " + e.message
			);
			throw(e);
		}
	}

	// Initialize Page - if defined init() method
	try{
		eval("spv.pages." + targetPage + ".init()");
	}catch(e){
		if(!((e.number & 0xFFFF) == 438)){
			alert(
				"spv.pages." + targetPage + ".init()\n" +
				(e.number & 0xFFFF) + " : " + e.message
			);
			throw(e);
		}
	}

	// Save active page ID
	spv.activePage = targetPage;
	$cookies('set', 'activePage', targetPage);

}


//----------------------------------------------------------
// Apply Changes
//----------------------------------------------------------
spv.action.applyChanges = function (){

	// Save
	spvConfig.save();
	external.max_Invoke("FlushConfig", "form");

	// Init
	spvConfig.init();

	// UI Message
	spv.ui.showMessage($lang("config_saved"), 3000);

}


//----------------------------------------------------------
// Reset Changes
//----------------------------------------------------------
spv.action.resetChanges = function (){

	if(spvConfig.modified && confirm("[" + $lang("china_spv") + "]\n\n" + $lang("config_reset_confirm"))){

		spvConfig.cancel();
		spvConfig.init();

		spv.action.activatePage(spv.activePage);
		spv.ui.showMessage($lang("config_reset"), 3000);

	}

}


//----------------------------------------------------------
// Reset Changes
//----------------------------------------------------------
spv.action.restoreDefault = function (){

	if(confirm("[" + $lang("china_spv") + "]\n\n" + $lang("confirm_restore_default"))){
		external.max_Invoke("DefaultConfig","All");
		spv.init(true);

		spv.ui.showMessage($lang("config_reset"), 3000);
	}

}


//----------------------------------------------------------
// Close Config Page
//----------------------------------------------------------
spv.action.close = function (isUnload){

	// Check if not saved and ask user for action
	if(spv.modified && confirm("[" + $lang("china_spv") + "]\n\n" + $lang("config_not_saved_confirm"))){
		spv.action.applyChanges();
	}else{
		spvConfig.modified = false;
	}

	spv.destroy();

	if(!isUnload) window.close();

}


//**********************************************************
// UI Control Functions
//**********************************************************
spv.ui = {};


//----------------------------------------------------------
// Show Message in Message Box
//----------------------------------------------------------
spv.ui.showMessage = function (strMessage, timeOut){

	var obj = document.getElementById("msgBox");

	if(obj){

		obj.innerHTML = strMessage;
		obj.style.display = "block";

		window.setTimeout(spv.ui.clearMessage, timeOut);

	}

}


//----------------------------------------------------------
// Hide Message in Message Box
//----------------------------------------------------------
spv.ui.clearMessage = function (){

	var obj = document.getElementById("msgBox");

	if(obj){

		obj.innerHTML = "";
		obj.style.display = "none";

	}

}


//----------------------------------------------------------
// Toggle Sidebar
//----------------------------------------------------------
spv.ui.toggleSidebar = function(){

	var obj = document.getElementById("sidebarWrapper");
	var objIMG = document.getElementById("btnSidebar");

	if($toggleElement("sidebar")){
		obj.style.width = '';
		objIMG.src = "images/btn_hide_sidebar.png";
	}else{
		obj.style.width = '8';
		objIMG.src = "images/btn_show_sidebar.png";
	}

}


//----------------------------------------------------------
// Set Page Header
//----------------------------------------------------------
spv.ui.setPageHeader = function(pageID){

	var output =
		$lang("page_" + pageID);

	$write(output, 'pageHeader', false);

}


//----------------------------------------------------------
// Build Page Links
//----------------------------------------------------------
spv.ui.buildPageLinks = function (){

	var output = "";

	for(label in spv.pages){
		if(spv.pages[label].type == 'separator'){
			output += "<hr/>";
		}else{
			output +=
				'<a id="pageLink_' + label + '" class="pageLink" href="javascript:void(0)" ' +
				'onclick="this.blur();spv.action.activatePage(\'' + label + '\');">' +
				'<img src="images/page_' + label + '.png"/> '+
				$lang("page_" + label) +
				'</a>\n';
		}
	}

	$write(output, 'pageLinks', false);

}


//----------------------------------------------------------
// Build PageContent
//----------------------------------------------------------
spv.ui.buildPage = function(targetPage){

	var obj = document.getElementById("pageContent");
	if(!obj){
		alert("spv.ui.buildPage :: missing 'pageContent' object");
	}

	var output = spvConfig.ui.buildItems(spv.pages[targetPage].items);

	output += '<br/><br/><br/><br/><br/><br/>';

	obj.innerHTML = output;

}