/* -------------------------------------------------------------- 
VGD.application.js
Copyright 2010 Antoine Doury @ AREA 17

Must be load at last
-------------------------------------------------------------- */

/********** DOM READY ************/

$(document).ready(function(){
  
  // cufon - visible
  $('h2, .u').css('opacity', 1);

	ID = $('body').attr('id');
	CLASS = $('body').attr('class');
	
	var isIE, isIE6, isIE7;
	
	var doc = $('html');
	if (doc.hasClass('ie')) isIE = true;
	if (doc.hasClass('ie6')) isIE6 = true;
	if (doc.hasClass('ie7')) isIE7 = true;
  
  // resize event
  if(isIE) {
    VGDsetPageSize();
    $(window).resize(function(){ VGDsetPageSize(); });
  }
  
  // general actions
  VGDtopLink();
  
  // homepage
  if($("#gallery").length > 0) $("#gallery").VGDgallery();
  
  // project list
  if($(".projects").length > 0) VGDprojectlist();
  
  // project detail
  if($("#project_nav").length > 0) VGDproject();
  
  // big zone are links!
  VGDzoneLinks();
  
});

function VGDsetPageSize(){
	var size = $(window).width();
	
	if(size < 1229) document.body.className = 'small';
	else document.body.className = 'medium';
	// if(size < 1089) $("body").removeClass('small medium large').addClass('small');
  // else if(size < 1229) $("body").removeClass('small medium large').addClass('medium');
	// else $("body").removeClass('small medium large').addClass('large');
}

function VGDtopLink(){
//
// Scroll to the top of the page on click
//
if($(".top-link").length > 0) {
    $(".top-link a").click(function(e){
        e.preventDefault();
        $.scrollTo("#header", 250);           
    });
}
}

function VGDprojectlist(){
  // Endless loading in the project list
  
  $("#footer, .top-link, #endlessLoader").hide();
  
  var endlessRunning      = false,
      projectsAllLoaded   = false,
      container           = $('#content'),
      loadercontent       = '<div id="endlessLoader" ><span>Loading</span></div>';
      
  $(".page").after(loadercontent);
  var loader = $('#endlessLoader');
  
  loader.fadeOut();
  
  container.endlessScroll({
    bottomPixels: 320,
    fireOnce: true,
    fireDelay: 10,
    callback: function(p){
      
      if (!endlessRunning && !projectsAllLoaded) {
        
        loader.stop(true, true).fadeIn();
        
        endlessRunning = true;
        var offset    = container.find('.cols').children().size(),
            pathname  = window.location.pathname,
            //'ajax/projects.html'
            url       = pathname + "?offset=" + offset;

         $.post(url,"", function (response){
                   if (response.length > 33) {
                     var result = $(innerShiv(response,false));
                     container.append(result);
                     loader.stop(true, true).fadeOut();
                   } else {
                     $('#footer, .top-link').show();
                     projectsAllLoaded = true;
                     loader.remove();
                   }
                   endlessRunning = false;
                 });

      } else {
      }
    }
  });
}

function VGDproject(){
  
  // project detail
  if($("#slideshow").length > 0) $('#slideshow').VGDslideshow();

  // next/prev project keyboards
  $(document).bind('keyup', function(e) {
   	switch(e.keyCode) {
     case 37:
        if($(window).scrollTop() <= 80) {
          var link = $("a.prev");
          if(link.length > 0) {
            link.addClass('hover');
            window.location = link.attr('href');
          }
          
        }
       break;
     case 39:
        if($(window).scrollTop() <= 80) {
          var link = $("a.next");
          if(link.length > 0) {
            link.addClass('hover');
            window.location = link.attr('href');
          }
        }
       break;
     }
   });
  
}



function VGDzoneLinks() {
  
  var cols = $(".from_the_blog, .project, .btcols, .profile");
  
  if(cols.length > 0) {
    cols.live('click', function() {
      var link = $(this).find('a:first');
      if(link.length > 0) window.location = link.attr('href');
    });
    cols.hover(
      function () { $(this).addClass('hover'); }, 
      function () { $(this).removeClass('hover'); 
    })
  }

  
}
