//foo
var cb_tracks = [{"track_id": 185893, "title": "Bully", "artist_name": "Lissie", "track_id": 185893}, {"track_id": 185945, "title": "Sun Body", "artist_name": "Christopher Willits", "track_id": 185945}, {"track_id": 180096, "title": "Five Little Rooms", "artist_name": "Menomena", "track_id": 180096}, {"track_id": 184982, "title": "Holler Boys", "artist_name": "Social Studies", "track_id": 184982}, {"track_id": 185906, "title": "The Hair Song", "artist_name": "BLACK MOUNTAIN", "track_id": 185906}, {"track_id": 183670, "title": "I Don't Believe You", "artist_name": "The Thermals", "track_id": 183670}, {"track_id": 178559, "title": "Johnny Utah", "artist_name": "Futurebirds", "track_id": 178559}, {"track_id": 185608, "title": "Three Sisters", "artist_name": "J. Tillman", "track_id": 185608}, {"track_id": 185477, "title": "Primitive 3D", "artist_name": "Deerhunter", "track_id": 185477}, {"track_id": 185573, "title": "For Ash", "artist_name": "Marnie Stern", "track_id": 185573}];

// Load SWFObject
function cbLoadSWFObject() {
  if(typeof deconcept == "undefined") {
    var script_node = document.createElement('script');
    script_node.type = 'text/javascript';
    script_node.src = 'http://widgets.cowbell.fm/javascripts/swfobject.js';
    document.getElementsByTagName('head')[0].appendChild(script_node);
  }
}

function getUpdate(type,pr1,pr2,swf) {
  cbLog(type)
  
  if (type == 'item') {
    cbTrackSelected(pr1);
  }
}

function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
}

function cbPlayTrack(index) {
  // Create the embed
  if (document.getElementById('cb_player_embed') == null) {
    player_node = document.createElement('div');
    player_node.id = 'cb_player_embed';
    document.getElementById('cb_player').insertBefore(player_node, document.getElementById('cb_player').childNodes[0]);
    
    var cbso = new SWFObject('http://cowbell.fm/mediaplayer.swf','cowbell_player','180','20','8');
    cbso.addParam('allowscriptaccess','always');
    cbso.addParam('allowfullscreen','true');
    cbso.addVariable('height','20');
    cbso.addVariable('width','180');
    cbso.addVariable('shuffle', 'false');
    cbso.addVariable('enablejs', 'true');
    cbso.addVariable('javascriptid','cowbell_player');
    cbso.addVariable('repeat', 'list');
    cbso.addVariable('file','http://widgets.cowbell.fm/playlist?ids%5B%5D=185893%26ids%5B%5D=185945%26ids%5B%5D=180096%26ids%5B%5D=184982%26ids%5B%5D=185906%26ids%5B%5D=183670%26ids%5B%5D=178559%26ids%5B%5D=185608%26ids%5B%5D=185477%26ids%5B%5D=185573');
    cbso.write('cb_player_embed');
  }
  
  play = function() {
    if (typeof(thisMovie('cowbell_player')) == 'undefined' || typeof(thisMovie('cowbell_player').itemData) == 'undefined') {
      cbLog('player not setup')
      setTimeout(play, 200)
      return;
    } else if (typeof thisMovie('cowbell_player').itemData(0) == 'undefined') {
      cbLog('tracks not loaded')
      setTimeout(play, 200);
      return;
    }
    
    thisMovie('cowbell_player').sendEvent('playitem', index);
    cbTrackSelected(index)
  }
  
  setTimeout(play, 100);
}

function cbTrackSelected(index) {
  var track = cb_tracks[index];
  document.getElementById('cb_play_button').style.display='none';
  document.getElementById('cb_now_playing').innerHTML = ''
    + '<a href="http://cowbell.fm/artist/' + track.artist_name + '" target="_blank" class="cb_thumbnail"><img src="' + track.thumbnail_url + '" class="" /></a>'
    + '<div class="cb_track"><a href="http://cowbell.fm/track/' + track.track_id + '" title="' + track.title + '">' + track.title + '</a></div>'
    + '<div class="cb_artist">By <a href="http://cowbell.fm/artist/' + track.artist_name + '" target="_blank">' + track.artist_name + '</a></div>'
    + '<div class="cb_artist_link"><a href="http://cowbell.fm/artist/' + track.artist_name + '" target="_blank" style="color:#fff !important;text-decoration:underline !important;">Listen to more songs by ' + track.artist_name + '</a></div>';  
}

