var widget = widget || {};

var loadLibrary = '';

widget.Launcher = new function() {
  function requestStylesheet( url, style_id ) {
    if ("jQuery" in window) {
      if(navigator.appName == "Microsoft Internet Explorer"){
      		this.loadjscssfile(url, "css");
      }else{
      jQuery('head').prepend(
        jQuery('<link rel="stylesheet" type="text/css"></link>')
        .attr('href', url)
        .attr('id', style_id)
      )
      }
    } else {
      document.write('<link rel="stylesheet" href="'+url+'" type="text/css" id="' + style_id + '"></link>');
    }
  }
  
  function basePath() {
    var scripts = document.getElementsByTagName("script");
    for (var i=0; i < scripts.length; i++) {
      if (scripts[i].src && scripts[i].src.match(/widget-launcher\.js(\?.*)?/)) {
        return scripts[i].src.replace(/widget-launcher\.js(\?.*)?/, '');
      }
    }
  }

  this.init = function() {
    var libraries = [
        [typeof jQuery, "jquery"], 
        [typeof jQuery != "undefined" && typeof jQuery.template, "jquery.template"],
        [false, "widget"],
        [false, "render_widget"],
        [false, "SpryEffects"]
      ];
    var scripts = document.getElementsByTagName("script");
    for (var i=0; i < scripts.length; i++) {
      if (scripts[i].src && scripts[i].src.match(/widget-launcher\.js(\?.*)?/)) {
        this.path = scripts[i].src.replace(/widget-launcher\.js(\?.*)?/, '');
        for (var i=0; i < libraries.length; i++) {
          if (libraries[i][0] == "undefined" || !libraries[i][0]) {
            var url = this.path + libraries[i][1] + ".js?v="+Math.ceil(Math.random()*10);
            if (i == libraries.length - 1) {
              loadLibrary = setTimeout(function () { widget.Launcher.requestContent(url, "widget.Launcher.loadedLibraries"); }, 1000);
              //this.requestContent(url, "widget.Launcher.loadedLibraries");
            } else {
              this.requestContent(url);
            }
          }
        }
        break;
      }
    }
  }

  this.loadedLibraries = function() {
  	clearTimeout(loadLibrary);
    if(navigator.appName != "Microsoft Internet Explorer"){
 	 	if(typeof jQuery == 'undefined' || typeof jQuery.template == 'undefined'){
      		throw("Widget requires jQuery and jQuery.template");
 	 	}
    	requestStylesheet(this.path + '../styles/widget.css?v='+Math.ceil(Math.random()*10), 'rProjects_widgetCSS');
 	}else{    
 		requestStylesheet(this.path + '../styles/widget.css?v='+Math.ceil(Math.random()*10), 'rProjects_widgetCSS');
 	}
    widget.buildWidget(RPROJECTS_ID);
  }
};

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", filename);
 } else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }

 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref);
}

widget.Launcher.requestContent = function( url, callback ) {
  // inserting via DOM fails in Safari 2.0, so brute force approach
  if ("jQuery" in window) {
  	jQuery.getScript(url,callback);
  } else {
  	onLoadStr = (typeof callback == "undefined") ? "" : 'onload="' + callback + '()" ';
    if (navigator.appName == "Microsoft Internet Explorer"){
  		if(typeof callback != "undefined"){
  			document.write('<script type="text/javascript">window.onload='+callback+'();</script>');
  		}
  	}
  	document.write('<script ' + onLoadStr + ' type="text/javascript" src="'+url+'"></script>');
  }
}

widget.Launcher.loadData = function(data, textStatus){
	$("#rProjects_widget").html(data);
}

widget.Launcher.init();
