/* ADDTHIS */

var addthis_config = {
  username: "andrewwk",
  ui_cobrand: "Andrew W.K.",
  ui_header_color: "#4C4C4C",
  services_compact: "email, print, facebook, twitter, google, stumbleupon, myspace, googlebuzz, digg, reddit, blogger, tumblr, favorites, more",
  services_expanded: "email, print, facebook, twitter, google, stumbleupon, myspace, googlebuzz, digg, reddit, blogger, tumblr, favorites, delicious, linkedin, gmail, yahoomail, hotmail, aolmail, technorati"
};

/* VIDEOS */

function writeQuicktimeCode(mediaPath, w, h) {
  h += 16;
  QT_WriteOBJECT(mediaPath, w, h, ''
  , 'CONTROLLER', 'TRUE'
  , 'OBJ#AUTOPLAY', 'true'
  , 'EMB#BGCOLOR', '#000000'
  , 'TARGET', 'myself'
  , 'EMB#BORDER', '0'
  , 'TYPE', 'video/quicktime');
}

function writeWindowsMediaCode(mediaPath, w, h) {
  h += 70;
  document.write("<object id='mediaPlayer' width='" + w + "' height='" + h + "' classid='CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject'>\n");
  document.write("<param name='fileName' value='" + mediaPath + "'>\n");
  document.write("<param name='bgcolor' value='#000000'>\n");
  document.write("<param name='animationatStart' value='true'>\n");
  document.write("<param name='transparentatStart' value='true'>\n");
  document.write("<param name='autoStart' value='true'>\n");
  document.write("<param name='ShowControls' value='true'>\n");
  document.write("<param name='ShowDisplay' value='false'>\n");
  document.write("<param name='ShowStatusBar' value='true'>\n");
  document.write("<param name='loop' value='0'>\n");
  document.write("<param name='bgcolor' value='#000000'>\n");
  document.write("<embed type='application/x-mplayer2' pluginspage='http://microsoft.com/windows/mediaplayer/en/download/' id='mediaPlayer' name='mediaPlayer' displaysize='4' autosize='0' bgcolor='black' showcontrols='1' showtracker='1' showdisplay='0' showstatusbar='1' videoborder3d='0' width='" + w + "' height='" + h + "' bgcolor='#000000' src='" + mediaPath + "' autostart='1' designtimesp='5311' loop='0' bgcolor='#000000'>\n");
  document.write("</embed>\n");
  document.write("</object>\n");
}

/* MUSIC */  

function playTrack(albumId, trackID) {
  document.getElementById("musicPlayer").triggerTrack(albumId, trackID);
}

function onSwfLoad() {
  document.getElementById("musicPlayer").filterAlbum(document.getElementById("music_id").innerHTML);
}