// Load the SWFObject.js
cbLoadSWFObject();

var css = '#cowbell_widget {  width:180px !important;  overflow:hidden !important;  background:#333 !important;  color:#fff !important;  font-size:11px !important;  line-height:14px !important;  text-align:left !important;  -moz-border-radius:4px;  font-family:\'Century Gothic\',\'Lucida Grande\',Arial,sans-serif !important;  font-size:11px !important;  letter-spacing:-0.25px !important;  line-height:14px !important;  position:relative !important;  left:0px !important;  top:0px !important;}#cowbell_widget * {  padding-top:0px !important;  padding-right:0px !important;  padding-bottom:0px !important;  padding-left:0px !important;  margin-top:0px !important;  margin-right:0px !important;  margin-bottom:0px !important;  margin-left:0px !important;  border-width:0px !important;  text-align:left !important;  position:relative !important;  left:0px !important;  top:0px !important;  font-size:11px !important;  color:#fff !important;  font-family:\'Century Gothic\',\'Lucida Grande\',Arial,sans-serif !important;  line-height:14px !important;}#cowbell_widget a, #cowbell_widget a:visited {    color:#52A0D7 !important;    text-decoration:none !important;    white-space:nowrap !important;}#cowbell_widget h3 {  font-size:12px !important;  padding:2px 10px !important;}#cowbell_widget #cb_tracks {  width:100% !important;}  #cowbell_widget #cb_tracks .cb_track {    background:#FFFFFF none repeat scroll 0% 50% !important;    margin:2px !important;    padding:3px 5px 3px 3px !important;    width:168px !important;    cursor:pointer;  }  #cowbell_widget #cb_tracks .cb_track img {    float:left;    display:block;    width:26px;    height:26px;    border:1px solid #555 !important;    margin-right:3px !important;  }  #cowbell_widget #cb_tracks .cb_track .cb_artist {    color:#000 !important;    font-weight:bold !important;    width:137px !important;  }  #cowbell_widget #cb_tracks .cb_track .cb_title {    font-weight:bold !important;    height:14px !important;    overflow:hidden !important;    width:137px !important;  }#cowbell_widget #cb_player {  background:#222 !important;  height:100px !important;  width:100% !important;  position:relative !important;}  #cowbell_widget #cb_player #cb_play_button {    background:transparent url(http://cowbell.fm/images/icons/48x48/play_bg222.gif) no-repeat scroll 6px 0px !important;    height:28px !important;    left:59px !important;    padding-top:50px !important;    position:absolute !important;    top:21px !important;    width:62px !important;    cursor:pointer !important;    color:#fff !important;  }  #cowbell_widget #cb_player #cb_now_playing {    padding:5px !important;    font-size:10px !important;    width:170px !important;  }  #cowbell_widget #cb_player #cb_now_playing .cb_thumbnail {    border:1px solid #AAAAAA !important;    float:left;    height:34px;    margin-right:4px !important;    width:34px;  }  #cowbell_widget #cb_player #cb_now_playing .cb_thumbnail img {    width:100%;    height:100%;  }    #cowbell_widget #cb_player #cb_now_playing .cb_track {      height:14px !important;      overflow:hidden !important;      width:130px !important;    }    #cowbell_widget #cb_player #cb_now_playing .cb_artist {      width:130px !important;    }    #cowbell_widget #cb_player #cb_now_playing .cb_artist_link {      margin-top:12px !important;      max-height:28px !important;    }#cowbell_widget #cb_get_widget {  padding:5px !important;  text-align:center !important;  width:170px !important;}  #cowbell_widget #cb_get_widget a {    color:#fff !important;    text-decoration:underline !important;  }';
var styleNode = document.createElement('style');
styleNode.type = 'text/css';
if(styleNode.styleSheet) {
	styleNode.styleSheet.cssText = css;
} else {
	styleNode.appendChild(document.createTextNode(css));
}
document.getElementsByTagName("head")[0].appendChild(styleNode);

