function JimetPageLoaderObj() {
    this.jimetAjaxObj = null;
    this.toggleDivId = null;
    this.postData = null;
    this.url = null;
    this.endFunc = null;
    this.endFuncParams = null;
    this.loaderIcon = null;
    this.loadingDiv = null;
    this.timeout    = 30000;
    this.purpose = 'pageLoader';

    this.finPosition = function(obj) {

        var curleft = curtop = 0;

        var jq = jQuery(document.getElementById(this.toggleDivId));
        var position = jq.offset();

        if(position!=null){
            curleft = position.left;
            curtop = position.top;
        }
        return [curleft,curtop];
    /*var curleft = curtop = 0;
		if (obj&&obj.offsetParent)
		{
			do
			{
				if(obj.className!='wrapper'){
				curleft += obj.offsetLeft-obj.scrollLeft;
				curtop += obj.offsetTop-obj.scrollTop;
				}else{
				if(obj.scrollTop){
					curleft += obj.offsetLeft;//-obj.scrollLeft;
					curtop += obj.offsetTop;//-obj.scrollTop;
					}else{
					curleft += obj.offsetLeft;
					curtop += obj.offsetTop;
				}
			}
			while (obj = obj.offsetParent);
		}
		else if(obj)
		{
			curleft = obj.offsetLeft-obj.scrollLeft;
			curtop = obj.offsetTop-obj.scrollTop;
		}
		return [curleft,curtop];*/
    };

    this.init = function(divId,url,data){
        this.toggleDivId = divId;

        var loadIc = document.createElement('div');
        var loadDiv = document.createElement('div');

        loadIc.style.cssText = 'position:absolute;display:none;';
        loadDiv.className = 'loadingDiv';

        var loadImg = document.createElement('img');
        loadImg.src = './common/images/loading.gif';

        document.getElementById("formContainer").appendChild(loadIc);
        document.getElementById("formContainer").appendChild(loadDiv);
		
        loadIc.appendChild(loadImg);

        this.loaderIcon = loadIc;
        this.loadingDiv = loadDiv;

        this.jimetAjaxObj = new JimetAjaxObj();

        this.url = appRoot + url;
        this.postData = data;

    //alert(lastScrollPosition);
    };

    this.doLoad = function (divId,url,data,isSynch) {

        this.init(divId,url,data);
        this.toggleLoading(true);
        //alert(document.getElementById(this.toggleDivId+'Loading').style.display);
        this.jimetAjaxObj.setPostData(this.postData+'&divId='+this.toggleDivId);
        this.jimetAjaxObj.setTimeout(this.timeout);
        if( isSynch )
            this.jimetAjaxObj.async = false;

        if(this.purpose=='doAjax')
        {
            this.jimetAjaxObj.setIsSubmit(true);
        }

        var self = this;
        this.loaderResult = function(result){

            if(result=='ajaxError'){

                self.toggleLoading();

                ajaxOnWork = false;
                return;
            }
            self.loadDiv (result);

            if(self.purpose == 'pageLoader')
            {
                if (self.endFunc&&self.endFuncParams)
                    self.endFunc(self.endFuncParams);
                else if (self.endFunc)
                    self.endFunc();
            } else {
                if (self.endFuncParams)
                    self.endFunc(result,self.endFuncParams);
                else
                    self.endFunc(result);
            }
            self.toggleLoading();

        // 			alert(self.toggleDivId+self.url+self.postData);
        };
        this.jimetAjaxObj.doPost(this.url,this.loaderResult);

    };

    this.abort = function()
    
    {
   	 
    	this.loaderResult = null;
    	this.jimetAjaxObj.req.abort();
    	this.jimetAjaxObj.req = null;
    	this.jimetAjaxObj = null;
    	this.loaderIcon.style.display = 'none';
    	this.loadingDiv.style.display = 'none';
    	
   	 	
    }
    
    this.loadDiv = function(result){
        if(document.getElementById(this.toggleDivId)&&this.purpose=='pageLoader'){

            if(document.getElementById(this.toggleDivId).type&&document.getElementById(this.toggleDivId).type=='text'){

                var qs = new Querystring(decodeURI(result));
                innerStr = '';
                if(qs.get('value'))
                    innerStr = qs.get('value');
                else
                    innerStr = result;
                document.getElementById(this.toggleDivId).value = innerStr;

            }
            else{
                //		  			if(this.toggleDivId=='cover')
                //		  			{
                //			  			document.getElementById(this.toggleDivId).innerHTML = '<center><input type="button" onclick="document.getElementById(\'cover\').style.display=\'none\'; " value="Vazgeç" /><br><br>'+result+'</center>';
                //		  			}
                //		  			else
                document.getElementById(this.toggleDivId).innerHTML = result;
            }
        }
        this.toggleLoading(true);
        ajaxOnWork = false;
        
        setTimeout(function() {
        	executeScript(result);
        },500);
        //this.executeScript(result);

        //alert(lastScrollPosition);
        if(lastScrollPosition)
            window.scrollTo(0,lastScrollPosition);

    //ajaxAbort = false;
    //alert(document.getElementById(this.toggleDivId+'Loading').style.display);
    };

    this.setTimeout = function(tmoutinmillis) {
        this.timeout = tmoutinmillis;
    };

    this.setEndFunc = function(func) {
        this.endFunc = func;
    };

    this.setPurpose = function(func) {
        this.purpose = func;
    };

    this.setEndFuncParams = function(paramArr) {
        this.endFuncParams = paramArr;
    };

    this.toggleLoading = function(show){

        var mainDiv = document.getElementById(this.toggleDivId);

        var pos = this.finPosition(mainDiv);

        if (mainDiv && this.loadingDiv){

            this.loaderIcon.style.position = 'absolute';

            if(show){
                this.loaderIcon.style.display = 'block';
                this.loaderIcon.className = 'none';
                this.loadingDiv.style.display = 'block';

                this.loadingDiv.style.zIndex = document.getElementById(this.toggleDivId).style.zIndex+1;
                this.loaderIcon.style.zIndex = this.loadingDiv.style.zIndex+1;
                this.loaderIcon.style.left = (pos[0]+(mainDiv.offsetWidth/2))-(this.loaderIcon.offsetWidth/2) + 'px';
                this.loaderIcon.style.top = pos[1]+((mainDiv.offsetHeight/2)) + 'px';

                this.loadingDiv.style.position = 'absolute';

                this.loadingDiv.style.height = (mainDiv.offsetHeight<this.loaderIcon.offsetHeight?this.loaderIcon.offsetHeight:mainDiv.offsetHeight) + 'px';
                this.loadingDiv.style.width = mainDiv.offsetWidth+ 'px';
                this.loadingDiv.style.left = pos[0]+ 'px';
                this.loadingDiv.style.top = pos[1]+ 'px';
            }
            else{
                jQuery(this.loaderIcon).animate({
                    opacity: 0
                }, "slow",function(){
                    this.style.display = 'none';
                    this.className = 'none';
                    document.getElementById("formContainer").removeChild(this);
                });
                jQuery(this.loadingDiv).animate({
                    opacity: 0
                }, "slow",function(){
                    this.style.display = 'none';
                    document.getElementById("formContainer").removeChild(this);
                });
            }
        }
    };

    this.executeScript = function(content)
    {
        //		alert(resp);// <script src=source.js olanlanları ayıklayalım
        var src = new RegExp('<script.*?src=".*?"');
        var repl = new RegExp('<script.*?src="');
        var matches = src.exec(content);

        /**ilerde jsin içinden script çalıştırmak istersek kullanılır
	   if (matches != null)
	    {
	        for (i = 0; i < matches.length; i++)
	        {
	            // source dosyas?n?n ad?
	            var scriptSrc = matches[i].replace(repl, '');
	            scriptSrc = scriptSrc.substring(0, scriptSrc.length-1);

	        }
	    }
	*/
        repl = new RegExp('<script.*?src=".*?".*?</script>');
        content = content.replace(repl, '');

        // inline scriptler i?in
        // script commentlerini kald?r
        repl = new RegExp('//.*?$', 'gm');
        content = content.replace(repl, '\n');

        // enterlar? clean
        repl = new RegExp('[\n\r]', 'g');
        content = content.replace(repl, ' ');

        //<script> i?indeki her?eyi match et
        src = new RegExp('<script.*?</script>', 'g');
        matches = content.match(src);

        if (matches != null)
        {
            for (i = 0; i < matches.length; i++)
            {
                // baştaki tagı kaldır
                var rep = new RegExp('<script.*?>', 'gm');
                var script = matches[i].replace(rep, '');

                // sondaki tagı kaldır
                rep = new RegExp('</script>', 'gm');
                script = script.replace(rep, '');

                // komutları çalıştır
                setTimeout(script, 250);
            }
        }
    }
}