$(function() {
  /* FEATURED INTRO */
  
  if ($("body.home-page").length == 1) {
    var fil = $(".slide").length;
    var fic = 1;
    var fi_advance = function() {
      // what this function returns is passed through to the click handler
      if (fic == fil) {
        return true;
      } else {
        $("#slide"+fic).hide();
        fic++;
        $("#slide"+fic).show();
        return false;
      }
    };
    $("#slide_continue a").click(function() { return fi_advance(); });
    $(".slide a").click(function() {
      // when a slide is clicked, advance to the next slide unless it's linked to a news entry
      var href_len = $(this).attr("href").length;
      var href_suffix = $(this).attr("href").substr((href_len - 5));
      if (href_suffix == "/news") {
        return fi_advance();
      } else {
        return true;
      }
    });
  }

  /* LIGHTBOX */

  if ($("a[rel*=lightbox]").length > 0) $("a[rel*=lightbox]").lightBox();

  /* INPUTS */

  var inputVals = {};
  $("input").each(function() {
    $(this).focus(function() {
      if (typeof(inputVals[$(this).attr("name")]) == "undefined") inputVals[$(this).attr("name")] = $(this).val();
      if ($(this).val() == inputVals[$(this).attr("name")]) $(this).val("");
    }).blur(function() {
      if (($(this).val() == "") && typeof(inputVals[$(this).attr("name")]) != "undefined") $(this).val(inputVals[$(this).attr("name")]);
    });
  });

  /* MAILING LIST */

  // TODO: remove alerts, put in dialog

  $("#icpsignup").submit(function() {
    var email = $("#fields_email").val();
    if ((email == "") || !(/^((([a-z]|\d|[!#\$%&'\*\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email))) {
      $("#fields_email").focus();
      alert("Please specify a valid email address.");
    } else {
      var dataString = "";
      $("#icpsignup input[name]").each(function() { dataString += $(this).attr("name")+"="+escape($(this).val())+"&"; });
      if (dataString.length) dataString = dataString.substr(0, (dataString.length - 1));
      $.ajax({
        
            type: $("#icpsignup").attr("method"),
            url: $("#icpsignup").attr("action"),
            data: dataString,
            dataType: "json",
            success: function(data) {
              if (data.status == "success") {
                alert('You have been signed up for the Andrew W.K. mailing list.');
              } else {
                alert(data.status);
              }
            },
            error: function() {
                alert('You could not be signed up for the Andrew W.K. mailing list at this time.  Please try again later.');
            }
          });
    }
    return false;
  });

  /* NEWS */

  if ($("body.news").length == 1) {    
    $(".share").each(function() {
      var id = $(this).attr("id").substr(5);
      var href = $("#title"+id).attr("href");
      if (typeof(href) == "undefined") href = location.href.replace(location.hash, "");
      var title = $("#title"+id).html();
      var addthis_share =
         {
          url : href,
          title: title,   
          templates: {twitter: 'Check out {{title}}: {{url}} via @Andrewwk'},
          description: '',
          screenshot: ''
        };
   
      addthis.button("#share_button"+id, addthis_config , addthis_share);    
    });

    if ($(".comments_link").length > 0) {
      var query = "?";
      $(".comments_link").each(function(i) { query += "url"+i+"="+encodeURIComponent($(this).attr("href"))+"&"; });
      query = query.substr(0, (query.length - 1));
      var disqusJS = "http://disqus.com/forums/andrewwk/get_num_replies.js"+query;
      $.getScript(disqusJS);
    }
  }

  /* IE6 HOVER */

  eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 t(a,b){g(3=0;3<a.h;3++){k c=[],d=[],n=[],e=[],4=[],l=[];8(a[3].5(\'.\')!=-1||a[3].5(\'#\')==-1){8(a[3].5(\'>\')!=-1){c[3]=a[3].f(a[3].5(\'>\')+2);a[3]=a[3].f(0,a[3].5(\'>\')-1)}8(a[3].5(\'.\')!=-1){d[3]=a[3].f(a[3].5(\'.\')+1);a[3]=a[3].f(0,a[3].5(\'.\'))}n[3]=a[3];8(!d[3])d[3]=\'\';8(c[3]){l[3]=r.o(n[3]);g(k j=0;j<l[3].h;j++){8(l[3][j].6.5(d[3])!=-1){4[3]=l[3][j].o(c[3]);g(k i=0;i<4[3].h;i++){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}}}s{4[3]=r.o(n[3]);g(k i=0;i<4[3].h;i++){8(4[3][i].6.5(d[3])!=-1){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}}}s 8(a[3].5(\'#\')!=-1){8(a[3].5(\'>\')!=-1){c[3]=a[3].f(a[3].5(\'>\')+2);a[3]=a[3].f(0,a[3].5(\'>\')-1)}a[3]=a[3].m(\'#\',\'\');e[3]=r.u(a[3]);8(e[3]){8(c[3]){4[3]=e[3].o(c[3]);g(k i=0;i<4[3].h;i++){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}s{e[3].p=9(){7.6+=\' \'+b};e[3].q=9(){7.6=7.6.m(b,\'\')}}}}}}',31,31,'|||_hoverItem|_hoverElement|indexOf|className|this|if|function||||_class|_id|substr|for|length|||var|_parent|replace|_tag|getElementsByTagName|onmouseover|onmouseout|document|else|hoverForIE6|getElementById'.split('|'),0,{}));
  /* insert your class and id ***********************
  _hoverClassName = 'hover';
  _hoverEl = ['ul.image > li'];
  /**************************************************/
  function ieHover() {
    hoverForIE6(['ul.image > li'], 'hover');
  }
  if (window.attachEvent && !window.opera){
    window.attachEvent("onload", ieHover);
  }
});
