// Calendar
$(function calendar() {
	$(".calendar a").bind('mouseenter', function(event) {
		var w = $(this).next().width();
		var p = $(this).parent().width();
		$(this).next().css({'left' : -w/2 + p/2+'px'});
		$(this).prev().css({'left' : -25 + p/2+'px'});
		$(this).next().show();
		$(this).prev().show();
	});
	$(".calendar a").bind('mouseleave', function(event) {
		$(this).next().hide();
		$(this).prev().hide();
	});
});

// Fancy
$(document).ready(function() {
  if ($("#gallery").length > 0)
    $("#gallery a").fancybox({ 'overlayShow': false, 'padding': 1 }); 
});

// Navigation
$('.nav-interface a').live('mouseover', function(){
	$(this).stop().fadeTo("fast", 0);
}).live('mouseout', function(){
	$(this).stop().fadeTo("fast", 1);
});

// link to audi A5
$(document).ready(function(){
  if (window.location.hash == "#a5"){
    $('.audi_tt').hide();
    $('.att').removeClass('selected');
    $('.a5').addClass('selected');
    $('.audi_a5').show();
  }
});

$(document).ready(function(){
  if (window.location.hash == "#session1-1"){
    $('#session1-1').click();
    $('#page1').show();
    $('#page0').hide();
    $('#page2').hide();
    $('#jump-page0').removeClass('current');
    $('#jump-page1').addClass('current');
  }
  if (window.location.hash == "#session1-2")
    $('#session1-2').click();
  if (window.location.hash == "#session1-3")
    $('#session1-3').click();
  if (window.location.hash == "#session1-4")
    $('#session1-4').click();
  if (window.location.hash == "#session1-5")
    $('#session1-5').click();
  
  if (window.location.hash == "#audi_tt1")
    $('#audi_tt1').click();
  if (window.location.hash == "#audi_tt2")
    $('#audi_tt2').click();
  if (window.location.hash == "#audi_tt3")
    $('#audi_tt3').click();
  if (window.location.hash == "#audi_tt4")
    $('#audi_tt4').click();
  if (window.location.hash == "#audi_tt5")
    $('#audi_tt5').click();
    
  if (window.location.hash == "#pink-1")
    $('#pink-1').click();
  if (window.location.hash == "#pink-2")
    $('#pink-2').click();
  if (window.location.hash == "#pink-3")
    $('#pink-3').click();
  if (window.location.hash == "#pink-4")
    $('#pink-4').click();
  if (window.location.hash == "#pink-5")
    $('#pink-5').click();  
    
  if (window.location.hash == "#piper-3")
    $('#piper-3').click();
  if (window.location.hash == "#piper-6")
    $('#piper-6').click();
  if (window.location.hash == "#piper-2")
    $('#piper-2').click();
  if (window.location.hash == "#piper-14")
    $('#piper-14').click();
  if (window.location.hash == "#piper-17")
    $('#piper-17').click();
    
  if (window.location.hash == "#doug-2")
    $('#doug-2').click();
  if (window.location.hash == "#doug-6")
    $('#doug-6').click();
  if (window.location.hash == "#doug-13")
    $('#doug-13').click();
  if (window.location.hash == "#doug-4")
    $('#doug-4').click();
  if (window.location.hash == "#doug-15")
    $('#doug-15').click();
    
  if (window.location.hash == "#rings-03")
    $('#rings-03').click();
  if (window.location.hash == "#rings-05")
    $('#rings-05').click();
  if (window.location.hash == "#rings-13")
    $('#rings-13').click();
  if (window.location.hash == "#rings-17")
    $('#rings-17').click();
  if (window.location.hash == "#rings-20")
    $('#rings-20').click();
    
  
  if (window.location.hash == "#audi_a5_1")
    $('#audi_a5_1').click();
  if (window.location.hash == "#audi_a5_2")
    $('#audi_a5_2').click();
  if (window.location.hash == "#audi_a5_3")
    $('#audi_a5_3').click();
  if (window.location.hash == "#audi_a5_4")
    $('#audi_a5_4').click();
  if (window.location.hash == "#audi_a5_5")
    $('#audi_a5_5').click();
  
  if (window.location.hash == "#resept1")
    $('#resept1').click();
  if (window.location.hash == "#resept2")
    $('#resept2').click();
  if (window.location.hash == "#resept3"){
    $('#resept3').click();
    $('#page1').show();
    $('#page0').hide();
    $('#page2').hide();
    $('#jump-page0').removeClass('current');
    $('#jump-page1').addClass('current');
  }
  if (window.location.hash == "#resept4")
    $('#resept4').click();
  if (window.location.hash == "#resept5"){
    $('#resept5').click();
    $('#page1').show();
    $('#page0').hide();
    $('#page2').hide();
    $('#jump-page0').removeClass('current');
    $('#jump-page1').addClass('current');
  }
});

// Errors

$.fn.delay = function(time, callback){
  jQuery.fx.step.delay = function(){};
  return this.animate({delay:1}, time, callback);
}

