/*

        Copyright 2006-2008 OpenAjax Alliance

        Licensed under the Apache License, Version 2.0 (the "License"); 
        you may not use this file except in compliance with the License. 
        You may obtain a copy of the License at
        
                http://www.apache.org/licenses/LICENSE-2.0

        Unless required by applicable law or agreed to in writing, software 
        distributed under the License is distributed on an "AS IS" BASIS, 
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
        See the License for the specific language governing permissions and 
        limitations under the License.
*/
OpenAjax.widget.GoogleGadgetModel = function( id, properties, views, url, frameID ) {
    if ( ! id ) {
        return;
    }

    // the WidgetModel class that we inherit from expects its hubId to be
    // set to "__<ID>"
    this.hubId = '__' + id;

    this.init( id, properties, views );
    this.url = url;
    this.frameID = frameID;
}

OpenAjax.widget.GoogleGadgetModel.prototype = new OpenAjax.widget.WidgetModel();

OpenAjax.widget.GoogleGadgetModel.prototype.getTopicInfo = function() 
{
    // Google Gadgets don't currently support pub/sub
    return null;
}

OpenAjax.widget.GoogleGadgetModel.prototype.listenForEvents = function()
{
    // Google Gadgets don't have a corresponding Widget object, so there are
    // no events to listen for.
}

// This method is used by Widget and WidgetModel classes to send events to each
// other.  For Google Gadgets, we override this method so we can intercept
// events and take the appropriate action with the Gadget.
OpenAjax.widget.GoogleGadgetModel.prototype.fireEvent = function( type, data )
{
    switch ( type ) {
        case "_propValueChange" : {
            this.updateIFrame( data );
            break;
        }
    
        // MashupMaker sends this "remove" method and expects a response on the
        // "openajax.widget.<ID>._removed" event when the widget has finished
        // running its "remove" callback routines.  Since Google Gadgets don't have
        // such callbacks, just send the response event immediately.
        case "remove" : {
            this.connHandle.publish( 'openajax.widget.' + this.gID + '._removed',
                    null );
            break;
        }
    }
}

// When a property changes, we 'notify' the Google Gadget by reloading it, with
// the updated values for the prefs.
OpenAjax.widget.GoogleGadgetModel.prototype.updateIFrame = function( data )
{
    var splitUrl = this.url.split("&")

    for ( var i = 0; i < data.length; i++ ) {
        var name = data[i].name;
        var value = data[i].value

        // XXX somewhat inefficient
        for (var j = 0; j < splitUrl.length; j++) {
          var isUP = false;
          var index = splitUrl[j].indexOf(name+"=");
          if (index != 0) {
            // up prefix sometimes is used - userprovided
            index = splitUrl[j].indexOf("up_"+name+"=");
            isUP = true;
          }

          if (index == 0) {
            var str = isUP ? "up_"+name : name;
            splitUrl[j] = str + "="+encodeURIComponent(value);
            break;
          }
        }
    }

    var url = "";
    for (var i = 0; i < splitUrl.length; i++) {
      url += splitUrl[i];
      if (i < splitUrl.length) {
        url +="&";
      }
    }

    document.getElementById( this.frameID ).setAttribute("src", url);
    this.url = url;
}

