function set_cookie(name, value, num_days, path, domain, secure) {
  var expires = -1;
  if ((typeof days == "number") && (days >= 0)) {
    var d = new Date();
    d.setTime(d.getTime()+(days*24*60*60*1000));
    expires = d.toGMTString();
  }
  value = escape(value);
  document.cookie = name + "=" + value + ";" +
    (expires != -1 ? " expires=" + expires + ";" : "") +
    (path ? "path=" + path : "") +
    (domain ? "; domain=" + domain : "") +
    (secure ? "; secure" : "");
}

function get_cookie(name) {
  var i = document.cookie.lastIndexOf(name + '=');
  if (i == -1)
    return null;
  var value = document.cookie.substring(i + name.length + 1);
  var end = value.indexOf(';');
  if (end == -1)
    end = value.length;
  value = value.substring(0, end);
  value = unescape(value);
  return value;
}

function delete_cookie(name) {
  set_cookie(name, "-", 0);
}

function mtvnPlayerLoaded() {}
function accepted_ads() {
  return get_cookie("iBeenBumpered") == '1';
}

function bumper_if_no_ad_cookie() {
  if (!accepted_ads())
    window.location = "/bumper.html?url=" + escape(document.location);
}

function outgoing_ok(key) {
  var params = "" + window.location.search;
  if (key == null) key = "destination=";
  var url_start = params.lastIndexOf(key);
  var url = params.substring(url_start + key.length);
  var url_end = url.indexOf(";");
  if (url_end = -1)
    url_end = url.length;
  url = unescape(url.substring(0, url_end));
  if (url.charAt(url.length - 1) == '#')
    url = url.substring(0, url.length - 1);
  window.location.replace(url);
}

function outgoing_close() {
  window.close();
}

function accept_ads() {
  set_cookie("iBeenBumpered", 1);
  outgoing_ok("url=");
}

var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December');

function date_quote(id_list) {
  var url = "/js/date_names.json";
  $.getJSON(url,
            function(data) {
              var d = new Date();
              var mon = d.getMonth() + 1;
              var day = d.getDate();
              var date_str = "" + d.getFullYear() + "-" + (mon < 10 ? '0' : '') + mon + '-' + (day < 10 ? '0' : '') + day;
              var str = data[date_str];
              if (str == null || str == '')
                str = data['not_found'];
              if (str == null || str == '')
                str = 'JSON Mystery';
             $(id_list).html(str);
            });
}

function date_str(id_list) {
  var d = new Date();
  $(id_list).html("" + DAY_NAMES[d.getDay()] + ", " + MONTH_NAMES[d.getMonth()] + " " + d.getDate());
}

function getPlayer() {
  return document.getElementById('player');
}

function getPlayer() {
    return document.getElementById('player');
}

function getFlashVersion(){ 
  // ie 
  try { 
    try { 
      // avoid fp6 minor version lookup issues 
      // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/ 
      var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
      try { axo.AllowScriptAccess = 'always'; } 
      catch(e) { return '6,0,0'; } 
    } catch(e) {} 
    return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
  // other browsers 
  } catch(e) { 
    try { 
      if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){ 
        return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
      } 
    } catch(e) {} 
  } 
  return '0,0,0'; 
}  

function closeMsg(){
  $('#update_msg').slideUp(500, function () {
      $('#update_msg').hide();
  });
}

/*
  da Hamma - shake the window and/or its contents
*/
var intHammaStep = 0;    // which step we're on
var blnHammering = 0;    // whether or not we're currently hamma-ing
var blnHammaWindow = 1;    // whether or not we try to hamma the window
var strHammaShoken = 'frame';  // element in page to be hamma'd
function daHamma () {
  if( window.outerHeight && screen.availHeight && ((screen.availHeight - window.outerHeight) < 30)) {
  blnHammaWindow = 0;
  } else {
  blnHammaWindow = 1;
  }
  if( blnHammering ) {
  return false;
  } else {
  blnHammering = 1;
  daHammaHamma();
  }
}
function daHammaHamma () {
  var intBrowserMoveAmount = 5;
  var intContentMoveAmount = 2;
  var intTimeoutAmount = 10 + (intHammaStep * 5);
  if( intHammaStep < 20 ) {
  var oddNess = intHammaStep % 2;
  intHammaStep++;
  var objS = document.getElementById(strHammaShoken);
  var intX = objS.style.left ? parseInt(objS.style.left) : 50;
  var intY = objS.style.top ? parseInt(objS.style.top) : 0;
  if( oddNess ) {
    if (blnHammaWindow) {try {window.moveBy(0,intBrowserMoveAmount); window.moveBy(intBrowserMoveAmount,0);} catch (err) {appendDevNote(err.description);}}
    intX = intX + intContentMoveAmount; objS.style.left = intX + '%';
    intY = intY + intContentMoveAmount; objS.style.top = intY + '%';
  } else {
    if (blnHammaWindow) {try {window.moveBy(0,-intBrowserMoveAmount); window.moveBy(-intBrowserMoveAmount,0);} catch(err) {appendDevNote(err.description);}}
    intX = intX - intContentMoveAmount; objS.style.left = intX + '%';
    intY = intY - intContentMoveAmount; objS.style.top = intY + '%';
  }
  setTimeout("daHammaHamma()", intTimeoutAmount);
  } else {
  intHammaStep = 0;
  blnHammering = 0;
  }  
}
/*  end daHamma  */

