ik = {
    cookieName:"ik_sid",
    cookieValue:"",

    $:function(id){
        return document.getElementById(id);
    },

    check_sid:function(){
		var readSIDResult = ik.readSID();

		if(readSIDResult){
			var checkUserDir = xhr.cookiexhr(document.getElementById('ik_path').innerHTML + "ac/ik/index.php?action=check&sid=" + readSIDResult);

		    return readSIDResult;
        }else{
		    var cRequestValue = xhr.cookiexhr(document.getElementById('ik_path').innerHTML + "ac/ik/index.php?action=generate");
		    var result = ik.setSID(ik.cookieName, cRequestValue);

		    return result;
        }
    },

    readSID:function(){
        var cValue = document.cookie;
        var cStartPos = cValue.indexOf(ik.cookieName);

        if(cStartPos == -1){
            cValue = null;
        }else{
            cStartPos = cValue.indexOf("=", cStartPos) + 1;
            var cEndPos = cValue.indexOf(";", cStartPos);
            if (cEndPos == -1){
                cEndPos = cValue.length;
            }
            cValue = unescape(cValue.substring(cStartPos, cEndPos));
        };

        return cValue;
    },

    setSID:function(cName, cValue){
        document.cookie = cName + "=" + escape(cValue);
		return escape(cValue);
    },

  /*
   * the add_* functions now call async_cookiexhr which is the asynchronous version
   * of cookiexhr
   *
   * async_cookiexhr calls ik.count_total on success hence the 3 calls below are commented out
   *
   */

    add_chapter:function(project_name, chapter_path, chapter_name, show_id, teaser, title, project_id, language){
      var requestStr = document.getElementById('ik_path').innerHTML + "ac/ik/index.php?action=add_chapter&sid=" + ik.check_sid() + "&project_name=" + project_name + "&chapter_path=" + chapter_path + "&chapter_name=" + chapter_name + "&show_id=" + show_id + "&teaser=" + teaser + "&title=" + title + "&project_id=" + project_id + "&language=" + language;
		  xhr.async_cookiexhr(requestStr);
      //ik.count_total();
    },

    add_article:function(project_name, article_path, show_id, teaser, title){
      var requestStr = document.getElementById('ik_path').innerHTML + "ac/ik/index.php?action=add_article&sid=" + ik.check_sid() + "&project_name=" + project_name + "&article_path=" + article_path + "&show_id=" + show_id + "&teaser=" + teaser + "&title=" + title;
		  xhr.async_cookiexhr(requestStr);
      //ik.count_total();
    },

    add_file:function(file_name, thumb_name){
      var requestStr = document.getElementById('ik_path').innerHTML + "ac/ik/index.php?action=add_file&sid=" + ik.check_sid() + "&file_name=" + file_name + "&tn=" + thumb_name;
      xhr.async_cookiexhr(requestStr);
      //ik.count_total();
    },

    show:function(language, view){
        var ik_popup;
		ik_popup = window.open(document.getElementById('ik_path').innerHTML + 'ac/ik/index.php?action=show&sid=' + ik.check_sid() + '&language=' + language + "&view=" + view, 'IK_popup', 'width=800, height=600, scrollbars=yes, location=no, menubar=yes, toolbar=yes');
        ik_popup.focus();

        return false;
    },

    show_bin:function(language, view){
		var requestStr = document.getElementById('ik_path').innerHTML + "ac/ik/index.php?sid=" + ik.check_sid() + '&language=' + language + "&view=" + view + "&template_path=" + escape(document.getElementById('ik_path').innerHTML);

		if (document.getElementById("block_3") != null)
		{
				document.getElementById("block_3").innerHTML = "";
		}
		document.getElementById("block_1").className = "wide";
		document.getElementById("content").className = "fader clearfix";
		//document.getElementById("content").innerHTML = "";

		var result = xhr.doxhr(requestStr, "content", "innerHTML");


	},

	remove_bin:function(target, language, view){
		var requestStr = document.getElementById('ik_path').innerHTML + "ac/ik/index.php?action=remove_bin&target=" + target + "&sid=" + ik.check_sid() + '&language=' + language + "&view=" + view + "&template_path=" + escape(document.getElementById('ik_path').innerHTML);

		if (document.getElementById("block_3") != null)
		{
				document.getElementById("block_3").innerHTML = "";
		}
		document.getElementById("block_1").className = "wide";
		document.getElementById("content").className = "fader clearfix";
		document.getElementById("content").innerHTML = "";

		var result = xhr.doxhr(requestStr, "content", "innerHTML");
	},

	remove_all_bin:function(language, view){
		var requestStr = document.getElementById('ik_path').innerHTML + "ac/ik/index.php?action=remove_all_bin&sid=" + ik.check_sid() + '&language=' + language + "&view=" + view + "&template_path=" + escape(document.getElementById('ik_path').innerHTML);

		var result = xhr.doxhr(requestStr, "content", "innerHTML");

		if (result != null)
		{
				if (document.getElementById("block_3") != null)
				{
						document.getElementById("block_3").innerHTML = "";
				}
				document.getElementById("block_1").className = "wide";
				document.getElementById("content").className = "fader clearfix";
		}
	},

    count_total:function(){
        var requestStr = document.getElementById('ik_path').innerHTML + "ac/ik/index.php?action=count&sid=" + ik.check_sid();
		var result = xhr.doxhr(requestStr, "ik_total", "innerHTML");
    }
};

