$(document).ready(function(){
  $("select, input:radio").uniform();
  
  $('.block').mouseenter(function(){
    ShowBlock($(this));
  });

  $('.block').mouseleave(function(){
    HideBlock($(this));
  });
  
  $('a.form-link').fancybox({
      'type': 'ajax',
      'ajax': { 
          dataFilter: function(data){
              return $(data).find('#form_container');
          }
       }
  });


/*  
  $('.top-menu li').hover(function(){
    ShowTopMenu($(this));
  }, function(){
    ShowTopMenu($(this));
  });
*/
  
  $("a.fancybox").fancybox({
    'transitionIn'  :  'elastic',
    'transitionOut'  :  'elastic',
    'speedIn'    :  600, 
    'speedOut'    :  200, 
    'overlayShow'  :  false
  });
  
  if($(".youtubeblock").length > 0) {
    $(".youtubeblock .youtube").each( function() {
      var CurrentUrl   = $(this).attr("href");
      var YouTubeImage = GetYouTubeScreen(CurrentUrl);
      $(this).html("<img style=\"width: 200px;\" src=\"" + YouTubeImage + "\" alt=\"youtube\" />"); 
    });
    if($.browser.msie && $.browser.version < 7){
      $(".youtubeblock .youtube img").load(function(){
        $(window).trigger('resize');
      });
    }
  }
  
  $('.youtubeblock .overlay').click(function(){ $(this).parent().find('a').click(); })
  $(".youtube").click(function() {
    $.fancybox({
        'padding'        : 0,
        'autoScale'      : false,
        'transitionIn'  : 'none',
        'transitionOut'  : 'none',
        'title'          : this.title,
        'width'          : 854,
        'height'        : 480,
        'titleShow'     : false,
        'href'          : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + "&autoplay=1",
        'type'          : 'swf',
        'swf'            : {
          'wmode'            : 'transparent',
          'allowfullscreen'  : 'true'
        }
      });

    return false;
  });  
});                                  

function ShowBlock(HoveredElement){
  
  var Color         = "#21358c";
  var ArrowImg      = 'pics/arrow-air-flow.png';
  var SlideShowClass= 'air-flow';
  var SwitchValue   = HoveredElement.attr('class').replace("block ","");
  
  switch(SwitchValue)
  {
  case 'air-flow':
    Color           = '#21358c';
    ArrowImg        = 'pics/arrow-air-flow.png';
    SlideShowClass  = 'air-flow';
    break;
  case 'maintenance':
    Color           = '#00a0b0';
    ArrowImg        = 'pics/arrow-maintenance.png';
    SlideShowClass  = 'maintenance';
    break;
  case 'personnel':
    Color           = '#005957';
    ArrowImg        = 'pics/arrow-personnel.png';
    SlideShowClass  = 'personnel';
    break;
  case 'korita':
    Color           = '#019390';
    ArrowImg        = 'pics/arrow-korita.png';
    SlideShowClass  = 'korita';
    break;
  }
  
  $('.slideshow .slide-item').each(function(){
    if($(this).css('display') == 'block'){
      if(!$(this).hasClass(SlideShowClass)){
        $(this).fadeOut("medium");
        $('.slideshow .slide-item.' + SlideShowClass).fadeIn("medium");
      }
    }
  });
  
  $(".block-top", HoveredElement).animate({ borderBottomColor: Color, color: "#666666" }, 200);      
  $("a", HoveredElement).animate({ color: Color }, 200);
  $("a.web-link", HoveredElement).css('background','url(' + ArrowImg + ') top right no-repeat');
  
  $('.block-bg', HoveredElement).fadeIn("fast");
  $('.block-bottom', HoveredElement).css('display','block').animate({ height: "180px" }, 200);
}

function HideBlock(HoveredElement){
  $(".block-top", HoveredElement).stop(true, true).animate({ borderBottomColor: "#CCCCCC", color: "#BBBBBB" }, 200);
  $("a", HoveredElement).stop(true, true).animate({ color: "#cccccc" }, 200);
  $("a.web-link", HoveredElement).css('background','url(pics/arrow-gray.png) top right no-repeat');
  
  $('.block-bg', HoveredElement).stop(true, true).fadeOut("fast");
  $('.block-bottom', HoveredElement).stop(true, true).animate({ height: "0px" }, 200, function(){
    $(this).hide();
  });
}

/*
function ShowTopMenu(HoveredElement){
  var ElementToAnimate = HoveredElement.find('a');
  if(parseInt(ElementToAnimate.height()) <= 5){
    ElementToAnimate.animate({ height: "24px" });
  } else {
    ElementToAnimate.animate({ height: "5px" });
  }
}
*/

function GetYouTubeScreen(Url) {
  if(Url === null){ 
    return ""; 
  }
  
  var VideoId;
  var Results;

  Results = Url.match("[\\?&]v=([^&#]*)");
  VideoId = ( Results === null ) ? Url : Results[1];
  
  return "http://img.youtube.com/vi/"+VideoId+"/0.jpg";
}

function FormCheck(){
	if($('.newsletter input#__d').val().length <= 0 || $('.newsletter input#__e').val().length <= 0 || $('.newsletter input#__f').val().length <= 0){
		alert('Please fill in all fields.');
		return false;
	} else {
		return true;
	}
}
