$(document).ready(loadHandlers);

function loadHandlers() {
  menuHandler();
  searchHandler();
  //fancyHandler();
  colorHandler();
}

function colorHandler() {
    $("a[rel=fancybox]").colorbox();

}


function fancyHandler() {
    $("a[rel=fancybox]").fancybox({
        changeFade              : 0,
        titleShow               : false,
        margin					: 0,
        'transitionIn'			: 'none',
        'transitionOut'			: 'none',
        'overlayShow'			: true
    });
}

function searchHandler() {
  $("#searchInput").val(window.parametr.searchString);
  $("#searchInput").focus(function(){ 
    if ($(this).val() == window.parametr.searchString){
      $(this).val("");
    }
  });

  $("#searchForm").submit(function(){ 
    if ($("#searchInput").val() == window.parametr.searchString){
      return false;
    }; return ($("#searchInput")[0].value.length > 3);
  });

 
}

function menuHandler() {
  $("li.haschildren span").click(function() { 
    $(this).parent().next().toggleClass("flat").toggleClass("unflat");
  });
}

function purposeFailHandler() {
  $("input.purpose").click(function() { 
    return false;
  });$("input.site").click(function() {
    return false;
  });
}

function popup(url,i_width,i_height) {
  var swidth=screen.width, sheight=screen.height;
  var min_x = 100;
  var min_y = 100;
  var max_x = swidth-50;
  var max_y = sheight-190;
  var owidth = i_width > max_x ? max_x : i_width < min_x ? min_x : i_width;
  var oheight = i_height > max_y ? max_y : i_height < min_y ? min_y : i_height;
  if ((owidth!=i_width) || (oheight!=i_height)) {
    pageWin = window.open(url,"","width="+owidth+",height="+oheight+",menubar=0,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
  } else {
    pageWin = window.open(url,"","width="+owidth+",height="+oheight+",screenX=20,screenY=40,left=20,top=40");
  }
  pageWin.focus();
  return false;
}

function addMapHandler() {
  $('#services div').hide();
  $(document).bind('hashChange', function(e, newHash) {
    $('#services div').each(function() {
      $(this).hide()
    } );
    $('#services .service' + newHash.substr(1)).fadeIn('slow');
  } );

}

(function($){
  var $document = $(document),
  iframe,
  hash = '',
  needHistoryAdd = /MSIE/.test(navigator.userAgent),
  afterHistoryRead = false,
  afterHistoryAdd = false;

  function check() {
    if (hash != (hash = document.location.hash)) {
      if (!afterHistoryRead && needHistoryAdd) historyAdd(hash);
      afterHistoryRead = false;
      $document.trigger('hashChange', [hash]);
    }
    setTimeout(check, 42);
  }

  function historyAdd(hash) {
    if (!iframe) iframe = $('<iframe style="display:none" src="javascript:false;"></iframe>').appendTo('body')[0];
    var d = iframe.contentDocument ||
    (iframe.contentWindow ? iframe.contentWindow.document : iframe.document);
    d.open();
    d.write('<html><head><title>' + document.title + '</title></head><body>');
    d.write($('<div/>').append($('<div id="hashdiv"></div>').text(hash)).html());
    d.write(
      '<script>' +
      'window._hash = document.getElementById("hashdiv").innerText;' +
      'window.onload = parent._historyRead;' +
      '</script>'
      );
    afterHistoryAdd = true;
    d.close();
  /*/ console.log('historyAdd: ' + hash); /*/
  }


  if ('onpropertychange' in document && 'attachEvent' in document) {
    document.attachEvent('onpropertychange', function(){
      if (event.propertyName == 'location') {
        check();
      /*/ console.log('onpropertychange: ' + document.location.hash); /*/
      }
    });
  }

  if (needHistoryAdd) {

    window._historyRead = function(){
      /*/ console.log('try historyRead: ' + this._hash);/*/

      if (!afterHistoryAdd) {
        var newHash = this._hash;
        /*/ console.log('newHash: ' + newHash + ', currentHash: ' + document.location.hash);/*/

        if (document.location.hash != newHash) {
          afterHistoryRead = true;
          document.location.hash = newHash;
        /*/ console.log('historyRead: ' + newHash); /*/
        }
      }
      afterHistoryAdd = false;
    };
  }

  $(check);


})(jQuery);
