/* 

Author: Mr. Henry
Date: 2009-09-22

Fueled by jQuery

*/

// Note: No conflict function

// This function must be called after including the jQuery javascript file, but before including any other conflicting library, 
// and also before actually that other conflicting library gets used, in case jQuery is included last. noConflict can be called at 
// the end of the jQuery.js file to globally disable the $() jQuery alias. jQuery.noConflict returns a reference to jQuery, so it 
// can be used to override the $() alias of the jQuery object.
// Use jQuery via jQuery(...)
//jQuery.noConflict();


/* Cufon
-----------------------------------------------------*/
Cufon.replace('.font-apex, h1, h2, h3, h4, h5, h6', {hover:true});


jQuery(document).ready(function(){
    
    /* Project detail slideshow
    -----------------------------------------------------*/
    var slideshow_nav_el    = $('.slideshow .slideshow-nav a');
    slideshow_nav_el.css({ 'opacity' : '0.1' });
    slideshow_nav_el.mouseover(function(){
        $(this).animate({ 'opacity' : '0.6' }, 100);
    });
    slideshow_nav_el.mouseout(function(){
        $(this).animate({ 'opacity' : '0.1' }, 100);
    });
    
    $('.slideshow-nav img').slideShowThumb();
    
    /* Local scroll
    -----------------------------------------------------*/
    if($('.projects .subnav').length>0){
      $('.projects .subnav').localScroll();
    }
    
    /* Validation
    -----------------------------------------------------*/
    if ($('#form-contact').length > 0) {
        var name    = new LiveValidation("uc_name", {onlyOnSubmit: true });
        var tel     = new LiveValidation("uc_telephone", {onlyOnSubmit: true });
        var email   = new LiveValidation("uc_email", {onlyOnSubmit: true });
        var question= new LiveValidation("uc_question", {onlyOnSubmit: true });
        name.add(Validate.Presence);
        tel.add(Validate.Presence);
        email.add(Validate.Presence);
        email.add(Validate.Email);
        question.add(Validate.Presence);
    }
    
    $(".slideshow").slideShow();
    
    /* Language
    -----------------------------------------------------*/
    $('#language ul').css({ 'display' : 'none' });
    $('#language').mouseenter(function(){
        var list    = $(this).find('ul');
        if (!(list.is(':animated'))) {
            $(this).css({ 'background-image' : 'url(/img/icons/ico-arrow-down.png)' });
            $(this).find('ul').show('fast');
        }
    });
    $('#language').mouseleave(function(){
        $(this).css({ 'background-image' : 'url(/img/icons/ico-arrow-right.png)' });
        $(this).find('ul').hide('fast');
    });
    
    /* Fix center on category blocks
    -----------------------------------------------------*/
    $(".cat").fixCenter();
    
    
    /* Fix vertical center spotlight on project thumbs
    -----------------------------------------------------*/
    $("body.projects .overview .block .spotlight").fixVerticalCenter();
    
    
    /* Projects filter
    -----------------------------------------------------*/
    $(".filter select").change(function(){
      $(".filter form").submit();
    });
    
    /* Quiet contact
    -----------------------------------------------------*/
    $(".project_contact_form").submit(function() {
      var error = false;
      var quiet_path = $("#quiet_path").val();
      
      
      $(this).find("input,textarea").each(function(){
        if($(this).hasClass("LV_invalid_field")){
          error = true;
        }
      });
      
      if(!error){
        var uc_data = "authenticity_token="+$("authenticity_token").val();
        uc_data += "&uc[name]="+$("#uc_name").val();
        uc_data += "&uc[email]="+$("#uc_email").val();
        uc_data += "&uc[telephone]="+$("#uc_telephone").val();
        uc_data += "&uc[question]="+$("#uc_question").val();
        $(".loading").html("<img src='/img/icons/ajax-loader.gif' style='vertical-align:middle;margin-right:5px;' alt='' /><span>"+$(".loading span").html()+"</span>");
        $(".loading").show();
        $.ajax({
           type: "POST",
           url: quiet_path,
           data: uc_data,
           success: function(msg){
             $(".loading").hide();
             if(msg=='true'){
                $(".project_contact_form").fadeOut(500,function(){
                  $(".project_contact_success").fadeIn(1000);              
                });
              }else{
                $(".project_contact_error").fadeIn(1000);
              }              
           },
           error:function (XMLHttpRequest, textStatus, errorThrown) {
             $(".loading").hide();
             $(".project_contact_error").fadeIn(1000);
           }
         });
        
      }
      return false;
    });

    
});

