if (window['loadFirebugConsole']) {
  window.loadFirebugConsole();
} else {
  if (!window['console']) {
    window.console = {};
    window.console.info = alert;
    window.console.log = alert;
    window.console.warn = alert;
    window.console.error = alert;
  }
}

$(document).ready( function() {
  var api = $('.scrollable').scrollable(
      {
        size: 5, 
        next: '#list_button_right a', 
        prev: '#list_button_left a',
        api: true
      }
  );
  

  var loadSnippetByHref = function(href, forceFocus) {
    if (!href) { return false; }
    forceFocus = forceFocus || false;
    var id = href.substr(1, href.length);  
    // already setup / exists
    if ( $( href ).length ) {
      $('#pages div.page').hide();
      $(href).show();
      return true;
    }
    // create
    $('#pages .page, #pages .text_allg, #homeslide').hide();
    $('#pages').prepend('<div id="'+id+'" class="page"><span class="page-loader">Loading ...</span></div>');
    $('#'+id).load( 'snippets/' + id + '.html', null, function(responseText, textStatus, XMLHttpRequest) {
      if (textStatus !== 'error') {
        $('.projectimages', $(this) ).cycle({
          prev: '#'+id+' .prev',
          next: '#'+id+' .next',
          timeout: 0 
        });
        $('#homeslide').cycle('stop');
        // @todo: handling here
        // $('#homeslide').hide();
      }
    });
  };
  
  if ( document.location.href.indexOf('#') > 0 ) {
    // @todo: fix #anchors
    /* var href = document.location.href.substring( document.location.href.indexOf('#'), document.location.href.length );
    var a = $(".scrollable a[href=" + href + "]"); 
    if ( a.length ) {
      index = $("ul.items li").index( $(a).parents('li')[0] );
      loadSnippetByHref( href );
      return api.click( index );
    } */
  }
  
 
  
  var handleSelect = function(event) {
    // 
    var target = event.originalTarget || event.srcElement;
    var a = $(target).parents('a')[0];
    return loadSnippetByHref( $(a).attr('href') );
  };
  // add the scrollable loading click
  $('.scrollable').bind('click.p7', handleSelect);
  
  $('#homeslide').cycle({
    timeout: 8000
  });

});

