/**
 * small work around x-platform 
 * @param {} x
 */
var debug=function(x) {
	if(window.console) {
		window.console.log(x);
	} else if(console.log) {
		console.log(x);
	} else {
		alert(x);
	}
}
/**
 * some meta data
 */
bsmo = {
	Version :'0.2',
	CodeName:'bsmo'
};
/**
 * some options
 */
bsmo.options = {
	color: {
		ok:'#6b8e23',
		error:'#b22222'
	}
};
/**
 * @author eike.reifhardt
 * @class bsmo
 * @static 
 */
bsmo.tool = {
	/**
	 * 
	 * @param {} value
	 * @param {} min
	 * @param {} max
	 * @return {}
	 */
	isBetween: function(value,min,max) {
		_min 		= parseInt(min);
		_max 		= parseInt(max);
		_varNum 	= parseInt(value);
		
		return ((_min <= _varNum) && (_max >= _varNum)) ? true : false;
	},
	/**
	 * 
	 * @param {} e
	 * @return {Boolean}
	 */
	wiretapLogin: function(e) {
	
		var _errorcolor=bsmo.options.color.error;
		var _okcolor=bsmo.options.color.ok;
		
		inp_user = e.username.getValue();
		inp_pass = e.password.getValue();
		
		_user_ok = false;
		_pass_ok = false;
		
		if(inp_user=="") {
			e.username.highlight({startcolor:_errorcolor,endcolor:e.username.style.background,afterFinish:markValid(e.username,_errorcolor)}); 
		} else {
			e.username.highlight({startcolor:_okcolor,endcolor:e.username.style.background,afterFinish:markValid(e.username,_okcolor)});
			_user_ok = true;
		}
		if(inp_pass=="") {
			e.password.highlight({startcolor:_errorcolor,endcolor:e.password.style.background,afterFinish:markValid(e.password,_errorcolor)});
		} else {
			e.password.highlight({startcolor:_okcolor,endcolor:e.password.style.background,afterFinish:markValid(e.password,_okcolor)});
			_pass_ok = true;
		}
		
		if(_user_ok && _pass_ok) {
			e.serialize(true);
			console.log(e);
			e.submit();
		}
		
		return false;
	}
};

/**
 * 
 */
bsmo.navigation = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("navigation-list");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") { // must be UPPERCASE, because of IE
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
};
/**
 * if the RenderEngine is Webkit - use the safari.css
 * @see http://blog.aint-no-soul.com/css/2008-11-05/simple-method-to-add-a-safari-css/
 */
bsmo.webKit = function() {
	if(Prototype.Browser.WebKit) {
		$('safari').media="all";
	}
};

/**
 * @author eike.reifhardt
 * @class bsmo
 */
