// charset=utf-8
// $Id: mcm.js 3070 2009-06-30 14:34:30Z hebing $
// +----------------------------------------------------------------------+
// | mcm                                                                  |
// | version 5.5                                                          |
// | (c) 2002-2009 monsun media (http://www.monsun-media.com)             |
// +----------------------------------------------------------------------+


/**
* mcm
*
* @author	dierker
*/
var mcm = {

	/**
	* open the homepage in the current window
	*
	* @param	Event	evt
	* @return	void
	*/
	openHomepage : function(evt){
		mcm.cancelEvent(evt);
		window.location = './';
	}
	,
	/**
	* open the history
	*
	* @param	Event	evt
	* @param	Object	optionsObj
	* @return	void
	*/
	openHistory : function(evt,optionsObj){
		if( typeof optionsObj!='object' ){;
			optionsObj = {};
		};
		var width = 400;
		var height = 400;
		if( optionsObj.width ){
			width = optionsObj.width;
		};
		if( optionsObj.height ){
			height = optionsObj.height;
		};
		var winOptions = 'width='+width+',height='+height+',resizable=yes,status=yes,scrollbars=yes';
		mcm.whHistory = window.open('mcm/history.php','mcmHistory',winOptions);
		if( mcm.whHistory.focus ){
			mcm.whHistory.focus();
		};
		mcm.cancelEvent(evt);
	}
	,
	/**
	* open the sitemap
	*
	* @param	Event	evt
	* @param	Object	optionsObj
	* @return	void
	*/
	openSitemap : function(evt,optionsObj){
		if( typeof optionsObj!='object' ){;
			optionsObj = {};
		};
		var width = 400;
		var height = 400;
		if( optionsObj.height ){
			height = optionsObj.height;
		}
		if( optionsObj.width ){
			width = optionsObj.width;
		};
		var winOptions = 'width='+width+',height='+height+',resizable=yes,status=yes,scrollbars=yes';
		
		mcm.whSitemap = window.open('mcm/sitemap.php','mcmSitemap',winOptions);
		if( mcm.whSitemap.focus ){
			mcm.whSitemap.focus();
		};
		mcm.cancelEvent(evt);
	}
	,
	/**
	* print the current page
	*
	* @param	Event	evt
	* @return	void
	*/
	openPrintversion : function(evt){
		window.print();
		mcm.cancelEvent(evt);
	}
	,
	/**
	* open the recomment-page-form
	*
	* @param	Event	evt
	* @param	Object	optionsObj
	* @return	void
	*/
	openRecommendPage: function(evt,optionsObj){
		if( typeof optionsObj!='object' ){;
			optionsObj = {};
		};
		var width = 500;
		var height = 460;
		var url = window.location.href;

		if( optionsObj.width ){
			width = optionsObj.width;
		};
		if( optionsObj.height ){
			height = optionsObj.height;
		};
		var winUrl = 'mcm/recommend.php?url='+escape(url);
		var winOptions = 'width='+width+',height='+height+',resizable=yes,status=yes,scrollbars=yes';
		mcm.whSitemap = window.open(winUrl,'mcmRecommend',winOptions);
		if( mcm.whSitemap.focus ){
			mcm.whSitemap.focus();
		};
		mcm.cancelEvent(evt);
	}
	,
	/**
	* open the site-search
	*
	* @param	Event	evt
	* @param	string	keyword
	* @param	Object	optionsObj
	* @return	void
	*/
	openSiteSearch : function(evt,keyword,optionsObj){
		if( typeof optionsObj!='object' ){;
			optionsObj = {};
		};
		var width = 400;
		var height = 400;
		if( optionsObj.height ){
			height = optionsObj.height;
		}
		if( optionsObj.width ){
			width = optionsObj.width;
		};
		var winUrl		= 'mcm/search.php?popupopened=1';
		var winOptions	= 'width='+width+',height='+height+',resizable=yes,status=yes,scrollbars=yes';
		if( keyword!=undefined && keyword!=null ){
			winUrl += '&keyword='+keyword;
		};
		mcm.whSearch = window.open(winUrl,'mcmSiteSearch',winOptions);
		if( mcm.whSearch.focus ){
			mcm.whSearch.focus();
		}
		mcm.cancelEvent(evt);
	}
	,
	/**
	* open a gallery
	*
	* <code>
	* var gallery_id = 1;
	* var optionsObj = { width:400, height:400, offset:1 };
	* var wh = mcm.openGallery(event,gallery_id,optionsObj);
	* </code>
	*
	* @param	Event	evt
	* @param	int		gallery_id		id of the gallery
	* @param	Object	optionsObj
	* @return	void
	*/
	openGallery : function(evt,gallery_id,optionsObj){
		if( typeof optionsObj!='object' ){;
			optionsObj = {};
		};
		var offset = 1;
		var width = 650;
		var height = 500;
		if( optionsObj.offset ){
			offset = optionsObj.offset;
		};
		if( optionsObj.width ){
			width = optionsObj.width;
		};
		if( optionsObj.height ){
			height = optionsObj.height;
		};
		var url = 'mcm/gallery/'+gallery_id+'/'+offset+'.html';
		mcm.whGallery = window.open(url,'mcmGallery','width='+width+',height='+height+',status=yes,menubar=yes,location=yes,resizable=yes');
		if( mcm.whGallery.focus ){
			mcm.whGallery.focus();
		};
		mcm.cancelEvent(evt);
	}
	,
	/**
	* open an image in a popup window
	*
	* @param	Event	evt
	* @param	object	optionsObj	params as JSON
	* @return	void
	*/
	openImage : function(evt,optionsObj){
		if( typeof optionsObj!='object' ){;
			optionsObj = {};
		};
		var winUrl = 'mcm/image.php';
		if( optionsObj.image_id ){
			winUrl += '?image_id='+optionsObj.image_id;
		};
		mcm.whImage = window.open(winUrl,'mcmImage','width=800,height=600');
		if( mcm.whImage.focus ){
			mcm.whImage.focus();
		}
		mcm.cancelEvent(evt);
	}
	,
	/**
	* open a lightbox
	*
	* @param	Event	evt
	* @param	string	domId
	* @return	void
	*/
	openLightbox : function(evt,domId){
		mcm.cancelEvent(evt);
		var elem = document.getElementById(domId);
		if( elem===null ){
			return;
		};
		if( !myLightbox ){
			return;
		};
		if( !myLightbox.start ){
			return;
		};
		myLightbox.start(elem);
	}
	,
	/**
	* open the contact-form
	*
	* @param	Event	evt
	* @param	object	paramsObj	params as JSON
	* @return	void
	*/
	openContactform : function(evt,paramsObj){
		var width = 400;
		var height = 500;
		
		var winUrl		= 'mcm/contact.php';
		var winOptions	= 'width='+width+',height='+height+',resizable=yes,status=yes,scrollbars=yes';
		
		if( paramsObj.employee_id ){
			winUrl += '?employee_id='+paramsObj.employee_id;
		} else if( paramsObj.email ){
			// encoded	: paramsObj.email == '##moc.rab@oof##'
			// plain	: paramsObj.email == 'foo@bar.com'
			var email = mcm.decodeEmail(paramsObj.email);
			winUrl += '?email='+email;
		}	
		mcm.whContactform = window.open(winUrl,'mcmContactform',winOptions);
		if( mcm.whContactform.focus ){
			mcm.whContactform.focus();
		};
		mcm.cancelEvent(evt);
	}
	,
	/**
	* decode an email (if it's really encoded, otherwise return the original string)
	*
	* @param	string	str
	* @return	string
	*/
	decodeEmail : function(str){
		var regEx = /^\#\#(.*)\#\#$/;
		if( regEx.exec(str)==null ){
			return str;
		};
		str = str.replace(/^\#\#(.*)\#\#$/,'$1');

		// reverse the string
		var newStr='';
		var i;
		for( i=str.length-1; i>=0; i-- ){
			newStr += str[i];
		};
		return newStr;
	}
	,
	/**
	* open a vcard for an employee
	*
	* @param	Event	evt
	* @param	int		employee_id
	* @return	void
	*/
	openVCard : function(evt,employee_id){
		var url = 'mcm/vcf/'+employee_id+'.vcf';
		window.location = url;
		mcm.cancelEvent(evt);
	}
	,
	/**
	* cancel the default event-handler
	*
	* @param	Event	evt
	* @return	void
	*/
	cancelEvent : function(evt){
		evt = evt ? evt : window.event ? window.event : null;
		if( evt==null ){
			return;
		};
		if( evt.stopPropagation && evt.preventDefault ){
			evt.stopPropagation();
			evt.preventDefault();
		}else{
			evt.cancelBubble = false;
			evt.returnValue = false;
		};
	}
	,
	/**
	* get X Koordinate
	*
	* @param	Event	evt
	* @return	int
	*/
	getPageX : function(evt){
		var x = evt.pageX ? evt.pageX : evt.clientX;
		return x;
	}
	,
	/**
	* get Y Koordinate
	*
	* @param	Event	evt
	* @return	int
	*/
	getPageY : function(evt){
		var y = evt.pageY ? evt.pageY : (parseInt(window.document.documentElement.scrollTop)+evt.clientY);
		return y;
	}
	,
	/**
	* open a popup
	*
	* @param	Event	evt
	* @param	string	winUrl
	* @param	string	winName		name of the window
	* @param	string	winOpts		options-string for window.open
	* @return	bool
	*/
	openPopup : function(evt,winUrl,winName,winOpts){
		var wh = window.open(winUrl,winName,winOpts);
		if( !wh ){
			return false;
		};
		if( wh.focus ){
			wh.focus();
		}
		mcm.cancelEvent(evt);
		return true;
	}
	,
	/**
	* create a XMLHttpRequest object
	* 
	* @return	object	XMLHttpRequest
	*/
	createXmlHttpRequest : function(){
		var httpReq=false;
		// initialize the xmlHttpRequest object
		try {
			httpReq = new ActiveXObject('Msxml2.XMLHTTP');
		}catch(e){
			try{
				httpReq = new ActiveXObject('Microsoft.XMLHTTP');
			}catch(oc){
				httpReq = null;
			};
		}
		if( !httpReq && typeof XMLHttpRequest!='undefined' ){
			httpReq = new XMLHttpRequest();
		};
		return httpReq;
	}
	,
	/**
	* add a CSS-class declaration to an element
	*
	* @param	object		el			reference to a node element
	* @param	string		className	name of a css class
	* @return	void
	*/
	addCssClass : function(el,className){
		var classAry = el.className.split(' ');
		var newClassAry = new Array();
		for( var i=0; i<classAry.length; i++ ){
			newClassAry[newClassAry.length] = classAry[i];
		}
		newClassAry[newClassAry.length] = className;
		el.className = newClassAry.join(' ');
	}
	,
	/**
	* remove an assigned CSS-class from an element
	*
	* @param	object		el			reference to a node element
	* @param	string		className	name of a css class
	* @return	void
	*/
	removeCssClass : function(el,className){
		var classAry = el.className.split(' ');
		var newClassName = '';
		for( var i in classAry) {
			if( classAry[i]==className ){
				continue;
			};
			newClassName += classAry[i] + ' ';
		};
		el.className = newClassName;
	}
	,
	/**
	* return true, when the className is assign to the element
	*
	* @param	object	el
	* @param	string	className
	* @return	void
	*/
	hasCssClass : function(el,className){
		var classAry = el.className.split(' ');
		var newClassName = '';
		for( var i in classAry) {
			if( classAry[i]==className ){
				return true;
			};
		}
		return false;
	}
};
