/*
	@company:  www.actum.cz
  @author:   David Sklar
	@site:     euronics.cz	
------------------------------------------- */
jQuery(function ($) {

  base.init();
  form.init();

  // PNG
  if ($.fn.ifixpng) {
    $('#mapa-280x165 img, #mapa-560x330 img, .p-list img.label, #o-poll big, #o-poll small, #vBox p.close a, .navigator a, .navigator-msie a').ifixpng();
  } 
  
  // galerie
  $('#content div.gall:not(div.box-590)').gallery(); 
  $('.produkt #content div.box-590').gallery({
    length: 4
  });
  $('#img-set').gallery({
      length: 3
    })
    .imgSwitch();
  
  // bublina
  $('#content acronym').tooltip({parentBox: '#content', linked: true});
  
  // mapky
  $('#mapa-280x165, #mapa-560x330').maps();
  
  // viewer
  $('a[@rel*=viewer]').lightBox();

});





/* zakladni vybava 
--------------------------------------- */
var base = {
  //
  init: function () {  
    this.corners('nav');    // kulate rohy: navigace
    this.corners('guide');  // kulate rohy: guide

    this.print();           // tisk: obsah + paticka
    this.poll();            // plovouci anketa
    
    this.list();            // inline seznam: prodejna
    this.table();           // pruhovana tabulka [class='overview']
    this.glossary();        // slovnicek pojmu: centrovany abecedni seznam
    this.newWindow();       // odkazy do noveho okna: paticka, a[rel='window']
     
  },

  // kulate rohy: navigace, guide
  corners: function (id) {
    var items, box;
    items = id == 'guide' ? $('#'+ id +'>ul>li>a') : $('#'+ id +'>ul>li>a');
    box = $('<span></span>');

    items.each(function (i) {
      var dw = 20;
      var w = $(this).width() + dw;
      $(this)
          .append('<i class="c-1"></i>')
          .append('<i class="c-2"></i>')
          .append('<i class="c-3"></i>')
          .append('<i class="c-4"></i>')
          .wrapInner(box);

      if (id == 'nav') {
        $(this).css({'padding-top': 0, 'padding-right': 0, 'padding-left': 0, 'width': w+'px'}); 
      } else {
        $(this).css({'padding': 0});
      } 
      
    });
  },

  // tisk
  print: function () {
    // 
    $('#footer ul:first').append('<li class="nob"><a href="">Tisk</a></li>');    
    $('#footer ul:first a:last').bind('click', function (e) {
      window.print();
      e.preventDefault();
    }); 
    //
    $('#content ul.operator').append('<li class="print"><a href="">Vytiskni</a></li>');
    $('#content ul.operator li:last a').bind('click', function (e) {
      window.print();
      e.preventDefault();
    });
  },
  
  // inline seznam (prodejna)
  list: function () {
    $('#content ul.inline li:last').addClass('nob');
  },
  
  // pruhovana tabulka (produkt)
  table: function () {
    $('#content table.overview tr:even').addClass('even');
  },
  
  // slovnicek pojmu: centrovany seznam
  glossary: function () {
    $('#glossary li a')
      .wrapInner('<span></span>')
      .css({'padding-right': 0, 'padding-left': 0}); 
  },
  
  // link do noveho okna: paticka, a[rel='window']
  newWindow: function () {
    $('#footer div a, a[rel="window"]').bind('click', function (e) {
      window.open(this);
      e.preventDefault();
    });
  },
  
  // plovouci anketa
  poll: function () {
    var pollBox = $('#o-poll');
    if (!pollBox.offset()) return;
    
    var y = pollBox.offset().top, scrollY;
    $(window).bind('scroll', function (e) {
      scrollY = $(this).scrollTop() + y;
      pollBox.animate({'top': scrollY +'px'}, {queue: false, easing: 'easeOutBounce', duration: 2000});

    });
    
  }

};



/* formulare
--------------------------------------- */
var form = {
  //
  init: function () { 
    this.preText();     // input: predefinovane hodnoty 
    this.selectBox();   // select: onchange odeslani, onchange presmerovani
  },

  // onchange
  selectBox: function () { 
    $('#center-box select.onchange')
      .each(function (i) {
        $(this).bind('change', function () {
          $(this).parents('form').submit();
        });
      });
    $('#center-box select.redirect')
      .each(function (i) {
        $(this).bind('change', function () {
          location.href = this.options[this.selectedIndex].value
        });
      });
  },

  // definovane hodnoty
  preText: function () {
    // vyhledavani
    this.toggleValue('#header input[type="text"]', TXT.search);
    this.toggleValue('input[type="text"].search', TXT.glossary);

    // odeslat priteli
    this.toggleValue('input[type="text"].name', TXT.send2Friend.name);
    this.toggleValue('input[type="text"].sender', TXT.send2Friend.sender);
    this.toggleValue('input[type="text"].recipient', TXT.send2Friend.recipient);
    this.toggleValue('textarea.message', TXT.send2Friend.message);

  },

  // focus - blur toggle
  toggleValue: function (obj, txt) {
    var $obj = $(obj);
    $obj
      .val(txt)
      .bind('focus', function () {
        if ($obj.val() == txt) {
          $obj.val('');
        }
      })
      .bind('blur', function () {
        if ($obj.val() == '') {
          $obj.val(txt);
        }
      });

    // msie focus fix
    if ($.msie) {
      $obj 
        .bind('focus', function () {
          $obj.addClass('focus');  
        })
        .bind('blur', function () {
          $obj.removeClass('focus');  
        });
    }

  }

}



/* 
--------------------------------------- */
var TXT = {

  // vyhledavani
  search:     'Hledej ve článcích',
  glossary:   'Hledat ve slovníku pojmů',

  // odeslat priteli
  send2Friend: {
    name:       'Váše jméno',
    sender:     'Váš e-mail',
    recipient:  'E-mail adresáta',
    message:    'Vaše zpráva …'

  }

};



// Prolinaci obrazky v headeru
function slideSwitch() {
    var $active = $('#banner IMG.active');

    if ( $active.length == 0 ) $active = $('#banner IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#banner IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1500, function() {
            $active.removeClass('active last-active');
        });
}

$(document).ready(function() {
$(function() {
    setInterval( "slideSwitch()", 3500 );
});

});
