/*
 2009 (c) Eugene Krivoruchko
 FlipFlip.Ru
 mailto: eugene@krivoruchko.info
*/

var section = new Array();
var nowSection;
var urlSection = Array('', 'about', 'gallery', 'service', 'feedback');
var height;

var galleryOptions = {
	wrapperClassName: 'rounded-white',
	transitions: ['expand', 'crossfade']
};

/* Кэш картинок */

jQuery.preloadImages = function() {
 for(var i = 0; i<arguments.length; i++) {
  jQuery("<img>").attr("src", arguments[i]);
 }
}

$(document).ready(function(){
 height = $('#main').height();
 if (height == 0) height = 461;

 section[$('.menu ul li.select').attr('value')] = true;
 if ($.browser.msie && /6.0/.test(navigator.userAgent)) {
  for (x in section) {
   if (section[x] == true) {
    $("." + urlSection[x] + " h2 a").css("background-image", "url(/images/label_" + urlSection[x] + "_hover.gif)");
   }
  }
 }

 /* Наведение на ФлиФлип */

 $(".copyright h2 a").mouseover(function(){
  $(this).css("background-image", "url(/images/flipflip_hover.gif)");
  return false;
 });

 $(".copyright h2 a").mouseout(function(){
  $(this).css("background-image", "url(/images/flipflip.gif)");
  return false;
 });

 /* Наведение на Меню */

 $(".menu ul li").mouseover(function(){
  if (section[$(this).attr("value")] != true) {
   var attrClass = $(this).attr("class");
   $("." + $(this).attr("class")).addClass('select');
   if ($.browser.msie && /6.0/.test(navigator.userAgent)) $("." + attrClass + " h2 a").css("background-image", "url(/images/label_" + attrClass + "_hover.gif)");
  }
  return false;
 });

 $(".menu ul li").mouseout(function(){
  if (section[$(this).attr("value")] != true) {
   $(this).removeClass('select');
   if ($.browser.msie && /6.0/.test(navigator.userAgent)) $("." + $(this).attr("class") + " h2 a").css("background-image", "url(/images/label_" + $(this).attr("class") + ".gif)");
  }
  return false;
 });

 /* Клик на лого */

 $(".logotype h1 a").click(function(){
  $(".body").addClass('face');
  if ($.browser.msie && /6.0/.test(navigator.userAgent)) $(".body").css("background-image", "url(/images/background_main_low_with_face.jpg)");

  $(".main-form").fadeOut("slow");

  for (x in section) {
   section[x] = false;
   $(".menu ul li").removeClass('select');
   if ($.browser.msie && /6.0/.test(navigator.userAgent)) $("." + urlSection[x] + " h2 a").css("background-image", "url(/images/label_" + urlSection[x] + ".gif)");
  }

  $("#main").html('');

  return false;
 });


 /* Клик на пункт меню */

 $(".menu ul li").click(function(){
  $(".body").removeClass('face');
  if ($.browser.msie && /6.0/.test(navigator.userAgent)) $(".body").css("background-image", "url(/images/background_main_low.jpg)");

  $(".main-form").fadeIn("slow");

  if (!section[$(this).attr("value")]) {
   nowSection = $(this).attr("value");

   $("#main").fadeOut("slow", function() {
    $.ajax({
     url: '/' + urlSection[nowSection] + '/?ajax=true',
     method: 'GET',
     cache: true,
     dataType: 'html',
      success: function(data) {
      $("#main").html(data);
      $("#main").fadeIn("slow");

      if (nowSection == 2) gallery();
      else $('#main').height(height);
     }
    });
    });
   }

  for (x in section) {
   section[x] = false;
   $(".menu ul li").removeClass('select');
   if ($.browser.msie && /6.0/.test(navigator.userAgent)) $("." + urlSection[x] + " h2 a").css("background-image", "url(/images/label_" + urlSection[x] + ".gif)");
  }

  section[$(this).attr("value")] = true;
  $(".menu ul li[value=" + $(this).attr("value") + "]").addClass('select');
  if ($.browser.msie && /6.0/.test(navigator.userAgent)) $(".menu ul li[value=" + $(this).attr("value") + "] h2 a").css("background-image", "url(/images/label_" + urlSection[$(this).attr("value")] + "_hover.gif)");

  return false;
 });


 /* Кэш */
 $.preloadImages("/images/ajax-loader.gif", "/images/arrow_right_hover.gif", "/images/arrow_right.gif", "/images/arrow_left_hover.gif", "/images/arrow_left.gif", "/images/background_main_low_with_face.jpg", "/images/background_main_low.jpg", "/images/background_form.png", "/images/label_about_hover.gif", "/images/label_service_hover.gif", "/images/label_gallery_hover.gif", "/images/label_feedback_hover.gif", "/images/round_no_hover.png", "/images/round_hover.png");

 /* А вдруг галерея */
 gallery();
});

function gallery() {
 var intUlGallery = $("ul.gallery").get().length;

 if (intUlGallery > 0) {
  $('#main').height((height - 26));

  if (intUlGallery > 1) {
   $('.page a').mouseover(function() {
   	var attrClass = $(this).attr("class");
   	$(this).addClass('select');

   	if ($.browser.msie && /6.0/.test(navigator.userAgent)) { $(this).css('background-image', 'url(/images/arrow_' + attrClass + '_hover.gif)'); }
   });
   $('.page a').mouseout(function() {
   	$(this).removeClass('select');
   	if ($.browser.msie && /6.0/.test(navigator.userAgent)) { $(this).css('background-image', 'url(/images/arrow_' + $(this).attr('class') + '.gif)'); }
   });

   $('.page a.right').click(function () {
    if (page == intUlGallery) page = 1;
    else page++;

    $("ul.gallery").fadeOut('slow', function() {
     $("ul.gallery").eq((page - 1)).fadeIn('slow');
    });

    return false;
   });

   $('.page a.left').click(function() {
    if (page == 1) page = intUlGallery;
    else page--;

    $("ul.gallery").fadeOut('slow', function() {
     $("ul.gallery").eq((page - 1)).fadeIn('slow');
    });

    return false;
   });

  }
 }
}