var TMZScriptLoader = {};
(function() {
  //only for global vars
  this.defined = function(x) { return eval("typeof " + x + " != 'undefined'"); }

  this.has_tag = function(tag, attr, url) {
    var tags = document.getElementsByTagName(tag);
    for (i = 0; i<tags.length; i++) {
      if (typeof tags[i].attributes[attr] != "undefined") {
        if (tags[i].attributes[attr].textContent == url) {
          return true; } } }
    return false;
  }
  
  this.load_css = function(url, props) {
    var e = document.createElement('link');
    e.setAttribute('rel','stylesheet');
    e.setAttribute('type','text/css');
    e.setAttribute('media','screen,projection');
    e.setAttribute('href', url);

    return this.load_tag(e, props);
  }

  this.load_js = function(url, props) {
    var e = document.createElement('script');
    e.setAttribute('type','text/javascript');
    e.setAttribute('src', url);
    
    return this.load_tag(e, props);
  }

  this.load_tag = function(e, props) { /* onload, place, remove, look_for */
    if (typeof props == 'undefined') props = {}; 
    props.place = props.place ? props.place : document.getElementsByTagName('head')[0];
    var loaded = false;
    
    if (!this.defined(props.look_for)) {
      e.onload = e.onreadystatechange = function() {
        var rs = this.readyState;
        if (rs && rs != 'complete' && rs != 'loaded') return;
        if(typeof props.onload == 'function') props.onload();
        if (props.remove) props.place.removeChild(this);
      }
      props.place.appendChild(e);
    }
    else
      if(typeof props.onload == 'function') props.onload();

    return e;
  }
}).apply(TMZScriptLoader);
  
var TMZRadioAffiliate = {};

(function() {
  affiliateSWFUrl = 'http://tmz.vo.llnwd.net/o28/radioaffiliate/radio_affiliate_module.swf';
  id = 0;
    
  this.embed = function(argsObject) {
    var domID = 'tmz-radio-affiliate-module-'+id;
    document.write('<div class="tmz-radio-affiliate-module" style="width:300px;height:375px" id="'+domID+'"></div>');
    var self = this;
    TMZScriptLoader.load_css('http://tmz.vo.llnwd.net/o28/radioaffiliate/radio_affiliate_module.css');
    TMZScriptLoader.load_js('https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js', {look_for: 'jQuery', onload:function() {
      TMZScriptLoader.load_js('http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js', {look_for: 'swfobject', onload:function() {
        TMZScriptLoader.load_js('http://tmz.vo.llnwd.net/o28/radioaffiliate/facebox.js', { onload:function() {
          self.render(domID, argsObject);
        }});
      }});
    }});
  };
  
  this.render = function(domID, argsObject) {
    var flashvars = {
      affiliateLogo: argsObject.affiliateLogo
    };
    var params = {
      allowscriptaccess: 'always',
      wmode: 'transparent'
    };
    var attributes = {};
    
    swfobject.embedSWF(affiliateSWFUrl, domID, '300', '375', '10.0.0', null, flashvars, params, attributes);
    id++;
  }
  
  this.loadVideoPlayer = function(url) {
    var r = new RegExp(/entry_id\/([^\/]*)\//);
    var entryid = url.match(r)[1];
    var kalturaCode = '<object id="kaltura_player_1308771786" name="kaltura_player_1308771786" type="application/x-shockwave-flash" allowFullScreen="true" allowNetworking="all" allowScriptAccess="always" height="363" width="550" bgcolor="#000000" xmlns:dc="http://purl.org/dc/terms/" xmlns:media="http://search.yahoo.com/searchmonkey/media/" rel="media:video" resource="http://www.kaltura.com/index.php/kwidget/cache_st/1308771786/wid/_591531/uiconf_id/4899061/entry_id/'+entryid+'" data="http://www.kaltura.com/index.php/kwidget/cache_st/1308771786/wid/_591531/uiconf_id/4899061/entry_id/'+entryid+'"><param name="allowFullScreen" value="true" /><param name="allowNetworking" value="all" /><param name="allowScriptAccess" value="always" /><param name="bgcolor" value="#000000" /><param name="wmode" value="transparent"/><param name="flashVars" value="&" /><param name="movie" value="http://www.kaltura.com/index.php/kwidget/cache_st/1308771786/wid/_591531/uiconf_id/4899061/entry_id/'+entryid+'" /><span property="media:width" content="550"><span property="media:height" content="363"></span> <span property="media:type" content="application/x-shockwave-flash"></span> </object>';
    
    jQuery.facebox(kalturaCode);
  };
}).apply(TMZRadioAffiliate);

