﻿
function $servidorPublicidade() {}

$servidorPublicidade.prototype.googleDocURL = "http://spreadsheets.google.com/ccc?key=tM47zcs-wAD9VLEXURcD8mw"
$servidorPublicidade.prototype.yahooApiURL = "http://query.yahooapis.com/v1/public/yql?q=";
$servidorPublicidade.prototype.googleID = "tM47zcs-wAD9VLEXURcD8mw/od6"


$servidorPublicidade.prototype.urlList = function(pubKey) {
    var urlGoogle = "http://spreadsheets.google.com/feeds/list/" + this.googleID + "/public/basic?alt=rss&sq=(activo=1%20and%20pubkey=" + pubKey + ")";
    //return (this.yahooApiURL + encodeURIComponent("select description from rss where url='" + urlGoogle + "'") + "&format=json&callback=?")
    return (this.yahooApiURL + encodeURIComponent("select description from rss where url='" + urlGoogle + "'") + "&format=json&diagnostics=false&callback=?")
 };

 $servidorPublicidade.prototype.urlCells = function() {
     var urlGoogle = "http://spreadsheets.google.com/feeds/cells/" + this.googleID + "/public/basic?alt=rss";
     return (this.yahooApiURL + encodeURIComponent("select description from rss where url='" + urlGoogle + "'") + "&format=json&diagnostics=false&callback=?")
 };


 $servidorPublicidade.prototype.SetHTML = function(pubKey, idDiv) {
     function f(data) {
         var n = data.query.count;
         var linha;
         if (n == 0) return
         if (n == 1) {
             linha = data.query.results.item.description;
         } else {
             var i = Math.ceil(n  * Math.random());
             linha = data.query.results.item[i-1].description;
         }
         linha = linha.slice(17);
         $(idDiv).html(linha);
     }

     $.getJSON(this.urlList(pubKey), f);
 }

    //var $pub = new $servidorPublicidade();
    
   
    
    