/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


//Get jquery library
//var APTLivignoService;


var jQueryScriptOutputted = false;




// function onFunctionAvailable(sMethod, oCallback, oObject, bScope) {
//  if (typeof(eval(sMethod)) === 'function') {
//   bScope ? oCallback.call(oObject) : oCallback(oObject);
//   } else {
//   setTimeout(function () {
//   onFunctionAvailable(sMethod, oCallback, oObject, bScope);
//   }), 50
//   }
//   }


function initJQuery() {

    //if the jQuery object isn't available
    if (typeof(jQuery) == 'undefined') {

        if (! jQueryScriptOutputted) {
            //only output the script once..
            jQueryScriptOutputted = true;

            
            document.write("<scr" +
                "ipt type=\"text/javascript\" src=\"http://www.livigno.eu/js/lib/jquery-1.3.2.min.js\"></scr" +
                "ipt>");
        }
        setTimeout("initJQuery()", 50);
    } else {        
        
        if(jQuery.fn.jquery.substring(0,3) == '1.2' || jQuery.fn.jquery.substring(0,3) == '1.1'){
             alert("Hai una versione di JQuery inclusa non compatibile: " + jQuery.fn.jquery);
        }else{
            $telnext = jQuery.noConflict();
        }

       
// $telnext = jQuery.noConflict();
       // $(function() {
       //     initAPTLivignoService();
       // });
    }

}

initJQuery();


if (!Function.prototype.bind) { // check if native implementation available
  Function.prototype.bind = function(){
    var fn = this, args = Array.prototype.slice.call(arguments),
        object = args.shift();
    return function(){
      return fn.apply(object,
        args.concat(Array.prototype.slice.call(arguments)));
    };
  };
}



function APTLivignoService(){


  this.apiKey = "";
  this.target = "content";
  this.type = "";
  this.css = "";
  this.numberofnews = "all";
  this.lang = "it";
  this.loadAPTnews = function() {
           
          
          jQuery('head').append('<link href="' + this.css + '" rel="stylesheet" /> ');
          
              jQuery.getScript("http://www.livigno.eu/ServicesAuth.jsp?apiKey=" + this.apiKey + "&numberofnews=" +  this.numberofnews + "&type=" +  this.type + "&lg=" + this.lang, function(data){
                
                   var cont = jQuery("#"+this.target);
                   var content;
                   if(jQuery.isArray(res)){
                        jQuery.each(res,function( intIndex, objValue ){
                            content = '<div class="apt_' + this.type + '"><div class="apt_' + this.type + '_title"><a target="blank" href="' + objValue.link +  '">' + objValue.tit1 + '</a></div>';
                            content = content + '<div class="apt_' + this.type + '_subtitle">' + objValue.sottotit1 + '</div>';
                            content = content + '<div class="apt_' + this.type + '_date">' + objValue.data1 + '</div>'
                            content = content + '</div>';
                            cont.append(jQuery( content ));
                        }.bind(this));
                   }else{
                       alert(res);
                   }
           }.bind(this));
       
  }



/*variable = {
    "apiKey" : "telnext",
    "target" : "content",
    "type" : "news",
    "css" : "http://www.livigno.telnext.it/css/service/rss/default.css",
    "numberofnews" : "all",
    "loadAPTnews" : function(){
      jQuery(document).ready(function() {

          jQuery('head').append('<link href="' + variable.css + '" rel="stylesheet" /> ');

          

          jQuery.each(APTLivignoService.type, function(index, value) {
              jQuery.getScript("http://www.livigno.telnext.it/ServicesAuth.jsp?apiKey=" + variable.apiKey + "&numberofnews=" +  variable.numberofnews + "&type=" +  value, function(data){
                   var cont = jQuery("#"+variable.target);
                   var content;
                   if(jQuery.isArray(res)){
                        jQuery.each(res,function( intIndex, objValue ){
                            content = '<div class="apt_' + value + '"><a target="blank" href="' + objValue.link +  '">' + objValue.tit1 + '</a>';
                            content = content + '<div class="apt_' + value + '_subtitle">' + objValue.sottotit1 + '</div>';
                            content = content + '<div class="apt_' + value + '_date">' + objValue.data1 + '</div>'
                            content = content + '</div>';
                            cont.append(jQuery( content ));
                        });
                   }else{
                       alert(res);
                   }
           });

          });
           


    });

    }
};*/
}

