/*
Author/Owner: 	Daniel Peel.
Contact: 		mintpants@tiscali.co.uk.
Details:		Air.Ajax Object in the Air namepsace.
Requires:       Base.js
Created: 		12.06.09
Updated: 	    18.06.09    -   Pulled the waiting notifications out into a seperate class to pass as an object to Ajax(new Air.AjaxLoading())
*/

Air.Ajax = function(args) {
    //********************  create AjaxObject  *********************//
    this.jaxObj = false;

    //firefox, netscape or safari
    if (window.XMLHttpRequest) {
        this.jaxObj = new XMLHttpRequest();
    }
    //interent explorer
    else if (window.ActiveXObject) {
        this.jaxObj = new ActiveXObject("Microsoft.XMLHTTP");

        if (!IsSet(this.jaxObj)) {
            this.jaxObj = new ActiveXObject("Msxml2.XMLHTTP");
        }
    }

    if (!IsSet(this.jaxObj))
        alert("Your Browser is not supported by this website please upgrade your browser.");
    //*******************  end Air.AjaxObject  *************************//

    if (args) {
        this.loadingAnimation = args.LoadingAnimation;
    }
    else {
        this.loadingAnimation = false;
    }
    this.ResponseText = "";
    this.requestArray = new Array();
}

//Static
Air.Ajax.OnQueue = false;
//Static
Air.Ajax.WarnOnQueue = function() {

    var links = peppy.query('a');

    for (i in links) {
        links[i].onclick = function() {
        if (Air.Ajax.OnQueue == true) {
                //returning false to the onclick event cancells the link
               return confirm("Items are still saving if you continue to navigate away from the page, your work will be lost! For christs sake dont do it!!!");               
            }
        }
    }
}

Air.Ajax.prototype.CallBack = function() {
    this.ResponseText = null;
    this.jaxObj = null;
}
//**************************************************************//
//					  GET data Air.Ajax request                 //

//requires the response div object not just a reference
Air.Ajax.prototype.Get = function(args) {

    Air.Ajax.OnQueue = true;
    if (this.jaxObj) {
        this.jaxObj.open("GET", args.RequestURL);

        var self = this;

        this.jaxObj.onreadystatechange = function() {

            //has the data been retrieved
            if (self.jaxObj.readyState == 4 && self.jaxObj.status == 200) {

                Air.Ajax.OnQueue = false;

                if (self.loadingAnimation != false) {
                    self.loadingAnimation.LoadingRemove();
                }

                self.ResponseText = self.jaxObj.responseText;
                self.CallBack();
                self.jaxObj = null;
            }
            else {

                if (self.loadingAnimation != false) {
                    self.loadingAnimation.Loading();
                }
            }
        }

        this.jaxObj.send(null);
    }
}
//***************************************************************//
//					   POST data Air.Ajax request                //

//requires the response div object not just a reference
Air.Ajax.prototype.Post = function(args) {

    Air.Ajax.OnQueue = true;

    if (this.jaxObj) {
        this.jaxObj.open("POST", args.RequestURL);
        this.jaxObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        this.jaxObj.setRequestHeader("Content-length", args.POSTData.length);
        this.jaxObj.setRequestHeader("Connection", "close");

        var self = this;

        this.jaxObj.onreadystatechange = function() {

            //has the data been retrieved
            if (self.jaxObj.readyState == 4 && self.jaxObj.status == 200) {
                Air.Ajax.OnQueue = false;

                if (self.loadingAnimation != false) {
                    self.loadingAnimation.LoadingRemove();
                }                
               
                self.ResponseText = self.jaxObj.responseText;
                self.CallBack();
                self.jaxObj = null;
            }
            else {
                if (self.loadingAnimation != false) {
                    self.loadingAnimation.Loading();
                }
            }
        }

        this.jaxObj.send(args.POSTData);
    }
}

//Status Feed ************************************************************
Air.AjaxStatusFeed = function(args) {
    this.message = args.Message;
    this.parentNode = args.ParentNode;
    this.id = args.ID;
}

Air.AjaxStatusFeed.prototype.LoadingRemove = function() {
    ById(this.parentNode).removeChild(ById(this.id));

}
Air.AjaxStatusFeed.prototype.Loading = function() {

    //Only load the loader once
    if (ById(this.id) != null) {
        return;
    }

    var loadingDiv = document.createElement("div");
    loadingDiv.setAttribute("id", this.id);

    loadingImg = document.createElement("img");
    loadingImg.setAttribute("src", "img/ajax-status-feed-loader.gif");
    loadingImg.style.float = "right";
    loadingImg.style.marginBottom = "-4px";
    loadingImg.style.marginRight = "5px"
    loadingDiv.appendChild(loadingImg);


    loadingTxt = document.createTextNode(this.message);

    //Append to wrapper
    loadingDiv.appendChild(loadingTxt);

    //Clear content
    ById(this.parentNode).innerHTML = null;
    
    //Append Child
    ById(this.parentNode).appendChild(loadingDiv);
}

//Animations *************************************************************
Air.AjaxLoading = function(args) {

    this.parentNode = args.ParentNode || "body";
    this.id = args.ID || "air-loading-content";
    this.message = args.Message || "Loading";
}

