/* Author: Republic Factory */

/*jslint undef: true, browser: true, indent: 2 */
/*global jQuery, $, window */

jQuery(document).ready(function ($) {
  
  
  /* CONTACT FORM VALIDATION - START */
  // name_validation();
  // email_validation();

  function name_validation() {
    if ($('input[name="name"]').val() == "") {
      $('input[name="name"]').removeClass('validate-ok');
      $('input[name="name"]').addClass('validate-failed');
      return false;
    } else {
      $('input[name="name"]').addClass('validate-ok');
      $('input[name="name"]').removeClass('validate-failed');
      return true;
    }
  }
  
  function email_validation() {
    if ($('input[name="email"]').val() == "" || !validate_email()) {
      $('input[name="email"]').removeClass('validate-ok');
      $('input[name="email"]').addClass('validate-failed');
      return false;
    } else {
      $('input[name="email"]').addClass('validate-ok');
      $('input[name="email"]').removeClass('validate-failed');
      return true;
    }
  }
  
  function validate_email(form_id, email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = $('input[name="email"]').val();
     
    if (reg.test(address) == false) {
      return false;
    }
    return true;
  }

  var height = $("#showreel_image").height();
  $("#showreel_play").attr('style', 'height: ' + height + 'px');
  
  $('input[name="name"]').keyup(function () { name_validation(); });
  $('input[name="email"]').keyup(function () { email_validation(); });

  $("#contact-submit").submit(function () {
    if(!name_validation() || !email_validation()){
      return false;
    }
  });
  
  
  /* CONTACT FORM VALIDATION - END */
  
  
  /* hide/show menu */
  $("nav > ul > li").mouseenter(function () {
    $("#nav1, #nav2, #nav3, #nav4").hide();
    $(this).children("ul").show();
    $(this).siblings("li").children("div").css("color", "#aaa");
    $(this).css("borderBottom", "none").children("div").css("color", "#000");
    $(this).children("ul").children("li").css('filter','');
    
  });
  
  $("nav > ul > li").mouseleave(function () {
    $(this).children("ul").hide();
    $(this).siblings("li").children("div").css("color", "#222");
    $(this).css("borderBottom", "1px solid #8d8d8d");
  });  


  /* lightbox */
  $("a[rel='colorbox']").colorbox({
    title: $(this).title,
    rel: 'colorbox',
    next: "Next &raquo;",
    previous: "&laquo; Previous",
    current: "",
    loop: false
  });
  
  /* lightbox */
  $("a[rel='colorbox-video']").colorbox({
    title: $(this).title,
    rel: 'colorbox-video', 
    next: "Next &raquo;",
    previous: "&laquo; Previous",
    current: "",
    iframe: true, 
    innerWidth: 920, 
    innerHeight: 735,
    photo: false
  });
  
  $("a[rel='colorbox-video_showreel']").colorbox({
    title: $(this).title,
    rel: 'colorbox-video_showreel', 
    next: "Next &raquo;",
    previous: "&laquo; Previous",
    current: "",
    iframe: true, 
    innerWidth: 920, 
    innerHeight: 735,
    photo: false
  });

  var originalSettings = new Array();
  $(document).bind('cbox_load', function(){

    if(originalSettings == ""){
      originalSettings[0] = $("#colorbox").css('left');
      originalSettings[1] = $("#colorbox").css('width');
      originalSettings[2] = $("#cboxWrapper").css('width');
    }else{
      $("#colorbox").css('left', originalSettings[0]);
      $("#colorbox").css('width', originalSettings[1]);
      $("#cboxWrapper").css('width', originalSettings[2]);
      $("#cboxContent").css('paddingLeft', 0);
      $("#cboxContent").css('paddingRight', 0);
      $("#cboxTopCenter").css('paddingLeft', 0);
      $("#cboxTopCenter").css('paddingRight', 0);
    }
    
  });
  
  $(document).bind('cbox_complete', function(){

    var originalSettings = new Array();
    originalSettings[0] = $("#colorbox").css('left');
    originalSettings[1] = $("#colorbox").css('width');
    originalSettings[2] = $("#cboxWrapper").css('width');

      var leftFloatFinal = $("#container").offset().left;
      var left = parseInt($("#colorbox").css('left'));
      if(left > leftFloatFinal){
        var changedPixels = left-leftFloatFinal;
        $("#colorbox").css('left', leftFloatFinal+'px');
        var colorboxWidth = parseInt($("#colorbox").css('width'));
        $("#colorbox").css('width', colorboxWidth + (2*changedPixels)+"px");
        var cboxWrapperWidth = parseInt($("#cboxWrapper").css('width'));
        $("#cboxWrapper").css('width', cboxWrapperWidth + (2*changedPixels)+"px");
        $("#cboxContent").css('paddingLeft', changedPixels+"px");
        $("#cboxContent").css('paddingRight', changedPixels+"px");
        $("#cboxTopCenter").css('paddingLeft', changedPixels+"px");
        $("#cboxTopCenter").css('paddingRight', changedPixels+"px");
      }
  });
  

  /* sort portfolio */
  // Custom sorting plugin
  // (function($) {
  //   $.fn.sorted = function(customOptions) {
  //     var options = {
  //       reversed: false,
  //       by: function(a) { return a.text(); }
  //     };
  //     $.extend(options, customOptions);
  //     $data = $(this);
  //     arr = $data.get();
  //     arr.sort(function(a, b) {
  //       var valA = options.by($(a));
  //       var valB = options.by($(b));
  //       if (options.reversed) {
  //         return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;       
  //       } else {   
  //         return (valA < valB) ? -1 : (valA > valB) ? 1 : 0; 
  //       }
  //     });
  //     return $(arr);
  //   };
  // })(jQuery);
  // 
  // // DOMContentLoaded
  // $(function() {
  // 
  //   var $filterType = $('#filter input[name="type"]');
  //   var $filterSort = $('#filter input[name="sort"]');
  //   var $applications = $('ul#portfolio');
  //   var $data = $applications.clone();
  // 
  //   $filterType.add($filterSort).change(function(e) {
  //     
  //     // filter
  //     if ($($filterType+':checked').val() == 'all') {
  //       var $filteredData = $data.find('li');
  //     } else {
  //       var $filteredData = $data.find('li[data-type=' + $($filterType+":checked").val() + ']');
  //     }
  // 
  //     // sorting
  //     if ($('#filter input[name="sort"]:checked').val() == "title") {
  //       var $sortedData = $filteredData.sorted({
  //         by: function(v) {
  //           return parseFloat($(v).find('strong').text());
  //         }
  //       });
  //     } else if ($('#filter input[name="sort"]:checked').val() == "cat") {
  //       var $sortedData = $filteredData.sorted({
  //         by: function(v) {
  //           return $(v).find('em[data-type=cat]').text().toLowerCase();
  //         }
  //       });
  //     } else if ($('#filter input[name="sort"]:checked').val() == "date") {
  //       var $sortedData = $filteredData.sorted({
  //         by: function(v) {
  //           return $(v).find('em[data-type=date]').text().toLowerCase();
  //         }
  //       });
  //     } else {
  //       var $sortedData = $filteredData.sorted({
  //         by: function(v) {
  //           return $(v).find('em[data-type=type]').text().toLowerCase();
  //         }
  //       });
  //     }
  // 
  //     // finally, call quicksand
  //     $applications.quicksand($sortedData, {
  //       adjustHeight: 'false',
  //       duration: 800,
  //       easing: 'easeInOutQuad'
  //     });
  //     
  //   });
  // 
  // });

});

