//var host = "http://www.iplantatree.org";
//var host = "http://localhost:8088";
     
function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
    
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}

JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);  
}

JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}

function getWidget(jsonData) {     
	 var serverhost  = jsonData.Widget.Data.serverHost;
	 var remotehost  = jsonData.Widget.Data.remoteHost;
	 var userId  = jsonData.Widget.Data.userId;
	 var widgetType  = jsonData.Widget.Data.widgetType;
	 var treeCount  = jsonData.Widget.Data.treeCount;
	 var co2Saving = jsonData.Widget.Data.co2Saving - 0;
	 var lang = jsonData.Widget.Data.lang;
	 
	 var widgetContainer = document.getElementById('ipatWidget');
	 // add float
	 var widgetLink = document.createElement('a');
	 if(userId == 'all') {
	 	widgetLink.href = serverhost;
	 } else {
	 	widgetLink.href = serverhost +'/user/profile.html?userId=' +userId;
	 }
	 widgetLink.style.textDecoration = 'none';
	 widgetLink.border = '0';
	 widgetLink.target = '_blank';
	 var widgetImage = document.createElement('img');
	 widgetImage.className = 'pngtrans';
	 widgetImage.style.border = '0px;'
	 widgetImage.border = '0';
	 widgetImage.src = serverhost + '/gfx/widget' +widgetType + '_' + lang + '.png';
	 widgetImage.alt = 'Baum pflanzen auf www.iplantatree.org';
	 if(widgetType == 1){
	 	widgetImage.width = 120;
	 	widgetImage.height = 191;
	 }else if(widgetType == 2){
	 	widgetImage.width = 219;
	 	widgetImage.height = 90;
	 }
	 
	 // treeCount
	 var treeCountDiv = document.createElement('div');
	 treeCountDiv.style.position = 'absolute';
	 treeCountDiv.zIndex = '2';
	 treeCountDiv.style.fontFamily = 'Arial';
	 treeCountDiv.style.fontSize = '13px';
	 treeCountDiv.style.fontWeight = 'bold';
	 treeCountDiv.style.color = '#99BB1C';
	 treeCountDiv.innerHTML = treeCount;
	
	 if(widgetType == 1){
	 	treeCountDiv.style.textAlign = 'right';
	 	treeCountDiv.style.width = '105px';
	 	treeCountDiv.style.marginTop = '125px';
	 	treeCountDiv.style.marginRight = '15px';
	 }else if(widgetType == 2){
	 	treeCountDiv.style.textAlign = 'right';
	 	treeCountDiv.style.width = '204px';
	 	treeCountDiv.style.marginTop = '25px';
	 	treeCountDiv.style.marginRight = '15px';
	 }
	 widgetContainer.appendChild(treeCountDiv);
	 
	 // co2saving
	 var co2savingDiv = document.createElement('div');
	 co2savingDiv.style.position = 'absolute';
	 co2savingDiv.zIndex = '2';
	 co2savingDiv.style.fontFamily = 'Arial';
	 co2savingDiv.style.fontSize = '13px';
	 co2savingDiv.style.fontWeight = 'bold';
	 co2savingDiv.style.color = '#99BB1C';
	 if(co2Saving.toFixed){
	 	co2savingDiv.innerHTML = co2Saving.toFixed(6);
	 }else{
	 	co2savingDiv.innerHTML = co2Saving
	 }	
	
	 if(widgetType == 1){
	 	co2savingDiv.style.textAlign = 'right';
	 	co2savingDiv.style.width = '105px';
	 	co2savingDiv.style.marginTop = '163px';
	 	co2savingDiv.style.marginRight = '15px';
	 }else if(widgetType == 2){
	 	co2savingDiv.style.textAlign = 'right';
	 	co2savingDiv.style.width = '204px';
	 	co2savingDiv.style.marginTop = '63px';
	 	co2savingDiv.style.marginRight = '15px';
	 }
	 widgetContainer.appendChild(co2savingDiv);
	 
	 
	 widgetLink.appendChild(widgetImage);
	 widgetContainer.appendChild(widgetLink);
	 
	 //tracking GA
	 var myFrame = document.createElement('iframe');
	 myFrame.src = serverhost + '/user/widgetTracking.html?remoteHost=' +jsonData.Widget.Data.remoteHost;
	 myFrame.style.border = '0px solid #FFFFFF';
	 myFrame.width = '5';
	 myFrame.height = '5';
	 myFrame.frameBorder = '0';
	 myFrame.border = '0';
	 myFrame.scrolling = 'no';
	 widgetContainer.appendChild(myFrame);
	 
	 correctPNG();
	 bObj.removeScriptTag(); 
  }
  
  
  function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}
