var ddMenuPopupId = '';
var slideCurrent = 1;
var slideCount = 3;
var slideTimer;


$(document).ready(function(){
  /* Projects */
  $(".ProjectLink").mouseover(function(e){
    $(this).next().css("top", e.pageY+23);
    $(this).next().css("left", e.pageX-42);
    $(this).next().fadeIn("fast");
  });
  $(".ProjectLink").mouseout(function(e){
    $(this).next().fadeOut("fast");
  });
  $(".ProjectLink").mousemove(function(e){
    $(this).next().css("top", e.pageY+23);
    $(this).next().css("left", e.pageX-42);
  });
  
  /* News */
  $(".NewsLink").mouseover(function(e){
    $(this).parent().next().css("top", e.pageY+23);
    $(this).parent().next().css("left", e.pageX-42);
    $(this).parent().next().fadeIn("fast");
  });
  $(".NewsLink").mouseout(function(e){
    $(this).parent().next().fadeOut("fast");
  });
  $(".NewsLink").mousemove(function(e){
    $(this).parent().next().css("top", e.pageY+23);
    $(this).parent().next().css("left", e.pageX-42);
  });
  
  /* DDLinks */
  $(".DDArrow").mouseout(function(e){
    if ((!isMouseInMenu($("#" + ddMenuPopupId), e)) && (!isMouseInMenuItem(this, e))) {
      $("#" + ddMenuPopupId).fadeOut("fast");
      ddMenuPopupId = "";
    }
  });

  /* DDMenu */
  $(".DropdownMenu").mouseout(function(e){
    if ((!isMouseInMenu(this, e)) && (!isMouseInMenuItem($(this).prev(), e))) {
      $(this).fadeOut("fast");
      ddMenuPopupId = "";
    }
  });
  $(".DropdownMenu2").mouseout(function(e){
    if ((!isMouseInMenu(this, e)) && (!isMouseInMenuItem($(this).prev(), e))) {
      $(this).fadeOut("fast");
      ddMenuPopupId = "";
    }
  });
  $(".DropdownMenu3").mouseout(function(e){
    if ((!isMouseInMenu(this, e)) && (!isMouseInMenuItem($(this).prev(), e))) {
      $(this).fadeOut("fast");
      ddMenuPopupId = "";
    }
  });

  /* Banner */
  $(".Slide").click(function(e){
    slideSwitch(this);
  });
  
  startAutoSwitch();

  /* Document onload parse URLs */
  /* parseDownloadURLs(); */

  $(".SectionMenu > ul > li > *:first").addClass("Highlighted");
});


function isMouseInMenu(object, event) {
  var menuItemOffset = $(object).offset();

  if (((event.pageX > menuItemOffset.left) && (event.pageX < menuItemOffset.left + $(object).width())) && 
      ((event.pageY > menuItemOffset.top) && (event.pageY < menuItemOffset.top + $(object).height()))) {
    return true;
  }
  else return false;
}


function isMouseInMenuItem(object, event) {
  var menuItemOffset = $(object).offset();

  if (((event.pageX > menuItemOffset.left) && (event.pageX < menuItemOffset.left + $(object).width() + 12)) && 
      ((event.pageY > menuItemOffset.top) && (event.pageY < menuItemOffset.top + $(object).height()))) {
    return true;
  }
  else return false;
}


function DDMenuPopup(object, popupId, invSign) {
  if (invSign == 0) { leftOffset = 47 }
  if (invSign == 1) { leftOffset = 425 }
  if (invSign == 2) { leftOffset = 185 }
  
  if ((ddMenuPopupId != popupId) && (ddMenuPopupId != '')) {
    $("#" + ddMenuPopupId).fadeOut("fast");
    ddMenuPopupId = "";
  }
  
  var menuItemOffset = $(object).offset();

  $("#" + popupId).css("top", menuItemOffset.top + $(object).height() - 1);
  $("#" + popupId).css("left", menuItemOffset.left + $(object).width()/2 - leftOffset);
  $("#" + popupId).fadeIn("fast");
  
  ddMenuPopupId = popupId;
}


function ToggleInformation() {
  $(".InformationContent").slideToggle("fast");
}


