$(document).ready(function(){

	// Odkaz do noveho okna
	$("a[rel='external']").attr('target', '_blank');

	// Odkaz, ktery zascrolluje na element - animace kotvy 
	$('a.scrollToElement').click(function(){
		 var $this = $(this);
		 $('html, body').animate({scrollTop: $($this.attr('href')).offset().top+'px'}, 800);
	   return false;
	}); 

	$('a[class*=fancybox]').each(function(i) {
		var url = $(this).attr('href');
		
		if (url.indexOf('?') >= 0) {
			url = url + '&ajax';
		}
		else {
			url = url + '?ajax';
		}
		
		$(this).attr('href', url);
	});

	// Vyhledavani
 	$('input#search_query').each(function(){
 		var btntext = $(this).attr('value');
 		$(this).focus(function(){
 			text = $(this).attr('value');
 			if( text == btntext ){
 				$(this).attr('value', '');
 				$(this).addClass('notBlank');
 			}
 		}).blur(function(){
 			var text2 = $(this).attr('value');
 			if( text2 == '' || text2 == undefined ){
 				$(this).removeClass('notBlank');
 				$(this).attr('value', btntext);
 			}
 		});
 	});

 	// Odkaz na zacatek stranky
    $('a.backToTop').click(function(){
        $('html, body').animate({scrollTop: '0px'}, 800);
        return false;
   });

	// Hover pro input odesilacich tlacitek - pro IE6
	$(".submit :submit").hover(
	  function () {
	    $(this).addClass("hover");
	  },
	  function () {
	    $(this).removeClass("hover");
	  }
	);

 	// Zalozky
 	$(".tabs-nav").idTabs();
 	
 	// Prehled barev
 	$('#colorBox').addClass('js');
 	$('#colorBox .color').attr('title', 'Vybrat barvu');
/*
 	$('#colorBox .color').each(function(){
 		this.title = 'Vybrat barvu';
 	})
*/ 	
 	// Výběr barev a velikostí
 	$('#product').each(function(){
     
        var color = $('#productAttrHeader .color', this),
            colorS = $('#color', this),
            sizeS = $('#size', this),
            copySizeS = sizeS.clone();
        
        // Pocatecni kontrola
        function selectTest(el)
        {   
            if( $('option:selected', el).attr('value') != '' )
            {
                var klass = $('option:selected', el).attr('class');
                sizeS.show().html($('.'+klass, copySizeS).html());
            }
            else
            {
                sizeS.hide();
            }
        };
        selectTest(colorS);
         
        // Vyber barvy selectem
        colorS.change(function(){
            selectTest(this);    
        });
        
        // Vyber barvy vzornikem
        color.click(function(){
            var klass = $(this).attr('class'); 
                klass = klass.replace(/.*(size-[0-9]*).*/g,'$1');
            $('.'+klass, colorS).attr('selected', true)
            colorS.trigger('change');      
        });  
    
    });
    
 	// Fancybox
 	$('.fancybox').fancybox();

    // Home crossroad
    $('#homeActionViewer').each(function(){
        var $item = $('.menu li', this),
            $fragments = $('.fragment', this),
            delay = 5, //sekundy
            len = $item.length,
            time = null,
            actual = 0,
            index = 0;
        
        $fragments.addClass('js-fragment')
      
        // funkce provádějící změnu obrázků a tříd
        var rotate = function(i, first){
            if(typeof i == 'undefined' || i!=index || first){
                index = typeof i == 'undefined' ? (index + 1) % len : i;
                $item
                    .removeClass('active')
                    .eq(index)
                        .addClass('active');
                        
                if(first){
                    $fragments
                        .css('z-index', '')
                        .eq(index)
                            .css('z-index', '2')
                }
                else{
                    $fragments
                        .css('z-index', '')
                        .eq(actual)
                            .css('z-index', '1')
                            .end()
                        .eq(index)
                            .stop()
                            .css({'z-index':'2', 'opacity': '0'})
                            .animate({'opacity': '1'}, 500);
                            
                             
                }
                actual = index;
            }
        };
        rotate(index, true);
        
        // objekt časovače
        var timer = function(){
            var time = null,
                w = window;
            return {
                add: function(callback, delay){
                    time = w.setTimeout(function(){
                        callback();
                        time = w.setTimeout(arguments.callee, delay)
                    }, delay)     
                }
                ,remove: function(){
                    time = w.clearTimeout(time);
                }
            }
        }();
                       
        $item.each(function(i){
                $(this)
                    .hover(
                        function(){
                            rotate(i);
                            timer.remove();
                        },
                        function(){
                            timer.remove();
                            timer.add(rotate, delay*1000);    
                        }
                    );    
        });
        
        timer.add(rotate, delay*1000);
    }); 
    
    $('#commentsBox').each(function(){
        var $item = $('li', this),
            $wrap = $('ul', this),
            $btn = $('<a href="#" class="show"></a>'),
            lg = {
                show: 'Zobrazit všechny komentáře',
                hide: 'Zobrazit jen poslední komentáře'
            };
            
        if($item.length > 2){
            var miH = $item.eq(0).outerHeight(true) + $item.eq(1).outerHeight(true) - 1;
            var maH = $wrap.outerHeight();
            $wrap
                .css({'height':miH+'px', 'overflow':'hidden'})
                .after($btn);
            
            $btn
                .wrap('<p class="all"></p>')
                .text(lg.show)
            
            $btn.click(function(){
                var $this = $(this);
                if($this.is('.hide')){
                    $wrap.animate({'height': miH+'px'}, 500)
                    $this
                        .removeClass('hide')
                        .text(lg.show);
                }
                else{
                    $wrap.animate({'height': maH+'px'}, 500)
                    $this
                        .addClass('hide')
                        .text(lg.hide);
                }
                return false;
            });
        }
    });
    
    
 });

