

function tgraphAppreciateButton(){this.loaded=false;};

var tsgraph= {
		protocol:"http://",
		appreciateButtons:[],
		socialOrigin:'http://dev.lush-artists.com',
		initialise:function()
		{
			onmsg = function(e){tsgraph.handleIframeCommand(e)};
			if (typeof window.addEventListener != 'undefined') { 
			  window.addEventListener('message', onmsg, false); 
			} else if (typeof window.attachEvent != 'undefined') { 
			  window.attachEvent('onmessage', onmsg); 
			}
			this.socialOrigin='http://dev.artist-in.me';	
		},
		requestData:function()
		{
			
		},
		findElements:function()
		{
			var appreciateButtons  =document.getElementsByTagName("taroblyAppreciate");
			
			for(var a=0;a<appreciateButtons.length;a++)
			{
				this.renderAppreciateButton(appreciateButtons.item(a));
			}

		},
		renderAppreciateButton:function(element)
		{
			var href = element.getAttribute('href');
			
			if((href=='')||(href==null))
			{
				href=document.location;
			}
			
			var socialsite = element.getAttribute('socialsite');
			if((href!='')&&(socialsite!=''))
			{
				//try to appreciateCollect
				var aprid=this.appreciateButtons.length;
				this.appreciateButtons[aprid]= new tgraphAppreciateButton();
				this.appreciateButtons[aprid].aprid=aprid;
				var apr = this.appreciateButtons[aprid];
				apr.element = element;
				apr.socialsite=socialsite;
				apr.href=href;				
				
				
				apr.pn = element.parentNode;
				
				var ifr = document.createElement("iframe");
				
				ifr.style.width="180px";
				ifr.style.height="20px";
				ifr.style.border="0px";
				
				ifr.setAttribute("id","tarobly_appreciateButton_"+apr.aprid);
				ifr.onload=function(){tsgraph.olTrigger(this)};
				
				
				ifr.src=this.urlBuilder(apr.socialsite,'getAppreciationButton',{href:''+href,felement:ifr.id});
				apr.ifr=ifr;
				apr.pn.insertBefore(ifr,element);
				element.style.display="none";
				element.setAttribute("loaded",'yes');
			}
		},
		urlBuilder:function(so,mthd,properties)
		{
			var query = [];
			for(var prop in properties)
			{
				if(typeof properties[prop]!='object')
				{
					
					query[query.length]=prop+"="+encodeURIComponent(properties[prop]);
				}
				else
				{
					
				}
			}
			query= query.join("&");
			return this.protocol+so+'/socialconnector/'+mthd+'?'+query;	
		},
		handleIframeCommand:function(ev)
		{
			
			if(ev.origin!=this.socialOrigin)
			{
					
				return false;
			}
			var data = ev.data;
			

			if(data.command=='resize')
			{
				this._(data.iframeid).style.width=data.width+"px";
				this._(data.iframeid).style.height=data.height+"px";

			}
			
		},
		_:function(id){
			return document.getElementById(id);
		},
		inspectIframesForChanges:function()
		{
			var elem = '';
			var apr='';
			
			for(var ab in this.appreciateButtons)
			{
				apr = this.appreciateButtons[ab]
				console.log('inspecting iframe',apr.aprid);
				if(!apr.loaded)
				{
					console.log('iframe not loaded',ab);
					continue;
				}
				elem = apr.ifr;
				
				var parts = elem.src.split("#");
				console.log('url is ',elem.src);
				if(typeof parts[1]!='undefined')
				{
					if(apr.lastIframeCommand!=parts[1])
					{
						this.handleIframeCommand(apr,elem,parts[1]);
						apr.lastIframeCommand=parts[1];
					}
					
				}
			}
			
		},
		olTrigger:function(elem)
		{
			var id = elem.getAttribute('id');
			var idparts = id.split("_");
			
			
			
			
			
			if(idparts[0]=='tarobly')
			{
				if(idparts[1]=='appreciateButton')
				{
					//do the appreciatebutton onload thing...
					
					this.appreciateButtons[idparts[2]].loaded=true;
					
				}
			}
		}
};

tsgraph.initialise();
tsgraph.findElements();

