//функции проекта
$(document).ready(function(){

   $('#print').click(_print);
   find_img();
   $('.menu .lev1').hover(function(){ hover_sub(this)});

   if (__class=='page_contacts'){
      _contacts();
   };
   /*if (__class=="page_with_form") {
      var params = {iface: 'online_request', _act: 'send_form', node_id:__node}
      $('.online_request div.form form').submit(function(){send(params,this);
                                                   return false;});
   };*/
});
//функции проекта-----------------------------------------------------------------------------

//функции для станицы контактов--------------------------------------------------------------
function _contacts() {
   var el = $('.contacts .but').click(function(){
      if ($(this).attr('id') == 'map') {
         var params = { iface : 'map',
                        object_id :_get_id(this) };
         $.fancybox.showActivity();
         GetXml(params,function(xml){_show(xml)});
         delete params;
      }
      else if ($(this).attr('id')=='letter'){
         var params = { iface : 'advices_form',
                        object_id:_get_id(this),
                        _act: 'get'};
         $.fancybox.showActivity();
         GetXml(params,function(xml){_show(xml)});
         delete params;
      }
   })
}

   function send(params,form){
        var $form = $(form);
        var errorFlag = false;
        $form.find('.p').removeClass('error');
        var $fields = $form.find('input, textarea');
         for (var i = 0; i < $fields.length; i++) {
            var $f = $fields.eq(i), $p = $f.parent();
            if(($f.attr('name'))&&($f.attr('name')!='__BACK')){
                var val = $f.val().trim();
                params[ $f.attr('name') ] = val;
                if (($p.hasClass('need-field')) && (val == '')) {
                    $p.addClass('error');
                    errorFlag = true;
                }
            }
         }
        if (!errorFlag) {
            $.fancybox.showActivity();
            GetXml(params,function(xml){ _show(xml); });
            for (var i=0; i<$fields.length; i++) {$fields.eq(i).val('')};
        }
      //function answer(xml){
      //     if(xml){
      //         var template = xml.getElementsByTagName("template")[0];
      //         if(template){
      //              $.fancybox(_get_content(template));
      //         }
      //     }
      //}
   }

//функции для страницы контактов-------------------------------------------------------------


//функции ajax запросов
function GetJson(data) {
      $.ajax({
      method: 'POST',
      url:'/json/',
      data:data,
      succses: function(data) {cont(data)},
      dataType: 'json'});

}
//функция для получения XML
function GetXml(vars,handler) {
   $.ajax({
      method: 'POST',
      url:'/ajax/',
      data: vars,
      dataType: 'xml',
      success: handler
      });
}

//интерфейсные функции--------------------------
//функиция выпадающего меню
function hover_sub(elem) {
   var el=$(elem).find('.submenu');
   if (el.css('display') == 'none') {
         el.slideDown('100');}
   else el.slideUp('100');
};
//функция для выдергивания ID из url вида /../../#cntID
function _get_id(el) {
   var str;
   var ID;
   if ($(el).attr('href')){
      str=$(el).attr('href');
   }
   else
   str = window.location.hash.toString();
   ID=str.split('cnt');
   window.location.hash=ID[0].slice(0,ID[0].lenght-2)
   return ID[1];

}

//функция для распечатки
function _print() {
   window.print();
}

//функция для отображения полученного xml
function _show(xml) {
   var content = xml.getElementsByTagName('template')[0]
   if(content)
      $.fancybox(_get_content(content));
      $("#advice-form form").submit(function(){var params = { iface: "advices_form",
                                                               object_id: _get_id(),
                                                               _act: 'send_form'};
                                                send(params,this); return false;});
}
//функция для выдергивания из xml контента
function _get_content(xml){
   var txt;
   if (xml.text != null) txt=xml.text
   else if (xml.textContent != null) txt=xml.textContent
   else txt = xml.firstChild.nodeValue;
   return txt;
}


function menuIE6(){
   $('.main_menu li a').mouseover(function(){
      alert('eoe')
      var el=$(this).find('span');
      el.css('clip','rect(0 227px 170px 0)');
   })
     $('.main_menu li a').mouseout(function(){
      var el=$(this).find('span');
      el.css('clip','rect(0 454px 170px 227px)');
   })
     return 5;

}


// поиск в div.text изображений и приписывание к ним класса .png

function find_img () {
   var el = $("div.text img").each(function(){ $(this).addClass('png') });
}

function fixPNG(element, sizing) {
   var __sizingRegExp = /iesizing\-(\w+)/;
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
        sizing = sizing || "crop";
        var sizingMatch = __sizingRegExp.exec(element.className)
        if (sizingMatch)
            sizing = sizingMatch[1];
        var url = "/i/ie/e.gif";
        if (element.tagName == "IMG") {
            if (/\.png$/.test(element.src)) {
                url = element.src;
                element.src = "/i/ie/e.gif";
            }
        } else {
            url = element.currentStyle.backgroundImage.match(/url\(["']?(.+\.png).*?["']?\)/i);
            var repeat=$(element).css('background-repeat');
            if (url) {
                url = url[1];
                element.runtimeStyle.backgroundImage = "none";
            }
        }
        if (url && (repeat=='no-repeat' || !repeat))
            element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + url + "',sizingMethod='" + sizing + "')";
        else
            element.runtimeStyle.filter = "true";
    }
}

