/*
 ** MADE WITH LOVE BY
 ** PEPPERMINT & SUPERHEROES
 ** www.peppermnt.com
 ** www.superhero.es
 */


jQuery(
        function ($) {
          var URI = new URIParser($),
                  $wrapper = $('div#wrapper');

          init();
          initNav();
          initContent();
          initScroller();

          function init() {

            $body = $('html,body').attr('scrollTop', 0);


            preload();

            $.ajaxSetup({
              cache: false,
              dataType: 'html',
              global: false,
              timeout: 5000
            });
          }

          function preload() {
            $('#preloader').show()
            $wrapper.css({height:0,overflow:'hidden'})
            $(window).bind('load', on_load)
          }


          function on_load(e) {
            $('#home_img').show()
            $wrapper.css({height:'auto',overflow:'auto'})

            $('div#preloader')
                    .css({display:'none'})
                    .remove();

            if (URI.segment(1) != '')
              $('body').triggerHandler('ACTIVATE_NAV', URI.segment(1));
          }

          function initNav() {
            new Nav($('div#navigation'));
          }

          function initContent() {
            new Content($('div#content'));
          }

          function initScroller() {
            new Scroller()
          }

        }
        );


function NavItem($self, index) {

  var href = $self.attr('href');
  var re = new RegExp('#(.*)$');
  var match = re.exec(href);
  var LINK;
  var ID = index;

  if(match){
    LINK = match[1];
  }

  init();
  initListeners();

  function init() {
    $self.bind('click', on_click)
    $self.bind('mouseover', on_mouse_over)
    $self.bind('mouseout', on_mouse_out)
  }
  function initListeners() {
    //$('body').bind('ACTIVATE_NAV',ACTIVATE_NAV);
    $('body').bind('TOP', ACTIVATE_NAV);
  }
  function on_click(e) {
    $('body').triggerHandler('ACTIVATE_NAV', [LINK,ID]);
    return false;
  }
  function on_mouse_over(e) {

  }
  function on_mouse_out(e) {

  }

  function ACTIVATE_NAV(e, id) {
    var li = $('li#nav-'+LINK)
    if (id == LINK) {
      if (!li.hasClass('active'))
        li.removeClass().addClass('active')
    } else {
      if (li.hasClass('active'))
        li.removeClass();
    }
  }

}

function Nav($self) {

  var active = true,
          _activenav = 'home',
          $xpand = $('a#expand-collapse');

  init();
  initListeners();

  function init() {


    new NavItem($('div#logo > a'), 0)

    $xpand.bind('click', on_expand_collapse)

    $('a[href*=#]').each(
            function (index) {
              new NavItem($(this), index + 1)
            });
//    $self.find('li').each(
//            function (index) {
//              new NavItem($(this), index + 1)
//            });


  }

  function on_activate_nav(e, i) {
    _activenav = i
  }


  function initListeners() {
    $('body').bind('ACTIVATE_NAV', on_activate_nav);

    $(window).bind('load', SHOW);
  }

  function on_expand_collapse() {
    if (active)
      COLLAPSE();
    else
      SHOW();

  }

  function SHOW(e) {
    active = true;
    $self.unbind('mouseover', on_collapsed_mouse_over)
    $xpand.removeClass()
    $self.animate({bottom:-10}, 1000, 'easeInOutQuad');
  }

  function COLLAPSE() {
    /*
     active=false;
     $xpand.removeClass().addClass('xpand');
     $self.animate({bottom:-135},1000,'easeInOutQuad',on_collapse_complete);
     */
  }

  function HIDE(e) {
    COLLAPSE();
  }

  function on_collapse_complete(e) {
    $self.bind('mouseover', on_collapsed_mouse_over)
  }

  function on_collapsed_mouse_over(e) {
    SHOW()
  }

}

function Content($self) {
  var _activeindex = 0,
          _activeid = 'home'

  init();
  initListeners();

  function init() {
    new Home($('div#home'))
    //new Portfolio($('div#content-work'));
    //		new Blog($('div#content-blog'));
    new About($('div#about'));
    new Download($('div#download'))
  }

  function initListeners() {
    $('body').bind('ACTIVATE_NAV', CHANGE_SECTION);
    $(window).bind('resize', on_resize);
  }

  function CHANGE_SECTION(e, id, index) {
    //set active for window resize

    _activeid = id
    _activeindex = index;

  }


  function on_resize(e) {

  }
}


function Download($self) {
  init()

  function init() {
  }
}

function About($self) {
  init();

  function init() {


  }
}


function Home($self) {
  var $imgwrapper = $self.find('div.content-item');//,
  //        $imgmap = $('map');

  init()
  function init() {
    $imgwrapper.css({opacity:1})

    // $imgmap.find('area').bind('click', on_map_click)

  }

  //  function on_map_click(e) {
  //    $('body').triggerHandler('ACTIVATE_NAV', $(this).attr('alt'))
  //    e.preventDefault();
  //  }
}

function Scroller() {

  if ($('body').attr('id') == 'index')
    init();

  function init() {

    $('body').bind('ACTIVATE_NAV', on_activate_nav);
    $(window).bind('scroll', on_scroll)
  }

  function on_activate_nav(e, id) {
    var position = $('div#' + id).offset().top;
    $body.stop().animate({scrollTop: position}, 1200, 'easeInOutQuart', on_scroll_complete);

  }

  function on_scroll_complete() {
  }

  function on_scroll(e) {

    var pos = ($.browser.safari) ? $('body').attr('scrollTop') : $body.attr('scrollTop')

    var content = new Array('home', 'examples', 'docs', 'download')

    for (var i = 0; i < content.length; i++) {


      var $prevtop = (i == 0) ? 0 : $('div#' + content[i - 1]).offset().top;
      var $curtop = $('div#' + content[i]).offset().top;


      if ($curtop < pos + 100 && pos + 100 > $prevtop) {
        $('body').triggerHandler('TOP', content[i])
      }


    }


  }

}


/*
 **
 ** URI PARSING
 **
 */

function URIParser($) {

  this.Hash = location.hash;
  this.HashObjects = this.Hash.split("/");

  initialize();

  function initialize() {

  }

}

URIParser.prototype.segment = function(id) {
  if (this.HashObjects[id])
    return this.HashObjects[id];
  else
    return '';
}

