function Size() {
	if ( $.browser.msie && $.browser.version == '7.0') {
		return false;
	}
	$('#letters').find('a').each(function(i,e) {
		var size = parseInt($(e).attr('class').replace('letter',''));
		$(e).css('outline', 'none');
		$(e).click(function(){
			var mysize = (size + 12);
			$('#main-center-in p, #main-center-in h2, .content').css('font-size',mysize + 'px');
			$('#letters').find('a').removeClass('on');
			$(e).addClass('on');
		});
	});
}
function Coms(){
	//definice "globalnich" fjíčur objektu Coms
	this.productPosition = 0;
	this.productsToShow = 3;
	this.addCount = 0;
	var me = this;
	this.shiftProductPos = function(dir) {
		if (me.productItems.length == 0) {
			return false;
		}
		 
		var pos = 0;
		switch (dir) {
			case 'r':
				if (((me.productItems.length + me.addCount) - me.productsToShow) > me.productPosition ) {
					me.productPosition++;
				}
				pos = me.productPosition;								
			break;
			case 'l':
			    if (0 < me.productPosition ) {
			    	me.productPosition--;
				}
				pos = me.productPosition ; 
			break;
			default:
				pos = me.productPosition = dir;
			break;
		} 
		me.productSlider.scrollTo(me.productItems[pos], 650 );
	    if (typeof me.scrollFinished == 'function') { 
	    	me.scrollFinished();
		}
	}	 
};
Coms.prototype.defGlobals = function() {
	//bindovani a provadeni funkci ktere jsou vicemene vsude stejne - je potreba provest az po inicializaci konkretnich objektu
	var me = this;  
	this.rArr.css('outline', 'none');
	this.lArr.css('outline', 'none');
	this.rArr.click(function(){ 	  
		me.shiftProductPos('r');	
	});	
	this.lArr.click(function(){
		me.shiftProductPos('l');		
	});
	this.shiftProductPos(0); 
}
Coms.prototype.initializeRP = function() {
	this.productsToShow = 1; 
	this.lArr = $('.listinig-left');
	this.rArr = $('.listinig-right');
	this.itemsObj = $('.right-productlist-item');
	this.productContainer = $('#right-productlist-container');
	this.productSlider = $('#right-productlist-mask');
	this.productItems = this.itemsObj.get(); 
	this.productContainer.width(this.productItems.length * 179);	
	$("#plselect").selectbox().bind('change', function(){
		window.location = $(this).val();          
	});			    
	$('#produkty-right-navi .jquery-selectbox-moreButton').text($("#plselect").attr('rel'));
	$("produkty-right-navi .jquery-selectbox-list span:last").addClass('last-item'); 	
}
Coms.prototype.initializeSP = function() {
	var me = this;                       
	this.lArr = $('.produkty-arrow-left');
	this.rArr = $('.produkty-arrow-right');
	this.productContainer = $('#bottom-product-container');
	this.productSlider = $('#bottom-product-mask');
	this.itemsObj = $('.bottom-product');
	this.productInfo = $('.produkty-ph');                       
	this.productItems = this.itemsObj.get();                 
	this.productContainer.width(this.productItems.length * 311);  
	this.scrollFinished = function() {
		var text = (me.productPosition + 1) + ' - ' + (me.productPosition + 3) + ' / ' + me.productItems.length; 
		if (me.productItems.length < 1) {
			text = '0';
		} else if (me.productItems.length <= 3) {
			text = '1 - ' + me.productItems.length + ' / ' + me.productItems.length;
		}
		me.productInfo.text(text);
	}    
	$('#onemocneni-listbox').smallSelectbox().change(function(){		
		$.cookie("kategorie", $(this).val(), { expires: 30, path: '/'});
		window.location.reload();
	});
	if ($('#onemocneni-listbox option[rel="active"]').length == 0) {
		$('#bottom-sub .jquery-selectbox-moreButton').text($("#onemocneni-listbox").attr('rel')); 
	}
}

