/*
 * iDiver Core - Web framework
 * Copyright (c) 2004-2006 m3y
 * $Revision: 1.19 $
 * Modified: $Date: 2006/10/24 06:57:21 $
 *
 * File Authors:
 *		m3y <m3y@unlight.ru>
 */

function windowOpen(Url, Width, Height, Name, Php) {
	if (navigator.userAgent.indexOf('Opera') >= 0) {
		var ow = document.body.clientWidth, oh = document.body.clientHeight;
	} else {
		var ow = screen.width, oh = screen.height;
	}
	Width = Width || 640;
	Height = Height || 480;
	var left = (ow - Width)/2;
	var top = (oh - Height)/2;

	var feats = new Array("width=" + Width, "height=" + Height, "left=" + left, "top=" + top, "scrollbars=1", "status=1", "resizable=1");

	if (!Name) Name = Url.replace(/([\W]+)/g, '');
	if (!Php) Php = 'window.php';
	if (Url == '') {
		Url = 'about:blank';
	} else {
		var base = document.getElementsByTagName('base');
		Url = base[0].href+Php+Url;
	}

	window.open(Url,Name,feats.join(',')).focus();
}

function load() {
	if (window.name) {
		focusForm();
		addEvent(document.body, 'keyup', formKeyup); // IE & Opera
		if (!window.opera) addEvent(window, 'keyup', formKeyup); // Gecko
	} else {
		addEvent(document.body, 'keyup', keyup); // IE & Opera
		if (!window.opera) addEvent(window, 'keyup', keyup); // Gecko
	}
	if (window.controller) InitControls();
	if (window.ToolTip) {
		ToolTip.Init();
		addEvent(document.body, 'keydown', keydown); // IE & Opera
		addEvent(window, 'keydown', keydown); // Gecko & Opera
	}
}

function focusForm() {
	if (document.forms[0]) {
		for (i=0; i<document.forms[0].length; i++) {
			var item = document.forms[0].elements[i];
			if (item.type == 'text' && !item.disabled && item.focus) {
				item.focus();
				return;
			}
		}
	}
}

function keyup(e) {
	if (e.keyCode == 123 && e.ctrlKey) {
		login(); // Ctrl+F12
	} else if (e.keyCode == 113 && e.ctrlKey) { // Ctrl+F2
		base = document.getElementsByTagName('base')[0].href;
		if (window.location.href.indexOf(base+'proto') != -1) {
			window.location.href = window.location.href.replace(base+'proto/', base);
		} else {
			window.location.href = window.location.href.replace(base, base+'proto/');
		}
	}
	if (window.ToolTip && e.keyCode == 17) ToolTip.CtrlPressed = false;
}

function formKeyup(e) {
	if (e.keyCode == 27) formClose(); // ESC
}

function formClose() {
	if (!document.forms[0]) return;
	form = document.forms[0];
	form.modified = false;
	for (i=0; i<form.elements.length; i++) {
		switch (form.elements[i].type) {
		case 'radio':
		case 'select-one':
			break;
		default:
			form.modified = form.elements[i].value != form.elements[i].defaultValue;
		}
		if (form.modified) break;
	}
	if (!form.modified) {
		window.close();
	} else {
		if (window.confirm('Data has been modified. Submit data?')) form.submit();
		else window.close();
	}
}

function keydown(e) {
	ToolTip.CtrlPressed = e.keyCode == 17; // Ctrl
}

function login() {
	windowOpen('?page=login', 500, 440, 'login');
}

function addEvent(node, evtType, func) {
	if (node.addEventListener) {
		node.addEventListener(evtType, func, false);
		return true;
	} else if (node.attachEvent ) {
		return node.attachEvent("on" + evtType, func);
	} else {
		return false;
	}
}

function switchLang(langFrom, langTo) {
	var href = window.location.href;

	// switch one to another
	re = new RegExp('/'+langFrom+'/'); // only one replace
	href = window.location.href.replace(re, '/'+langTo+'/');

	// no replacement occured
	if (href == window.location.href) {
		// switch from default language
		var base = document.getElementsByTagName('base');
		href = window.location.href.replace(base[0].href, base[0].href+langTo+'/');
	}

	if (href == window.location.href) return true;

	window.location.href = href;
	return false;
}

function createFlash(Id, Url, Width, Height, Img, ImgW, ImgH, Alt) {
	obj = document.getElementById(Id);
	if (!obj || !Url || !Width || !Height) return;

	if (Img) {
		ImgW = ImgW || Width;
		ImgH = ImgH || Height;
		Alt = Alt || '';
		imgtag = '<img src="' + Img + '" width="' + ImgW + '" height="' + ImgH + '" alt="' + Alt + '" />';
	} else {
		imgtag = '';
	}

	obj.innerHTML ='<object type="application/x-shockwave-flash" data="' + Url + '" width="' + Width + '" height="' + Height + '">' +
		'<param name="movie" value="' + Url + '" />' + imgtag +
		'</object>';
}

addEvent(window, 'load', load);
