/**
 * myreisen.de CMS und PORTAL
 *
 * @category   RdeCMS
 * @package    Rde
 * @copyright  Copyright (c) 2006 bison GmbH (http://www.bison-soft.de)
 * @author     bison GmbH <info@bison-soft.de>
 * @version    $Id: cmsapp.js,v 1.10 2009/01/13 21:51:56 wirth Exp $
 * @license    commercial
 */

/**
 * CmsApp Javascipt Funktionen
 *
 * @category   RdeCMS
 * @package    Rde
 * @subpackage Rde_Module_JScript
 */

/**
 * Das globale RdeCms Namespace-Objekt
 * @class RdeCms
 * @static
 */
if (typeof RdeCms == 'undefined' || RdeCms == null) {
    var RdeCms = {};
}

/**
 * Gibt RdeCms Namespace zurueck und erzeugt ihn ggf.
 *
 * RdeCms.namespace('RdeCms.property.package','property.package',...);
 *
 * Vorsicht: keine reservierten JScript-Woerter verwenden!
 *
 * @method namespace
 * @static
 * @param  {String*} 1-n anzulegende Namespaces
 * @return {Object}  letzter angelegter Namespace
 */
RdeCms.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; ++i) {
        d=a[i].split('.');
        o=RdeCms;

        // RdeCms prefix ignorieren
        for (j=(d[0] == 'RdeCms') ? 1 : 0; j<d.length; ++j) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};

RdeCms.namespace('util','menumulti','cmspopup','website');

/*
 * Util
 */
RdeCms.util.trim = function(str) {
   return str.replace(/^\s*|\s*$/g,'');
}

RdeCms.util.getElementsByAttr = function(elem, tagname, attrname, attrval) {
	var lst = (tagname == '*' && elem.all ? elem.all : elem.getElementsByTagName(tagname));
	var ret = new Array();
	var re = (typeof attrval != 'undefined')? new RegExp('(^|\s)' + attrval + '(\s|$)') : null;
	var attr;
	for (var i=0; i<lst.length; i++) {
		attr = lst[i].getAttribute && lst[i].getAttribute(attrname);
		if (typeof attr == 'string' && attr.length > 0)
			if (typeof attrval == 'undefined' || (re && re.test(attr)))
				ret[ret.length] = lst[i];
	}
	return ret;
}

RdeCms.util.getElementValue = function(elem) {
	if (elem && typeof elem != 'undefined')
		return (elem.selectedIndex != 'undefined' && elem.selectedIndex != null ? elem[elem.selectedIndex].value : elem.value);
	return '';
}

RdeCms.util.addLoadEvent = function(func) {
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
	} else { 
		window.onload = function() { 
			if (oldonload) 
				oldonload(); 
			func(); 
		} 
	} 
}

/*
 * MenuMulti-Form
 */
RdeCms.menumulti.Go = function(e) {
	if (e && e.form && (typeof e.form != 'undefined')) {
		var target = e.form.action, ele = e.form.elements,
			u = RdeCms.util, i = 0,
			pl = u.trim(u.getElementValue(ele['plist'])).split('|'),
			kl = '|' + u.trim(u.getElementValue(ele['klist'])) + '|',
			de = u.trim(u.getElementValue(ele['docext']));

		if (!target.length || !pl.length || !de.length)
			return;

		while(i < pl.length) {
			target += ',' + pl[i] + ',' + u.getElementValue(ele[pl[i]]);
			if (pl[i] == e.name)
				break;
			i++;
		}
		while((i+1) < pl.length && kl.indexOf('|' + pl[i++] + '|') >= 0)
			target += ',' + pl[i] + ',' + u.getElementValue(ele[pl[i]]);
		window.location.href = target + de;
	}
}

/*
 * CmsPopup
 */
RdeCms.cmspopup.ComElement = null;

RdeCms.cmspopup.Open = function(url,name,ww,wh) {
	ww = (typeof ww == 'undefined' || ww == null ? 250 : (ww > screen.width ? screen.width : ww));
	wh = (typeof wh == 'undefined' || wh == null ? 150 : (wh > screen.width ? screen.width : wh));
	name = (typeof name == 'undefined' || name == null ? 1 : name);

	var wp = 'resizable=no,modal=yes,scrollbars=yes,dependent=yes,dialog=yes,minimizable=no,left='+(screen.width-ww)/2 +
			 ',top='+(screen.height-wh)/2+',width='+ww+',height='+wh;
	var win=window.open(url,name,wp);

	if(win != null) {
		if (typeof win.opener == 'undefined' || win.opener == null)
      		win.opener = self;
      	RdeCms.cmspopup.AutoClose(win);
		win.focus();
	}
	return win;
};

