$(document).ready(function(){

    var countBoxes = 0;
    $('.basket-side-box').each(function(){
	if(countBoxes > 2)
	{
	    $(this).hide();
	}
	countBoxes++;
    });
    
    $('#showAllBasketSide').click(function(){
	$('.basket-side-box').show();
	$(this).hide();
    });

    $('#personalyTransport').hide();
    $('.address-box').hide();
    $("div[id^='address']").hide();

    $("input[name='transport']:checked").each(function(){
        if($(this).hasClass('displayShops'))
        {
            $('#personalyTransport').show();
        }
        if($(this).attr('class').match('showAddress'))
        {
            $('.address-box').show();
            var identif = $(this).attr('class').replace(/.*showAddress/, '');
            $("#address"+identif).show();
        }
    });
    
    $("input[name='transport']").click(function(){
        $('#personalyTransport').hide();
        $("[id^='address']").hide();
        $('.address-box').hide();
        if($(this).hasClass('displayShops'))
        {
            $('#personalyTransport').show();
        }
        if($(this).attr('class').match('showAddress'))
        {
            $('.address-box').show();
            var identif = $(this).attr('class').replace(/.*showAddress/, '');
            $("#address"+identif).show();
        }

    });

//    $("[class*='showAddress']").click(function(){
//        $("[id^='address']").hide();
//        $('.address-box').show();
//        var identif = $(this).attr('class').replace(/.*showAddress/, '');
//        $("#address"+identif).show();
//    });
            // JS HELP
            $('.tip-price, .tip-priceDiscount').each(function(){    	
            	var $this = $(this),
            		cons = {
                		top: 7,
                		left: 19	
            		},
            		time = null,
                    $box = $('#tip-price');
                    if($this.is('.tip-priceDiscount')){
                        $box = $('#tip-priceDiscount');
                    }

                if($box.length){
                    $box.css({'position':'absolute', 'z-index':'10'});
                    
                    $this
                        .hover( 
                            function(e){
                        		$box.hide();
                    			time = setTimeout(function(){
                        			$box.show().css({'opacity':'0', 'top':'', 'left':''});
                        			var offset = $this.offset();
                        			var scrollTop = $('body').scrollTop() ||  $('html').scrollTop();
                        			
                    				var left = offset.left + $this.outerWidth() + 10;
                    				
                    				/*(offset.top - scrollTop) < $box.outerHeight() ? $box.removeClass('reverse') : $box.addClass('reverse');*/
                    				
                        			$box.show().css({'left': left, 'opacity':''})
                        			var top = offset.top - ($box.outerHeight()/2) + 10;
                        			$box.show().css({'top': top}); 
                        			
                        		}, 500);
                        		
                        		return false;
                        	},
                        	function(e){
                        		time = clearTimeout(time);
                        		$box.hide();
                            }
                        );
                }
            });

    
	
			if($.browser.msie && $.browser.version < 7){
				$('.news-box .reset li .img img, .norm-crossroad .reset li .img img, .search-results .reset li .img img, .att-images ul span img').each(function(){
					var $this = $(this);
					var width = $this.width();
					var height = $this.height();
					
					if(width > height && width > 111){
						$this.attr('width', 111);	
					}
					else if(height > width && height > 111){
						$this.attr('height', 111);	
					}
					
				});
				$('#main-menu li, #tool-menu .basket, #tool-menu .login').hover(
					function()
					{
						$(this).addClass('hover');
					},
					function()
					{
						$(this).removeClass('hover');
					}
				)
			}
			
			$('.contestForm #vote').bind('keyup keydown', function(e){
				var reg = /\D/g;
				var reg2 = /\D/;
				var val = $(this).val();
				$(this).val(val.replace(reg, ''));
				if(reg2.test(val)){
					$('.contestForm span.error').fadeIn(400, function(){
						$(this).fadeOut(400);
					})
				}
			});
			
			function control(first){
				$('tr').each(function(){
					var $box = $(this);
					$('.disable-check', $box).each(function(){
						var $chck = $('input[type="checkbox"]', this);
						var $text = $('input[type="text"]', this);
						$text.attr('disabled', $chck.attr('checked') ? false : true);
						$chck.attr('checked') ? $text.removeClass('disable') : $text.addClass('disable');
						if(first){
							$chck.click(function(){
								var index = $('.disable-check', $box).index($(this).parents('.disable-check'));
								$('.disable-check', $box).not(':eq('+index+')').find('input[type="checkbox"]').attr('checked', false);
								control(false)	
							});
						}
					});
				});
			}
			control(true);
			
			/* HOVER */
            $('#shareList').each(function(){
               var timer = null;
               
               $(this)
                .hover(
                    function(){
                        timer = clearTimeout(timer);
                        $(this).addClass('hover');   
                    }, 
                    function(){
                        var $this = $(this);
                        timer = setTimeout(function(){
                            $this.removeClass('hover');
                        }, 100)
                    }
                );
            });
            
			$('.reaction').click(function(){
				var sum = $(this).prev().text().replace('.', '')
				var $text = $('.new-comment textarea')
				$text.val($text.val()+'['+sum+']');
			});
	
            $('.vote').each(function(){
                var $this = $(this);
                $('p a', $this).click(function(){                 
                    $.ajax({
                        type: 'GET',
                        //url: ''
                        data: $(this).attr('href').substr(1)+'&ajax=true',
                        dataType: 'xml',
                        success : function(xml){
                            $('p a', $this).remove();
                            var count = $('count', xml).text();
                            var vote = parseFloat($('vote', xml).text());
                            $('p span', $this).width(vote*21);
                            $('h3 span span', $this).text(count);
                            //alert(count+', '+vote);
                        }
                    });
                    return false;
                });
            });
            
            
            // funny action images
            $('.crossroad-action li').each(function(){
                var $this = this;
                var $item = $('.item', $this);
                var size = $item.size();
                var sum = 5;
                var top = Math.floor(size/sum)
                if(top>0){
                    var wOff = $('.wrap', $this)[0].offsetLeft;
                    $('.wrap', $this).css('left', -81+'px')
                    
                    for(var i = 1; i<=top; i++){
                        $item.slice(sum+((i-1)*sum), sum*2+((i-1)*sum)).css({'top':(150*i)+'px', 'left':(-162-18)*i+'px'})


                        
                        $('.img', $this).height($('.img', $this).height()+(150*i))
                    }
                }
                //alert(top);
            }); 

            
            // Init pdBox
            $('.thickbox').pdBox({minWidth: 400});
            
            
            // Basket
            $('.basket table tr:last-child').each(function(){
                if($(this).hasClass('noborder'))
                {
                    $(this).prev().addClass('last');
                }
                else
                {
                    $(this).addClass('last');
                }
            });
            
            // Payment and transport
            var Order = function(){
                this.box = {
                    $pay: $('.payment-box'),
                    $trans: $('.transport-box')
                };
                this.check = function(c){
                   $('.row:not(.'+c+')', this.box.$pay).slideUp(250).attr('checked', false);
                    $('.row.'+c, this.box.$pay).filter(':hidden').slideDown(250);        
                };
                var that = this;
                this.init = function(){
                    $('.radio:checked', this.box.$trans).each(function(){
                        var id = $(this).attr('id');
                        that.check(id);      
                    });
                    $('.radio', this.box.$trans).click(function(){
                        var id = $(this).attr('id');
                        that.check(id);      
                    });
                };
                this.init();
            };
            var order = new Order();
            
            // Focus Bonus input
            $('.bonus-box .row').each(function(){
            	var $this = $(this);
            	$('input[type=radio]', $this).click(function(){
					$('input[type=text]', $this).focus();
				});
            	$('input[type=text]', $this).bind('click, focus', function(){
					$('input[type=radio]', $this).attr('checked', 'true');
				});
				
			});
            
            // Adjust heights
            function init(){
                $('.product-list').adjustHeights({
                    elements : ['.status'],
                    vAlign : 'center',
                    adjust : false
                });
                
                $('.product-scroll').adjustHeights({
                    elements : ['.name', '.desc','.prices'],
                    vAlign : 'top'
                });
                
                $('.product-list:not(.product-side) ul').adjustHeights({
                    elements : ['.name','p.small','.prices'],
                    vAlign : 'top'
                });
                
                $('#product .product-object .img-box').adjustHeights({
                    elements : ['.status'],
                    vAlign : 'center',
                    adjust : false
                });
                
                $('.article-box').adjustHeights({
                    elements : ['.wrap'],
                    vAlign : 'top'
                });
                
                $('.dictionary-message').adjustHeights({
                    elements : ['h2, p'],
                    vAlign : 'center'
                });
                
                
                /* POSITIONED CORNER IE 6 RECALCULATE */
                $('.cor').css('position','absolute');
                
            }
            
             // Focus
        	$('input#q').each(function(){
        		var btntext = $(this).attr('value');
        		$(this).focus(function(){
        			text = $(this).attr('value');
        			if( text == btntext ){
        				$(this).attr('value', '');
        			}
        		}).blur(function(){
        			var text2 = $(this).attr('value');
        			if( text2 == '' || text2 == undefined ){
        				$(this).attr('value', btntext);
        			}
        		});
        	}); 
            
            // Volaní záložek
            $('.tabs-box').pdTabs({/*callFunction: function(){init()}*/});
            
            $('.tabs-box .tab-list li.active').each(function(){
                $(this).next().addClass('next');
                $(this).prev().addClass('prev');
            });
            $('.tabs-box .tab-list li a').click(function(){
                if($(this).parent().is('.active')){
                    $('.tabs-box .tab-list li').removeClass('next').removeClass('prev');
                    $(this).parent().next().addClass('next');
                    $(this).parent().prev().addClass('prev');
                }
                      
            });
            
            // FONT SIZE
            $(document).fontSizeListener({
                element: '#main-menu',
                callFunction : function(){init()}
            });
            
            // ROZBALOVACÍ MENU 
            $('ul.prod ul').each(function(){
                var status = true;
                var el = $(this);
                var count = 0;
                var langs = {
                    cs:{
                        open: 'Další kategorie'
                       ,close: 'Skrýt další kategorie'
                    }
                   ,sk:{
                        open: 'Ďalšie kategórie'
                       ,close: 'Skryť ďalšie kategórie'
                    } 
                };
                var langcode = $('html').attr('lang');
                var newUl = $('<ul style="border:none;"></ul>')
                
                $('li', this).each(function(i){
                    if(i>2){
                        newUl.append($(this));
                        if($(this).hasClass('.active')){
                            count++;
                        }
                    }
                });
                
                if(count<=0){
                    newUl.hide();
                    status = false;
                    el.append('<li><a href="#" class="next">'+ langs[langcode]['open'] +'</a></li>');
                }
                else{
                    el.append('<li><a href="#" class="next">'+ langs[langcode]['close'] +'</a></li>');
                }
                el.append(newUl);
                
                $('.next', this).click(function(){
                    if(status)
                    {
                        newUl.slideUp()
                        status = false;
                        $(this).text(langs[langcode]['open'])     
                    }
                    else
                    {
                        newUl.slideDown()
                        $(this).text(langs[langcode]['close'])
                        status = true;
                    }
                    return false;
                });
            });
            
            // FILTR
            var Filter = function(){
                
                var setC = '';
                var setL = '';
                
                this.box = {
                    country: $('#filterCountry')
                    ,locality:  $('#filterType')
                    ,producer:  $('#filterProducer')
                }
                
                this.cBox = {
                    country: this.box.country.html()
                    ,locality: this.box.locality.html()
                    ,producer: this.box.producer.html()
                }
        
                this.controlLocality = function(){
                    //var c = $('option', this).filter(':selected').attr('class').replace(/\s/g, ".");
                    var c = $('option', this).filter(':selected').attr('class').split(' ');
                    	c.shift();
                        loc = c.shift();
                        
                    //console.log('country: '+ setC);
					//console.log('loc: '+ loc);
					    
                    if(loc != '' && typeof(loc)!='undefined')
                    {
						var sel = $('option:selected',that.box.producer).val();
                    	that.box.producer.html(that.cBox.producer);
                    	$('option[value="'+sel+'"]',that.box.producer).attr('selected',true);
                        $('option:not(:first-child):not(.'+loc+')', that.box.producer).remove();
                        //$('option.'+c, that.box.producer).show();
                    }
                    else{
                    	var sel = $('option:selected',that.box.producer).val();
                    	that.box.producer.html(that.cBox.producer);
                    	$('option[value="'+sel+'"]',that.box.producer).attr('selected',true);
                        $('option:not(:first-child):not(.'+setC+')', that.box.producer).remove();
                        //$('option.'+setC, that.box.producer).show();
                    }
                };
                
                this.controlProducer = function(){
                    var c = $('option', this).filter(':selected').attr('class')//.split(' ');
                    	loc = c.replace(/country(\d*)/g, '').replace(/^\s*/g, '').replace(/\s*$/g, '').replace(/\s/g, ', .');
    
                    	//c.shift();
                    	//loc = c.shift();
                        
                        //console.log(loc);
                    
					//console.log('V country: '+ setC);
					//console.log('V loc: '+ loc);    
                    
                    if(loc != '' && typeof(loc)!='undefined')
                    {
                    	var sel = $('option:selected',that.box.locality).val();
                    	that.box.locality.html(that.cBox.locality);
                    	$('option[value="'+sel+'"]',that.box.locality).attr('selected',true);
                        $('option:not(:first-child):not(.'+loc+')', that.box.locality).remove();
                        $('option:not(:first-child):not(.'+setC+')', that.box.locality).remove();
                        /*
                        for(var i = 0; i < c.length; i++)
                        {
                            $('option.'+c[i], that.box.locality).show();
                        }
                        */
                    }
                    else{
                    	var sel = $('option:selected',that.box.locality).val();
                    	that.box.locality.html(that.cBox.locality);
                    	$('option[value="'+sel+'"]',that.box.locality).attr('selected',true);
                        $('option:not(:first-child):not(.'+setC+')', that.box.locality).remove();
                        //$('option.'+setC, that.box.locality).show();
                    }
                };
                
                var that = this;
                
                this.controlCountry = function(){
                    setC = $('option', this).filter(':selected').attr('class');
                    //console.log('country: '+ setC);
                    if(setC != '')
                    {
                        that.box.locality.attr('disabled', false);
                        that.box.locality.html(that.cBox.locality);
                        that.box.producer.html(that.cBox.producer);
                        $('option:first-child', that.box.locality).attr('selected', true);
                        $('option:first-child', that.box.producer).attr('selected', true);
                        $('option:not(:first-child):not(.'+setC+')', that.box.locality).remove();
                        $('option:not(:first-child):not(.'+setC+')', that.box.producer).remove();
                        //$('option.'+setC, that.box.locality).show();
                        //$('option.'+setC, that.box.producer).show();
                        that.box.producer.bind('change', that.controlProducer);
                    }
                    else{
                        that.box.locality.attr('disabled', true);
                        that.box.locality.html(that.cBox.locality);
                        that.box.producer.html(that.cBox.producer);
                        $('option:first-child', that.box.locality).attr('selected', true);
                        $('option:first-child', that.box.producer).attr('selected', true);
                        $('option', that.box.locality).show();
                        $('option', that.box.producer).show();
                        that.box.producer.unbind('change');
                    }
            };
    
            this.init = function(){
                if($('option:first-child', this.box.country).is(':selected'))
                {
                    this.box.locality.attr('disabled', true);    
                }
                else{
                    setC = $('option', this.box.country).filter(':selected').attr('class');
                    $('option:not(:first-child)', that.box.locality).hide();
                    $('option.'+setC, that.box.locality).show();
                    var c = $('option', this.box.locality).filter(':selected').attr('class').replace(/\s/g, ".");
                    if(c != '')
                    {
                        $('option:not(:first-child)', that.box.producer).hide();
                        $('option.'+c, that.box.producer).show();
                    }
                    that.box.producer.bind('change', that.controlProducer);
                }
                this.box.country.bind('change', this.controlCountry);
                this.box.locality.bind('change', this.controlLocality);
            };
            this.init();
        };
        
        $('.filter-box').each(function(){        
            var filter = new Filter();
        });
        
        // Open box
        $('.open-box').each(function(){
            var $this = $(this);
            var $chck = $('h2 input', this);
            var attr = $chck.attr('checked')
            var size = 0;
            $('.box :input', $this).each(function(){
                if($(this).is(':text') && $(this).val() != '')
                {
                    size++;
                }
                else if($(this).attr('checked'))
                {
                    size++;
                }
            });
            if(attr || size != 0)
            {
                $chck.attr('checked', true);
                $this.data('status', true);
            }
            else
            {
                $('.box', this).hide();
                $this.data('status', false);
            }
            $('h2 input', $this).click(function(){
                if($this.data('status'))
                {
                    $chck.attr('checked', false);
                    $('.box', $this).slideUp('fast');
                    $this.data('status', false);
                }
                else
                {
                    $chck.attr('checked', true);
                    $('.box', $this).slideDown('fast');
                    $this.data('status', true);
                }
            });
        });
        
        $('.openItem').each(function(){
            var $this = $(this);
            var $chck = $('#extraAction6', this);
            var attr = $chck.attr('checked');
            var size = 0;
            $('.box :input', $this).each(function(){
                if($(this).is(':text') && $(this).val() != '')
                {
                    size++;
                }
                else if($(this).attr('checked'))
                {
                    size++;
                }
            });
            if(attr || size != 0)
            {
                $chck.attr('checked', true);
                $this.data('status', true);
            }
            else
            {
                $('.box', this).hide();
                $this.data('status', false);
            }
            $chck.click(function(){
                if($this.data('status'))
                {                
                    $(this).attr('checked', false);
                    $('.box', $this).slideUp('fast');
                    $this.data('status', false);
                }
                else
                {                
                    $(this).attr('checked', true);
                    $('.box', $this).slideDown('fast');
                    $this.data('status', true);
                }
            });
                 
	        var blurSize = 0;
            $('.box :input', $this).bind('blur', function(){
            	blurSize = 0;
	        	$('.box :input', $this).each(function(){
	                if ( $(this).val() != '' ) {
	                    blurSize++;
	                }		            
				});				
				if (blurSize==3) {
					var data = 'albatrosID='+$('#albatrosID', $this).attr('value')+'&albatrosFirstname='+$('#albatrosFirstname', $this).attr('value')+'&albatrosLastname='+$('#albatrosLastname', $this).attr('value')+'&btnAlbatros=true&ajax=true'
					$.ajax({ 
              			type: "POST",
              			data: data,
              			success: function(responseXML) {
              				$('img, .col-r .message', $this).remove();
							$('.col-r', $this).prepend(responseXML);		        		    
              			}
                  });
				} 				
	        });
        });
        
        // CONFIRM MAIL
        var interval = null;
        var counter = 0;
	 $('#login-info').hide();
        $('#orderemail:not(.noAjax)').bind('focus blur', function(e){
            var el = $(this);
            var pEL = $(this).parents('div.login');
            var url = window.location.href;
            var oldEmail = el.val();
            var email = el.val();
            var rMail = /.+@.+\..+/;
            function testEmail(){
                email = el.val();
                if (email != oldEmail || counter == 0)
                {
                    counter++;
                    oldEmail = email;
                    
                    if (rMail.test(email))
                    {  
                        $.ajax({ 
                  			type: "POST",
                  			url: url,
                  			data: 'ajax=true&email='+email,
                  			//dataType: 'xml',
                  			success: function(responseXML)
                  			{
                  			   $('#send-pass').remove();
                  			   if(responseXML!='0')
					   {
					      pEL.addClass('highlight');
					    $('#login-info').show();
					      el.parents('p').after(responseXML);
					      $('#send-text').hide().slideDown(300, function(){
						  $('#send-pass .int-text').focus().bind('keyup keydown', function(e){
						      if(e.keyCode == '13')
						      {
							  $('#send-pass .btn').trigger('click');
							  return false;
						      }
						  });
					      });
					      $('#send-pass .btn').click(function(e){
						  var pass = $('#pwdLogin').val();
						  $.ajax({
								    type: "POST",
								    url: '/uzivatel?ajax=true&xml=true',
								    data: 'loginname='+email+'&password='+pass+'&btnLoginSubmit=Login',
								    dataType: 'xml',
								    success: function(responseXML)
								    {
										    //var elements = $('user',responseXML).children();
										    $('*', $('user',responseXML)).each(function() {
											    $('#order'+this.tagName).val($(this).text())
											    });

											    // HLÁŠKY
											    $('.message').remove();
											    var mType = $('messages',responseXML).children()[0].tagName;
											    var mText = $('messages',responseXML).text();
											    var message = $('<div class="message '+ mType +'-message"></div>');
											    message.append('<div class="spc"><div class="corner"><p></p></div></div>');
											    message.find('p').text(mText);
							    $('#orderForm .in').prepend(message);

							    // VYPNUTNÍ AJAXU
							    if (mType=='ok'){
								$('#send-pass').remove();
								el.unbind('change keyup');
								$('.ajaxRemove').remove();
								pEL.removeClass('highlight');
							    }

							    // ROZKLIKAVANI CHECKBOXŮ
							    $('.open-box').each(function(){
								var $this = $(this);
								var size = 0;
								$('.box :input', $this).each(function(){
								    if($(this).val() != '')
								    {
									size++;
								    }
								});
								if(size != 0 && !$this.data('status'))
								{
								    $('input[type="checkbox"]', this)
									.trigger('click')
									.attr('checked', true);
								}
							    });
								    },
								    complete: function(XMLHttpRequest, errorText, exception)
								    {

								    }
						  });
						  e.preventDefault();
					      });
					   }
					   else
					   {
						pEL.removeClass('highlight');
					   }   
                  			},
                  			complete: function(XMLHttpRequest, errorText, exception)
                  			{	 
                                 
                  			}
                    	});
                    }
                    else{
                        pEL.removeClass('highlight');
                        $('#send-pass').remove();
                    }
                }
            }   
            
            if(e.type == 'focus')
            {
                interval = clearInterval(interval);
                interval = setInterval(function(){
                testEmail()
                }, 200);
            }
            else if (e.type == 'blur')
            {
                interval = clearInterval(interval);
            }  
                
            //return false;
        });
        
        // OPEN SLIDE FORM
        var loaded = function(){
            this.onload = null;
        };
        var img1 = document.createElement('img');
        $(img1).bind('load', loaded).attr('src', '/img/bg/slideform.png');
        $('#tool-menu .logform a').slideForm();
        
        // AFFIL LINK, BONUS LINK
        $('.affil-link input, .user-bonus input:not(.disable)').click(function(){
            this.select();
        });
        
        // CROSSROAD IMG
       /* $('#home-crossroad').each(function(){
            var speed = 200;
            var $this = $(this);
            var $li = $('li', $this);
            $li.each(function(){
                    $('img', this).height(294);
                    $('img', this).css({'position':'absolute', 'top':'0', 'left':'0'});
                })
                .hover(
                    function(){
                        $('img', this).animate({
                            top: '-5px',
                            height: '304px'
                        }, speed);
                    }, 
                    function(){
                        $('img', this).animate({
                            top: '0',
                            height: '294px'
                        }, speed);
                    }
                );
                                function(){
                        $('img', this).animate({
                            'top': '0px',
                            'height': '-='+sum+'px'
                        }, speed);
                    
        });*/
        
        /* SCROLL */
        $('.product-scroll').each(function(){
            var $this = $(this)
            var scroll = {
                el: $('ul', this)
                ,elWidth: function(){return scroll.el.width();}
                ,elScroll: function(){return scroll.el[0].scrollWidth;}
                ,elScrollL: function(){return scroll.el[0].scrollLeft;}
                ,itemWidth: function(){return $('li', scroll.el).width();}
                ,coef: 1
                ,hover: function(e){
                    e = e || window.event;
                    if(e.type == 'mouseenter'){
                        $(e.target).addClass('hover');
                    }
                    else if(e.type == 'mouseleave'){
                        $(e.target).removeClass('hover');
                    }   
                }
                ,controlN: function(){
                      if(left + scroll.elWidth() >= scroll.elScroll()){
                          $('.next', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                      }
                      if($('.prev', $this).is('.disable') && left > 0){
                          $('.prev', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
                      }
                }
                ,controlP: function(){
                    if(left <= 0){
                        $('.prev', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                    }
                    if($('.next', $this).is('.disable') && left + scroll.elWidth() < scroll.elScroll()){
                        $('.next', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                    }
                }
                ,next: function(){
                    scroll.el.stop();
                    var length = scroll.itemWidth() * scroll.coef;
                    left = left + length;
                    scroll.controlN();
                    scroll.el.animate({
                        scrollLeft: left +'px'
                    }, 300*scroll.coef);        
                }
                ,prev: function(){
                    scroll.el.stop();
                    var length = scroll.itemWidth() * scroll.coef;
                    left = left - length;
                    scroll.controlP();
                    scroll.el.animate({
                        scrollLeft: left +'px'
                    }, 300*scroll.coef);        
                }
            };
            if(scroll.elWidth() < scroll.elScroll() ){
                $this.append('<span class="prev-wrap"><span class="prev"></span></span><span class="next-wrap"><span class="next"></span></span>');
                var left = scroll.elScrollL();
                scroll.el.css('overflow-x', 'hidden');
                scroll.controlN();
                scroll.controlP();
                $('.next:not(.disable)', this).click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                $('.prev:not(.disable)', this).click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
            }
            else{
                $('.product-list-mini-scroll').addClass('no-scroll').removeClass('product-list-mini-scroll');
            }
        });
        
        /* SCROLL */
        $('.map-box-scroll, .brands-box-scroll .spc').each(function(){
            var $this = $(this)
            var scroll = {
                el: $('p', this)
                ,elWidth: function(){return scroll.el.width();}
                ,elScroll: function(){return scroll.el[0].scrollWidth;}
                ,elScrollL: function(){return scroll.el[0].scrollLeft;}
                ,itemWidth: function(){return $('.item', scroll.el).width();}
                ,coef: 1
                ,hover: function(e){
                    e = e || window.event;
                    if(e.type == 'mouseenter'){
                        $(e.target).addClass('hover');
                    }
                    else if(e.type == 'mouseleave'){
                        $(e.target).removeClass('hover');
                    }   
                }
                ,controlN: function(){
                      if(left + scroll.elWidth() >= scroll.elScroll()){
                          $('.next', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                      }
                      if($('.prev', $this).is('.disable') && left > 0){
                          $('.prev', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
                      }
                }
                ,controlP: function(){
                    if(left <= 0){
                        $('.prev', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                    }
                    if($('.next', $this).is('.disable') && left + scroll.elWidth() < scroll.elScroll()){
                        $('.next', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                    }
                }
                ,next: function(){
                    scroll.el.stop();
                    var length = scroll.itemWidth() * scroll.coef;
                    left = left + length;
                    scroll.controlN();
                    scroll.el.animate({
                        scrollLeft: left +'px'
                    }, 300*scroll.coef);        
                }
                ,prev: function(){
                    scroll.el.stop();
                    var length = scroll.itemWidth() * scroll.coef;
                    left = left - length;
                    scroll.controlP();
                    scroll.el.animate({
                        scrollLeft: left +'px'
                    }, 300*scroll.coef);        
                }
            };
            if(scroll.elWidth() < scroll.elScroll() ){
                $this.append('<div class="nav"><span class="prev-wrap"><span class="prev"></span></span><span class="next-wrap"><span class="next"></span></span></div>');
                var left = scroll.elScrollL();
                scroll.el.css('overflow-x', 'hidden');
                scroll.controlN();
                scroll.controlP();
                
                $('.next:not(.disable)', this).click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                $('.prev:not(.disable)', this).click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
            }
            else{
                $('.product-list-mini-scroll').addClass('no-scroll').removeClass('product-list-mini-scroll');
            }
        });
        
        
        /* Klikani na cely motiv */
        /*$('.home #motive').css('cursor', 'pointer').click(function(){
            window.location = $('a', this).attr('href');
            return false;
        })*/
        
        /*
        $('#head-trailer').each(function(){
            var $item = $('a', this),
                count = 0,
                timer = null,
                size = $item.size();
                
            $item.not(':first').hide();
            
            function rotate (){
                timer = null;
                count = (count+1) % size;
                $item
                    .css('z-index', '')
                    .eq(count)
                        .css('z-index', '100')
                        .fadeIn(1000, function(){
                            $item.not(':eq('+count+')').hide(); 
                            timer = setTimeout(rotate, 6500);    
                        });
            };
            
            timer = setTimeout(rotate, 5000);
        });
        */
        
        
        
// wine list
var listType = $('#listType').val();
// nastaveni limitu pro vinny listek
var limit = 330;
if(listType==5){
    limit = 280;
} else if(listType==6){
    limit = 230;
}

// ziskani popisky produktu
$('.load').blur(function(){
	var code = $(this).attr('value');
	var kam = $(this).attr('title');

	if (code != '' && $('#'+kam).attr('value') == '')
	{
		$.ajax({
	        type: 'GET',
	        url: '/ajax/loadpreview',
	        data: 'code='+$(this).attr('value'),
	        success : function(xml){
				$('#'+kam).attr('value',xml.substr(0,limit));
                                refreshCounter($('#'+kam));
	        }
	    })  
	}      
});
// nastaveni limitu pri nahrani stranky
$('.maxl').each(function(){
    refreshCounter($(this));
});
// nastaveni pocitadla pri uvolneni klavesy
$('.maxl').keyup(function(){
    refreshCounter($(this));
});      
// samotna funkce starajici se o refresh
function refreshCounter(element){
    if (element.val().length > limit) element.val(element.val().substr(0,limit));
    element.parent().next().text('Počet zbývajících znaků: ' + (limit - element.val().length));
}
});