Air.AjaxLoading.prototype.LoadingRemove = function() {

    if (this.parentNode == 'body') {
        document.body.removeChild(ById(this.id));
    }
    else {
        ById(this.parentNode).removeChild(ById(this.id));
    }
}

Air.AjaxLoading.prototype.Loading = function() {
    //Only load the loader once
    if (ById(this.id) != null) {
        return;
    }

    //Create the wrapper
    var loadingDiv = document.createElement("div");
    loadingDiv.setAttribute("id", this.id);
    loadingDiv.style.position = 'absolute';
    loadingDiv.style.visibility = 'hidden';
    loadingDiv.style.top = '0px';
    loadingDiv.style.left = '0px';
    loadingDiv.style.textAlign = 'center';
    loadingDiv.style.fontSize = "12px";
    loadingDiv.style.color = '#999999';
    loadingDiv.style.backgroundColor = "transparent";

    //Create the load image
    loadingImg = document.createElement("img");
    loadingImg.setAttribute("src", "img/ajax-loader.gif");

    //Append to the wrapper
    loadingDiv.appendChild(loadingImg);

    //Create the load message    
    spacer = document.createElement("div");
    spacer.setAttribute("id", this.id + "_spacer");
    loadingDiv.appendChild(spacer);

    loadingTxt = document.createTextNode(this.message);

    //Append to wrapper
    loadingDiv.appendChild(loadingTxt);

    //Append the wrapper to the DOM so we can access its offset properties
    document.body.appendChild(loadingDiv);

    if (this.parentNode == 'body') {
        //load the inner window size
        innerWindowSize();

        loadingDiv.style.padding = "20px";

        loadingDiv.style.top = divScreenCenter(loadingDiv.offsetWidth, loadingDiv.offsetHeight).verCent + "px";
        loadingDiv.style.left = divScreenCenter(loadingDiv.offsetWidth, loadingDiv.offsetHeight).horCent + "px";

        loadingDiv.style.backgroundColor = "#ffffff";
        loadingDiv.style.border = "1px solid #000000";

    }
    else {

        var pNode = ById(this.parentNode);
        pNode.appendChild(loadingDiv);

        loadingDiv.style.position = null;
        loadingDiv.style.top = null;
        loadingDiv.style.left = null;
        loadingDiv.style.width = 'inherit';
        //Center the loadingDiv withint the parent wrapper
        loadingDiv.style.marginTop = (pNode.offsetHeight / 2) - (loadingDiv.offsetHeight / 2) + "px";
    }
    //Show the loading div
    loadingDiv.style.visibility = 'visible';
}


//// Push And Pop Queue
//Air.Ajax = function() {
//    //********************  create Air.AjaxObject  *********************//
//    this.jaxObj = false;

//    //firefox, netscape or safari
//    if (window.XMLHttpRequest) {
//        this.jaxObj = new XMLHttpRequest();
//    }
//    //interent explorer
//    else if (window.ActiveXObject) {
//        this.jaxObj = new ActiveXObject("Microsoft.XMLHTTP");

//        if (!IsSet(this.jaxObj)) {
//            this.jaxObj = new ActiveXObject("Msxml2.XMLHTTP");
//        }
//    }

//    if (!IsSet(this.jaxObj))
//        alert("Your Browser is not supported by this website please upgrade your browser.");
//    //*******************  end Air.AjaxObject  *************************//

//    this.ResponseText = "";
//    this.requestArray = new Array();
//}

//Air.Ajax.prototype.ResponseText = function() { return this.ResponseText };
//Air.Ajax.prototype.CallBack = function(response) {
//    this.ResponseText += response;
//}
//Air.Ajax.prototype.Add = function(args) {

//    var self = this;
//    var response;
//    //Get Request
//    var _Get = function(requestQueueItem) {
//        self.jaxObj.open("GET", requestQueueItem.URL);

//        self.jaxObj.onreadystatechange = function() {
//            //has the data been retrieved
//            if (self.jaxObj.readyState == 4 && self.jaxObj.status == 200) {
//                response = self.jaxObj.responseText;
//                //data retrieved
//                //recurse the closure popping off the last added
//                if (self.requestArray.length > 0)

//                    _Get(self.requestArray.pop());
//            }
//            else {

//            }
//        }

//        self.jaxObj.send(null);
//    }
//    //***********************************************************************

//    //Add to the request array
//    this.requestArray[this.requestArray.length] = { Type: args.Type, URL: args.URL, PostData: args.PostData };

//    //init the first request
//    if (this.requestArray.length > 0)
//        _Get(this.requestArray.pop()); //alert(this.ResponseText);

//    return response;
//}

////***************************************************************//
////					   POST data Air.Ajax request                //

////requires the response div object not just a reference
//Air.Ajax.prototype.Post = function(args) {

//    this.jaxObj.open("POST", args.RequestURL);
//    this.jaxObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
//    this.jaxObj.setRequestHeader("Content-length", args.POSTData.length);
//    this.jaxObj.setRequestHeader("Connection", "close");

//    var self = this;

//    this.jaxObj.onreadystatechange = function() {

//        //has the data been retrieved
//        if (self.jaxObj.readyState == 4 && self.jaxObj.status == 200) {
//            self.LoadingRemove();
//            self.ResponseText = self.jaxObj.responseText;
//            self.CallBack();
//        }
//        else {
//            self.Loading();
//        }
//    }

//    this.jaxObj.send(data);
//}


