var language=1;
var hideDiv=true;

document.onclick = hidePopup

function setActive(id){
    var arrLink=$("a.activeTopButton")

    var link=$("#"+id)
    if(arrLink.length!=0){
        if(arrLink.attr("id")!=id){
           arrLink.removeClass("activeTopButton")
        }
        else return false
    }

    link.addClass("activeTopButton")
    return true
}

function addRubrActive(id){

    var link=$("#"+id)
    link.addClass("activeTopButton")
    if(link.hasClass("lev1")|| link.hasClass("lev2"))
    {
         $("#div"+id).addClass("activeTopButton")
    }
}

function removeAllActiveButton()
{
   var sub=$("#lev1_sub")
   if(sub.css("display")=="block")
   {
     sub.css("display","none")
     var links=$("a.subactivelink")

     links.each(function (){

           $(this).removeClass("subactivelink")
           $(this).removeClass("activeTopButton")
           $(this).css("display","none")

           $("#div"+$(this).attr("id")).removeClass("activeTopButton")

	   if($(this).hasClass("lev1"))
	   {
	      $("#div"+$(this).attr("id")).removeClass("lev1")
	   }
	   else if($(this).hasClass("lev2"))
	   {
	      $("#div"+$(this).attr("id")).removeClass("lev2")
	   }
	   $("#div"+$(this).attr("id")).css("display","block")
     })

     var act_div=$("a.activeTopButton")
     act_div.removeClass("activeTopButton")
   }
}

