/* Author: <19*/
$(document).ready(function(){

    $.easing['cc'] = function(p, n, firstNum, diff) {
        var c=firstNum+diff;  
        if ((p/=0.5) < 1)
            return c/2*p*p*p + firstNum;
        else
            return c/2*((p-=2)*p*p + 2) + firstNum;
    }; 

    var W = $(window).width(),
        H = $(window).height()
        mainHeight = Math.floor(W/2.56),
        navigation = $('nav ol li'),
        spaceNeeded = 240;

    //(H<'600')? $('#logo').animate({opacity:0},100).css({bottom:'-100px'}) : $('#logo').animate({opacity:1},100).css({bottom:'0px'});

$('#main').height( mainHeight );

if( (spaceNeeded + mainHeight)>H  ) {
    mainHeight = H-spaceNeeded;
    imgH = Math.floor(W/2.56);
    avalH = H-spaceNeeded;
    (mainHeight > Math.floor(W/2.56))? mainHeight=Math.floor(W/2.56): mainHeight = H-spaceNeeded;
    
    $('#main,#clip').height( mainHeight ).css({ 'min-height':H-spaceNeeded });
    $('#clip img').css({
        'top':0-(imgH-avalH),
        'clip':'rect('+(imgH-avalH)+'px,'+W+'px,'+(imgH+spaceNeeded)+'px,0px)'
        });
}

navigation_over = function() {
    $(this).not('.on').css({
        'background-color':'white',
        'color':'#d80000'
        });
    $(this).not('.on').children().css({
        'color':'#aa0000'
        });
};
navigation_out = function() {
    $(this).not('.on').css({
        'background-color':'transparent',
        'color':'white'
        });
    $(this).not('.on').children().css({
        'color':'white'
        });
};

(navigation.length)? navigation.hoverIntent( navigation_over, navigation_out): '';

    
/* =========================================================================== */
(function($,sr){
  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;
 
      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null; 
          };
 
          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);
 
          timeout = setTimeout(delayed, threshold || 100); 
      };
  }
	// smartresize 
	jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
 
})(jQuery,'smartresize');

/* ============   +    ======================================================= */

    $(window).smartresize(function() {
        
            var nW = $(window).width(),
            nH = $(window).height(),
            mainHeight = Math.floor(W/2.56);
            
            if(W!=nW || H!=nH) {}
                W = nW,
                H = nH;
            
            if( (spaceNeeded + mainHeight)>H ) {    
                mainHeight = H-spaceNeeded;
                imgH = Math.floor(W/2.56);
                avalH = H-spaceNeeded;
                (mainHeight > Math.floor(W/2.56))? mainHeight=Math.floor(W/2.56): mainHeight = H-spaceNeeded;
                $('#main,#clip').height( mainHeight ).css({ 'min-height':H-spaceNeeded });
                $('#clip img').css({
                    'top':0-(imgH-avalH),
                    'clip':'rect('+(imgH-avalH)+'px,'+W+'px,'+(imgH+spaceNeeded)+'px,0px)'
                    });
            } else {
                imgH = Math.floor(W/2.56);
                $('#main,#clip').height( imgH ).css({ 'min-height':imgH });
                $('#clip img').css({
                    'top':'0px',
                    'clip':'rect(0px,'+W+'px,'+(imgH)+'px,0px)'
                    });
            }                

    });
});

