

function createCon()
{
  var connection = null;
  try
  {
    // Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
    connection = new XMLHttpRequest();
  }
  catch(e)
  {
    try
    {
      // MS Internet Explorer (ab v6)
      connection  = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        // MS Internet Explorer (ab v5)
        connection  = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
        connection  = null;
      }
    }
  }
  return connection;
}

var oc_as = {
  httpCon : createCon(),
  handleHttpState : function()
  {
    if(oc_as.httpCon.readyState == 4)
    {
      if(oc_as.httpCon.status == 200)
      {
        oc_as.parseData();
      }
    }
  },
  parseData : function()
  {
    var response = oc_as.httpCon.responseText.split("||");

    var status = response[0];
    var id = response[1];
    if(response[2])
      var head   = Base64.decode(response[2]);

    if(response[3])
      var body   = Base64.decode(response[3]);
    if(response[4])
      var type   = Base64.decode(response[4]);

    var elementID = type+id;
    
    if(status=="000")
    {
      alert(head);
    }
    else if(status=="001")
    {
      // Formular
      document.getElementById(elementID).innerHTML = body;
    }
    else if(status=="002")
    {
      // Text
      document.getElementById(elementID).innerHTML = body;
    }
    else if(status=="003")
    {
      var funcString = 'oc_as.show_edit('+id+',\''+type+'WAIT\',\''+Base64.encode(head)+'\')';
      setTimeout(funcString, 1000);
      this.timeWaiting++;
      // DEBUG
      //document.getElementById(elementID).innerHTML = '<pre>'+body+'</pre>';
      //this.timeWaiting+" "+funcString;
    }
    else if(status=="004")
    {
      var el_del = document.getElementById(elementID);

      el_del.innerHTML = "This Element has been removed.";
      setTimeout('oc_as.remove_node(\''+elementID+'\')', 2000);

    }
    else alert("Unbekannter Statuscode ! " + oc_as.httpCon.responseText);
  },
  remove_node : function(nodeID)
  {
    var node = document.getElementById(nodeID);
    node.parentNode.removeChild(node);
  },
  del_entry : function(id,type,args)
  {
    var del = confirm("Wirklich löschen ?");

    if(del)
    {
      this.show_edit(id, type+'DEL', args);
    }
  },
  show_edit : function(id,type,args)
  {
    oc_as.httpCon.open("POST", "./oc_as/oc_asEdit.php",true);
    oc_as.httpCon.onreadystatechange = this.handleHttpState;
    oc_as.httpCon.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    oc_as.httpCon.setRequestHeader("Connection", "close");
    var qstring;
    if(args)
      qstring = "args="+args+"&id="+id+"&action="+type;
    else
      qstring = "id="+id+"&action="+type
    oc_as.httpCon.send(qstring);
//    alert(qstring);
  },
  onSubmitWait : function(id,type,args)
  {
//    alert(args);
//    this.timeWaiting = 0;
//    document.getElementById(type+id).innerHTML = this.ajaxWaitIco;
    setTimeout('oc_as.show_edit('+id+',\''+type+'WAIT\',\''+args+'\')', 1000);
    return true;
  },
  timeWaiting : 0,
  ajaxWaitIco : '<img src="./images/icons/wait30trans.gif" class="ajaxwait" alt="please wait" />'
};

var oc_asImg = {
  show : function(element)
  {
    
    var div = document.getElementById(this.imgDivID);
    var img = document.getElementById(this.imgTagID);

    div.style.display = "block";

    var source = element.getAttribute("src");

    img.src = source.replace(/thumb\//g, "");
    img.title = element.getAttribute("title");
    img.alt = element.getAttribute("alt");
    img.name = element.getAttribute("id");

    clearInterval(this.imgState);
    this.imgState = setInterval("oc_asImg.resize()",1000);
    this.resize();

  },
  next : function()
  {
    if(this.imgList.length<2) return false;
    var img = document.getElementById(this.imgTagID);
    var x = 0;

    while(this.imgList[x] != img.getAttribute("name"))
    {
      if(x<this.imgList.length) x++;
    }
    if(x == (this.imgList.length-1)) x = -1;

    this.show(document.getElementById(this.imgList[x+1]));
  },
  previous : function()
  {
    if(this.imgList.length<2) return false;

    var img = document.getElementById(this.imgTagID);
    var x = 0;

    while(this.imgList[x] != img.getAttribute("name"))
    {
      if(x<this.imgList.length) x++;
    }
    if(x == 0) x = (this.imgList.length);

    this.show(document.getElementById(this.imgList[x-1]));
  },
  resize : function()
  {
    var div = document.getElementById(this.imgDivID);
    var back = document.getElementById(this.imgBgID);
    var img = document.getElementById(this.imgTagID);
    var nav = document.getElementById(this.imgNavID);
    var resize = false;

    img.style.height = "";
    img.style.width  = "";

    var ix = img.offsetWidth;
    var iy = img.offsetHeight;

    var nx = ix;
    var ny = iy;

    var navx = nav.offsetWidth;

    var dx = div.offsetWidth;
    var dy = div.offsetHeight;

    var mx = dx*0.9; // 10% padding
    var my = dy*0.9;

    if(mx<ix)
    {
      nx = mx;
      ny = iy/(ix/mx);

      ix = nx;
      iy = ny;
      resize = true;
    }
    if(my<iy)
    {
      nx = ix/(iy/my);
      ny = my;
      resize = true;
    }

    if(resize)
    {
      img.style.height = ny+'px';
      img.style.width  = nx+'px';
    }
    nav.style.left = (back.offsetWidth/2)-(navx/2)+'px';

    back.style.marginTop = -1*(back.offsetHeight/2)+'px';
    back.style.marginLeft = -1*(back.offsetWidth/2)+'px';

  },
  hide : function()
  {
    var div  = document.getElementById(this.imgDivID);
    div.style.display = "none";
    clearInterval(this.imgState);
  },
  imgState : null,
  imgDivID : "oc_as_image",
  imgBgID  : "oc_as_image_show",
  imgTagID : "oc_asShowImg",
  imgNavID : "oc_as_image_nav",
  imgList  : new Array()
}


/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