bsmo.collapse = Class.create(bsmo, {
	params: 
	{
		'css_basename'	: 'collapse',
		'css_wrapper'	: 'collapse-container'
	},
	
	_store: [],
	_itemsCount: 0,
	
	/**
	 * registers events for appropriated elements
	 * 
	 * @constructor
	 */
	initialize: function() {
		
		$$('.'+this.params.css_basename).each(function(e) {
			Event.observe(e, 'click', this.bend.bindAsEventListener(this));
			this._store[this._itemsCount]={
				name : this.getTarget(e.down().href),
				height : 0
			};
			this._itemsCount++;
		}.bind(this));
	},
	
	/**
	 * neat method to get the target
	 * or better said a work around for the IE and it's lack in the target specs
	 * @param {} string
	 * @return {}
	 */
	getTarget: function(string) {
		res = string.match(/((:?#collapse\-)([a-z-A-Z0-9\_\-]+))/).last();
		return (typeof res == 'string') ? res : false;
	},

	/**
	 * does the whole magic. Bend lower! hihi ;)
	 * @param {} x
	 * @return {Boolean}
	 */
	bend: function(x) {
		var tmpTarget = x.target;
		if(tmpTarget.tagName=="SPAN") // enables the icon to trigger the bend method
			 tmpTarget = tmpTarget.previousSibling;
		var target = this.getTarget(tmpTarget.href); // quotes, etc
		var targetObject = $(target);
		var container = targetObject.down('.collapse');
		
		if(typeof target == 'string') {
			hasFlash = (typeof targetObject.down('#cognitiveSeduction') == 'function') ? true : false;
			
			if(targetObject.getStyle('overflow') != 'hidden') {
				for(i=0; this._itemsCount>i; i++) {
					if(this._store[i].name == target) {
						this._store[i].height = targetObject.getHeight();
					}
				}
				
				targetObject.setStyle({'overflow':'hidden'});
				container.addClassName("cover");
				
				if(hasFlash) {
					$('cognitiveSeduction').fade();
				}
				
				targetObject.morph('height:29px', 
					{
					duration:0.2,
					transition: Effect.Transitions.sinoidal
					}
				);
				
				
			} else {

				for(i=0; this._itemsCount>i; i++) {
					if(this._store[i].name == target) {
						currentTarget = this._store[i];
					}
					
				}
				
				if(hasFlash) $('cognitiveSeduction').appear();
				container.removeClassName("cover");
				
				targetObject.morph('height:'+currentTarget.height+'px', 
					{
					duration:0.3,
					transition: Effect.Transitions.spring,
					afterFinish: (targetObject.setStyle({'overflow':''}))
					}
				);
				
			}
			//debug(this);
			return false;

		}
	}
	
});

/**
 * changes the body class name, depending on screen width 
 * and also the flash content, if available for that size.
 * 
 * @todo add functionality of params._sizes.{} 
 * @author eike.reifhardt
 * @class bsmo
 * @version 0.2
 */
bsmo.bodyswitch = Class.create(bsmo, {

	/**
	 * modify this to your needs.
	 * @todo make all params rewritable
	 */
	params: 
	{
		'_width'			: '0',
		'_flashContainer' 	: 'cognitiveSeduction',
		'_sizes'			: {
			'small' 			: '1',
			'medium' 			: '2',
			'large' 			: '3'
		}
	},
	
	/**
	 * @constructor
	 */
	initialize: function() {
		this.params._width = document.viewport.getWidth();
		this.eventResize = this.setBodyStyle.bindAsEventListener(this);
		this._registerEvent();
		this.setBodyStyle();
	},

	/**
	 * registers all events (actually only one)
	 * @private 
	 * @return none
	 */
	_registerEvent:function() {
		Event.observe(window, 'resize', this.eventResize);
		// window.onunload=this._destroyEvent();
	},
	
	/**
	 * destroys all events
	 * @private 
	 * @return none
	 */
	_destroyEvent:function() {
		Event.stopObserving(window, 'resize', this.eventResize);
	},
	
	/**
	 * Detect window width and set approriate className 
	 * @return none
	 */
	determineBodyWidth: function() {
		this.params._width = document.viewport.getWidth();
	},
	
	/**
	 * as the method name states, it does what it does ;)
	 * @return none
	 */
	setBodyStyle: function() {
		this.determineBodyWidth();
		if(this.params._width>1320) {
			$$('body').first().className="seven-fifths";
			if(this.options.flashFile != undefined) swfobject.embedSWF(this.options.flashFile, this.params._flashContainer, '1320', '111', '9.0.0', contextPath+'misc/js/expressInstall.swf', flashvars, params, attributes);
		}			
		if(bsmo.tool.isBetween(this.params._width,1140,1320)) {
			$$('body').first().className="six-fifths";
			if(this.options.flashFile != undefined) swfobject.embedSWF(this.options.flashFile, this.params._flashContainer, '1140', '111', '9.0.0', contextPath+'misc/js/expressInstall.swf', flashvars, params, attributes);
		}
		if(bsmo.tool.isBetween(this.params._width,950,1140)) {
			$$('body').first().className="five-fifths";
			// set flash
			if(this.options.flashFile != undefined) swfobject.embedSWF(this.options.flashFile, this.params._flashContainer, '950', '111', '9.0.0', contextPath+'misc/js/expressInstall.swf', flashvars, params, attributes);
		}
		if(this.params._width<950) {
			$$('body').first().className="four-fifths";
			// set flash
			if(this.options.flashFile != undefined) swfobject.embedSWF(this.options.flashFile, this.params._flashContainer, '750', '111', '9.0.0', contextPath+'misc/js/expressInstall.swf', flashvars, params, attributes);
		}
	}

});


/**
 * autogrow textareas //inspired by jQuery plugin autogrow
 * @author thomas.noack
 * @class bsmo
 * @version 0.2
 */
bsmo.autogrow = Class.create(bsmo, {

	/**
	 * modify this to your needs.
	 * @todo make all params rewritable
	 */
	params: 
	{
		'_minHeight'	: 133,
		'_maxHeight'	: 0,
		'_lineHeight'	: 19,
		'_increments'	: 0
	},
	
	/**
	 * @constructor
	 */
	initialize: function(e) {
		this.dummy 				= null;
		this.textarea 			= e;
		
		//if(Prototype.Browser.WebKit) return false;
		
		var self = this;			
		this.textarea.setStyle({overflow: 'hidden', display: 'block'});
		this.textarea.observe('focus', function() { self.startExpand() } ).observe('blur', function() { self.stopExpand() });
		Event.observe(window, 'resize', function() { self.checkExpand() });
		this.checkExpand();
		
	},
	
	stopExpand: function() {
		clearInterval(this.interval);	
	},

	startExpand: function() {				
	  var self = this;
		this.interval = window.setInterval(function() {self.checkExpand()}, 400);
	},

	checkExpand: function() {
		if (this.dummy == null)
		{
			this.dummy = new Element('div').setStyle({
											fontSize  : this.textarea.getStyle('font-size'),
											fontFamily: this.textarea.getStyle('font-family'),
											width     : this.textarea.getStyle('width'),
											paddingTop   : this.textarea.getStyle('padding-top'),
											paddingBottom   : this.textarea.getStyle('padding-bottom'),
											paddingLeft   : this.textarea.getStyle('padding-left'),
											paddingRight   : this.textarea.getStyle('padding-right'),
											lineHeight: this.params._lineHeight+'px',
											overflowX : 'hidden',
											position  : 'absolute',
											top       : 0,
											left 	  : '-9999px'
											});
			$(document.body).appendChild(this.dummy);
		}
		else
		{
			// Always update the textarea's width due to body switcher
			this.dummy.setStyle({width: this.textarea.getStyle('width')});
		}
			
		// Strip HTML tags
		var html = this.textarea.getValue().replace(/(<|>)/g, '');
			
		// IE is different, as per usual
		if (Prototype.Browser.IE)
		{
			html = html.replace(/\n/g, '<BR>new');
		}
		else
		{
			html = html.replace(/\n/g, '<br>new');
		}

		this.dummy.innerHTML = html;
		
		if (this.params._maxHeight > 0 && (this.dummy.getHeight() + this.params._lineHeight > this.params._maxHeight))
		{
			this.textarea.setStyle({overflowY : 'auto'});	
		}
		else
		{
			this.textarea.setStyle({overflowY : 'hidden'});

			if ( this.textarea.getHeight() < parseInt(this.dummy.getHeight()+this.params._lineHeight) || (this.dummy.getHeight() < this.textarea.getHeight() ) )
			{
				newHeight = (parseInt(this.dummy.getHeight() + this.params._lineHeight) > this.params._minHeight) ? parseInt(this.dummy.getHeight() + this.params._lineHeight) : this.params._minHeight;
				
				this.textarea.morph('height: '+newHeight+ 'px', 
									{duration:0.3,transition: Effect.Transitions.sinoidal}
				); 
			}
		}
		
	}

});

/**
 * modifies the border color of the givin element
 * @static
 * @param {} e
 * @param {} color
 */
var markValid=function(e, color) {
	e.style.border="1px solid "+color;
}

/**
 * @requires shadowbox-2.0.js
 * @param {} content
 * @param {} player
 * @param {} title
 */
var openShadowbox = function(content, player, title){

	Shadowbox.open({
		content:    content,
		player:     player,
		title:      title,
		width: 		400,
		height: 	200,
		options:{enableKeys:false, displayNav:true, flashBgColor:'transparent'}
	});
};

/**
 * @param {} printURI
 */
var printPopup = function(printURI) {
 	printWin = window.open(printURI, 'PrintPage','toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=640,height=480');
	printWin.focus();
	printWin.print();
}