/* depends on YUI AJAX functionality */
var Piczo = {
	WebClient: function(host, format, version) {
        var webHost = null;
        var webFormat=null;
        var formatVersion = null;
        var deltaUrl = null;
        var deleteUrl = null;
        var logVMPUrl = null;
        var copyUrl = null;
        if (!host) {
            throw "apiHost is required";
        }
        this.webHost = host;
        if (format) {
            this.webFormat = format;
        } else {
            this.webFormat = "js";
        }
        if (version) {
            this.formatVersion = version;
        } else {
            this.formatVersion = 1;
        }
        this.deltaUrl = this.webHost + "update/"+this.webFormat+"-"+this.formatVersion;
        this.deleteUrl = this.webHost + "delete/"+this.webFormat+"-"+this.formatVersion;
        this.copyUrl = this.webHost + "zone/consume";
        this.logVMPUrl = this.webHost + "go/registerunder13entry";
        this.createUrl = this.webHost + "create/"+this.webFormat+"-"+this.formatVersion;
        
        this.sendDelta = function(compOrAssetId, deltaObj, callback) {
            var assetId = compOrAssetId;
            if (typeof compOrAssetId == "object") {
                assetId = compOrAssetId.assetId;
            }
            //if this is a cross domain request, open an iframe with the helper content on the target domain
            if (this.webHost.indexOf(location.hostname) == -1) {
                var webIframe = Piczo.IframeFactory.getIframe(this);
                YAHOO.util.Event.addListener(webIframe, "load",  function() {
                    //rerun this same method from the target iframe, so we'll end up in the else clause below
                    this.contentWindow.webClient = new this.contentWindow.Piczo.WebClient(this.webHost, this.webFormat, this.formatVersion);
                    this.contentWindow.webClient.sendDelta(assetId, deltaObj, callback);
                }, webIframe);
                webIframe.src= this.webHost+"ajaxhelper";
            } else {
                //this is running in the context of the iframe already
                var params="pz_id="+assetId;
                for (elem in deltaObj) {
                    params+="&"+encodeURIComponent(elem)+"="+encodeURIComponent(deltaObj[elem]);
                }
               var txn = YAHOO.util.Connect.asyncRequest("POST", this.deltaUrl, new Piczo.Callback(callback) , params);
            }
        };
        this.sendCreate = function(assetTypeId, createObj, callback) {
            //if this is a cross domain request, open an iframe with the helper content on the target domain
            if (this.webHost.indexOf(location.hostname) == -1) {
                var webIframe = Piczo.IframeFactory.getIframe(this);
                YAHOO.util.Event.addListener(webIframe, "load",  function() {
                    //rerun this same method from the target iframe, so we'll end up in the else clause below
                    this.contentWindow.webClient = new this.contentWindow.Piczo.WebClient(this.webHost, this.webFormat, this.formatVersion);
                    this.contentWindow.webClient.sendCreate(assetTypeId, createObj, callback);
                }, webIframe);
                webIframe.src= this.webHost+"ajaxhelper";
            } else {
                //this is running in the context of the iframe already
                var params = "/pz_Asset/pz_assetTypeId="+assetTypeId;
                for (elem in createObj) {
                    params+="&"+encodeURIComponent(elem)+"="+encodeURIComponent(createObj[elem]);
                }
                var txn = YAHOO.util.Connect.asyncRequest("POST", this.createUrl, new Piczo.Callback(callback) , params);
               
            }
        
        };
        this.sendDelete = function(assetIds,callback) {
            //if this is a cross domain request, open an iframe with the helper content on the target domain
            if (this.webHost.indexOf(location.hostname) == -1) {
                var webIframe = Piczo.IframeFactory.getIframe(this);
                YAHOO.util.Event.addListener(webIframe, "load",  function() {
                    //rerun this same method from the target iframe, so we'll end up in the else clause below
                    this.contentWindow.webClient = new this.contentWindow.Piczo.WebClient(this.webHost, this.webFormat, this.formatVersion);
                    this.contentWindow.webClient.sendDelete(assetIds, callback);
                }, webIframe);
                webIframe.src= this.webHost+"ajaxhelper";
            } else {
                //this is running in the context of the iframe already
                var paramArray = [];
                for (var i = 0; i < assetIds.length; i++) {
                    paramArray.push("["+i+"]//pz_id="+assetIds[i]);
                }
                var params = paramArray.join("&");
               var txn = YAHOO.util.Connect.asyncRequest("POST", this.deleteUrl, new Piczo.Callback(callback) , params);
            }
        };
        this.copyAsset = function(assetId, paramObj, callback) {
            if (this.webHost.indexOf(location.hostname) == -1) {
                var webIframe = Piczo.IframeFactory.getIframe(this);
                YAHOO.util.Event.addListener(webIframe, "load",  function() {
                    //rerun this same method from the target iframe, so we'll end up in the else clause below
                    this.contentWindow.webClient = new this.contentWindow.Piczo.WebClient(this.webHost, this.webFormat, this.formatVersion);
                    this.contentWindow.webClient.copyAsset(assetId, paramObj, callback);
                }, webIframe);
                webIframe.src= this.webHost+"ajaxhelper";
            } else {
                //this is running in the context of the iframe already
                var params="assetId="+assetId;
                for (elem in paramObj) {
                    params+="&"+encodeURIComponent(elem)+"="+encodeURIComponent(paramObj[elem]);
                }
               var txn = YAHOO.util.Connect.asyncRequest("POST", this.copyUrl, new Piczo.Callback(callback) , params);
            }
            
        };
        
        this.logPicziVmpView = function(paramObj) {
        	    if (this.webHost.indexOf(location.hostname) == -1) {
                var webIframe = Piczo.IframeFactory.getIframe(this);
                YAHOO.util.Event.addListener(webIframe, "load",  function() {
                    //rerun this same method from the target iframe, so we'll end up in the else clause below
                    this.contentWindow.webClient = new this.contentWindow.Piczo.WebClient(this.webHost, this.webFormat, this.formatVersion);
                    this.contentWindow.webClient.logPicziVmpView( paramObj);
                }, webIframe);
                webIframe.src= this.webHost+"go/public?view=/jsp/ajax/ajaxHelper.jsp";
            } else {
                //this is running in the context of the iframe already
                var params= "";//"assetId="+assetId;
                for (elem in paramObj) {
                    params+="&"+encodeURIComponent(elem)+"="+encodeURIComponent(paramObj[elem]);
                }
               var txn = YAHOO.util.Connect.asyncRequest("POST", this.logVMPUrl, new Piczo.Donothig() , params);
            }
        };

	},
	
	Donothig: function()
	{
	},
	
	Callback: function(callback) {
	   this.callback=callback;
	   this.success = function(o) {
           if (this.callback) {
               this.callback.success(o);
           }
           if (self.parentIframe) {
               parent.Piczo.IframeFactory.releaseIframe(self.parentIframe);
           }
       };
       this.failure =  function(o) {
           if (this.callback) {
               this.callback.failure(o)
           }
           if (self.parentIframe) {
               parent.Piczo.IframeFactory.releaseIframe(self.parentIframe);
           }
       };
	   
	},
	IframeFactory: {
		iframes: [],
		maxIframeCount: 10,
		getIframe: function(client) {
		    var returnIframe = null;
			if (this.iframes.length < this.maxIframeCount) {
				var returnIframe = document.createElement("iframe");
				document.body.appendChild(returnIframe);
				returnIframe.style.display="none";
				this.iframes.push(returnIframe);
				returnIframe.busy = true;
				
			} else {
				var i=0;
				var returnIframe = this.iframes[0];
				while (returnIframe.busy) {
					i++;
					if (i == maxIframeCount) {
						alert("ran out of iframes");
						return;
					}
					returnIframe = this.iframes[i];
				}
				
			}
			if (returnIframe != null) {
			    YAHOO.util.Event.removeListener(returnIframe, "load");
	            YAHOO.util.Event.addListener(returnIframe, "load",  function() {
	                //link  contentWindow back to iframe
	                this.contentWindow.parentIframe = this;
			    }, returnIframe);
			}
            returnIframe.deltaUrl = client.deltaUrl;
            returnIframe.copyUrl = client.copyUrl;
            returnIframe.webHost = client.webHost;
            returnIframe.webFormat = client.webFormat;
            returnIframe.logVMPUrl= client.returnIframe;
            returnIframe.formatVersion = client.formatVersion;
			
			return returnIframe;
			 
		},
		releaseIframe: function(apiIframe) {
			apiIframe.busy = false;
		}
	},
	
	debugCallback: {
		success: function(responseObj) {
			alert("success");
			if (responseObj.list) {
				for (var i = 0; i < responseObj.list.length; i++) {
					alert(responseObj.list[i].pz_relatedUserId);
				}
			}
		},
		failure: function() {
			alert("failure");
		}
	}
};