var _http_path = 'http://www.agmeka.lt';
var click = 0;

$(document).ready(function() {
  function openByHash() {
    var str = location.hash;
    if( str.length > 0 ) {
      var iid = str.substr(0,str.length);
      var parentQ = $(iid).parent().parent().children('.question_');
      var parentA = $(iid).parent().parent().children('.answer');
      
      if( parentQ.hasClass('open') ) {
        parentQ.removeClass('open');
        parentQ.addClass('close');
        parentA.show();
      }
    }
  }
  
  openByHash();
  
  $('.bigimage img').show();
  
  $('a').click(function() {
    var showHide = $(this).parent();
    if( showHide.hasClass('question_') ) {
      click++;
      if( showHide.hasClass('open') ) {
        showHide.removeClass('open');
        showHide.addClass('close');
        showHide.parent().children('.answer').show();
      }
      else {
        showHide.removeClass('close');
        showHide.addClass('open');
        showHide.parent().children('.answer').hide();
      }
    }

    if( $(this).parent().parent().children('.question_').hasClass('question_') == false ) {
      var url = $(this).attr('href');
      var hashPos = url.indexOf("#");

      if( hashPos > 0 ) {
        var iid = url.substr(hashPos,url.length-hashPos);
        var parentQ = $(iid).parent().parent().children('.question_');
        var parentA = $(iid).parent().parent().children('.answer');
        
        if( parentQ.hasClass('open') ) {
          parentQ.removeClass('open');
          parentQ.addClass('close');
          parentA.show();
        }
      }
    }
  });

  $('.question_').click(function() {
    if( click == 0 ) {
      if( $(this).hasClass('open') ) {
        $(this).removeClass('open');
        $(this).addClass('close');
        $(this).parent().children('.answer').show();
      }
      else {
        $(this).removeClass('close');
        $(this).addClass('open');
        $(this).parent().children('.answer').hide();
      }
    }
    
    click = 0;
  });
  
  $("#main_block .more").click(function() {
    $(this).hide();
    $(".less").show();
    $("#short").hide();
    $("#full").show();
    $("#paging").show();
    
    $.ajax({
      async: "false",
      type: "GET",
      url: _http_path + "/index.php?ajax=1&nr=1",
      success: function(msg) {}
    });
  });
  
  $("#main_block .less").click(function() {
    $(this).hide();
    $(".more").show();
    $("#short").show();
    $("#full").hide();
    $("#paging").hide();
    
    $.ajax({
      async: "false",
      type: "GET",
      url: _http_path + "/index.php?ajax=1&nr=0",
      success: function(msg) {}
    });
  });

  
  $(".faq li").click(function() {
    var child = $(this).children("img.faq_btn");
    var open = 'client/images/faq_open.gif';
    var close = 'client/images/faq_close.gif';
    var id = child.attr("rel");

    if( child.attr("src") == open ) {
      child.attr("src", close);
      $("#a"+id).removeClass("hiden");
    }
    else {
      child.attr("src", open);
      $("#a"+id).addClass("hiden");
    }
  });
  
});