function executeScript(content)
{
    //		alert(resp);// <script src=source.js olanlanları ayıklayalım
    var src = new RegExp('<script.*?src=".*?"');
    var repl = new RegExp('<script.*?src="');
    var matches = src.exec(content);

    /**ilerde jsin içinden script çalıştırmak istersek kullanılır
   if (matches != null)
    {
        for (i = 0; i < matches.length; i++)
        {
            // source dosyas?n?n ad?
            var scriptSrc = matches[i].replace(repl, '');
            scriptSrc = scriptSrc.substring(0, scriptSrc.length-1);

        }
    }
*/
    repl = new RegExp('<script.*?src=".*?".*?</script>');
    content = content.replace(repl, '');

    // inline scriptler i?in
    // script commentlerini kald?r
    repl = new RegExp('//.*?$', 'gm');
    content = content.replace(repl, '\n');

    // enterlar? clean
    repl = new RegExp('[\n\r]', 'g');
    content = content.replace(repl, ' ');

    //<script> i?indeki her?eyi match et
    src = new RegExp('<script.*?</script>', 'g');
    matches = content.match(src);

    if (matches != null)
    {
        for (i = 0; i < matches.length; i++)
        {
            // baştaki tagı kaldır
            var rep = new RegExp('<script.*?>', 'gm');
            var script = matches[i].replace(rep, '');

            // sondaki tagı kaldır
            rep = new RegExp('</script>', 'gm');
            script = script.replace(rep, '');

            // komutları çalıştır
            setTimeout(script, 250);
        }
    }
}