$(document).ready(function(){
  $(".fieldWithErrors_f").show(function() {
    $(".error_f").animate({opacity: "show", left: "-=30"}, "500")
	});
	$(".fieldWithErrors_f").click(function() {
    $(".error_f").animate({opacity: "hide"}, "slow")
	});
	$(".fieldWithErrors_f").focus(function() {
    $(".error_f").animate({opacity: "hide"}, "slow")
	});
	
	$(".fieldWithErrors_i").show(function() {
    $(".error_i").animate({opacity: "show", left: "-=30"}, "500")
	});
	$(".fieldWithErrors_i").click(function() {
    $(".error_i").animate({opacity: "hide"}, "slow")
	});
	$(".fieldWithErrors_i").focus(function() {
    $(".error_i").animate({opacity: "hide"}, "slow")
	});
	
	$(".fieldWithErrors_o").show(function() {
    $(".error_o").animate({opacity: "show", left: "-=30"}, "500")
	});
	$(".fieldWithErrors_o").click(function() {
    $(".error_o").animate({opacity: "hide"}, "slow")
	});
	$(".fieldWithErrors_o").focus(function() {
    $(".error_o").animate({opacity: "hide"}, "slow")
	});
	
	
	$(".fieldWithErrors_fone").show(function() {
    $(".error_fone").animate({opacity: "show", left: "-=30"}, "500")
	});
	$(".fieldWithErrors_fone").click(function() {
    $(".error_fone").animate({opacity: "hide"}, "slow")
	});
	$(".fieldWithErrors_fone").focus(function() {
    $(".error_fone").animate({opacity: "hide"}, "slow")
	});
	
	$(".fieldWithErrors_email").show(function() {
    $(".error_email").animate({opacity: "show", left: "-=30"}, "500")
	});
	$(".fieldWithErrors_email").click(function() {
    $(".error_email").animate({opacity: "hide"}, "slow")
	});
	$(".fieldWithErrors_email").focus(function() {
    $(".error_email").animate({opacity: "hide"}, "slow")
	});
});


// Auth
$(document).ready(function(){
  var matches = /login=[^;]*/.exec(document.cookie);
  var login = matches && matches.length > 0 ? unescape(matches[0].substr(6)) : null;
  if (login && login.length > 0)
  {
    $(".height_form").html('<ul class="nav-input nav-interface" id="nav_auth"><li class="nav-exit"><a href="/cgi-bin/logout.rb"></a></li><li class="user_name"><span id="audi_user">' + login + '</span></li></ul>');
    $(".nav-club").show();
    
    if (login == "admin")
    {
      $('.admin_flag').show();
    }
  }

  var bad_login = function()
  {
    if ($(".error_login").length == 0)
      $("#loginform").append('<div class="error_login"><div>Ошибка в логине</div><span></span></div>');
    $(".error_login").animate({opacity: "show", right: "-=30"}, "slow");
    $("input[name=login]").click(function(){
      $(".error_login").animate({opacity: "hide"}, "slow");
    });
    $("input[name=login]").focus(function(){
      $(".error_login").animate({opacity: "hide"}, "slow");
    });
  };
  

  var bad_password = function()
  {
    if ($(".error_pwd").length == 0)
      $("#loginform").append('<div class="error_pwd"><div>Ошибка в пароле</div><span></span></div>');
    $(".error_pwd").animate({opacity: "show", right: "-=30"}, "slow");
    $("input[name=password]").click(function(){
      $(".error_pwd").animate({opacity: "hide"}, "slow");
    });
     $("input[name=password]").focus(function(){
      $(".error_pwd").animate({opacity: "hide"}, "slow");
    });
  };


  $("#loginform").submit(function()
  {
    $.ajax({
      url: "/cgi-bin/login.rb", 
      type: "POST",
      data: $("#loginform").serialize() + "&ajax=1", 
      success: function(data)
      {
        if ($('input[name=login]').attr("value") == "admin")
	  window.location = '/cgi-bin/register.rb';
	else
	  window.location = '/club.html';
      },
      error: function(data)
      {
	if (data.responseText == "bad-login")
	  bad_login();
	else if (data.responseText == "bad-password")
	  bad_password();
      }
    });
    return false;
  });

  if (window.location.search == "?status=bad-login")
    bad_login();
  else if (window.location.search == "?status=bad-password")
    bad_password();
});

// Audi info
$('.audi_info').click( function(){
  $('.audi_desc').slideToggle('slow');
  $(this).toggleClass('selected_info');
});

// Admin flags
$(document).ready(function(){
  if (window.location.search == "?source=OPERATOR")
    $('.audi_operator').css("color","#B89867");
  
  if (window.location.search == "?source=USER")
    $('.audi_user').css("color","#B89867");
    
  if (window.location.search == "")
    $('.audi_all').css("color","#B89867");

});


// Thanks
$(document).ready(function(){
  if ($("#thanks").length > 0 && (document.cookie || "").indexOf("thanks=1") == -1)
  {
    document.cookie = "thanks=1";
    $("#thanks").show();
  }
});

