$(document).ready(function(){ 
          
    
    $('a.extLink').attr('target', '_blank'); 
    
    $('#brainsLogo').mouseover(function(){
        $(this).attr('src', $(this).attr('src').replace('brains-grey.gif', 'brains.gif'));
    });
    $('#brainsLogo').mouseout(function(){
        $(this).attr('src', $(this).attr('src').replace('brains.gif', 'brains-grey.gif'));
    });
    
    $('#orderFormClose').click(function(){
        $('#orderForm').fadeOut('slow');
        return false;
    });
    
    $('#orderFormOpen').click(function(){
        $('#orderForm').vCenter();
        $('#orderForm').fadeIn('slow');
        return false;    
    });
    
    $('#subscribeByMailClose').live('click', function(){
        $('.subscribeByMail').each(function(){
            if ($(this).data("qtip")) $(this).qtip("destroy");
        });
        return false;  
    });
    
    var subscriptionFilter;
    var subscriptionType;
    $('.subscribeByMail').click(function(){
     
         if ($(this).attr('rel')){
            subscriptionFilter = $(this).attr('rel');  
         } else {subscriptionFilter = '';}
         
         if ($(this).hasClass('subscrSearch')){
            subscriptionType = 'search';
         } else {
            subscriptionType = 'all';
         }
         
         // Destroy currrent tooltip if present
         $('.subscribeByMail').each(function(){
            if ($(this).data("qtip")) $(this).qtip("destroy");
         });
         
         $(this).qtip({
               content: $('#subscribePopUp').html(), 
               position: {
                  corner: {
                     tooltip: 'rightBottom', 
                     target: 'topMiddle' 
                  }
               },
               show: {
                  when: false, 
                  ready: true,
                  delay: 0,
                  effect: { length: 0, type: 'fade' } 
               },
               hide: false, 
               style: {
                  border: {
                     width: 2,
                     radius: 2,
                     color: '#327EA1'
                  },
                  width: { max: 328 }, 
                  padding: 10, 
                  textAlign: 'center',
                  tip: false
               }
            });

        return false;
    });
    
    $('#subscribeByMailBtn').live('click', function(){
        $.getJSON($(this).closest('form').attr('action'), 
          {
            filter: subscriptionFilter,
            email: $(this).closest('form').find('input:text').val(),
            type: subscriptionType
          },  
          function(data) {
              if (typeof(data.newsletter) != 'undefined' ){
                  $('.qtip .subscriptionMsg p').html(data.newsletter.message);
                  if(data.newsletter.type == 'error'){
                      $('.qtip .subscriptionMsg').removeClass('error-1 ok-1').addClass('error-1').show();            
                  } else {
                      $('.qtip .subscriptionMsg').removeClass('error-1 ok-1').addClass('ok-1').show();  
                  }
                  
                  
              }
          }    
        );

        // susieti su REF ir su DB link
        return false;      
    });
    
        
    
    $('#searchBtn').click(function(){ 
        var str = '';
        var form = $(this).closest('form');
        form.find('input, select').each(function(){
            if ($(this).val() != ''){
                str += '&' + $(this).attr('name') + '=' + $(this).val().replace(/[=\+\s&\?]+/g, '+');    
            }
        });

        if (str != '')
        {
            document.location = form.attr('action') + '?' + str.substring(1);
        }
        return false;
    });
    
    // catpcha
    $('#orderFormRegenCaptcha').click(function(){
        $('#orderFormCatpcha').attr('src', $('#orderFormCatpcha').attr('src').split('?')[0] + "?" + new Date().getTime());
        return false;
    });
    
    $('#mdm-frm').submit(function(){
        $('#orderFormSendBtn').trigger('click');
        return false;
    });
    
    $('#mdm-frm input, #mdm-frm textarea').focus(function(){
        $('#mdm-frm .message').fadeOut('slow');    
    });
    
    $('#orderFormSendBtn').click(function(){
        var params = '';
        $('#mdm-frm input, #mdm-frm textarea').each(function(){
            params += $(this).attr('name') + '=' + escape($(this).val()) + '&';
        });
        $.ajax({
            type: "POST",
            cache: false,
            url: $('#mdm-frm').attr('action'),
            data: params.slice(0, -1),
            success: function(msg){
                $('#mdm-frm .message').hide();
                if (msg == 'success'){
                    $('#feedback-success').show();
                } else {
                    var arr = msg.split('|', 2);
                    if (typeof arr[1] != 'undefined') {
                        if (arr[0] == 'success') {
                            $('#feedback-message').removeClass('error-1').addClass('ok-1');    
                        } else {
                            $('#feedback-message').removeClass('ok-1').addClass('error-1');    
                        }
                        $('#feedback-message p').text(arr[1]);
                        $('#feedback-message').show();    
                    }
                    else{
                        $('#feedback-connection-error').show();    
                    }    
                }
            }
        });
        return false;    
    });
    
    $('#attentionOff').click(function(){
        document.cookie = "attention=on; path=/";
        $('#attentionOff').hide();
        $('#attentionOn').show();
        return false;    
    });
    
    $('#attentionOn a.fr').click(function(){
        document.cookie = "attention=off; path=/";
        $('#attentionOn').hide();
        $('#attentionOff').show();
        return false;    
    });
    
    if (document.cookie.indexOf('attention=off') != -1){
        $('#attentionOn').hide();
        $('#attentionOff').show();    
    }

});