function cbLog(message) {
  if (typeof(console) != 'undefined') {
    console.log(message)
  } else if(document.getElementById('cb_debug_container')) {
    c=document.getElementById('cb_debug_container');
    c.innerHTML = c.innerHTML + message + "<br/>";
  }
}

// Build the widget
document.getElementById('cowbell_widget').innerHTML = '<h3>Popular on Cowbell.fm</h3><div id="cb_player">  <div id="cb_play_button" onclick="cbPlayTrack(0)">click to play</div>  <div id="cb_now_playing"></div></div><div id="cb_tracks">        <div class="cb_track" onclick="cbPlayTrack(0)">    <img alt="Default_artist_40px" src="http://cowbell.fm/images/default_artist_40px.png" />    <div class="cb_artist">Lissie</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(0); return false;" title="Bully">Bully</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(1)">    <img alt="5290435_thumb40" src="http://s3.amazonaws.com/cowbell.fm/thumbnails/38218/5290435_thumb40.jpg" />    <div class="cb_artist">Christopher Willits</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(1); return false;" title="Sun Body">Sun Body</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(2)">    <img alt="3398684-1917164966_thumb40" src="http://s3.amazonaws.com/cowbell.fm/thumbnails/39106/3398684-1917164966_thumb40.jpg" />    <div class="cb_artist">Menomena</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(2); return false;" title="Five Little Rooms">Five Little Rooms</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(3)">    <img alt="3255504_thumb40" src="http://s3.amazonaws.com/cowbell.fm/thumbnails/104578/3255504_thumb40.jpg" />    <div class="cb_artist">Social Studies</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(3); return false;" title="Holler Boys">Holler Boys</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(4)">    <img alt="3457058-2029703204_thumb40" src="http://s3.amazonaws.com/cowbell.fm/thumbnails/31780/3457058-2029703204_thumb40.jpg" />    <div class="cb_artist">BLACK MOUNTAIN</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(4); return false;" title="The Hair Song">The Hair Song</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(5)">    <img alt="3261477_thumb40" src="http://s3.amazonaws.com/cowbell.fm/thumbnails/43531/3261477_thumb40.jpg" />    <div class="cb_artist">The Thermals</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(5); return false;" title="I Don\'t Believe You">I Don\'t Believe You</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(6)">    <img alt="Default_artist_40px" src="http://cowbell.fm/images/default_artist_40px.png" />    <div class="cb_artist">Futurebirds</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(6); return false;" title="Johnny Utah">Johnny Utah</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(7)">    <img alt="3266940_thumb40" src="http://s3.amazonaws.com/cowbell.fm/thumbnails/42940/3266940_thumb40.jpg" />    <div class="cb_artist">J. Tillman</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(7); return false;" title="Three Sisters">Three Sisters</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(8)">    <img alt="Ins33763_thumb40" src="http://s3.amazonaws.com/cowbell.fm/thumbnails/79027/INS33763_thumb40.gif" />    <div class="cb_artist">Deerhunter</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(8); return false;" title="Primitive 3D">Primitive 3D</a>    </div>  </div>    <div class="cb_track" onclick="cbPlayTrack(9)">    <img alt="3828630_thumb40" src="http://s3.amazonaws.com/cowbell.fm/thumbnails/55738/3828630_thumb40.jpg" />    <div class="cb_artist">Marnie Stern</div>    <div class="cb_title">      <a href="#" onclick="cbPlayTrack(9); return false;" title="For Ash">For Ash</a>    </div>  </div>  </div><div id="cb_get_widget">  <a href="http://cowbell.fm/widget?type=popular" target="_blank">get this widget at cowbell.fm</a></div>';
