// YCMS Extender / Extending yourCMS Template System with additional jQuery options

var YCMS = ({
	Dummy: function(){
		return;
	},
	
	Internal: {
		InfoWindowMode: 'hidden',
		InfoWinTmpMsg: '',
		InfoWindowTimeout: null,
		InfoWindowQuestions: null
	},
	
	UI: {
		Block: function(){
			$.blockUI({
				message: '<img src="'+ YCMS.Sys.Template +'busy.gif">'
			});
		},
		
		BlockMessage: function(msg){
			$.blockUI({
				message: msg,
				css: {
					width: 'auto',
					height: 'auto',
					cursor: 'auto'
				}
			});
		},
		
		BlockElement: function(o){
			o.block({
				message: '<img src="'+ YCMS.Sys.Template +'busy.gif">'
			});
		},
		
		UnblockElement: function(o){
			o.unblock();
		},
		
		Unblock: function(){
			$.unblockUI();
		},
		
		Dialog: function(m){
			var posTop = (YCMS.Util.ScrollXY()[0] + 100)+'px'
			$.blockUI({
				message: m,
				css: {
					borderStyle: 'none',
					width: 0,
					height: 0,
					position: 'absolute',
					top: posTop
				}
			});
		}
	},

	Util: {
		ToXML: function(content){
			try{
				var xml = (new DOMParser()).parseFromString(content, "text/xml");
			}
			catch(e){
				try{
					var xml = new ActiveXObject("Microsoft.XMLDOM");
					xml.async = "false";
					xml.loadXML(content);
				}catch(e){
					throw e;
				}
			}
			
			return xml;
		},
		KeyCode: function(code){
			try{
				if(code.keyCode){
					return code.keyCode;
				}
				if(code.which){
					return code.which;
				}
			}catch(e){
				return code.keyCode;
			}	
		},
		
		ScrollXY: function(){
			if(window.pageXOffset != null){
				return Array(window.pageYOffset, window.pageXOffset);
			}else if(document.body != null && document.body.scrollTop != null){
				return Array(document.body.scrollTop, document.body.scrollLeft);
			}else if(document.documentElement != null && document.documentElement.scrollTop != null){
				return Array(document.documentElement.scrollTop, document.documentElement.scrollLeft);
			}
		},
		
		ToURLString: function(oldString){
			if(oldString == null)
			{
				oldString = '';
			}
			var a = /[\s\+\*\~\:\.\,\;\?\´\`\=\)\(\&\%\$\§\!\"\/\[\]\{\}\@\|\>\<\§]/gi;
			oldString = oldString.replace(/[äÄ]/gi, 'ae');
			oldString = oldString.replace(/[öÖ]/gi, 'oe');
			oldString = oldString.replace(/[üÜ]/gi, 'ue');
			oldString = oldString.replace(/[ß]/gi, 'ss');
			oldString = oldString.replace(a, "-").toLowerCase();
			return oldString;
		}
	},
	
	Sys: null,
	
	Forms: {
		IsWrong: function(o){
			try{
				o.attr('class', 'error');
			}catch(e){
				alert(e);
			}
		},
		
		RemoveWrongEffect: function(o){
			try{
				if(o.attr('class') == 'error'){
					o.css('backgroundColor', null);
					o.attr('class', 'active');
				}
			}catch(e){
				alert(e);
			}
		}
	},
	
	Info: {
		Question: function(s, btn_array){
			YCMS.Info.Hide();
			$('#YCMS-Question input.YCMS-Confirmation-Button').unbind('click');
			$('#YCMS-Question').remove();
			YCMS.Internal.InfoWindowQuestions = btn_array;
			var str = '<div id="YCMS-Question">';
			for(var i = 0; i<btn_array.length; i++){
				str += '<input type="button" value="'+ btn_array[i].Value +'" rel="'+btn_array[i].Name+'" class="YCMS-Confirmation-Button" id="YCMS-Confirmation-Button-'+ btn_array[i].Name +'"/>&nbsp;&nbsp;&nbsp;';
			}
			str = s + '<br /><br />' + str;
			str += '</div>';
			YCMS.Info.Display(str);
			$('#YCMS-Question input.YCMS-Confirmation-Button').bind('click', function(){
				var btn_array = YCMS.Internal.InfoWindowQuestions;
				for(var i = 0; i<btn_array.length; i++){
					if(btn_array[i].Name == $(this).attr('rel')){
						if(btn_array[i].Func != null){
							$(this).unbind('click');
							btn_array[i].Func();
						}
					}
				}
				
			});
		},
		
		Display: function(s){
			$('#YCMS-Warning-Message').html(s);
			
			window.clearTimeout(YCMS.Internal.InfoWindowTimeout);
			YCMS.Internal.InfoWindowTimeout = null;

			$('#top-warning').css('top', (YCMS.Util.ScrollXY()[0] + 10) + 'px');
			
			if(YCMS.Internal.InfoWindowMode == 'hidden'){
				$('#top-warning').css({opacity: 0, display: 'block'});
				$('#top-warning').animate({opacity: 1}, 350, 'easeOutQuad');
				YCMS.Internal.InfoWindowMode = 'visible';
			}else{
				YCMS.Internal.InfoWinTmpMsg = s;
				$('#top-warning').animate({opacity: 0.1}, 150, 'easeInQuad', function(s){
					$('#top-warning').animate({opacity: 1}, 200, 'easeOutQuad');
					YCMS.Internal.InfoWindowMode = 'canceled';
				});
			}
			YCMS.Internal.InfoWindowTimeout = window.setTimeout(YCMS.Info._Standby, 4500);
		},
		
		_Standby: function(){
			window.clearTimeout(YCMS.Internal.InfoWindowTimeout);
			YCMS.Internal.InfoWindowTimeout = null;
			
			if(YCMS.Internal.InfoWindowMode != 'canceled '){
				$('#top-warning').animate({opacity: 0.65}, 600, 'easeInQuad');
				YCMS.Internal.InfoWindowMode = 'standby';
				YCMS.Internal.InfoWindowTimeout = window.setTimeout(YCMS.Info.Hide, 5000); 
				
			}else{
				YCMS.Internal.InfoWindowMode = 'visible';
				YCMS.Internal.InfoWindowTimeout = window.setTimeout(YCMS.Info._Standby, 3000);
			}
		},
		
		Hide: function(){
			window.clearTimeout(YCMS.Internal.InfoWindowTimeout);
			YCMS.Internal.InfoWindowTimeout = null;
			
			if(YCMS.Internal.InfoWindowMode != 'canceled'){
				$('#top-warning').fadeOut(800);
				YCMS.Internal.InfoWindowMode = 'hidden';
//				$('#top-warning').animate({opacity: 0}, 800, 'easeInQuad', function(){
//					$('#top-warning').css({opacity: 0, display: 'none'});
//					YCMS.Internal.InfoWindowMode = 'hidden';
//				});
			}
		}
	}
});