xhr = {
    containerType:"null",

	responseCheck:function(message){
	    cookiexhrValue = escape(message);

		if(cookiexhrValue.indexOf('%0A%0D') == 0){
			cookiexhrValue = cookiexhrValue.substr(6, cookiexhrValue.length);
		};

        return unescape(cookiexhrValue);
	},

  async_cookiexhr: function(url) {
    jQuery('body').addClass('wait');

    jQuery.ajax({
      type: 'get',
      url: url + "&dud=" + new Date().getTime(),
      success: ik.count_total,
      complete: function() {
        jQuery('body').removeClass('wait');
      }
    });
  },

    cookiexhr:function(url){

		var hashDate = new Date();
        var request;

        try{
            request = new XMLHttpRequest();
        }catch(error){
            try{
                request = new ActiveXObject("MSXML2.XMLHttp.6.0");
            }catch(error){
                return true;
            }
        };

        request.open('get', url + "&dud=" + hashDate.getTime(), false);
        request.send(null);

		var cookiexhrValue = null;

		if(request.status == 200){
		    cookiexhrValue = xhr.responseCheck(request.responseText);
        };

        return unescape(cookiexhrValue);
    },

    doxhr:function(url, container, type){
		var hashDate = new Date();

        if(type != "null"){
            xhr.containerType = type;
        }

		if(container != "null"){
			if(!document.getElementById || !document.createTextNode) {
				return;
			}

			xhr.outputContainer = document.getElementById(container);

			if(!xhr.outputContainer){
				return;
			}
		}else{
			xhr.outputContainer = "null";
        }

        var request;

		try{
            request = new XMLHttpRequest();
        }catch(error){
            try{
//                request = new ActiveXObject( "Microsoft.XMLHTTP" );
                request = new ActiveXObject("MSXML2.XMLHttp.6.0");
            }catch(error){
                return true;
            }
        }

        request.open('get', url + "&dud=" + hashDate.getTime());

        request.onreadystatechange = function(){
            if(request.readyState == 1){
				if(container != "null"){
					xhr.outputContainer.value = '';
                }
            }

            if(request.readyState == 4){
                if (/200|304/.test(request.status)){
                    xhr.retrieved(request);
                }else{
                    xhr.failed(request);
                }
            }
        }

        request.send(null);

        return false;
    },

    failed:function(requester){
        return true;
    },

    retrieved:function(requester){
        var data = xhr.responseCheck(requester.responseText);
        //data = data.replace( /\n/g, '<br />' );

        if(xhr.outputContainer != "null"){
		    switch(xhr.containerType){
			case "value":
			     xhr.outputContainer.value = data;
				 return data;
			     break;
		       case "innerHTML":
          if (jQuery(xhr.outputContainer).hasClass('infokorb_items'))
            jQuery('.infokorb_items').html(data);
			     xhr.outputContainer.innerHTML = data;
           // re-initialise the Milkbox for infokorb thumbnails
           if (xhr.outputContainer == document.getElementById('content')) {
             if (Milkbox) Milkbox.initialize();
           }
			     break;
		       case "null":
			     return data;
			     break;
		       default:
			     return data;
			     break;
		    }
        }else{
			return data;
        }
        return false;
    }
};