/*  Snap Gallery  */
function SnapGallery() {
  this.items = null;
  this.total = 0;
  this.current_i = 0;
  this.init = function() {
    this.getItems();
    this.getTotal();
    this.items.hide();
    this.getCurrent().show();
    //this.getVote();
    //add onclick functions to prev & next buttons
    this.getPrevBtn().attr('onclick', 'javascript:gallery.previous();return false');
    this.getNextBtn().attr('onclick', 'javascript:gallery.next();return false');
    this.hideShowPrevNext();
    //vertically center the image
    this.vertCenter( $('ul#snaps_slideshow li.current img') );
    //add the photo count and prepend member name with By
    $('div#snaps_slideshow_controls').append(this.photoCount());
    this.prependMember();
    // load the other images
    this.loadImages();
  };
  this.getCurrent = function() {
    this.items.each(
      function ( index ) {
        //alert ( $(this) +' '+ index);
        if ( $(this).hasClass('current') ) {
          this.current_i = 0;
        }
      }
    );
    return $('ul#snaps_slideshow li.current'); 
  };
  this.getItems = function() {
    this.items = $('ul#snaps_slideshow li');
    //return(this.items);
  };
  this.getTotal = function() {
    this.total = this.items.length
    return this.total;
  };
  this.getVote = function() {
    return('Vote Slider');
  };
  this.setCurrent = function(new_current_i) {
  	var old_current_i = this.current_i;
  	$(this.items[new_current_i]).addClass('current').show();
    $(this.items[old_current_i]).removeClass('current').hide();
    this.current_i = new_current_i;
    this.hideShowPrevNext();
  }
  this.next = function() {
    //alert('next photo');
    this.setCurrent ( this.current_i + 1);
  };
  this.previous = function() {
    //alert('previous photo');
    this.setCurrent ( this.current_i - 1);
  };
  this.getPrevBtn = function() {
  	return $('a#snaps_slideshow_controls_prev')
  };
  this.getNextBtn = function() {
    return $('a#snaps_slideshow_controls_next');
  };
  this.hideShowPrevNext = function() {
    /* Hide or show next and previous buttons as nessessary */
    if (this.current_i>0)
      this.getPrevBtn().show();
    else
      this.getPrevBtn().hide();
    if (this.current_i<this.total-1)
      this.getNextBtn().show();
    else
      this.getNextBtn().hide();
    //vertically center the image
    this.vertCenter( $('ul#snaps_slideshow li.current img') );
    //reload font magic
    this.reloadFont();
    //change photo count
    $('div#snaps_slideshow_controls p.count').replaceWith(this.photoCount());
    
  };
  this.vertCenter = function(obj) {
  	var max_height = 550;
  	obj.css('margin-top', max_height/2 - obj.height()/2);
  }
  this.reloadFont = function() {
    if (century) {
    sIFR.replace(century, {
      selector: '.snaps_caption p',
      bgcolor: 'FFFFFF',
      css: '.sIFR-root {color: #394c8c; text-align: center;}'
    });	
    }
  };
  this.photoCount = function() {
  	return '<p class="count">'+(this.current_i+1)+' of '+this.total+'</p>';
  };
  this.loadImages = function() {
  	this.items.each(
      function ( index ) {
      	//parse the image data from the span
      	var img_data = $(this).children('span').text().split(';');
      	var width = img_data[0].substring(6);
		var height = img_data[1].substring(7);
		var src = img_data[2].substring(4);
		//replace the loading images with a real image tag
		var img_tag = '<img src='+src+' width='+width+' height='+height+' />';
		$(this).find('img.loading').replaceWith(img_tag);
      }
    );
  };
  this.prependMember = function() {
  	$('ul#snaps_slideshow p.member').prepend('By ');
  }
}