function onTopButtonClick(id){

      setActive(id)

      removeAllActiveButton()

      var hash = $("#"+ id).attr("href")
      hash = hash.replace(/^.*#/, '')

      $.historyLoad(hash)

      //alert("button:"+hash)

      $("#alpha_content").empty();
      $("#alpha_content").load("showVMGAlpha.do?language="+language+"&objectTypeName='"+id+"'")
}


function resizeTopArea()
{
    var text_area_top=$("#text_area_top")
    var text_area_top_content=$("#text_area_top_content")
    var button_div=$("#buttons")

    if(text_area_top_content.length!=0 && text_area_top_content.height()>button_div.height())
    {
       text_area_top.css("height", "100%")
    }
    $("#text_area_bottom").css("display", "none")
}

function resizeBottomArea()
{
    if($("#text_area_top > img").length>0)
    {
       var text_area_bottom=$("#text_area_bottom")
       var button_div=$("#buttons")

       var text_area_bottom_height=text_area_bottom.height() + text_area_bottom.position().top
       var button_div_height=button_div.height()+button_div.position().top

       if(text_area_bottom_height<button_div_height)
       {
          text_area_bottom.css("height",text_area_bottom.height() + button_div_height-text_area_bottom_height-22+"px")
       }
    }
}

function onRubrButtonClickAction(id, levClass)
{
     levClass=$.trim(levClass)
     id=$.trim(id)

     var sub_lev_link=$("a." + id)
     var lev_link=$("a." + levClass)

     var prev_active_link=$("a.activeTopButton div." + levClass)

     if(prev_active_link.length!=0)
     {
	var prev_link_id=prev_active_link.attr("id").replace("div", "")

	if(prev_link_id==id)return false
	else
	{
           $("#" + prev_link_id).removeClass("activeTopButton")
           //$("#div" + prev_link_id).removeClass("activeTopButton")
           var prev_link_arr=$("a."+prev_link_id)

           prev_link_arr.each(function (){

                   $(this).removeClass("subactivelink")
                   $(this).removeClass("activeTopButton")
                   $(this).css("display","none")

                   //$("#div"+$(this).attr("id")).removeClass("activeTopButton")

		   if($(this).hasClass("lev1"))
		   {
		      $("#div"+$(this).attr("id")).removeClass("lev1")
		   }
		   else if($(this).hasClass("lev2"))
		   {
		      $("#div"+$(this).attr("id")).removeClass("lev2")
		   }
		   $("#div"+$(this).attr("id")).css("display","block")
            })

           /*dopisat' rekursivno*/
	}
     }

     if($("#"+ id).hasClass("main"))
     {

        var main_act_link=$("a.activeTopButton")
        if(main_act_link.length!=0)
        {
          var act_id=main_act_link.attr("id")
	  if(act_id!=id)
	  {
	     main_act_link.removeClass("activeTopButton")
	     if(id!="lev1")
	     {
	       $("#lev1_sub").css("display","none")
	     }
             addRubrActive(id)
	  }
        }
        else
        {
           addRubrActive(id)
        }
     }
     else
     {
         addRubrActive(id)
     }

     if(sub_lev_link.length!=0)
     {
           sub_lev_link.each(function (){
		   $(this).addClass("subactivelink")
		   if($(this).hasClass("lev1"))
		   {
		      $("#div"+$(this).attr("id")).addClass("lev1")
		   }
		   else if($(this).hasClass("lev2"))
		   {
		      $("#div"+$(this).attr("id")).addClass("lev2")
		   }
		   $("#div"+$(this).attr("id")).css("display","block")

            })

            //resizeBottomArea()

     }
     /*
     var hash = $("#"+ id).attr("href")
     hash = hash.replace(/^.*#/, '')

     $.historyLoad(hash)

     if($("#"+ id).hasClass("main"))
     {
            var act_top_button=$("a.main.activeTopButton")
            if(act_top_button.length!=0)
	    {
		 var id=act_top_button.attr("id")
		 if(id=="lev1") id="subjtipology"

		 $("#alpha_content").empty();
                 $("#alpha_content").load("showVMGAlpha.do?language="+language+"&objectTypeName='"+id+"'")
	    }
     }*/
}

function onRubrButtonClick(id, levClass)
{
    var sub=$("#lev1_sub")

    if(id=="lev1")
    {
            if(sub.css("display")=="none")
            {
               sub.css("height","100%")
               sub.css("display","block")

               $("#lev1_sub div").css("height","100%")
               $("#lev1_sub div").css("display","block")

               onRubrButtonClickAction(id, levClass)
            }
            else
            {
               removeAllActiveButton()
               sub.css("display","none")
            }
     }
     else
     {
        onRubrButtonClickAction(id, levClass)
     }
}

// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser

var prevUrl=null;
function pageload(hash) {

    // hash doesn't contain the first # character.

    if(hash && (hash!="start" && hash.indexOf("rubr")==-1)) {
        // restore ajax loaded state
        /*if($.browser.msie) {
            // jquery's $.load() function does't work when hash include special characters like aao.
            hash = encodeURIComponent(hash)
        }*/

        restoreRubrikatorState(false,hash)

        $("#text_area_top").load(hash,resizeTopArea)

    } else {
        // start page
        var text_area_top=$("#text_area_top")
        var text_area_bottom=$("#text_area_bottom")

        if(hash.indexOf("rubr")==-1)
        {

            if($("#text_area_top #main_img").length==0)
	    {
		text_area_top.empty()

		$(document.createElement("img"))
		     .attr({ id: 'main_img', src: 'images/main.jpg '})
		     .appendTo(text_area_top)
	    }

            text_area_top.css("height", "284px")
            text_area_bottom.css("display", "block")
            text_area_bottom.css("height", "63px")
            removeAllActiveButton()
            $("a.activeTopButton").removeClass("activeTopButton")
        }
        else
        {
            var button=$("a.activeTopButton")
            if(button.length!=0 && button.attr("id")=="lev1"){
               restoreRubrikatorState(false,hash)
            }
            else restoreRubrikatorState(true,hash)


        }

    }

    //restoreAlphabetState(hash)
}

function restoreRubrikatorState(openRubrikator,hash)
{
    var idx=hash.indexOf("object=")
    var object=null;

    if(idx!=-1)
    {
       var object=hash.substr(idx+7)
       idx = object.indexOf("&")
       if(idx!=-1)
       {
	   object=object.substr(0,idx)
       }

       var rubrElem=$("#"+object)
       if($("#"+object).length!=0)
       {
	       rubrElem.addClass("activeTopButton")
	       var children=$("a."+ object)
	       if(children.length!=0)
	       {
		   children.addClass("subactivelink")
		   children.css("display","block")
	       }

	       var classId=rubrElem.attr("class")
	       idx= classId.indexOf(" ");
	       if(idx!=-1)
	       {
		   classId=classId.substr(0,idx)
		   var parent=$("#"+classId)
		   if(parent!=null)
		   {
		      parent.addClass("activeTopButton")
		      children=$("a."+ classId)
		      children.addClass("subactivelink")
		      children.css("display","block")
		   }
	       }
       }
       else object=null;
    }

    var sub=$("#lev1_sub")

    if((openRubrikator && sub.css("display")=="none") || null!=object)
    {
        sub.css("height","100%")
	sub.css("display","block")

        $("#lev1").addClass("activeTopButton")

	$("#lev1_sub div").css("height","100%")
	$("#lev1_sub div").css("display","block")

        var sub_link=$("#lev1_sub a.lev1")

        if(sub_link.length!=0) {
           sub_link.css("display","block")
        }
    }

    var idx=hash.indexOf("objectTypeName=")
    var objectTypeName=null;

    if(idx!=-1)
    {
       objectTypeName=hash.substr(idx+15)
       idx = objectTypeName.indexOf("&")
       if(idx!=-1)
       {
	   objectTypeName=objectTypeName.substr(1,idx-2)
           $("#"+objectTypeName).addClass("activeTopButton")
       }
    }

}

function restoreAlphabetState(hash)
{
  var idx=hash.indexOf("&textValue")
  var idxSearch=hash.indexOf("#search.do")
  if(idx!=-1 && idxSearch!=-1)
  {
     $("#alphabet a").each(function() {

       var href=$(this).attr("href")
       var letter=href.substr(idx)

       var newHref=hash.substr(idxSearch);
       newHref=newHref.replace(letter, "")+letter

       $(this).attr("href", newHref)

     })


  }
  else
  {
      var act_top_button=$("a.main.activeTopButton")

      if(act_top_button.length!=0)
      {
         var id=act_top_button.attr("id")
         if(id=="lev1") id="subjtipology"
         $("#alpha_content").empty();
         $("#alpha_content").load("showVMGAlpha.do?objectTypeName='"+id+"'")
      }
      else
      {
         $("#alphabet a").attr("href", "#")
      }

  }

}

function onListLinkAction(link)
{
  var hash = $(link).attr("href")
  hash = hash.replace(/^.*#/, '')
  $.historyLoad(hash)
}


function onPageClickAction(link)
{
  var hash = $(link).attr("href")
  hash = hash.replace(/^.*#/, '')
  $.historyLoad(hash)
}

function onTabClickAction(link)
{
  var hash = $(link).attr("href")
  hash = hash.replace(/^.*#/, '')
  $.historyLoad(hash)
}


function onLetterClick(linkID)
{
  $("a.activeLetter").removeClass("activeLetter")
  var currentLetter=$("#"+linkID)
  currentLetter.addClass("activeLetter")

  var hash = currentLetter.attr("href")
  hash = hash.replace(/^.*#/, '')
  //alert("alpha:"+hash)
  $.historyLoad(hash)
}


function showRequest(formData, jqForm, options) {

     var queryString = "search.do?"+$.param(formData);
     $("#text_area_top").load(queryString,resizeTopArea)
     return false;
}

function hidePopup(e)
{
   if(hideDiv){

	    var popups = new Array()
	    popups = document.getElementsByTagName('div')
	    for (i = 0; i < popups.length; i++){
            popId = new String(popups[i].getAttribute("id"))
            if (popId.indexOf('qs') == 0 || popId.indexOf('switch') == 0 || 'alphabet_letters'==popId){
                popups[i].style.display = 'none'

                if(popId=="qs_3174554")$("#ajaxDIV_3174555").css("display","block")
            }
	}
    }
    else{
       hideDiv=true
    }

}


function onClickAlphaAction() {

   var alphabet_letters=$("#alphabet_letters")

   if(0!=alphabet_letters.length && alphabet_letters.css("display")=="none")
   {
        alphabet_letters.css("display", "block")
        hideDiv=false
   }
}

//-----------------------------------Ready functions------------------------------------------//
$(document).ready(function(){

    //initForm();
    //$.historyInit(pageload, false);
    initDialog();

    $("#alpha_content").bind('click', function(event) {
          onClickAlphaAction();
    })

    var rubr=$("#lev1");
    var sub=$("#lev1_sub");

    if(!rubr.hasClass("activeTopButton") && sub.css("display")=="block")
    {
        $("#lev1_sub a.activeTopButton").removeClass("activeTopButton")
        rubr.addClass("notActiveMainRubrButton")
    }

})

/*
$("#alpha_content").ready(function() {

    restoreAlphabetState(location.href)
})
*/

$(function(){

      $(".textObject #CONTENT_value").dialog({
            autoOpen: false,
            position: ["center",200],
            minWidth: "550px",
            width: "600px",
            bgiframe: true,
            modal: true,
            resizable:false,
            dialogClass:"dialogClass",
            title: "Описание"

          });
          $("#openTextD").click(function(){
		    $("#CONTENT_value").css("display", "block");

		    $('.ui-widget-header').each(
		          function()
		          {
		             //$(this).css("background","#3D3DCF");
		             //$(this).css("border","#3D3DCF");

		    });

		    $('.ui-dialog').each(
		        function()
		        {
		          $(this).css("background","#eeefea");
		          $(this).css("padding","0");
		    });

		    $(".ui-dialog-titlebar").each(function(){

		          $(this).css("background","#665e56");
		          $(this).css("border","none");
		          $(this).css("height","21px");
		    });

		    $(".ui-dialog .ui-dialog-title").each(function(){

		          $(this).css("float","none");
		    });

		    $('.ui-dialog-content').each(function(){
		          $(this).css("padding","0");
		    });

		    $("#CONTENT_value").dialog("open");
         });
    });


/*
$(function () {
    $('.bubbleInfo').each(function () {
        var distance = 10;
        var time = 250;
        var hideDelay = 500;

        var hideDelayTimer = null;

        var beingShown = false;
        var shown = false;
        var trigger = $('.trigger', this);
        var info = $('.popup', this).css('opacity', 0);


        $([trigger.get(0), info.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                return;
            } else {
                beingShown = true;

                info.css({
                    top: -10,
                    left: 130,
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    left: '=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function() {
                    beingShown = false;
                    shown = true;
                });
            }

            return false;
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    top: '-=' + distance + 'px',
                    left: '=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });

            }, hideDelay);

            return false;
        });
    });
});

*/


$(function() {
    $(".carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        mouseWheel: true,
        visible: 1,
        circular: false,
        afterEnd: function(a) {
           var span=$("#currentNumber");
           var spanPrev=$("#prevNumber");
           if(a.prevObject.length>spanPrev.text())
           {
              span.text(parseInt(span.text())-1)
           }
           else span.text(parseInt(span.text())+1)

        },
        beforeStart: function(a) {
           var span=$("#prevNumber");
           var spanNumber=$("#number");
           var number=a.prevObject.length;
           span.text(number)
       }

    });
});

$(".imageDIV").ready(function(){

    $("img.imagePositionStyle").removeClass("imagePositionStyle");
});

function onRubrTopButtonClick()
{
    var sub=$("#lev1_sub")

    if(sub.css("display")=="none")
    {
       sub.css("height","100%")
       sub.css("display","block")

       $("#lev1_sub div").css("height","100%")
       $("#lev1_sub div").css("display","block")

       if($("div.subactivelink").length==0)onRubrButtonClickAction("lev1", "lev1")
       else $("#lev1").addClass("activeTopButton")

    }
    else
    {
       sub.css("display","none")
       sub.css("height","0")
       $("#lev1").removeClass("activeTopButton")
    }

}
