/*
	@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();
  
  $('ul.main li.active a').append('<span class="a1"></span><span class="a2"></span>');
});





/* zakladni vybava 
--------------------------------------- */
var base = {
  //
  init: function () {  

    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']
     
  },

  // 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('form.search 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 );
});
});



/**
 * welcomeBanner
 * @author Michal Petrek, <michal.petrek@actumg2.cz>
 * @copyright (c) 2011 Actum|G2, www.actumg2.cz
 * @package welcomeBanner
 * @version 1.0.1
 */
var welcomeBanner = {    
    Define: {
        isTimeout: true,
        timeout: 10000,
        cookieLive: 3600*24*30*12,
        checkCookie: true        
    },        
    init: function( Define ) {
        welcomeBanner.Define = Define;                
    },       
    run: function() {
        if( welcomeBanner.Define.checkCookie ) {
            check = this.checkCookie();    
        } else {
            check = false;
        }
        if(!check) {
            this.openFancyBox();
            this.setCookie('welcomeBannerEuronics', 'welcomeBannerEuronics', welcomeBanner.Define.cookieLive );
        }
    },    
    openFancyBox: function() {        
        $('#wbcah').lightBox();
        $('#wbcah').trigger('click');                          
        if(welcomeBanner.Define.isTimeout) {
            setTimeout('welcomeBanner.closeFancyBox()', welcomeBanner.Define.timeout );
        }               
        $("#lightbox-nav").css("cursor", "pointer");
        $("#lightbox-nav").click(function(){
            window.location = "http://www.hvezdyeuronics.cz";
        });
    },    
    closeFancyBox: function() {
        $('#lightbox-secNav-btnClose').trigger('click');
    },        
    setCookie: function(c_name,value,exdays) {
        var exdate=new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
        document.cookie=c_name + "=" + c_value;
    },
    getCookie: function(c_name) {
        var i,x,y,ARRcookies=document.cookie.split(";");
        for (i=0;i<ARRcookies.length;i++) {
            x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
            y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
            x=x.replace(/^\s+|\s+$/g,"");
            if (x==c_name) {
                return unescape(y);
            }
        }
     },
     checkCookie: function() {
        checkCookie = this.getCookie("welcomeBannerEuronics");
        if(checkCookie!=null && checkCookie!="") {
            return true;
        } else {
            return false;
        }
     }    
}