RdeCms.cmspopup.AutoClose = function(win) {
	window.onunload = function() {
	    try {
    		win.close();
		} catch(ex) {};
    };
};

RdeCms.cmspopup.PrepareUrl = function(form,enames_param,ename_url) {
	var found_url = eval(form[ename_url]);
	var cele = enames_param.replace(/ /g,'').split(',');
	var first = true;
	var fterm;

	if (found_url) {
		found_url.value = found_url.value.replace(/\?.*/g,'');
		if (cele.length) {
			for (i = 0; i < cele.length; i++) {
				tmp = cele[i].split(':');
				e_name = tmp[0];
				tmp = eval(form[tmp.length > 1 ? tmp[1] : e_name]);
				if (tmp && typeof tmp != 'undefined') {
					fterm = (first ? '?' : '&');
					found_url.value = found_url.value + fterm + e_name +
									  '=' + tmp.value;
					first = false;
				}
			}
		}
		return ename_url;
	}
	return '';
};

RdeCms.cmspopup.Req = function(form,enames_req,ename_url,name,ww,wh) {
	var found_url = eval(form[ename_url]);
	var cele = enames_req.replace(/ /g,'').split(',');

	if (cele.length) {
		RdeCms.cmspopup.ComElement = {};
		RdeCms.cmspopup.ComElement.length = 0;
		for (i = 0; i < cele.length; i++) {
			tmp = cele[i].split(':');
			e_name = tmp[0];
			tmp = eval(form[tmp.length > 1 ? tmp[1] : e_name]);
			if (tmp && typeof tmp != 'undefined') {
				RdeCms.cmspopup.ComElement[e_name] = tmp;
				RdeCms.cmspopup.ComElement.length++;
			}
		}
	}

	if (found_url && RdeCms.cmspopup.ComElement.length)
	    return RdeCms.cmspopup.Open(found_url.value,name,ww,wh);
	return false;
};

RdeCms.cmspopup.Res = function(form,enames_res) {
    var parent = window.opener;

    if (typeof parent != 'undefined' && parent != null &&
        (parent.RdeCms.cmspopup.ComElement &&
         parent.RdeCms.cmspopup.ComElement.length &&
         typeof parent.RdeCms.cmspopup.ComElement.length !=  'undefined')) {
		var cele = enames_res.replace(/ /g,'').split(',');
		var tmp;
		if (cele.length) {
			for (i = 0; i < cele.length; i++) {
				tmp = cele[i].split(':');
				e_name = tmp[0];
				tmp = eval(form[tmp.length > 1 ? tmp[1] : e_name]);
				if (tmp && typeof tmp != 'undefined' && parent.RdeCms.cmspopup.ComElement[e_name]) {
					parent.RdeCms.cmspopup.ComElement[e_name].value = tmp.value;
					if (parent.RdeCms.cmspopup.ComElement[e_name].onchange)
						parent.RdeCms.cmspopup.ComElement[e_name].onchange();
				}
			}
		}
	}
    RdeCms.cmspopup.Close();
};

RdeCms.cmspopup.Resize = function(ele) {
	if (ele == null || typeof ele == 'undefined')
		return;

	var ww = (ele.offsetWidth > screen.width ? screen.width : ele.offsetWidth);
	var wh = (ele.offsetHeight > screen.height ? screen.height : ele.offsetHeight);

	try {
		window.moveTo((screen.width-ww)/2,(screen.height-wh)/2);
	} catch (e) {}

	if (window.innerWidth && typeof window.innerWidth != 'undefined') { // Gecko
		ww += (window.outerWidth > window.innerWidth  ? (window.outerWidth - window.innerWidth) : 0);
		wh += (window.outerHeight > window.innerHeight  ? (window.outerHeight - window.innerHeight) : 0);
		try {
			window.resizeTo(ww,wh);
		} catch (e) {}
	} else { // IE
		ele = document.documentElement;
		if (!(ele && typeof ele != 'undefined' && ele.clientWidth && typeof ele.clientWidth != 'undefined'))
			ele = document.body;
		try {
			window.resizeBy((ww - ele.clientWidth),(wh - ele.clientHeight));
		} catch (e) {}
	}

};

RdeCms.cmspopup.Init = function(container_element_id) {
	if (container_element_id && typeof container_element_id != 'undefined')
		RdeCms.cmspopup.Resize(document.getElementById(container_element_id));
	window.focus();
};

RdeCms.cmspopup.Close = function() {
	try {
		if (typeof window != 'undefined' && window != null)
			window.close();
	} catch(ex) {}; //IE u.U. 'access denied'
};

