
if(!SAPO.Communication||SAPO.Communication=='undefined'){SAPO.namespace('Communication');}
SAPO.Communication.Syndication=function(url,options)
{this.init(url,options);};SAPO.Communication.Syndication.prototype={init:function(url)
{this.feeds=[];if(url&&typeof(url)!='undefined'&&url!=null){var id=this.push(url,arguments[1]||{});if(id!==false&&typeof(id)!='undefined'){this.run(id);}}
this._doDebug=false;},push:function(url)
{if(!url||typeof(url)=='undefined'){if(this.exception){this.exception.log('Missed URL');}
throw"Missed URL in SAPO.Communication.Syndication";return false;}
try{var options=Object.extend({objectName:false,onComplete:false,onLoading:false,onTimeout:false,onExit:false,timeout:10,optOnComplete:false,optOnLoading:false,optOnTimeout:false,optOnExit:false,charset:'utf-8'},arguments[1]||{});this.feeds.push({});this.onStart=false;this.onEnd=false;var id=this.feeds.length-1;this.feeds[id].u=url;this.feeds[id].onComplete=options.onComplete;this.feeds[id].onLoading=options.onLoading;this.feeds[id].onTimeout=options.onTimeout;this.feeds[id].onExit=options.onExit;if(options.objectName){this.feeds[id].obj=options.objectName;}else{if(typeof(SAPO.Utility)!='undefined'&&typeof(SAPO.Utility.Crypto)!='undefined'){this.feeds[id].obj='json'+SAPO.Utility.Crypto.md5(this.feeds[id].u);}else{this.feeds[id].obj='json'+Math.round(1000000*Math.random());}}
this.feeds[id].urlJSON=false;this.feeds[id].urlImage=false;this.feeds[id].req=0;this.feeds[id].limitReq=parseInt((options.timeout*1000)/100);this.feeds[id].stoReq=false;this.feeds[id].charset=options.charset;this.feeds[id].optionsOnLoading=options.optOnLoading||false;this.feeds[id].optionsOnComplete=options.optOnComplete||false;this.feeds[id].optionsOnTimeout=options.optOnTimeout||false;this.feeds[id].optionsOnExit=options.optOnExit||false;return id;}catch(e){if(this.exception){this.exception.log(e,'push');}}
return false;},run:function(id)
{if(id!==false&&typeof(id)!='undefined'){try{if(this.feeds[id].onLoading){if(this.feeds[id].optionsOnLoading){this.feeds[id].onLoading(this.feeds[id].optionsOnLoading);}else{this.feeds[id].onLoading();}}
this._setUrl(id);this._createScriptTag(id);}catch(e){this.exception.log(e,'run (ID: '+id+')');}}},runAll:function()
{if(this.feeds.length>0){this.onStart=arguments[0]||false;if(this.onStart){this.onStart();}
var argToOnEnd=arguments[1]||false;this.iter=0;this.setInter=setInterval(function(arguments){this.run(this.iter);if(this.iter===(this.feeds.length-1)){this.onEnd=argToOnEnd;}
this.iter++;if(this.iter==this.feeds.length){clearInterval(this.setInter);}}.bindObj(this),300);}},remove:function(id)
{if(typeof(this.feeds[id])!='undefined'&&this.feeds[id]!=null){try{this.feeds[id]=null;}catch(e){this.exception.log(e,'delete (ID: '+id+')');}}},removeAll:function()
{if(this.feeds.length>0){for(var i=0;i<this.feeds.length;i++){this.remove(i);}}},destroy:function()
{for(var i in this){this[i]=null;}},_setUrl:function(id)
{var re=new RegExp("\\?(.+)");if(re.test(this.feeds[id].u)){this.feeds[id].urlJSON=this.feeds[id].u+'&jsonTag='+this.feeds[id].obj;}else{this.feeds[id].urlJSON=this.feeds[id].u+'?jsonTag='+this.feeds[id].obj;}},_createScriptTag:function(id)
{try{this.feeds[id].script=document.createElement('SCRIPT');this.feeds[id].script.type='text/javascript';this.feeds[id].script.src=this.feeds[id].urlJSON;this.feeds[id].script.charset=this.feeds[id].charset;document.getElementsByTagName('HEAD')[0].appendChild(this.feeds[id].script);this._callBack(id);}catch(e){if(this.exception){this.exception.log(e,'_createScriptTag');}}},_callBack:function(id)
{try{if(this.feeds[id].req<this.feeds[id].limitReq){if(this.feeds[id].onComplete){if(this.feeds[id].optionsOnComplete){this.feeds[id].optionsOnComplete.__id=id;this.feeds[id].onComplete(eval(this.feeds[id].obj),this.feeds[id].optionsOnComplete);this._removeScript(id);}else{this.feeds[id].onComplete(eval(this.feeds[id].obj));this._removeScript(id);}}
if(this.feeds[id].onExit){if(this.feeds[id].optionsOnExit){this.feeds[id].onExit(this.feeds[id].optionsOnExit);}else{this.feeds[id].onExit();}}
if(this.onEnd&&id==(this.feeds.length-1)){this.onEnd();}}else{throw"Time out ";}}catch(e){if(this.feeds[id].req<this.feeds[id].limitReq){if(this.feeds[id].stoReq){clearTimeout(this.feeds[id].stoReq);}
this.feeds[id].req++;this.feeds[id].stoReq=setTimeout(function(){this._callBack(id);}.bindObj(this),100);}else{if(this.exception){this.exception.log(e+' - URI: '+this.feeds[id].urlJSON,'_callBack');}
if(this.feeds[id].onTimeout){if(this.feeds[id].optionsOnTimeout){this.feeds[id].onTimeout(this.feeds[id].optionsOnTimeout);}else{this.feeds[id].onTimeout();}}
this._removeScript(id);if(this.feeds[id].onExit){if(this.feeds[id].optionsOnExit){this.feeds[id].onExit(this.feeds[id].optionsOnExit);}else{this.feeds[id].onExit();}}}}},_removeScript:function(id)
{try{if(this.feeds[id].script.parentNode&&typeof(this.feeds[id].script.parentNode)!='undefined'){this.feeds[id].script.parentNode.removeChild(this.feeds[id].script);}}catch(e){if(this.exception){this.exception.log(e,'_removeScript');}}},_debug:function(txt)
{if(this._doDebug){document.getElementById('debug').innerHTML+=txt+'<br/>';}}};