function Dump(destinationDiv, widgetName)
{
  this.HTMLdump = HTMLdump;
  var _widgetName = widgetName;
  
  var _destinationDiv = destinationDiv;

  function HTMLdump(obj, className) 
  {
    var openTag = '';
    var closeTag = '';
    var stringa = '';
    
    if (isNumeric(className) == true)
    {
      openTag = closeTag = '';      
    }    
    else
    {
      openTag = (isValid(className) == false) ? '<div>' : '<div class="' + className + '">';
      closeTag = '</div>';
    }
    
    
    stringa = openTag;  
  	for (var i in obj) 
  	{
  		var className = ( i + '').toUpperCase(); 
      
      if (typeof obj[i] == "object")
        stringa += HTMLdumpJSONObject(obj[i], className);
      else
  		{
         var s = new String(obj[i]);
         var tx = s.replace(/\n/gi, "<br/>");
         stringa += '<div class="' + className + '">' + tx + "</div>";
      }
      
    }
  
  	stringa += closeTag;
  
  	return stringa;
  }

  function HTMLdumpJSONObject(obj, className)
  {
      var stringa = '';

  		if (className == 'TABLE')
  		{
  	      stringa += HTMLdumpTable(obj);
      }
  		else if (className == 'INPUT')
  		{
          stringa += HTMLdumpInput(obj);
      }
  		else if (className == 'SCRIPT')
  		{
          stringa += HTMLdumpScript(obj);
      }
  		else if (className == 'TEXTAREA')
  		{
          stringa += HTMLdumpTextArea(obj);
      }      
  		else if (className == 'AJAXCOMMAND')
  		{
          stringa += HTMLdumpAJAXCommand(obj);
      }
  		else if (className == 'CHART')
  		{
          stringa += HTMLdumpChart(obj);
      } 
  		else if (className == 'OBJECT')
  		{
          stringa += HTMLdumpObject(obj);
      }
  		else if (className == 'IMG')
  		{
          stringa += HTMLdumpImage(obj);
      }
  		else if (className == 'AUDIO')
  		{
          stringa += HTMLdumpAudio(obj);
      }
  		else if (className == 'TWEET')
  		{
          stringa += HTMLdumpTWEET(obj);
      }
  		else if (className == 'FBLIKE')
  		{
          stringa += HTMLdumpFBLIKE(obj);
      }
  		else if (className == 'PLUSONE')
  		{
          stringa += HTMLdumpPlusOne(obj);
      }
  		else if (className == 'LINK')
  		{
          stringa += HTMLdumpLink(obj);
      }
  		else 
  		{
          stringa += HTMLdump(obj, className);
      }
      
      return stringa;
        
  }

  function HTMLdumpTWEET(obj)
  {
    var s = document.createElement('SCRIPT');
    var c = document.getElementsByTagName('script')[0];
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'http://platform.twitter.com/widgets.js';
    c.parentNode.insertBefore(s, c);  

    return HTMLdumpLink(obj);  
  }
  
  function HTMLdumpFBLIKE(obj)
  {
    FB.init({ status : true, cookie : true, xfbml : true });    
    
    var stringa = '<fb:like ';
    
    for (var attr in obj)
    {
        stringa += attr + '="' + obj[attr] + '" ';
    }
    
    stringa += '/>'; 
    
    return stringa;
  
  }

  function HTMLdumpPlusOne(obj)
  {
    
    var stringa = '<g:plusone ';
    
    for (var attr in obj)
    {
        stringa += attr + '="' + obj[attr] + '" ';
    }
    
    stringa += '/>'; 
    
    return stringa;
  
  }

  function HTMLdumpScript(obj)
  {
    var stringa = '<script ';
    
    for (var attr in obj)
    {
        stringa += attr + '="' + obj[attr] + '" ';
    }
    
    stringa += '></script>'; 
    
    return stringa;
  
  }

  function HTMLdumpLink(obj)
  {
    var stringa = '<a ';
    
    for (var attr in obj)
        if (attr != 'Content')
          stringa += attr + '="' + obj[attr] + '" ';

    
    stringa += '>';  
        
    var cn = (typeof obj.Content == 'object' && isValid(obj.Content.img) == true ) ? HTMLdumpImage(obj.Content.img) : obj.Content;
    stringa += cn;
          
    stringa += '</a>';   
    
    return stringa;
  
  }

  
  function HTMLdumpImage(obj)
  {
    var stringa = '<img ';
    
    for (var attr in obj)
    {
        stringa += attr + '="' + obj[attr] + '" ';
    }
    
    stringa += '>'; 
    
    return stringa;
  
  }

  function HTMLdumpAudio(obj)
  {
    var stringa = '<audio ';
    
    for (var attr in obj)
    {
        stringa += attr + '="' + obj[attr] + '" ';
    }
    
    stringa += '>';
    stringa +='<a target=\"_blank\" href=\"' + obj.SRC + '\">';
    stringa +='<img border=\"0\" src=\"' + Session_SkinPath + 'PlayButton.png">';
    stringa +='</a>'; 
    stringa += '</audio>';
            
    return stringa;  
  }

  function HTMLdumpObject(obj)
  {
    var stringa = '<object style="';
    
    /*
    var msg = 'clientWidth:' + _destinationDiv.clientWidth + ', clientHeight:' + _destinationDiv.clientHeight;
    msg += 'offsetWidth:' + _destinationDiv.offsetWidth + ', offsetHeight:' + _destinationDiv.offsetHeight;
    alert(msg);
    */
    
    if( obj.style != 'undefined')
    {
      for (var st in obj.style)
        stringa +=  st + ':\'' + obj.style[st] + '\';';      
    }

    stringa += '" ';

    if(obj.attributes != 'undefined')
    {                  
      for (var at in obj.attributes)
      {
        stringa +=  at + '="' + obj.attributes[at] + '" ';
      }
    }

    stringa += '>';
    

    if (obj.params != 'undefined')
    {
      for (var i=0;i<obj.params.length;i++)
      {
        stringa += '<param name="' + obj.params[i].name + '" value="' + obj.params[i].value + '" >\r';
      }
    }
    
    if (obj.embed != 'undefined')
    {
      stringa += '<embed ';
      for (var em in obj.embed)
      {
        stringa +=  em + '="' + obj.embed[em] + '" ';
      }
      
      var w = _destinationDiv.offsetWidth * 0.8;
      var h = w * 3/4;
      
      if (w != 0 && h != 0)
      {
        stringa +=  'width = "' + w + 'px" ';
        stringa +=  'height ="' + h + 'px" ';            
      }
            
      stringa += ' >';
    }    
    
    stringa += '</object>';

    return stringa;
  
  }
  
  function getSafeSize()
  {
    var size = getObjectSize(_destinationDiv);
    var windowSize = getWindowSize();
    
    var safeWidth = (size.width > 120) ? size.width : (windowSize.width * 0.95).toFixed(0);
    var safeHeight = (size.height > 120) ? size.height : (windowSize.height * 0.95).toFixed(0);
  
    size.width = safeWidth;
    size.height = safeHeight;
    
    return size;  
  }
    
  function HTMLdumpChart(obj)
  {
    var safeSize = getSafeSize();
    
    _destinationDiv.style.width = safeSize.width + 'px';
    _destinationDiv.style.height = safeSize.height + 'px';
    
    var stringa ='<object type=\"image/svg+xml\" ';
    stringa +='data="' + obj.URL + '&name=Bubbles&Width=' + safeSize.width + '&Height=' + safeSize.height + '"';
    stringa +=' name="Bubbles" width="' + safeSize.width + '" height="' + safeSize.height + '" >';

    return stringa;
  }
    
  function HTMLdumpTextArea(obj)
  {
    
    var stringa = '<TEXTAREA ';
    
    for (var att in obj)
    {
      if (att != 'label')
        stringa += att + ' = \"' + obj[att] + '\"'
    }
    
    var controllerName = _widgetName + 'List';
      stringa += ' onchange=\"' + controllerName + '.updateFormProperty(\'' + obj.id + '\', value)\" ';

    stringa += ' >';
    
    stringa += obj.value;
    
    stringa += '</TEXTAREA>';

    if (isValid(obj.label) == true)
    {
        var label = '<label for=\"' + obj.id + '\">' + obj.label + '</label>';
        stringa = label + stringa;
    }
    
    addFormPropertyToController(obj);
        
    return stringa;
  }

  function HTMLdumpInput(obj)
  {
    //var stringa = '<INPUT value=\"' + obj.Text + '\" type=\"' + obj.Type + '\" onclick=\"' + obj.Command + '\" >';
        
    var stringa = '<INPUT ';
    
    for (var att in obj)
    {
      if (att != 'label')
        stringa += att + ' = \"' + obj[att] + '\"'
    }

    var controllerName = _widgetName + 'List';

    if (obj.type == 'submit')
    {
      stringa += ' onclick=\"' + controllerName + '.submit()\" ';
    }
    else
    {
      addFormPropertyToController(obj)
      stringa += ' onchange=\"' + controllerName + '.updateFormProperty(\'' + obj.id + '\', value)\" ';
    }
        
    stringa += ' >';

    if (isValid(obj.label) == true && obj.type != 'submit')
    {
        var label = '<label for=\"' + obj.id + '\">' + obj.label + '</label>';
        stringa = label + stringa;
    }

    return stringa;
  }

  function addFormPropertyToController(obj)
  {
    var controller = document.getElementById(_widgetName + 'List');      
    //controller.addFormProperty(obj.id, obj.type, obj.value);      
    var controllerName = _widgetName + 'List';
    eval(controllerName + '.addFormProperty(\'' + obj.id + '\', \'' + obj.type + '\', \'' + obj.value + '\')');
  }

  function HTMLdumpAJAXCommand(obj)
  {
    var stringa = '<DIV class=\"AJAXCommand\" onclick=\"' + _widgetName + 'List.AJAXCommand(\'' + obj.Key + '\', \'' + obj.Value + '\')\" >';
    stringa += (typeof obj.Content == 'object' && isValid(obj.Content.img) == true ) ? HTMLdumpImage(obj.Content.img) : obj.Content;
    stringa += '</DIV>';
    return stringa;
  }

  function HTMLdumpFunction(obj)
  {
      alert(obj);
  }


  function HTMLdumpTable(obj) 
  {
  	var tableStruct = new Array();
    
    var stringa = '<table>';
    var columnFormats = new Array();
    
    var header = '';
    var column = 0;
    for (var hc in obj[0])
    {
       var fmt = getCellType(obj[0][hc]);

       columnFormats.push(fmt);

       var cellFormat = ' class="' + columnFormats[column] + 'Header"';
       header += '<th' + cellFormat + '>' + hc + '</th>'

       column++;
    }
    
    header = '<tr>' + header + '</tr>'
    stringa += header;

    var maxColumn = column - 1;    
    var lightCell = false;
    var lightString = '';
    
    for (var i=0;i<obj.length;i++)
    {
      var rowStruct = new Array();
            
      stringa += '<tr>';
      
      column = 0;
      for (var cell in obj[i])
      {
        var cellStruct = {};
        var cellFormat = '';

        var val = formatCell(i,column,obj[i][cell], tableStruct, maxColumn);
        
        if (val.isLight == true)
           lightString = 'Light';
        else
           lightString = '';   

        cellFormat = ' class="' + columnFormats[column] + 'Cell' + lightString + '"';        
        stringa += '<td' + cellFormat + '>' + val.value + '</td>';
        
        cellStruct.value = (typeof obj[i][cell] == "object") ? HTMLdump(obj[i][cell]) : obj[i][cell];
        cellStruct.isLight = val.isLight;
        rowStruct.push(cellStruct);
               
        column++;
      }
      
      tableStruct.push(rowStruct);
      
      stringa += '</tr>';
    }
  
  	stringa += '</table>';
  	
  	return stringa;
  }  

  function formatCell(row,column,obj, tableStruct, maxColumn)
  {
    var retOb = {};
    var isLight = false;
    var value = '';
    
    if (row == 0 )
    {
      isLight = false;
      value = (typeof obj == "object") ? HTMLdump(obj) : obj; 
    }
    else if (row > 0 && tableStruct[row-1][column].value == obj && column != maxColumn)
    {
        isLight = tableStruct[row-1][column].isLight;
    }
    else
    {
      isLight = !tableStruct[row-1][column].isLight;
      value = (typeof obj == "object") ? HTMLdump(obj) : obj; 
    }
    
    retOb.isLight = isLight;
    retOb.value = value;
    
    return retOb;  
  }

  function getCellType(obj)
  {
    var retFormat = '';
       
   if (typeof obj == "object")
      retFormat = ''; 
   else
   {
     if (isNumeric(obj) == true)
     {
        retFormat = 'Numeric';
     }
     else
        retFormat = 'String';
   }
    
    return retFormat;
  }

  function isNumeric(value)
  {
    var bRet = false;
    var expression = new String(value);
    if (expression == '' || !expression.match(/^[-]?\d*\.?\d*$/)) 
        bRet = false;
    else
      bRet = true;
    
     return bRet;
  }

  function getObjectSize(obj) 
  {
    var width = obj.offsetWidth;
    var height = obj.offsetHeight;
    
    var retSize = 
    {
      'width':width,
      'height':height
    }
    
    return retSize;
  }  

  function getWindowSize() 
  {
    
    var width = 0;
    var height = 0;
    
    if( typeof( window.innerWidth ) == 'number' ) 
    {
      //Non-IE
      width = window.innerWidth;
      height = window.innerHeight;
    } 
     else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
     {
      //IE 6+ in 'standards compliant mode'
      width = document.documentElement.clientWidth;
      height = document.documentElement.clientHeight;
    } 
     else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
     {
      //IE 4 compatible
      width = document.body.clientWidth;
      height = document.body.clientHeight;
    }
    
    var retSize =
    {
      'width':width,
      'height':height
    }
    
    return retSize;    
  }  
}

