//################################################
//# made by Florian Sabolewski - homepage@22y.de #
//################################################

var armada_screen = { 

	http_request_ar : {},

	areas 			: {},
	
	slide_activ		: {},
	
	load_func_save	: {},
	
	last_load		: {},

	mouse_xpos		: {},
	
	mouse_ypos		: {},
	
	script_name		: null,
	
	c_area_ids : {},
	
	init : function( script_name ) {
	
		this.script_name = script_name;
		this.find_actions();
		document.onmousemove = armada_screen.get_mouse_pos;
		this.get_mouse_pos();
		
		if( arguments.length > 1 ) armada_screen.c_area_ids = arguments[1].split(",");
			
		if(typeof(armada_admin) != 'undefined') armada_admin.init();

		return null;
	},
	
	
	get_script_name : function() {
	
		return this.script_name;
	},


	is_ie : function() {
	
		return (window.ActiveXObject) ? true : false;
	},


	new_xmlhttp_obj : function() {
	
		var http_request = null;
	
		if( window.XMLHttpRequest ) {
			
			http_request = new XMLHttpRequest();
	
			if( http_request.overrideMimeType ) http_request.overrideMimeType( 'text/html' );
	
	      } else if( window.ActiveXObject ) {
	
			try {
	
				http_request = new ActiveXObject( 'Msxml2.XMLHTTP' );
	
			} catch( e ) {
	
				try {
	
					http_request = new ActiveXObject( 'Microsoft.XMLHTTP' );
	
				} catch( e ) {
				
				}
			}
		}
	      
		if(http_request) return http_request;
	
		alert('Cannot create XMLHTTP instance');
		return null;
	},
	
	
	reload_all_areas : function() {
	
		var c_area_ids_l = armada_screen.c_area_ids.length;
		
		for( var i=0; i<c_area_ids_l; ++i ) {
			
			armada_screen.load_last_loaded( 'content_'+armada_screen.c_area_ids[i] );
		}
		
		return true;
	},
	

	load_last_loaded : function( id ) {
	
		if( armada_screen.last_load[id] && ( typeof(armada_screen.last_load[id]) != 'undefined' ) ) {

			if( typeof( armada_screen.last_load[id][2] ) != 'undefined' ) return armada_screen.send_per_GET( armada_screen.last_load[id][1], armada_screen.last_load[id][0], id );

			return armada_screen.send_per_GET( armada_screen.last_load[id][1], armada_screen.last_load[id][0], id, armada_screen.last_load[id][2], armada_screen.last_load[id][3] );
		
		} else {

			var content_id = id.match( /(\d+)$/gi );
		
			var get_params = ( content_id && content_id.length == 1 ) ? 'a=print_inline&area_id=' + content_id[0] : '';
		
			return armada_screen.send_per_GET( this.get_script_name(), get_params, id );
		}
	},	

	
	//send_per_GET( URL, PARAM, DIV [, FUNC_NAME, FUNC_PARAMS, ... ] );
	
	send_per_GET : function(url, get_data, div_id) {

		if( armada_screen.is_ie() ) window.location.href = url + "?" + get_data.replace(/a=print_inline&/,"");

		if( url == '' ) url = this.get_script_name();

		if( armada_screen.http_request_ar[div_id] = this.new_xmlhttp_obj() ) {
		
			if( ( div_id == "overlay_inhalt" ) && ( document.getElementById('overlay_popup_loading').style.display != 'inline' ) ) document.getElementById('overlay_popup_loading').style.display = 'inline';
	    
			if( document.getElementById(div_id).style.display != 'block' ) document.getElementById(div_id).style.display = 'block';
			
			var param_l = arguments.length;

			armada_screen.last_load[div_id] = new Array();
			armada_screen.last_load[div_id][0] = get_data;
			armada_screen.last_load[div_id][1] = url;

			if(param_l > 3) {
						
				var func_name = arguments[3];
				var param = '';

				if( param_l > 5 ) {
							
					for( var i=4; i<param_l; ++i ) {
						
						param = param + ( (i != 4) ? ", '" : "" ) + arguments[i] + ( ( i != ( param_l - 1 ) ) ? "'" : "" );
					}
					
				} else {
				
					param = arguments[4];
				}
				
				armada_screen.last_load[div_id][2] = func_name;
				armada_screen.last_load[div_id][3] = param;

				armada_screen.load_func_save[get_data] = ( typeof( param ) != 'undefined' ) ? ( ''+func_name+'(\''+param+'\');' ) : ( ''+func_name+'();' );				
			}

			armada_screen.http_request_ar[div_id].onreadystatechange = function() {
		
				if( armada_screen.http_request_ar[div_id].readyState == 4 ) {
		
					if( armada_screen.http_request_ar[div_id].status == 200 ) {

						result = armada_screen.http_request_ar[div_id].responseText;
									
						if(div_id == "overlay_inhalt") {
					
							if(document.getElementById('overlay_popup').style.display != 'inline') document.getElementById('overlay_popup').style.display = 'inline';
							if(document.getElementById('overlay_popup_loading').style.display != 'none') document.getElementById('overlay_popup_loading').style.display = 'none';
	    				}
	    			
						document.getElementById(div_id).innerHTML = result;  
						if(document.getElementById(div_id).style.display != 'block') document.getElementById(div_id).style.display = 'block';
					
						if( typeof(armada_screen.load_func_save[get_data]) != 'undefined' && armada_screen.load_func_save[get_data] != 'undefined();' ) {
						
							eval( armada_screen.load_func_save[get_data] );
						
							delete armada_screen.load_func_save[get_data];
						}
					
						armada_screen.find_actions();
									
						return true;
					
					} else {
					
						alert('There was a problem with the request. Please try again.');
					}	
				}
				
				return null;
			}
	
			armada_screen.http_request_ar[div_id].open('GET', url + ((get_data.length == 0) ? ('') : ('?' + get_data)), true);
			armada_screen.http_request_ar[div_id].send(null);
		}
		
		return null;
	},


	send_per_POST : function(url, form_id, div_id) {

		var param_l = arguments.length;

		if(param_l > 3) {
						
			var func_name = arguments[3];
			var param = '';
			var param_tmp = '';
				
			if(param_l > 5) {
							
				for(var i=0; i<param_l; ++i) {
					
					if(i > 2) {
							
						if(i != 3) param_tmp = ", '";
						param_tmp = param_tmp + arguments[i];
						if(i != (param_l-1)) param_tmp = "'";
						param = param + param_tmp;
						param_tmp = '';
					}
				}
					
			} else {
				
				param = arguments[4];
			}

			armada_screen.load_func_save[(form_id + div_id)] = ''+func_name+'(\''+param+'\');';	
		}		
		
		l_a = armada_screen.areas.length;

		if(l_a > 0) {
			
			var temp_c = null;
			
			for(var q = 0; q < l_a; ++q) {
				
				temp_c = tinyMCE.get(armada_screen.areas[q]).getContent();
				document.getElementById(armada_screen.areas[q]).value = temp_c;
			}
			
			this.reset_areas();
		}

		return this.send_per_POST_do(url, form_id, div_id);
	},
	
	
	send_per_POST_do : function( url, form_id, div_id ) {
	
		if( armada_screen.http_request_ar[div_id] = this.new_xmlhttp_obj() ) {
		
			var post_data = "";
			var search_multi_form = form_id.indexOf( "|" );
	
			if( search_multi_form == -1 ) {
		
				var form_x = new Array;
				form_x[0] = form_id;
				var form_x_count = 1;
			
			} else {
			
				var form_x = form_id.split( "|" );
				var form_x_count = form_x.length;
			}
			
			var form_i_count = new Array;
			var i = new Array;
			var my_id = "";
	
			for( var x=0; x<form_x_count; ++x ) {
			
				if( document.forms[form_x[x]] ) {
			
					form_i_count[x] = document.forms[form_x[x]].length;
		
					for( i[x]=0; i[x]<form_i_count[x]; ++i[x] ) {
	
						if( ! ( ( ( document.forms[form_x[x]].elements[i[x]].type == 'radio' ) || ( document.forms[form_x[x]].elements[i[x]].type == 'checkbox' ) ) && ( document.forms[form_x[x]].elements[i[x]].checked != true ) ) || ( document.forms[form_x[x]].elements[i[x]].type == 'file' ) ) {
	
							my_id = ( document.forms[form_x[x]].elements[i[x]].id != "" ) ? document.forms[form_x[x]].elements[i[x]].id : document.forms[form_x[x]].elements[i[x]].name;
	
							if( document.forms[form_x[x]].elements[i[x]].type != 'select-multiple' ) {
							
								if( post_data != "" ) post_data = post_data + "&";

								post_data = post_data + encodeURIComponent(my_id) + "=" + encodeURIComponent(document.forms[form_x[x]].elements[i[x]].value);
						
							} else {
						
								l_y = document.forms[form_x[x]].elements[i[x]].length;
						
								for( var y=0; y<l_y; ++y ) {
							
									if( document.forms[form_x[x]].elements[i[x]][y].selected ) {

										if( post_data != "" ) post_data = post_data + "&";
				
										post_data = post_data + encodeURIComponent(my_id) + "=" + encodeURIComponent(document.forms[form_x[x]].elements[i[x]][y].value);
									}
								}
							}
						}
					}
				}
			}
	
		      
			armada_screen.http_request_ar[div_id].onreadystatechange = function() {
		
				if( armada_screen.http_request_ar[div_id].readyState == 4 ) {
	      
					if( armada_screen.http_request_ar[div_id].status == 200 ) {
	
						result = armada_screen.http_request_ar[div_id].responseText;
	            		document.getElementById( div_id ).innerHTML = result;

	            		if( document.getElementById( div_id ).style.display != 'block' ) document.getElementById( div_id ).style.display = 'block';

						if( armada_screen.load_func_save[(form_id + div_id)] != 'undefined' ) {
					
							eval( armada_screen.load_func_save[(form_id + div_id)] );
						
							delete armada_screen.load_func_save[(form_id + div_id)];
						}

	            		armada_screen.find_actions();
	            		
	            		return true;
	            		
					} else {
	         
						alert('There was a problem with the request. Please try again.');
	         		}
				} 
				
				return null;
			}
	      
			armada_screen.http_request_ar[div_id].open( 'POST', url, true );
			armada_screen.http_request_ar[div_id].setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
			armada_screen.http_request_ar[div_id].setRequestHeader( 'Content-length', post_data.length );
			armada_screen.http_request_ar[div_id].setRequestHeader( 'Connection', 'close' );
			armada_screen.http_request_ar[div_id].send( post_data );
		}
		
		return null;
	},
	
	
	reset_areas : function( r ) {

		if(typeof(tinyMCE) != 'undefined') {

			var l_a = armada_screen.areas.length;
		
			for( var i = 0; i < l_a; ++i ) {

				tinyMCE.execCommand('mceFocus', false, armada_screen.areas[i]);    
				tinyMCE.execCommand('mceRemoveControl', false, armada_screen.areas[i]);
			}

			armada_screen.areas = new Array();
			
			if(r) var reload_areas = window.setTimeout("armada_screen.reload_areas()", 50);	
		}
		
		return true;
	},


	reload_areas : function() {

		if(typeof(tinyMCE) != 'undefined') {

			var a = document.getElementsByTagName("textarea");		
			var l_a = a.length;

			for( var y = 0; y < l_a; ++y ) {

				armada_screen.areas[y] = ''+a[y].id+'';
				
				tinyMCE.idCounter=0;
				tinyMCE.execCommand('mceAddControl', false, ''+a[y].id+'');
			}
		}
		
		return true;
	},


	find_actions : function() {
	
		if(typeof(tinyMCE) != 'undefined') {
	
			this.reset_areas(true);
			
			if( !this.is_ie() ) {
			
				var b = document.getElementsByTagName("div");
		
				var l_b = b.length;
			
				for(var i = 0; i < l_b; ++i) {
				
					if(b[i].className.match(/not\_published/gi) != null) {
					
						b[i].xOpacity = .5;
						this.set_opacity(b[i]);
					
						b[i].onmouseover = function() { armada_screen.slide_opacity(.99, true, this.id, .02, 50, 1); };
						b[i].onmouseout = function() { armada_screen.slide_opacity(.5, false, this.id, .02, 50, 1); };
					}
				}
			}
		
			return true;
		}
		
		return null;
	},


	typeOf : function(t) {
	
		return String(t.constructor).split(" ")[1].split("()").join("");
	},


	set_opacity : function(obj) {

		if( obj.xOpacity > .99 ) obj.xOpacity = .99;
		if( obj.xOpacity < 0 ) obj.xOpacity = 0;

		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	
		return null;
	},

	get_mouse_pos : function( e ) {
		
		if(!e) e = window.event;
	
		if(e) { 
	  
			if( e.pageX || e.pageY ) {
	    
				armada_screen.mouse_xpos = parseInt(e.pageX);
				armada_screen.mouse_ypos = parseInt(e.pageY);
	      
			} else if( e.clientX || e.clientY ) {
	    
				armada_screen.mouse_xpos = parseInt(e.clientX + document.body.scrollLeft);
				armada_screen.mouse_ypos = parseInt(e.clientY + document.body.scrollTop);
			}
			
			return null;
		}
		
		armada_screen.mouse_xpos = 0;
		armada_screen.mouse_ypos = 0;

		return null;
	},

	show_hide_div_mousepos : function( div_id ) {

	    var el = document.getElementById( div_id );

	    el.style.position = "absolute";
	    el.style.top = ( armada_screen.mouse_ypos + 9 ) + "px";
	    el.style.left = ( armada_screen.mouse_xpos + 9 ) + "px";

	    return this.show_hide_div( div_id );
	},

	
	show_hide_div : function( div_id ) {

		// function( div_id, check_id, rew, displaymode, must_display )
				
	    var check_id = ( arguments.length > 1 ) ? arguments[1] : null;
	    var rew = ( arguments.length > 2 ) ? arguments[2] : null;
	    var displaymode = ( ( arguments.length > 3 ) && ( arguments[3] ) ) ? 'inline' : 'block';
	    var must_display = ( arguments.length > 4 ) ? arguments[4] : null;
	    var el = document.getElementById( div_id );
	    var check = null;
	    
	    if( ( el.style.display == '' ) && ( el.className.indexOf( 'dont_display' ) != -1 ) ) check = true;
	
	    if( ( check === true ) || ( ( el.style.display == 'none' ) && ( must_display !== false ) ) || ( must_display === true ) ) {
	    	
	    	if( check_id ) document.getElementById( check_id ).checked = ( rew ) ? false : true;
			el.style.display = displaymode;
	        	
		} else {
		
	    	if( check_id ) document.getElementById( check_id ).checked = ( rew ) ? true : false;
			el.style.display = 'none';
	    }
	    
	    return null;
	},

	
	show_hide_outline : function(div_id, on_off) {
    
		var el = document.getElementById( div_id );
	
		if( el !== null ) el.style.outline = ( on_off ) ? '1px #C00 dotted' : '';
	
		return null;
	},
	
	
	open_overlaypopup : function(ueberschrift, datei, get_data) {
	
		document.getElementById('trans_bg').xOpacity = 0;
		this.set_opacity(document.getElementById('trans_bg'));
		this.slide_opacity(.56, true, 'trans_bg', .08, 10, 10);
		document.getElementById('overlay_popup_loading').style.zIndex = 101;
	    document.getElementById('overlay_popup').style.zIndex = 102;
	    document.getElementById('overlay_uebers').innerHTML = ueberschrift;
		this.send_per_GET(datei, get_data, 'overlay_inhalt');

		return null;
	},
	
		
	close_overlaypopup : function() {
	
		this.find_actions();
		this.slide_opacity(0, false, 'trans_bg', .08, 10, -10);
	    document.getElementById('overlay_popup').style.border = "";
	    document.getElementById('overlay_popup').style.backgroundColor = "";		
		document.getElementById('overlay_popup').style.display = 'none';
		document.getElementById('overlay_inhalt').style.display = 'none';
		document.getElementById('overlay_popup_loading').style.zIndex = -11;
	    document.getElementById('overlay_popup').style.zIndex = -12;
	    document.getElementById('overlay_inhalt').innerHTML = '';

		return null;
	},


	slide_opacity : function(to, reverse, div_id, faktor, time, zindex) {
		
		if( ( typeof(armada_screen.slide_activ[div_id]) != 'undefined' ) && ( armada_screen.slide_activ[div_id] != to ) ) return null;
		
		armada_screen.slide_activ[div_id] = to;
		
		check = false;
		
		if(reverse) {
	
			if(document.getElementById(div_id).style.display != 'block') document.getElementById(div_id).style.display = 'block';
			if(document.getElementById(div_id).style.zIndex != zindex) document.getElementById(div_id).style.zIndex = zindex;
			
			if(document.getElementById(div_id).xOpacity < to) {
			
				document.getElementById(div_id).xOpacity = document.getElementById(div_id).xOpacity + faktor;
				
			} else {
				
				check = true;
				document.getElementById(div_id).xOpacity = to;
			}
				
		} else {
					
			if((document.getElementById(div_id).xOpacity > to) && ((document.getElementById(div_id).xOpacity - faktor) > to)) {
		
				document.getElementById(div_id).xOpacity = document.getElementById(div_id).xOpacity - faktor;
			
			} else {
			
				check = true;
				document.getElementById(div_id).xOpacity = to;
				
				if(to == 0) document.getElementById(div_id).style.display = 'none';
				if(document.getElementById(div_id).style.zIndex != zindex) document.getElementById(div_id).style.zIndex = zindex;
			}
		}

		this.set_opacity(document.getElementById(div_id));
		
		if(!check) {
		
			trans_slide = setTimeout("armada_screen.slide_opacity(" + to + ", " + reverse + ", '" + div_id + "', " + faktor + ", " + time + ", " + zindex + ")", time);
			
		} else {
		
			delete armada_screen.slide_activ[div_id];
		}

		return null;
	},

	
	openinfo : function( id ) {
	
		window.open("index.php?a=pp&artikel_id="+id, "", 'scrollbars=yes,width=700,height=600,left=12,top=12,locationbar=false,menubar=false,resizable=yes');	
	}
}