/*!
 * stuffs
 *
 * Copyright 2011, Peder Johnsen
 *
 */

$(document).ready(function() {

    var ie=false;
    var inputsearch;

    //GamesShowcase stuff
    $('div.gamesShowcase').mouseenter(function(){
      //$(this).find('div.gamePresenter').hide();
      $(this).find('div.gameHandler').show();
      $(this).find('div.gameHandler').scrollTo({top:'267px', left:'0px'}, 500);
    });
    $('div.gamesShowcase').mouseleave(function(){
      $(this).find('div.gameHandler').scrollTo({top:'0px', left:'0px'}, 200, {onAfter:function(){
        $(this).hide();
      } });
      //$(this).find('div.gamePresenter').show();
    });

    //When a game is downloaded update total downloads!
    $(".btn").click(function() {
      if($(this).attr('rel') == "downloads")
      {
        var filedownloads = $('span#fileDownloads').text();
        filedownloads++;
        $('span#fileDownloads').text(filedownloads++);

        var totaldownloads = $('span#totalDownloads').text().replace(',', '');
        totaldownloads++;
        $('span#totalDownloads').text($.number_format(totaldownloads));
      }
    });

    //Remove removable message_box
    $(".clickable").click(function(e) {
      e.preventDefault();

      $(this).fadeOut("slow", function() { $(this).remove(); });
    });

    //Ask user to confirm important actions!
    $('.ask').click(function(e) {
      e.preventDefault();
      thisHref = $(this).attr('href');

      if(confirm(this.rel)) {
        window.location = thisHref;
      }
    });

    //Tell user to login to perform action!
    $('.loginfirst').click(function(e) {
      e.preventDefault();
      
      $('.later span').show();
    });

    //Search input focus and blur effect
    $('li.search input:text').focus(function(e) {
      e.preventDefault();

      if($(this).val() == 'Search the entire site...')
      {
        $(this).val('');
        $(this).removeClass('inactive');
        inputsearch='site';
      }
      if($(this).val() == 'Search the entire archive...')
      {
        $(this).val('');
        $(this).removeClass('inactive');
        inputsearch='archive';
      }
    });
    $('li.search input:text').blur(function(e) {
      e.preventDefault();

      if(inputsearch=='site' && ($(this).val() == 'Search the entire site...' || $(this).val() == ''))
      {
        $(this).val('Search the entire site...');
        $(this).addClass('inactive');
      }
      if(inputsearch=='archive' && ($(this).val() == 'Search the entire archive...' || $(this).val() == ''))
      {
        $(this).val('Search the entire archive...');
        $(this).addClass('inactive');
      }
    });

    //Set qTip
    $('li.btn a').qtip({
      position: {
        my: 'top center',
        at: 'bottom center'
      },
      style: {
        tip: true,
        classes: 'ui-tooltip-shadow ui-tooltip-dark'
      }
    });

    //Games showcase hover state

    var imagecount=$('.slides_container > img').size();
    if($('.slides_container > iframe').size()>0)
    {
      imagecount=imagecount+$('.slides_container > iframe').size();
    }
    if(imagecount>1)
    {
      var bulletswidth=imagecount*24;

      $('.navArrows').hide();
      $('#slides').hover(function(){
        $('.navArrows').show();
      },function(){
        $('.navArrows').hide();
      });

      $('#slides').slides({
        preload:true,
        effect:'fade',
        preloadImage:'/media/images/loading.gif',
        crossfade:true,
        play:0
      });

      $('#slides .pagination').css('width',bulletswidth);
    }
    else
    {
      $('.navArrows').hide();
      $('.slides_container').show();
    }
});
