$(document).ready(function() {
  
  $('input[type=text]').focus(function() {
        if ($(this).val() == $(this).attr('title')) {
            clearMePrevious = $(this).val();
            $(this).val('');
        }
    });
    $('input[type=text]').blur(function() {
        if ($(this).val() == '') {
            $(this).val(clearMePrevious);
        }
    }); 

  /* disclaimer */

//  $("#usptv").click(function(ev) {
//    
//    ev.preventDefault();
//    if(confirm(leave_site_info)) {
//      window.location = $(this).attr('href');
//    }
//  });
//
//  $("#kariera").click(function(ev) {
//    ev.preventDefault();
//    if(confirm(leave_site_info)) {
//      
//      window.location = $(this).attr('href');
//    }
//  });
//
//  $(".product_list a").click(function(ev) {
//    ev.preventDefault();
//    if(confirm(leave_site_info)) {
//      window.location = $(this).attr('href');
//    }
//  });
//  $('a').click(function(ev){
//    var value = $(this).attr("href");
////    console.log([value, value.indexOf('http'), value.indexOf(window.location.host)]);
////    console.log(window.location.host);
//    $.alerts.cancelButton = '&nbsp;'+msg_cancel+'&nbsp;';
//    if(value && (!value.indexOf('http') ) && ( value.indexOf(window.location.host) == -1 ) && ($(this).attr("rel") != 'external') && !jConfirm(leave_site_info, ''))
//    {
//      ev.preventDefault();
//      Cufon.refresh();
////      console.log('prevent');
//    }  
//    
//  });

//  console.log(window.location);

  $('#shortcut').change(function() {
    window.location.href = $(this).val();
  });

 
  $('form[name="search_form"]').submit(function(ev) {
    var curTime = new Date();
    curTime = curTime.getTime();
    curTime = curTime;
    var prevTime = $.cookie('CakeCookie[searchTime]');
    var interval = (curTime - prevTime) / 1000;

    var inpVal = $('form[name="search_form"] > input[name="keywords"]').val();

    if(interval >= 3) {

      if(jQuery.trim(inpVal.length) > 3) {

        $.cookie('CakeCookie[searchTime]', curTime);
        return true;
      } else if(jQuery.trim(inpVal.length) > 0) {
        alert('Proszę wpisać więcej niż 3 znaki')
      }
    } else {
      alert('Wyszukiwarka odblokuje się ponownie za ' + (15 - parseInt(interval)) + ' sekund.');
    }

    return false;
  });
  
  if($.cookie('CakeCookie[text-size]')) {
    var textSize = $.cookie('CakeCookie[text-size]');
    var activeTextSize = $.cookie('CakeCookie[active-text-size]');

    $('html').removeClass('medium').removeClass('default').removeClass('big');
    $('html').addClass(textSize);
      
    Cufon.refresh();
  }
  
  
   
  if($.cookie('CakeCookie[text-size]')) {
    var a = $("#accessbility2 li a");
    var textSize = $.cookie('CakeCookie[text-size]');
    
    a.parent().removeClass('s');
    
    jQuery.each(
      a,function(){
      if(textSize == 'small' && a.hasClass('font-small')){
        if( $(this).attr('rel')=="small" )
        { 
          $(this).parent().addClass('s');            
        } 
      }
      if(textSize == 'medium' && a.hasClass('font-medium')){
        if( $(this).attr('rel')=="medium" )
        { 
          $(this).parent().addClass('s');            
        } 
      }
      if(textSize == 'big' && a.hasClass('font-big')){
        if( $(this).attr('rel')=="big" )
        { 
          $(this).parent().addClass('s');            
        } 
      }
    });
  }


  $("#accessbility2 li a").click(function() {
    $(this).parent().parent().children('li').removeClass('s');
    $(this).parent().addClass('s');
    
    $.cookie('CakeCookie[active-text-size]', $(this).parent().attr('class'), {
      path : '/'
    });    

    $('html').removeClass('medium').removeClass('default').removeClass('big');
    $('html').addClass($(this).attr('rel'));
    Cufon.refresh();
    $.cookie('CakeCookie[text-size]', $(this).attr('rel'), {
      path : '/'
    });
    return false;
  });
  
  // kontrasty ------------------
  if($.cookie('CakeCookie[contrast]')) {
    var contrast = $.cookie('CakeCookie[contrast]');
    $('html').removeClass('high_contrast_1').removeClass('high_contrast_2').removeClass('high_contrast_3').removeClass('high_contrast_4');
    $('html').addClass(contrast);
    Cufon.refresh();

  }

  $("#themes li a").click(function() {
    $(this).parent().parent().children('li').removeClass('s');
    $(this).parent().addClass('s');
    $('html').removeClass('high_contrast_1').removeClass('high_contrast_2').removeClass('high_contrast_3').removeClass('high_contrast_4');
    $('html').addClass($(this).attr('rel'));
    Cufon.refresh();
    $.cookie('CakeCookie[contrast]', $(this).attr('rel'), {
      path : '/'
    });
    return false;
  });

  /* obsluga pola select w stopce */
  $("#go_to_drug_page").change(function() {
    pageSelected = $(this).val();
    window.location = pageSelected;
  });
  /* animacja dla ads-ow na stronie glownej */
  if($('.jCarouselLite > ul > li').length > 1) {
    $(".jCarouselLite").jCarouselLite({
      btnNext : ".next_big, .next",
      btnPrev : ".prev_big, .prev"
    });
    Cufon.refresh();
  }

  /* zgloszenia */
  $("#ad1").click(function() {
    $("#show_ad1").slideDown();
    $(this).parent().fadeOut();
  });

  $("#ad2").click(function() {
    $("#show_ad2").slideDown();
    $(this).parent().fadeOut();
  });

  $('.img-carousel-link').mouseover(function() {
    $('.img-carousel').parent().parent().addClass('hover-off');
    $(this).parent().removeClass('hover-off').addClass('hover-on');
    $('.ads_carousel > .control').addClass('hover-off');
    $(this).children('.img-carousel-down').css('display', 'none');
    $(this).children('.img-carousel-up').css('display', 'block');
    $(this).css('height', '92');
    $(this).parent().css({
      'height' : '92'
    });
  });

  $('.img-carousel-link').mouseleave(function() {
    $('.img-carousel').parent().parent().removeClass('hover-off');
    $('.img-carousel').parent().parent().removeClass('hover-on');
    $('.ads_carousel > .control').removeClass('hover-off');
    $(this).parent().css('height', '63');
    $(this).css('height', '63');
    $(this).children('.img-carousel-down').css('display', 'block');
    $(this).children('.img-carousel-up').css('display', 'none');
  });
  
  /*
   * shortcut select
   */

  var shortcutsSelect = $("#shortcuts");

  shortcutsSelect.hover(function() {
    // $(this).parent().parent().css('height', '178px');
    // $(this).parent().css('height', '178px');
    $(this).children('ul').show();
    $(this).children('p').children('em').css('backgroundPosition', '-67px -50px');
    $(this).css('border-radius', '17px');
    $("#footer").css('paddingTop','180px');
  }).mouseleave(function() {
    // $(this).parent().parent().css('height', '50px');
    // $(this).parent().css('height', '50px');
    $(this).children('ul').hide();
    $(this).children('p').children('em').css('backgroundPosition', '-67px -42px');
    $(this).css('border-radius', '20px');
    $("#footer").css('paddingTop','0px');
  });
  /*
   * product select
   */

  var productsSelect = $("#go_to_drug_page");
  productsSelect.hover(function() {
    $(this).css({
      'width' : '500px',
      'border-radius' : '15px'
    });
    $(this).children('ul').show();
    $(this).children('p').children('em').css('backgroundPosition', '-67px -50px');

    if( $('html').hasClass('high_contrast_2')==true ){
      $(this).children('p').css({
        'borderBottom' : '1px solid #000066'
      });      
    };
    
    if( $('html').hasClass('high_contrast_3')==true ){
      $(this).children('p').css({
        'borderBottom' : '1px solid #FFF000'
      });      
    };
    
    if( $('html').hasClass('high_contrast_4')==true ){
      $(this).children('p').css({
        'borderBottom' : '1px solid #8F8001'
      });      
    };
    
    $(this).children('p').css({
      'paddingBottom' : '5px',
      'width' : '500px'
    });    

    $(this).css('cursor', 'default');

  }).mouseleave(function() {
    $(this).css({
      'width':'165px',
      'border-radius':'15px'
    });
    $(this).children('ul').css('display', 'none');
    $(this).children('p').children('em').css('backgroundPosition', '-67px -42px');
    $(this).children('p').css({
      'borderBottom' : 'none',
      'paddingBottom' : '0',
      'width' : 'auto'
    });
  });
  
  if($('.breadcrumb-category').length > 0)
  {
    selectedCat = $('.breadcrumb-category').attr('id');
    selectedCat = selectedCat.split('breadcrumb_');
    $('.'+selectedCat[1]).addClass('current-category');
  }
 
 
//  $('a[rel*=external]').click(function() {
//    window.open(this.href);
//    return false;
//  });

});

