// IE setTimeout/setInterval extra arguments fix
// http://webreflection.blogspot.com/2007/06/simple-settimeout-setinterval-extra.html
/*@cc_on @*/
(function(f){
 window.setTimeout =f(window.setTimeout);
 window.setInterval =f(window.setInterval);
})(function(f){return function(c,t){var a=[].slice.call(arguments,2);return f(function(){c.apply(this,a)},t)}});


function trace (argument) {
  console.log(argument)
}


var pvp = {
  
  _init : function() {
    pvp.loadVideo();
    window.setTimeout(pvp.prepRetweet, 500);
    pvp.changeInputValues();
    $(document).pngFix(); 
    $('input.text').click(function() {
      this.select();
    });
    pvp.ajaxPhotos();
  }
  
  ,loadVideo : function() {
    // load video
    var i = setInterval(function(start_position) {
      loadNewVideo(bg_video_id, start_position);
      setVolume(0);
      clearInterval(i);
    }, 3000, 0);
  }
  
  ,resizeFrame : function() {
    var w = $(window).width();
    var h = $(window).height();
    $("div#bg object").css('width', w);
    $("div#bg object").css('height', h);
  }
  
  ,ajaxPage : function(url) {
    var footMargin = $('div#main div.layout').height();
    //$('div#footer').css({ marginTop: footMargin + "px" });
    $('div#main').fadeOut('slow', function() {
      $.get(url, null, function(data) {
        $('div#main').html($($(data).find('div#main')).html());
        $('div#main').fadeIn('slow', function() { });
        $('div#footer').css({ marginTop: '0' });
        pvp.ajaxPhotos();
        window.setTimeout(pvp.prepRetweet, 500);
      });
    });
  }
  
  ,ajaxPhotos : function() {
    $('a.black').click(function() {
      pvp.ajaxPage($(this).attr('href'));
      return false;
    });
  }
  
  ,prepRetweet : function() {
    var _url = window.location.href;
    var _url = _url.replace(/((?:\?|&)?fbc_receiver=.+)?(?:#.*)?$/, "");
    var url = escape((typeof tweetmeme_url == "string") ? tweetmeme_url : ((typeof TWEETMEME_URL == "string") ? TWEETMEME_URL : _url)).replace(/\+/g, "%2b");
    var source = (typeof tweetmeme_source == "string") ? escape(tweetmeme_source) : ((typeof TWEETMEME_SOURCE == "string") ? escape(TWEETMEME_SOURCE) : false);
    var style = (typeof tweetmeme_style == "string") ? escape(tweetmeme_style) : ((typeof TWEETMEME_STYLE == "string") ? escape(TWEETMEME_STYLE) : "normal");
    var service = (typeof tweetmeme_service == "string") ? escape(tweetmeme_service) : ((typeof TWEETMEME_SERVICE == "string") ? escape(TWEETMEME_SERVICE) : false);
    var service_api = (typeof tweetmeme_service_api == "string") ? escape(tweetmeme_service_api) : ((typeof TWEETMEME_SERVICE_API == "string") ? escape(TWEETMEME_SERVICE_API) : false);
    var alias = (typeof tweetmeme_alias == "string") ? escape(tweetmeme_alias) : ((typeof TWEETMEME_ALIAS == "string") ? escape(TWEETMEME_ALIAS) : false);
    var src = "http://api.tweetmeme.com/button.js";
    switch (style) {
    case "compact":
      var h = 20;
      var w = 90;
      break;
    case "rednose":
      var h = 61;
      var w = 50;
      break;
    default:
      var h = 61;
      var w = 50;
      break
    }
    src += "?style=" + style;
    if (source != false) {
      src += "&source=" + source
    }
    if (service) {
      src += "&service=" + service
    }
    if (service_api) {
      src += "&service_api=" + service_api
    }
    if (alias) {
      src += "&alias=" + alias
    }
    // gea - 20 nov 2009 - this is snapshot of tweetmeme button.js - need to append opposed to doc.write due to ajax
    //document.write('<iframe src="' + src + '" height="' + h + '" width="' + w + '" frameborder="0" scrolling="no"></iframe>');
    src += "&url=" + url;
    $('div.retweet').each(function() {
      $(this).append($('<iframe src="' + src + $(this).attr('alt') + '" height="' + h + '" width="' + w + '" frameborder="0" scrolling="no"></iframe>'));
    })
    tweetmeme_url = null;
    TWEETMEME_URL = null;
    tweetmeme_source = null;
    TWEETMEME_SOURCE = null;
    tweetmeme_service = null;
    TWEETMEME_SERVICE = null;
    tweetmeme_service_api = null;
    TWEETMEME_SERVICE_API = null;
    tweetmeme_style = null;
    TWEETMEME_STYLE = null
    
    pvp.retweetMargin();
  }
  
  ,retweetMargin : function() {
    if ($('div.retweet').length > 0) {
      $('div.retweet').each(function() {
        var h = $(this).prev().height() - $(this).height() - 2;
        $(this).css({ marginTop: h + "px" });
      });
    }
  }
  
  ,changeInputValues : function() {
    $('div#left input.submit').attr({ value: 'SUBMIT' });
    $('div#left input.text').attr({ value: 'YOUR EMAIL' });
    $('div#main input.text').attr({ value: 'YOUR EMAIL' })
  }
}

$(function() {
  pvp._init();
});

jQuery.event.add(window, "load", pvp.resizeFrame);
jQuery.event.add(window, "resize", pvp.resizeFrame);