Coms.prototype.initializeHP = function() {
	this.products = $('#product-overview');
	this.categories = $('#vyberte-onemocneni');
	this.categoriesTop = $('#vyberte-onemocneni-top');
	this.categoriesBottom = $('#vyberte-onemocneni-bottom');
	//slider pro obrazky produktu
	this.productSlider = $('#product-overview-mask');
	this.productContainer = $('#product-overview-container');
	this.itemsObj = $('.product-item:visible');
	this.productItems = this.itemsObj.get();
	this.lArr = $('.tools-arrow-left');
	this.rArr = $('.tools-arrow-right');
	this.addCount = $('.product-item-last').length; 
    this.restartButton = $('.product-item-last');
	this.ordering = eval("ordering = " + $("#json-data").text());

	var me = this;
	
	this.restartButton.click( function(){
		me.shiftProductPos(0);   
	});
	$('#product-type-selector a').click(function(){
		$('#product-type-selector a').removeClass('on');
		$(this).addClass('on');
		me.itemsObj.hide();
		var cat = $.cookie('kategorie') || '.product-item';
		if (cat != '.product-item') {
			cat = '.catid' + cat;
		}
		me.productSlider.find(cat + $(this).attr('rel')).show();
		me.itemsObj = me.productSlider.find(cat + $(this).attr('rel'));
		me.productItems = me.itemsObj.get();
		if (me.productItems.length < 4) {   
			me.restartButton.hide();
	       	me.addCount = 0;
		} else {                    
			me.restartButton.show();
			me.addCount = 1;
		}                                         
		me.productContainer.width((me.productSlider.find(cat + $(this).attr('rel')).length + me.addCount) * 150);
		me.shiftProductPos(0);
	});
	$('#produkt-button').bind('click',function(){
		me.itemsObj = $('.product-item');
		$.cookie("kategorie", '', { expires: 30, path: '/'});
		me.productItems = me.itemsObj.get();
		me.restartButton.show();
		me.addCount = 1;
		me.shiftProductPos(0);		
		me.productContainer.width((me.productItems.length + me.addCount) * 150);
		me.itemsObj.show();
	 	me.activeProducts();
	});
	$('#kategorie-button').bind('click',function(){		
	 	me.activeCategories();
	});
	
	this.activeCategories = function() {        
		$('.bt-vyberte-onemocneni-pas, .bt-vyberte-onemocneni').removeClass('bt-vyberte-onemocneni-pas').addClass('bt-vyberte-onemocneni');  
		$('.bt-vyberte-pripravek-pas, .bt-vyberte-pripravek').removeClass('bt-vyberte-pripravek').addClass('bt-vyberte-pripravek-pas');
		me.categories.show();
		me.categoriesTop.show();
		me.categoriesBottom.show();
		me.products.hide();			
	}
	this.activeProducts = function() { 
		$('#product-type-selector a').removeClass('on');
		$('#product-type-selector a:last').addClass('on');
		$('.bt-vyberte-onemocneni-pas, .bt-vyberte-onemocneni').removeClass('bt-vyberte-onemocneni').addClass('bt-vyberte-onemocneni-pas');
		$('.bt-vyberte-pripravek-pas, .bt-vyberte-pripravek').removeClass('bt-vyberte-pripravek-pas').addClass('bt-vyberte-pripravek');
		me.categories.hide();
		me.categoriesTop.hide();
		me.categoriesBottom.hide();
		me.products.show();			
	}	
    this.itemsObj.bind('click', function(){
		window.location = $(this).find('a').attr('href');
	});
   	this.itemsObj.each(function(){	
		var preloadImage = $(this).find('img').attr('alt');
		var preloadTitle = $(this).find('img').attr('title');
		var preloadHref = $(this).find('a').attr('href');
		$(this).qtip({
				content: $('<a>').attr('href',preloadHref).append($('<img/>').attr('src','/dbpic/' + preloadImage + '-x150_250').attr('title',preloadTitle)),
	        	position:{
					corner: {
						target: 'topLeft',
						tooltip: 'bottomLeft'
					}
				},
	        	hide: {
	        		fixed: true
	        	},
	        	style: {
	        		padding: '5px 15px' 
	        	}
			});  
   	});
	this.categories.find('a').each(function(i, e)	{
		$(e).bind('click', function() { 
			var cat = parseInt($(e).attr('rel'));
			$.cookie("kategorie", $(e).attr('rel'), { expires: 30, path: '/'});
			if (me.ordering[cat] != 'undefined') {
				$('.product-item').attr('rel','0');
				for (var i in me.ordering[cat]) {
					$('.product-item.pr' + me.ordering[cat][i]).attr('rel',parseInt(999 - i));
				}
				$('#product-overview-container .product-item, #product-overview-container .product-item-last').tsort({attr:"rel", place:"org",order:"desc"});	
			}
			me.itemsObj.hide();
			me.productSlider.find(".catid" + $(e).attr('rel')).show();
			me.itemsObj = me.productSlider.find(".catid" + $(e).attr('rel'));
			me.productItems = me.itemsObj.get();
			if (me.productItems.length < 4) {   
				me.restartButton.hide();
	        	me.addCount = 0;
			} else {                    
				me.restartButton.show();
				me.addCount = 1;
			} 
			me.productContainer.width((me.productSlider.find(".catid" + $(e).attr('rel')).length + me.addCount) * 150);
			me.activeProducts();
			me.shiftProductPos(0);	
		});
		if (i > 2) {
			$(e).hide();
		}
	});
	this.categories.find('a.laste').unbind('click').click(function(){
		$.cookie("kategorie", 'all', { expires: 30, path: '/'});
		me.itemsObj = $('.product-item');
		me.productItems = me.itemsObj.get();
		me.itemsObj.show();
		me.addCount = 1;
		me.productContainer.width((me.itemsObj.length + me.addCount) * 150);
		me.activeProducts();
		me.shiftProductPos(0);	
	});
	this.categories.height(86);
	this.categories.css('overflow','hidden');
	var direction = 'open';
	this.categories.bind('mouseenter',
		function () {
			if (direction == 'open') {
				$(this).find('a').show();
				$(this).animate({
					height: parseInt(me.categories.find('a').length * 25 + 14) + 'px'
				}, 50, function () {
					direction = 'close' ;
				} );
			}
	 });
	this.categories.bind('mouseleave', 
		function(){
			var catEl = $(this);
			if (direction == 'close') {	
				$(this).animate({
					height: parseInt(3 * 25 + 11) + 'px'
				}, 50, function() {
				    catEl.find('a').each(function(i, e){
						if (i > 2) {
							$(e).hide();
						}
					});
					direction = 'open';
				});
			}
			setTimeout(function(){				
				catEl.animate({
					height: parseInt(3 * 25 + 11) + 'px'
				}, 50, function() {
				    catEl.find('a').each(function(i, e){
						if (i > 2) {
							$(e).hide();
						}
					});
					direction = 'open';
				});
			},60);	
	});
	//this.productContainer.width(this.productItems.length * 150);
	this.activeCategories();
}