function openOrderWindow(orderId, groupId) {
  $("#"+orderId).load("/fcgi-bin/cman/cman.fcgi", { module: "orders", object: "form", action: "get", groupid: groupId });
  $("#"+orderId).modal({onOpen: function (dialog) {
    dialog.overlay.fadeIn('fast', function () {
      dialog.container.fadeIn('fast');
    });
    dialog.data.show();
  }});
}


function openVotingsWindow(votingId) {
  $("#"+votingId).load("/fcgi-bin/cman/cman.fcgi", { module: "votings", object: "voting", action: "get", placeid: "1" });
  $("#"+votingId).modal({onOpen: function (dialog) {
    dialog.overlay.fadeIn('fast', function () {
      dialog.container.fadeIn('fast');
    });
    dialog.data.show();
  }});
}


function slideSwitch(object) {
  stopAutoSwitch();

  $(".N" + slideCurrent).removeClass("Current");
  $(object).addClass("Current");
  
  if ($(object).hasClass("N1")) { slideCurrent = 1 }
  if ($(object).hasClass("N2")) { slideCurrent = 2 }
  if ($(object).hasClass("N3")) { slideCurrent = 3 }

  $(".SlideArrow").animate( {right: $(".Current").css("right")}, "fast");
  slideScroll(slideCurrent);

  startAutoSwitch();
}


function slideScroll(slideId) {
  var leftOffset = (slideId-1)*960;
  $(".BanContainer").animate( {left: "-" + leftOffset + "px"}, "fast");
}


function startAutoSwitch() {
  slideTimer = setTimeout("slideNext()", 10000);
}


function stopAutoSwitch() {
  clearTimeout(slideTimer);
}


function slideNext() {
  stopAutoSwitch();

  $(".N" + slideCurrent).removeClass("Current");

  (slideCurrent == 3) ? slideCurrent = 1 : slideCurrent = slideCurrent + 1;

  $(".N" + slideCurrent).addClass("Current");
  $(".SlideArrow").animate( {right: $(".Current").css("right")}, "fast");
  slideScroll(slideCurrent);

  startAutoSwitch();
}


function helpfulVoteSubmit(message) {
  var pageURL = window.location;
  
  $(".HelpfulVote").html('<p>Это то, что вы искали? <span class="Answer"><em>Спасибо за ответ!</em></span></p>');
  $.get("/fcgi-bin/cman/cman.fcgi", { module: "orders", object: "order", action: "submit", groupid: "1", helpfulanswer: message, pageurl: pageURL });
}


//function parseDownloadURLs() {
//  var re = /\/download\//;
//  var filePath = $(".TextContent a[href^='/download/']").attr("href");
//
//  if (filePath) {
//    filePath = filePath.replace(re, "");
//    $(".TextContent a[href^='/download/']").attr("href","javascript:openRegformWindow('" + filePath + "')").addClass("Highlighted");
//  }
//}


function parseDownloadURLs() {
  var re = /\/download\//;
  var filePath;

  $(".TextContent a[href^='/download/']").each(function(i) {
    filePath = $(this).attr("href");
    filePath = filePath.replace(re, "");
    $(this).attr("href","javascript:openRegformWindow('" + filePath + "')");
  });
}


function openRegformWindow(filePath) {
  $("#RegForm").load("/fcgi-bin/cman/cman.fcgi", { module: "storage", object: "regform", action: "get", groupid: "1", filepath: filePath }, function(){
    if ($.cookie('cman-storage-visitor') && ($.cookie('cman-storage-visitor') != "")) {
      $(".IsAuthorized").toggle();
      $(".simplemodal-container").css("height", 125);
    }
    else {
      $(".IsNotAuthorized").toggle();
      $(".simplemodal-container").css("height", 205);
    }
  });

  $("#RegForm").modal({onOpen: function (dialog) {
    dialog.overlay.fadeIn('fast', function () {
      dialog.container.fadeIn('fast');
    });
    dialog.data.show();
  }});
}


function toggleRegForm() {
  $(".simplemodal-container").animate({height: 475});
  $(".RegisterVisitor").slideDown();
}


function checkToMail() {
  var formLocation = $("input[name='location']").val();
  
  var re = /tomail=./;

  if ($("#tomail2").attr("checked")) {
    formLocation = formLocation.replace(re, "tomail=0");
  }
  if ($("#tomail3").attr("checked")) {
    formLocation = formLocation.replace(re, "tomail=1");
  }

  $("input[name='location']").attr("value", formLocation);
}
