/*
 * Core
 *
 * @date: 2009-09-04 09:53:39
 * @author: David Sklar
--------------------------------------- */

jQuery(function ($) {

  // center thumbnails
  $('.i-box a, .gift .i-box span, #master a, .gall a').imgCenter();



  // switchboard
  $('.switchboard').switchboard({ TXT: {prev: 'P&#345;edchoz&iacute;', next: 'Dal&#353;&iacute;'} });
  $('.controller').switchboard({ control: false });
  //$('.tabs').switchboard({group: '.group', control: false, active: 'active', origin: 2});
  
  
  // fancybox

  if ($.fn.fancybox) {
    var gall_group = $.makeArray($('.gall a'));
    if (gall_group.length > 1) {
      $('#master a').fancybox({ itemArray: gall_group });
    } else {
      $('#master a').fancybox();
    }

    $('a.iframe').fancybox();
    //$('a.frame').fancybox({ frameWidth: 300, frameHeight: 200 });
      
  }
  
  

  

  // msie fix
  if ($.browser.msie) {
    $('#header .navbar li:last, #header .client-bar li:last, #nav>ul>li:last, #footer li:last').addClass('last-child');
    if (parseInt($.browser.version) < 7) {
      $('#nav>ul>li').hover(
        function () { $(this).addClass('hover'); },
        function () { $(this).removeClass('hover'); }
      );
      
      //
      $('#cart div:first-child').addClass('first-child');
      
    }
  }














  /* panel switch
  --------------------------------------- */
  if ($('table.collation, table.p-tbl').length > 0) {
    $('ul.toolbar').prepend('<li class="panel-switch"><a href="">'+ TEXT.panel.packed +'</a></li>');
    $('.guide').css({width: 940});
    $('.guide h4').css({width: 940});

    
    var pController = $('.panel-switch a');
    pController.toggle(
      function () {
        $('.guide').animate({width: 735}, {duration: 300, easing: 'easeOutQuint'});
        $('.guide h4').animate({width: 730}, {duration: 300, easing: 'easeOutQuint'});
        
        $('#content').animate({width: 730}, {duration: 300, easing: 'easeOutQuint', complete: function () {
          $('#sidebar').css({width: 205, height: 'auto' });
          $('#sidebar div[id!="nav"]').css({opacity: 0}).animate({opacity: 1}, 500);
          pController.html(TEXT.panel.unpacked);
          pController.addClass('set');
        }});
      },
      function () {
        $('#sidebar').css({width: 0, height: 0});
        $('.guide').css({width: 940});
        $('.guide h4').animate({width: 940}, {duration: 250, easing: 'easeOutQuint'});
        
        $('#content').animate({width: 940}, {duration: 250, easing: 'easeOutQuint', queue: false, complete: function () {
          pController.html(TEXT.panel.packed);
          pController.removeClass('set');
        }});
      }

    );
    
    
    //
    var params = $('.params label');
    var p_box = $('.params');
    var p_checker = $('<p class="checker"><label><input type="checkbox" /> <a href="">'+ TEXT.brands.check +'</a></label></p>');

    var set = [], step = Math.ceil(params.length / 4);
    var point = step;

    for (var j = 0; j < 4; j++) {
      set[j] = $('<ul></ul>');
    }

    var j = 0;
    params.each(function (i) {
      var item = $('<li></li>').append(this);
      set[j].append(item);

      k = i + 1;
      if (k == point || k == params.length) {
        p_box.append(set[j]);
        point += step;
        j++;
      }
    });

    p_box.find('ul:first').addClass('first-child');
    p_box.find('ul:last').addClass('last-child');
    $('.params legend ').after(p_checker);


    p_checker.find('input, a').bind('click', function (e) {
      var target = $(e.target);

      if (target.is('a')) {
        e.preventDefault();
      }
      if (target.is(':checked')) {
        p_box.find('input').attr('checked', 'checked');
      } else if (target.is('a') && !p_checker.find('input').is(':checked')) {
        p_box.find('input').attr('checked', 'checked');
      } else {
        p_box.find('input').removeAttr('checked');
      }
    });

    p_box.find('input').bind('click', function (e) {
      if ($(this).parents('li') && !$(this).attr('checked')) {
        p_checker.find('input').removeAttr('checked');
      }
    });
    
    
  }
  




  /* filter: brands
  --------------------------------------- */
  var brands = $('.brands label');
  var parent = brands.parent();
  var box = $('<div></div>').addClass(parent.attr('class'));
  var slider = $('<div></div>').css({position: 'relative', overflow: 'hidden'});
  var controller = $('<a href="">'+ TEXT.brands.controll +'</a>');
  var checker = $('<p class="checker"><label><input type="checkbox" /> <a href="">'+ TEXT.brands.check +'</a></label></p>');
      
  var set = [], step = Math.ceil(brands.length / 3);
  var point = step;

  for (var j = 0; j < 3; j++) {
    set[j] = $('<ul></ul>');
  }
  
  var j = 0;
  brands.each(function (i) {
    var item = $('<li></li>').append(this);
    set[j].append(item);
    
    k = i + 1;
    if (k == point || k == brands.length) {
      box.append(set[j]);
      point += step;
      j++;
    }
  }); 
  
  box.insertAfter(parent);
  box.find('ul:first').addClass('first-child');
  box.find('ul:last').addClass('last-child');
  parent.remove();
  
  box.wrapInner(slider);
  box.prepend($('<p class="dropdown"></p>').append(controller));
  slider = box.children('div');
  slider.prepend(checker);
  
  box.data('height', box.height() - slider.height());
  slider.data('height', slider.height());

  
  
  if (!$.browser.msie) {
    box.css({height: box.data('height')});
    slider.css({'top': (-1) * slider.data('height')});
    
    controller.toggle(
      function (e) {
        e.preventDefault();
        controller.parent().addClass('set');

        box.animate({height: box.data('height') + slider.data('height')}, {duration: 200, easing: 'easeOutQuint'});
        slider.animate({top: 0}, {duration: 400, easing: 'easeOutQuint', complete: function () {
          controller.addClass('set');
        }});
      },
      function (e) {
        e.preventDefault();

        slider.animate({top: (-1) * slider.data('height')}, {duration: 150, easing: 'easeOutQuint'});
        box.animate({height: box.data('height')}, {duration: 300, easing: 'easeOutQuint', complete: function () {
          controller.parent().removeClass('set');
          controller.removeClass('set');
        }});
      }
    );

  } else {
    slider.css({height: 0});
    
    controller.toggle(
      function (e) {
        e.preventDefault();
        controller.parent().addClass('set');

        slider.animate({height: slider.data('height')}, {duration: 400, easing: 'easeOutQuint', complete: function () {
          controller.addClass('set');
        }});
      },
      function (e) {
        e.preventDefault();

        slider.animate({height: 0}, {duration: 300, easing: 'easeOutQuint', complete: function () {
          controller.parent().removeClass('set');
          controller.removeClass('set');
        }});
      }
    );
  }

  
  
  if (box.hasClass('expand')) {
    if (!$.browser.msie) {
      box.css({height: box.data('height') + slider.data('height')});
    }
    slider.css({top: 0});
    controller.parent().addClass('set');
    controller.trigger('click');
  }

  checker.find('input, a').bind('click', function (e) {
    var target = $(e.target);

    if (target.is('a')) {
      e.preventDefault();
    }
    if (target.is(':checked')) {
      slider.find('input').attr('checked', 'checked');
    } else if (target.is('a') && !checker.find('input').is(':checked')) {
      slider.find('input').attr('checked', 'checked');
    } else {
      slider.find('input').removeAttr('checked');
    }
  });
  
  box.find('input').bind('click', function (e) {
    if ($(this).parents('li') && !$(this).attr('checked')) {
      checker.find('input').removeAttr('checked');
    }
  });



  /* control bar
  --------------------------------------- */
  $('.control-bar select').bind('change', function () {
    $(this).closest('form').submit();

  });







  /* gallery
  --------------------------------------- */
  if ($('#col .gall').length > 0) {
    $('#col .gall').imgSwitch();
    if ($('#col .gall a').length > 3) {
      $('#col .gall').gallery({ easing: 'easeOutQuint', color:  {live: '#3b3b3b', dead: '#ccc', duration: 300} });
    }
  }
  
  
  
  /* variants
  --------------------------------------- */
  var vBox = $('.variants');
  
  if (vBox.length > 0) {

    $('<p class="dropdown"><a href="">'+ TEXT.variants.packed +'</a></p>').appendTo(vBox);
    var controller = $('.variants .dropdown a'), tmp;
    
    vBox.data('height', {packed: 150, unpacked: vBox.height() + controller.outerHeight(true)});
    vBox.height(vBox.data('height').packed);
  
    tmp = controller.clone().appendTo('body').html('<b>'+ TEXT.variants.unpacked +'</b>');
    controller.data('width', {packed: controller.width(), unpacked: tmp.width()});
    tmp.remove();
  
    controller.toggle(
      function () {
        vBox.animate({height: vBox.data('height').unpacked}, {duration: 350, easing: 'easeOutQuint', complete: function () {
          controller.html(TEXT.variants.unpacked);
          controller.animate(
            {width: controller.data('width').unpacked},
            {duration: 350, easing: 'easeOutQuint', complete: function () {
              controller.addClass('set');
            }
          });
        }});
      },
      function () {
        vBox.animate({height: vBox.data('height').packed}, {duration: 350, easing: 'easeOutQuint', complete: function () {
          controller.html(TEXT.variants.packed);
          controller.animate(
            {width: controller.data('width').packed},
            {duration: 200, easing: 'easeOutQuint', complete: function () {
              controller.removeClass('set');
            }
          });
        }});
      }
    );
    
    vBox.bind('click', function (e) {
      var target = $(e.target);
      if (target.is('input[type="radio"]')) {
        target.closest('form').submit();
      }
    });
  
  }
  
  
  
  
  
  
  
  /* guide
  --------------------------------------- */
  var gBox = $('div.guide div.col-3.package');

  if (gBox.length > 0) {

    $('<p class="dropdown"><a href="">'+ TEXT.categories.packed +'</a></p>').appendTo(gBox);
    var controller = $('.guide .dropdown a'), tmp;

    gBox.data('height', {packed: controller.parent().outerHeight(true), unpacked: gBox.height() + controller.parent().outerHeight(true)});
    gBox.height(gBox.data('height').packed);
  
    tmp = controller.clone().appendTo('body').html(TEXT.categories.unpacked);
    controller.data('width', {packed: controller.width(), unpacked: tmp.width()});
    tmp.remove();
  
    controller.toggle(
      function () {
        gBox.animate({height: gBox.data('height').unpacked}, {duration: 350, easing: 'easeOutQuint', complete: function () {
          controller.html(TEXT.categories.unpacked);
          controller.animate( {width: controller.data('width').unpacked}, {duration: 350, easing: 'easeOutQuint', complete: function () { controller.addClass('set'); } });
        }});
      },
      function () {
        gBox.animate({height: gBox.data('height').packed}, {duration: 350, easing: 'easeOutQuint', complete: function () {
          controller.html(TEXT.categories.packed);
          controller.animate( {width: controller.data('width').packed}, {duration: 200, easing: 'easeOutQuint', complete: function () { controller.removeClass('set'); } });
        }});
      }
    );
    
  
  }
  
  
  
  
  
  
  
  
  

  
  // forms focus-blur
  $('input[type="text"], input[type="password"]')
    .bind('focus', function () { $(this).stop().animate({'backgroundColor': '#fff3c7'}, {queue: false, duration: 500}); })
    .bind('blur', function () { $(this).stop().animate({'backgroundColor': '#fff'}, {queue: false, duration: 250}); });
  /*$('button[class!="btn-add-cart"], input[type="button"][class!="btn-add-cart"]')
    .bind('focus', function () { $(this).stop().animate({'backgroundColor': '#a70101'}, {queue: false, duration: 200}); })
    .bind('blur', function () { $(this).stop().animate({'backgroundColor': '#3b3b3b'}, {queue: false, duration: 500}); })
    .hover(
      function () { 
        $(this).stop().animate({'backgroundColor': '#a70101'}, {queue: false, duration: 200});
      },
      function () { 
        $(this).stop().animate({'backgroundColor': '#3b3b3b'}, {queue: false, duration: 500}); 
      }
    );*/
    
  var toggle = function (selector, text) {
    $(selector).val(text);
    $(selector)
      .bind('focus', function () { $(this).val(''); })
      .bind('blur', function () { if ($(this).val() == '') { $(this).val(text); } });
  };
  
  toggle('#header input', TEXT.form.search);
  toggle('#newsletter input', TEXT.form.email);


  
  






  /* quick navigation
  --------------------------------------- */
  $('#nav>ul>li').each(function (i) {
    var ul = $('<ul></ul>'), count = $(NAV[i]).length - 1, css;
    $(NAV[i]).each(function (i) {
      css = count == i ? ' class="last-child"' : '';
      ul.append('<li'+ css +'><a href="'+ this.url +'">'+ this.label +'</a></li>');
    });
    ul.css({visibility: 'hidden'});
    $(this).append(ul);
  });
  
  //
  var nav_item = $('#nav>ul>li');
  var nav_box = $('#nav>ul>li>ul').css({visibility: 'visible'});
  var nav_slider = $('<div></div>').css({position: 'absolute', overflow: 'hidden'});

  nav_box.wrap(nav_slider);
  nav_slider = nav_item.children('div');
  
  nav_box.data('height', nav_box.outerHeight(true));

  nav_box.css({top: (-1) * nav_box.data('height'), left: 0});
  nav_slider.css({width: nav_box.css('width'), height: 0});


  nav_item.hover(
    function (e) {
      $(this).children('div').stop().animate({height: nav_box.data('height')}, {queue: false, duration: 200, easing: 'easeOutQuint', complete: function () {
        $(this).find('ul').stop().animate({top: 0}, {queue: false, duration: 300, easing: 'easeOutQuint'});
      }});
      
    },
    function (e) {
      $(this).find('ul').stop().animate({top: (-1) * nav_box.data('height')}, {queue: false, duration: 200, easing: 'easeOutQuint'});
      $(this).children('div').stop().animate({height: 0}, {queue: false, duration: 200, easing: 'easeOutQuint'});
    }
  );
  
  









  
  /* add to cart
  --------------------------------------- */
  var cart = $('#cart');
  var cart_summary = $('#cart-summary').css({overflow: 'hidden'});
  var cart_slide = $('<div></div>').css({position: 'relative', overflow: 'hidden'});
  var cart_slider, cart_timer;
  var cart_delay = 3000;
  
  //
  var cart_btn = cart.find('div:first');
  var cart_link = cart_btn.find('h3 a');
  cart_btn.css({cursor: 'pointer'});
  cart_btn.hover(
    function () { cart_link.css({'text-decoration': 'underline'}); },
    function () { cart_link.css({'text-decoration': 'none'}); }
  );
  cart_btn.bind('click', function () {
    window.location.href = cart_link.attr('href');
  });

  cart.data('packed', cart.height());
  /*
  $('.btn-add-cart').closest('form').bind('submit', function (e) {
    var product, tpl;
    
    product = $(this).closest('li');

    tpl = '<p><b>'+ TEXT.cart.notice +':</b><br /><a href="">'+ product.find('h3').text() +'</a></p>';
    tpl += '<p class="flr"><big><b>'+ product.find('big').text() +'</b></big></p>';
    tpl += '<a href="'+ product.find('h3 a').attr('href') +'" class="i-box"><img src="'+ product.find('.i-box img').attr('src').replace(new RegExp('100/100', 'i'), '75/75') +'" alt="'+ product.find('h3').text() +'" /></a>';
    tpl += '<p class="right"><a href="">'+ TEXT.cart.checkout +'</a></p>';
    tpl += '<img src="../gfx/ico_close.gif" alt="'+ TEXT.cart.close +'" class="btn-close" />';
    
    cart_summary.html(tpl);
    $('#cart a.i-box').imgCenter();
    
    cart.css({height: 'auto'});
    cart_summary.wrapInner(cart_slide);
    cart_slider = cart_summary.children('div');
    cart.addClass('expand').data('unpacked', cart.height()).data('slider', cart_slider.height()).removeClass('expand');

    cart_slider.css({top: (-1) * cart.data('slider')});
    
    openCart();
    
    e.preventDefault();
  });
  
  cart.bind('mouseover', function () {
    clearTimeout(cart_timer);
  });
  cart.bind('mouseout', function () {
    cart_timer = setTimeout(function(){closeCart()}, cart_delay);
  });
  cart.find('.btn-close').live('click', function () {
    
    closeCart();
  });
  */
  
  var openCart = function () {
    cart.addClass('expand')/*.css({height: cart.data('packed')})*/;
    cart.stop().animate({height: cart.data('unpacked')}, {queue: false, duration: 200, easing: 'easeOutQuint'});
    cart_slider.animate({top: 0}, {queue: false, duration: 300, easing: 'easeOutQuint'});
    
    clearTimeout(cart_timer);
    cart_timer = setTimeout(function(){closeCart()}, cart_delay);
  };

  var closeCart = function () {
    if (cart_slider) {
      cart_slider.stop().animate({top: (-1) * cart.data('slider')}, {queue: false, duration: 500, easing: 'easeOutQuint'});
      cart.stop().animate({height: cart.data('packed')}, {queue: false, duration: 500, easing: 'easeOutQuint'});
      cart.removeClass('expand');
    }
    
  };
  
  
  


  /* login gate
  --------------------------------------- */
  var gate = $('#login-gate');
  var gate_summary = $('#login-gate div').css({overflow: 'hidden'});
  var gate_slider = $('#login-gate fieldset').css({position: 'relative', overflow: 'hidden'});
  var gate_slider, gate_timer, gate_status = true;
  var gate_delay = 3000;

  gate_summary.append('<img src="http://cs.s.mall.cz/CZ10MA/gfx/ico_close.gif" alt="'+ TEXT.cart.close +'" class="btn-close" />');
  gate.data('packed', 0);
  
  gate.data('counter', 0);
  $('.btn-login-gate').bind('click', function (e) {
    e.preventDefault();
    if (gate_status) {
      gate.css({height: 'auto'});
      gate.addClass('expand').data('unpacked', gate.height()).data('slider', gate_slider.height()).removeClass('expand');
      gate_slider.css({top: (-1) * gate.data('slider')});

      openGate();
    } else {
      closeGate();
    }
    
  });
  
  
  gate.bind('mouseover', function () {
    clearTimeout(gate_timer);
  });
  gate.bind('mouseout', function () {
    gate_timer = setTimeout(function(){closeGate()}, gate_delay);
  });
  gate.find('input, button').bind('focus', function (e) {
    clearTimeout(gate_timer);
  });
  
  gate.find('.btn-close').live('click', function () {
    closeGate();
  });

  var openGate = function () {
    gate.addClass('expand');
    gate_status = false;
    
    gate.stop().animate({height: gate.data('unpacked')}, {queue: false, duration: 200, easing: 'easeOutQuint'});
    gate_slider.animate({top: 0}, {queue: false, duration: 300, easing: 'easeOutQuint'});
    
    gate.find('input, button').blur().removeAttr('disabled');
    gate.find('input:first').focus();

    clearTimeout(gate_timer);    
  };

  var closeGate = function () {
    if (gate_slider) {
      gate_slider.stop().animate({top: (-1) * gate.data('slider')}, {queue: false, duration: 500, easing: 'easeOutQuint'});
      gate.stop().animate({height: gate.data('packed')}, {queue: false, duration: 500, easing: 'easeOutQuint'});
      gate.removeClass('expand');
      
      gate.find('input, button').attr('disabled', 'disabled');
      
      gate_status = true;
    }
    
  };










  
  
  // action-set list
  var action_set = $('.s-list>li');
  //var color = {};
  
  //action_set.find('.i-box a:eq(1)').addClass('active');
  //color = {dead: action_set.find('i-box a:eq(1)').css('border-top-color'), live: $this.find('a:first').css('border-top-color') };
  //action_set.find('.i-box a:eq(1)').removeClass('active');
  
  //console.log(color.live);
  //console.log(color.dead);
  
  action_set.find('ol').bind('mouseover focus', function (e) {
    var target = $(e.target), a, index;
    
    if (target.is('a')) {
      index = target.closest('ol').find('li').index(target.closest('li'));

      a = target.closest('ul>li').find('.i-box a');
      
      
      a.removeClass('active');
      a.eq(index).addClass('active');
      
      //a.stop().animate({'borderColor': color.dead}, {duration: 300, complete: function () { a.removeClass(css); }});
      //a.eq(index).stop().animate({'borderColor': color.live}, {duration: 300, complete: function () { a.eq(index).addClass(css); }});
    
    }    
  });
  
  action_set.find('ol').bind('mouseout', function (e) {
    $(e.target).closest('ul>li').find('.i-box a').removeClass('active');
    
    //$(e.target).closest('ul>li').find('.i-box a').stop().animate({'borderColor': color.dead}, {duration: 300, complete: function () { $(e.target).closest('ul>li').find('.i-box a').removeClass(css); }});
   
  });

  
  // tables
  $('table.p-tbl, table.collation, table.overview')
    .bind('mouseover', function (e) {
        if ($(e.target).parents('tbody').length > 0) {
          $(e.target).parent('tr').find('th, td').stop().animate({'color': '#000','backgroundColor': '#fff3c7'}, {queue: false, duration: 150});
        }
      }
    )
    .bind('mouseout', function (e) {
        if ($(e.target).parents('tbody').length > 0) {
          var tmp = $(e.target).parent('tr').hasClass('odd') ? '#eee' : '#fefefc';
          $(e.target).parent('tr').find('th, td').stop().animate({'color': '#3b3b3b', 'backgroundColor': tmp}, {queue: false, duration: 400});
        }
      }
    );
  
  
  

  


  
  


  

  


});





/* texts
--------------------------------------- */
var TEXT = {

  cart:       {
    notice:   'Do košíku bylo přidáno',
    checkout: 'Obsah košíku',
    close:    'Zavřít'
  },

  brands:     {
    controll: 'Značka',
    check:    'Označit vše'
  },
  
  categories:   {
    packed:   'Všechny kategorie',
    unpacked: 'Hlavní kategorie'
  },
  variants:   {
    packed:   'Všechny varianty',
    unpacked: 'Zabalit varianty'
  },
  parameters: {
    packed:   'Další parametry',
    unpacked: 'Základní parametry'
  },
  
  panel:      {
    packed:   'Zobrazit levý panel',
    unpacked: 'Skrýt levý panel'
  },
  
  form:       {
    search:   'Hledaný výraz…',
    email:    'Váš e-mail'
  },
  
  print:      'Tisknout stránku',
  favourite: {
    label:    'Oblíbené',
    title:    'Přidat stránku do oblíbených'
  }

};
