jQuery(function($){


  //animate slide
  var SpeedAnimate = 1000;



  $( "#show" ).click(function() {
    if (!$( "#show" ).data('animation_started')) {
      $( "#show" ).data('animation_started', true);
      $("#slide").animate({"left": "+=200%"}, SpeedAnimate, function(){$( "#show" ).data('animation_started', false);});
      $("#show").animate({"left": "-55px"}, SpeedAnimate);
      $(".map").css("visibility","hidden");
      $(".gallery_close").animate({"right": "-=50px"}, SpeedAnimate).css("display","none");
      $(".gallery").css("display","none");
    }
    

  });


  
  $( ".close" ).click(function() {
    $("#slide").animate({"left": "-=200%"}, SpeedAnimate);
    $("#show").animate({"left": "-10px"}, SpeedAnimate);
    $(".gallery_close").animate({"right": "+=45px"}, SpeedAnimate).css("display","block");
  });

  
  $(".body_gall .gallery_close").animate({"right": "+=45px"}, SpeedAnimate).css("display","block");

  $(".body_gall .gallery_close").click(function(evt){
    evt.preventDefault();
    $(this).animate({"right": "-=50px"}, SpeedAnimate, function () {
    history.back();
    });
  });
  $('.body_map #show').animate({"left": "+=45px"}, SpeedAnimate).css("display","block");
  $('.body_map #show').click(function(evt){
    evt.preventDefault();
    $(this).animate({"left": "-=50px"}, SpeedAnimate, function () {
    history.back();
    });
  });

  $('a.gallery_1').click(function(evt){
    evt.preventDefault();
    var gall_click = $(this);
    $("#slide").animate({"left": "-=200%"}, SpeedAnimate, function(){
    window.location = gall_click.attr('href');
    $("#slide").animate({"left": "+=200%"}, SpeedAnimate);
    });
  });
  
  $('a.map').click(function(evt){
    evt.preventDefault();
    var map_click = $(this);
    $("#slide").animate({"left": "-=200%"}, SpeedAnimate, function(){
    window.location = map_click.attr('href');
    $("#slide").animate({"left": "+=200%"}, SpeedAnimate);
    });
  });


  //resize function
  $(window).resize(function(){

 /* //vertical middle
  var wHeight = $(window).height();
  var hHeight = $('#header').height();
  var cHeight = wHeight - hHeight;
  $('#slide').each(function(){
    var bHeight = $(this).height();

    if ( cHeight > bHeight ){
    $(this).css('margin-top',  Math.floor(((cHeight) - (bHeight))/2) + 'px');
   // $(this).css('margin-bottom',  Math.floor(((cHeight) - (bHeight))/2) + 'px');
    }
    else {
    $(this).css('margin-top', 20 + 'px');
    $(this).css('margin-bottom', 20 + 'px');
    }
  });
   marginTop = parseInt($('#slide').css('margin-top'), 10);   */

     $('.accord_desc').each(function(){
  
    $(this).parent().parent().hover(function(){
    
    
      var accordHeight = $(this).height();
      console && console.log(accordHeight);
      
      $('#slide').css('margin-top', (marginTop - Math.floor(accordHeight)/2) + 'px');
      $('#slide').css('margin-bottom', (marginTop - Math.floor(accordHeight)/2) + 'px');
    
    });  
  });

  // menu
  var menu_topWidth = $('#menu_top').width();
  var logoWidth=$("#logo").width();
  var langWidht=$("#lang").width();
  var findWidth=$("#find_1").outerWidth(true);
  var menuWidth=$("#menu").width();
  var menu =  menu_topWidth - logoWidth - langWidht - findWidth;
  var aSize = $("#menu > ul > li > a").size();
  var x = (menu - menuWidth)/ aSize;

  $('#menu > ul > li > a').each(function(){
    var aWidth = $(this).width();
    $(this).css('width', aWidth + Math.floor(x) - 1 + 'px' );
  });

  //header_title 
  var headerWidth = $('.header_padding').width(); 
  var header_links = $(".header_links > ul > li ").size();
  var liWidth = $(".header_links > ul > li ").width();
  var linksWidth = header_links*liWidth;      

  $('.header_title').each(function(){

  var titleWidth = $(this).outerWidth();
  var titleWidth = headerWidth - linksWidth;

  $(this).css('width', titleWidth - 20 + 'px');
  $(".header_links").css('width', linksWidth + 15 + 'px');
  var headerHeight = $('.header_padding').height();
  var titleHeight = $(this).height() + 5;

  if (headerHeight > titleHeight) {
    $(this).css('padding-top', ((headerHeight - $(this).height()) / 2) + 'px');
    $('.header_padding').css('padding-top', 10 + 'px');
    $('.header_padding').css('padding-bottom', 10 + 'px');

  }
  else {
    $(this).css('padding-top', 0 + 'px');
    $('.header_padding').css('padding-top', 30 + 'px');
    $('.header_padding').css('padding-bottom', 30 + 'px');

  }
  });

  //gallery button




  var descHeight = $(".desc_gall").height();
  var footerHeight = $('.footer_gallery').height();

  $(".desc_gall").css("padding-top", (footerHeight-descHeight)/2 + 'px');

  var map = $('.map').height();
  var map_back = $('.body_map #show').height();     




  });

  $(window).resize();
/*
$(".accordion > .accordion_click").each(function () {
$(this).click(function(){
$(this).parent().children('.accordion_desc').slideDown();
$(this).parent().parent().find('.acc_active').removeClass('acc_active').children('.accordion_desc').slideUp().parent().children('.accordion_click').find('span').removeClass('span_active').addClass('span_not_active');
$(this).find('span').addClass('span_active');
$(this).parent().addClass('acc_active');
var c = $(this).parent().height() - $(this).height();
var d = $(this).parent().children('.accordion_desc').find('div').outerHeight(); 
console && console.log(Math.floor(d)/2);
console && console.log(marginTop);
$('#slide').css('margin-top', (marginTop - Math.floor(d))/2 + 'px');
});
});*/



  
  if(0)
  $('.login_1').click(function(){
    $('#cboxClose1').addClass('cClose');
  });

  $('.cboxOverlay').click(function(){
    $('#cboxClose').removeClass('cClose');
  });



  //plugins fieldfocus
  $('input.fieldfocus,textarea.fieldfocus').fieldFocus();
  $('.body_form .Nome').fieldFocus();
  $('#login input').fieldFocus();

  //plugins menu
  $('ul.main').superfish();
  $('ul.lang').superfish();


  //Table/add color every second line
  $("tr:nth-child(odd)").addClass("odd");


  //Header Links/remove border from last link   
  $(".header_links li:last-child").addClass("rem");


  //plugins colorbox
  $(".inline-gallery a").colorbox();
  
  if(0)
  $('a.login_1').colorbox({onOpen: function () {$('#colorbox #cboxWrapper').addClass('closebox');}, onClosed: function () {$('#colorbox #cboxWrapper').removeClass('closebox');}});

  $(".body_accordion .accord_2 a").click(function(){
  $('body').addClass('body_colorbox');
  });
  $('#cboxClose').click(function(){
  $('body').removeClass('body_colorbox');
  });
  $('#cboxOverlay').click(function(){
  $('body').removeClass('body_colorbox');
  });

  //plugins gallery

  // $( "#accordion" ).accordion({
  // collapsible: true,
  // animated: "fade"
  // });





  //plugins for form page
  /* -A121
  $('.body_form .selectBlock').sSelect();
  $(".body_form ul.newList li").first().css('display', 'none');
  */
  /* A121 */
  $('.selectBlock').each(function(i,el){
    var opt_select = { defaultText: (($(this).attr("title") != "")?($(this).attr("title")):(general_text_for_select)) ,ddMaxHeight: '200px' };
    
    $(this).sSelect(opt_select);
    
  });
  /* -A121 */
  
  $('.body_form .defaultP input').ezMark();
  $('.body_form .customP input[type="checkbox"], .table .customP input[type="checkbox"]').ezMark({checkboxCls: 'ez-checkbox-green', checkedCls: 'ez-checked-green'});

  //add class custom to checkbox
  $('.defaultP ').last().addClass('custom');
  $('.ez-checkbox-green').prependTo(' .custom span');


  $('.lang > li > a').click(function () {
    return false;
  });



  var c= 0;
  c=$('.gall_img div').size();
  var a = $('li.current').index();
  for( var n = 0 ; n < c ; n++){
    $('<img src="images/loading_gall.gif" alt="none">').appendTo('.slides_container').wrap($('<div class="slide"><div class="item"></div></div>'));
  };
  $('.item img').eq(0).attr('src',$('span.src').eq(0).text());
  $('.item img').eq(0).attr('alt',$('span.alt').eq(0).text());

  $('#slides').slides({
    generateNextPrev: true
  });
  $('.next').clone(true).appendTo(' .desc_slides ');
  var origPager = $('ul.pagination');
  origPager.clone(true).prependTo('.desc_slides');
  origPager.remove();
  $('.prev').clone(true).prependTo('.desc_slides');
  $('#slides .prev').text('');
  $('#slides .next').text('');
  $('#slides .desc_slides .prev').text('<');
  $('#slides .desc_slides .next').text('>');
  $("div.desc_gall").text($("#slides .slide img").attr("alt"));


  $('#slides .next').addClass('next_gall');
  $('#slides .desc_slides .next').removeClass('next_gall');
  $('#slides .prev').addClass('prev_gall');
  $('#slides .desc_slides .prev').removeClass('prev_gall');

  //add class on click and hover


  $(".next_gall").mouseover(function(){
    $(this).addClass('next_hover');
  }).mouseout(function(){
    $(this).removeClass('next_hover');
  });

  $(".next_gall").mousedown(function(){
    $(this).addClass('next_click');
  }).mouseup(function(){
    $(this).removeClass('next_click');
  });

  $(".prev_gall").mouseover(function(){
    $(this).addClass('prev_hover');
  }).mouseout(function(){
    $(this).removeClass('prev_hover');
  });

  $(".prev_gall").mousedown(function(){
    $(this).addClass('prev_click');
  }).mouseup(function(){
    $(this).removeClass('prev_click');
  });






    


});
