/************************
  Define PushMsg class
  
  Version: beta 0.2
  Last Modify: 2006-11-24
  Change Log:
    - Remove variables: "this.base", "this.ref"
  
 ************************/
var gXmlHttp;

function clsPutImg(_obj)
{
  this.oBound = _obj;
  this.currentIndex = 0;
  this.imageURI = '';

  this.srvURL = 'http://ajax.takol.tw/public/HeaderImage.php';


  /*
     function alertError
  */
  this.alertError = function(_err)
  {
    switch (_err)
    {
      case 201:
        alert('PutImage Error:\r\nYou can only bind "PutImage" class onto a <IMG> element!');
        break;
      default:
        alert('PutImage Error\r\n(unknown)');
    }
  }

  /*
     function init
  */
  this.init = function()
  {
    if (this.oBound.nodeName != 'IMG')
      this.alertError(201);
    this.imageURI = this.oBound.src;
  }
  
  /*
     function goNext
  */
  this.go = function(_request)
  {
    if (!this.srvURL) return false;
    if (!_request) _request = 'next';
    var _xmlhttp = this.createXMLHttp();
    var _date = new Date();
    _xmlhttp.open('GET', this.srvURL+'?reference='+this.oBound.id+'&request='+_request+'&currentIndex='+this.currentIndex+'&rand='+_date.getMilliseconds(), true);
    _xmlhttp.onreadystatechange = function()
    {
    	if (_xmlhttp.readyState== 4)
      {
    		if (_xmlhttp.status == 200)
        {
          //alert(_xmlhttp.responseText);
    			var _ref = _xmlhttp.responseXML.getElementsByTagName('Reference')[0].firstChild.nodeValue;
    			var _idx = _xmlhttp.responseXML.getElementsByTagName('Index')[0].firstChild.nodeValue;
    			var _uri = _xmlhttp.responseXML.getElementsByTagName('URI')[0].firstChild.nodeValue;
          document.getElementById(_ref).PutImg.update(_idx, _uri);
        }
      }
    }
    _xmlhttp.send(null);
  }


  /*
     function update
  */
  this.update = function(_idx, _uri)
  {
    if (_uri == '') return false;
    this.currentIndex = _idx;
    this.imageURI = _uri;
    this.oBound.src = _uri;
  }


  /*
     function setServer
  */
  this.setServer = function(_url)
  {
    this.srvURL = _url;
  }
  
  
  /*
     function createXMLHttp
  */
  this.createXMLHttp = function()
  {
    // Define XMLHttpRequest object
    _xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
     try {
      _xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       _xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       _xmlhttp = false;
      }
     }
    @end @*/
    if (!_xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
    		_xmlhttp = new XMLHttpRequest();
    	} catch (e) {
    		_xmlhttp=false;
    	}
    }
    if (!_xmlhttp && window.createRequest) {
    	try {
    		_xmlhttp = window.createRequest();
    	} catch (e) {
    		_xmlhttp=false;
    	}
    }
    
    return _xmlhttp;
  }

  this.init();
  return true;
}


function bind_PutImg_to_object(_obj)
{
  if (!_obj) return false;
  
  if (!_obj.PutImg)
    return _obj.PutImg = new clsPutImg(_obj);
  else
    return true;
}