Coms.prototype.initialize = function() { 
	var me = this;			  
	this.comsList = $('.product-overview-sub');
	this.switcher = $('.pripravky-vice-link');
	this.select = $('#select-category');
	this.nutriTable = $('#nutric-table-content'); 
	this.nutriLink = $('.slozeni');
	this.printPage = $('.tisknout-stranku');
    this.allowHide = false;
	                                     
	this.printPage.bind('click', function(){
		window.print();
	});                                           
	
	this.toggleComs = function(val) {
		me.comsList.hide();	 
		$('#produkty-vice .cat' + val).show();
	    me.allowHide = false;
	}
	this.nutriTable.hide();
		                        
	this.select.smallSelectbox().change(function(){
		me.toggleComs($(this).val());
	});	
	$('body').click(function(){
		if (me.allowHide == false) {
			me.allowHide = true;	
		} else {
			me.comsList.hide();
		}
	});			
	this.comsList.click(function(){
		me.allowHide = false;
	});
	this.comsList.hide();
}; 
$(document).ready(function (){ 
	var sizeButons = new Size();
	
	var coms = new Coms();
	if ($('#vyberte-buttony').length > 0){		
		coms.initializeHP();
		coms.defGlobals(); 
	} else if ($('#bottom-product-mask').length > 0) { 
		coms.initializeSP();
		coms.defGlobals();
	}
	if ($('#produkty-vice').length > 0) {
		var pp = new Coms();   
		pp.initialize();
	}
	if ($('#right-productlist-mask').length > 0) {
		var rightComs = new Coms();
		rightComs.initializeRP(); 
		rightComs.defGlobals();
	}
	if ($('#bmi-form').length > 0) {
		$("#bmi-form").find('select').smallSelectbox();		
	}
	if ($("#recepty-vice").length > 0) {
		$("#selectrecipe").smallSelectbox().change(function(){
			/*opera neumi kreslit - hack */
			$('#recipe-kategory-title').hide();
			$('#recipe-kategory-title').show();
		    /*opera sucks - hack */
			
			$('#recipe-kategory-title').text($("#recepty-vice .jquery-selectbox-moreButton").text());
			$('.recipe_text').hide();
			$('.rec' + $(this).val()).show();
			if ($(this).val() == 'all') {
				$('.recipe_text').show();
			}
		});
	}
	if ($("#obce").length > 0) {
		$("#selobec").smallSelectbox().change(function(){
			//$('#obec-title').text($("#recepty-vice .jquery-selectbox-moreButton").text());
			window.location = '?id=' + $(this).val();
		});	
		$('#obce .item-0').hide();		
	}
	
	if ($('input[name=fr_captcha]').length > 0) {
		$('input[name=fr_captcha]').keyup(function(e){
  			if (e.keyCode == 13) {
				$(this).parents('form')[0].submit();
			}
		});
	}
	if ($('input[name=fr_Mobil]').length > 0) {
		$('input[name=fr_Mobil]').keyup(function(e){
  			if (e.keyCode == 13) {
				$(this).parents('form')[0].submit();
			}
		});
	}
	if ($('.enquiry_link').length > 0) {
		$('.enquiry_link').click(function(){
			window.location = $(this).find('a')[0];
		});	
		$('.enquiry_link').mouseenter(function(){
			$(this).find('a').addClass('hover');
		});
		$('.enquiry_link').mouseleave(function(){
			$(this).find('a').removeClass('hover');
		});
				
	}	
});
