function saturate(img) {   
  var img2 = Pixastic.process(img, "desaturate");
  img2.onmouseover = function() {Pixastic.revert(this);}
}

function hns(id){
    if ($('#'+id).css('display')=='none') {
        $('#'+id).slideDown(300);
    }
    else  {$('#'+id).slideUp(300);}
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function desaturate_all(){
    $(".desaturate").attr({onmouseout: "saturate(this);"});     
    $.each($(".desaturate"), function(){       
        $(this).load(function(){
            var img3 = Pixastic.process(this, "desaturate");
            if (img3!='undefined')
                img3.onmouseover = function() {Pixastic.revert(this);};
        })
    })
}

function desaturate_all2(){
    $(".desaturate").attr({onmouseout: "saturate(this);"});
    $.each($(".desaturate"), function(){
        var img3 = Pixastic.process(this, "desaturate");
        if (img3!='undefined')
            img3.onmouseover = function() {Pixastic.revert(this);};
    })
}

function mycarousel_initCallback(carousel)
{
//    // Disable autoscrolling if the user clicks the prev or next button.
//    carousel.buttonNext.bind('click', function() {
//        carousel.startAuto(0);
//    });
//
//    carousel.buttonPrev.bind('click', function() {
//        carousel.startAuto(0);
//    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}

$(function() {
    $('img.image1').data('ad-desc', 'Whoa! This description is set through elm.data("ad-desc") instead of using the longdesc attribute.<br>And it contains <strong>H</strong>ow <strong>T</strong>o <strong>M</strong>eet <strong>L</strong>adies... <em>What?</em> That aint what HTML stands for? Man...');
    $('img.image1').data('ad-title', 'Title through $.data');
    $('img.image4').data('ad-desc', 'This image is wider than the wrapper, so it has been scaled down');
    $('img.image5').data('ad-desc', 'This image is higher than the wrapper, so it has been scaled down');
    var galleries = $('.ad-gallery').adGallery({slideshow: {
                       start_label: 'Старт',
                       stop_label: 'Стоп'}});
    $('#switch-effect').change(
      function() {
        galleries[0].settings.effect = $(this).val();
        return false;
      }
    );
    $('#toggle-slideshow').click(
      function() {
        galleries[0].slideshow.toggle();
        return false;
      }
    );
    $('#toggle-description').click(
      function() {
        if(!galleries[0].settings.description_wrapper) {
          galleries[0].settings.description_wrapper = $('#descriptions');
        } else {
          galleries[0].settings.description_wrapper = false;
        }
        return false;
      }
    );
  });

$(document).ready(function() {

    var img_path = '/wp-content/themes/eks/images/';
    
    //карусель с фотками
    $('#photo-carousel').jcarousel({
        auto: 3,
        scroll: 1,
        wrap: "both",
        initCallback: mycarousel_initCallback
    });

    //переключающийся блок партнеры/клиенты
    $('a.eks-switcher').click(function(){
        $('a.eks-switcher', $(this).closest('.eks-sw')).removeClass('current');
        $(this).addClass('current');
        $('.eks-switcher-content', $(this).closest('.eks-sw')).hide();
        $('.eks-switcher-content:eq('+$(this).index()+')', $(this).closest('.eks-sw')).show();
        return false;
    })

    //раскрашиваем таблицы
    $(".main-content table.zebra tr:nth-child(odd), .main-content table.gray-zebra tr:nth-child(even)").addClass("even");

    //обесцвечивание
    desaturate_all();

    //сворачивание формы
    $('#open-form, #toggle-form-icon').click(function(){
        if ($('.toggle-form').css('display')=='none')
            $('#toggle-form-icon img').attr('src',img_path+'open.png');
        else $('#toggle-form-icon img').attr('src',img_path+'close.png');
        $('.toggle-form').toggle();
        return false;
    });
    if ($('#ie7').length>0){
        $('#open-form').click().click();
    }

    //меню в сайдбаре   
    $('.sidebar ul.menu > li').not('.current-menu-item, .current-menu-ancestor, .current-post-ancestor').add_layer("url("+img_path+"blue.png) #1296e5 top left no-repeat");
    $('.sidebar ul.menu > li').not('.current-menu-item, .current-menu-ancestor, .current-post-ancestor').add_layer("url("+img_path+"blue.png) top right no-repeat");
    $('.sidebar ul.menu > li > div > div > div > div > a').add_layer("url("+img_path+"dual.gif) bottom repeat-x");

    //имитация placeholder
    var s_clear = true;
    $('#s').mousedown(function(){
        if (s_clear){
            $(this).val('');
            s_clear = false;
        }
    }).blur(function(){
         if ($(this).val()=='') {
             s_clear = true;
             $(this).val('Поиск');
         }
     });

     //верхнее меню - последний пункт
     $('.top-menu ul li:last-child').addClass('last-child');

     //онлайн консультация
     $('.consult-toggler').click(function(){
         return false;
     })
     $('.consult').hover(function(){        
        $(this).animate({marginRight: '0'}, 300, "linear");
     }, function(){        
        $(this).animate({marginRight: '-120px'}, 300, "linear");
     })
     $('.consult-content a').click(function(){        
         window.open('/chat/chat.php','','width=590,height=610,left=0,top=0,resizable=yes,menubar=no,location=no,status=yes,scrollbars=yes');         
         return false;
     })

     //список городов 
     
     if ($('#ie7').length>0){
         $('ul.city-switcher li:first-child a').append('<a href="#" id="toggle-city"></a>');
         $('#toggle-city').click(function(){
             if ($('ul.city-switcher').hasClass('visible')){
                 $('ul.city-switcher').removeClass('visible');
                $('ul.city-switcher li:not(:first-child)').hide();
                $('ul.city-switcher.visible li:first-child a span').addClass('t2');
             }else{
                  $('ul.city-switcher').addClass('visible');
                  $('ul.city-switcher li:not(:first-child)').show();
                  $('ul.city-switcher.visible li:first-child a span').removeClass('t2');
             }
         });
     }else{
         $('ul.city-switcher').mouseenter(function(){
             $('ul.city-switcher').addClass('visible');
            $('ul.city-switcher li:not(:first-child)').show();
         }).mouseleave(function(){
             $('ul.city-switcher').removeClass('visible');
             $('ul.city-switcher li:not(:first-child)').hide();
         });
     }
     

     //станции метро
     $('.control-station').click(function(){
         if ($(this).hasClass('active')) $(this).removeClass('active');
         else $(this).addClass('active');
         $('ul.reset', $(this).closest('.metro-station')).toggle();
         return false;
     })

     //toggle брендов
     $('#brands-toggler').click(function(){
         $('.brands-hidden').toggle(0, desaturate_all2);
         return false;
     })
})