jQuery.fn.slideShow = function() {
  var total = $(this).children(".slideshow-img").children("img").length;
  var current = 0;
  var ph = "<div class=\"slideshow_ph\"><span>x</span></div>";
  var grid = $(this);
  var fading = false;
  
  $("ul.slideshow-nav .prev").append(ph);
  $("ul.slideshow-nav .next").append(ph);
  
  $(this).find(".prev, .next").each(function(){
    $(this).attr("href","javascript:;");
  });
  $(".slideshow-nav .prev a, .slideshow-nav .next a").each(function(){
    $(this).attr("href","javascript:;");
  });
  
  checkCurrent();
  
  $(this).find(".prev, .next").click(function(){
    if(!($(this).hasClass("prev") && current==0) && !($(this).hasClass("next") && current==total-1)){
       fadeImage(grid,$(this));
    }
   
  });
  $(".slideshow-nav .prev a, .slideshow-nav .next a").click(function(){
    if(!($(this).parent().hasClass("prev") && current==0) && !($(this).parent().hasClass("next") && current==total-1)){
      fadeImage(grid,$(this));
    }
  });
  
  function fadeImage(grid,sender){
    if(!fading){
      fading = true;
      grid.find("#project_img_"+current).fadeOut(700);
      if(sender.hasClass("prev") || sender.parent().hasClass("prev")){
        if(current==0){current = total-1;}else{current --;}
      }else{
        if(current==total-1){current = 0;}else{current ++;}
      }
      var next = 0;
      var prev = 0;
      if(current==0){prev = total-1;}else{prev = current-1;}
      if(current==total-1){next=0;}else{next = current+1;}
      
      $(".slideshow-nav .prev > a").html($("#project_thumb_"+prev).html());
      $(".slideshow-nav .next > a").html($("#project_thumb_"+next).html());
      
      grid.find("#project_img_"+current).fadeIn(700,function(){
        fading = false;
      });
      
      $(".position").html((current+1)+"/"+total);
      Cufon.replace(".position");
      $('.slideshow-nav img').slideShowThumb();
      
      checkCurrent();
    }
  };
  
  function checkCurrent(){
    if(current==0){
      $("ul.slideshow-nav .prev a,p.slideshow-nav .prev").css("visibility","hidden");
      $("ul.slideshow-nav .prev .slideshow_ph").show();
    }else{
      $("ul.slideshow-nav .prev a,p.slideshow-nav .prev").css("visibility","visible");
      $("ul.slideshow-nav .prev .slideshow_ph").hide();
    }
    if(current==total-1){
      $("ul.slideshow-nav .next a,p.slideshow-nav .next").css("visibility","hidden");
      $("ul.slideshow-nav .next .slideshow_ph").show();
    }else{
      $("ul.slideshow-nav .next a,p.slideshow-nav .next").css("visibility","visible");
      $("ul.slideshow-nav .next .slideshow_ph").hide();
    }
  }
};

jQuery.fn.slideShowThumb = function() {  
  $(this).each(function(){
    $(this).css({ 'opacity' : '0.75' });
    $(this).mouseover(function(){
        $(this).animate({ 'opacity' : '1' }, 100);
    });
    $(this).mouseout(function(){
        $(this).animate({ 'opacity' : '0.75' }, 100);
    });
  });
  
};

jQuery.fn.fixCenter = function() {
  $(this).each(function(){
    var height = $(this).height();
    var t_height = $(this).children("h2").height();
    var center = (height-t_height-8)/2;
    $(this).children("h2").css("margin-top",center+"px");
  });
};

jQuery.fn.fixVerticalCenter = function() {
  $(this).each(function(){
    var height      = $(this).outerHeight();
    var offset      = (height / 2) - 10;
    //console.log(offset);
    if (height > 26) {
        $(this).css({ 'margin-top':(0 - offset) });
    }
  });
};



