
var naradiasistUI = function() {
	return {
		
		self: this,
		
		

		init: function() {
			if ($('#promo-slide').get(0)) {
				this.promoSlidePreload();
			}
			
			this.fixFormLabels();
			this.homepageBubble();
			this.searchFieldStyling();
			this.fadeOutFlashes();
			this.requireMarks();
		},
		
		
		
		/**
		 * Preloading obrazu pro promo slide
		 */
		promoSlidePreload: function() { 
			var imagesBeyond = $('#promo-slide .imgs-beyond').children('img').get();
			var bigImage = $('#promo-slide .big-img a').children('img:first');
			re = /(-(beyond|blurred))?(\.[a-z]+)$/;
			$(new Image()).attr('src', bigImage.attr('src').replace(re, '-blurred$3')).load();
			$(new Image()).attr('src', bigImage.attr('src').replace(re, '-beyond$3')).load();
			for (index in imagesBeyond) {
				$(new Image()).attr('src', $(imagesBeyond[index]).attr('src').replace(re, '-blurred$3')).load();
				$(new Image()).attr('src', $(imagesBeyond[index]).attr('src').replace(re, '$3')).load();
			}
		},
		
		
		
		/**
		 * Promo slide vyrobku Asist 
		 */
		promoSlide: function() { 
			
			var bigImg = $('#promo-slide .big-img a').children('img:first');
			var origSrc = bigImg.attr('src');

			// Definice linku kotvy
			$('#promo-slide .big-img a').attr('href', bigImg.attr('longdesc'));
						
			// Rozostrene obrazky produktu v pozadi
			var imagesBeyond = {

				// DOM elementy
				images: $('#promo-slide .imgs-beyond').children('img'),

				// vraci celkovou sirku obrazku
				totalWidth: function() {
					var total = 0;
					for (index in this.images.get()) {
						var item = $(this.images[index]);
						total += item.attr('width') + parseInt(item.css('padding-left'));
					}
					return total;
				}
			};
			
			// Rotace produktu
			var rotate = function(offset) {

				var self = this;

				re = /(\.[a-z]+)$/;
				var blurredSrc = origSrc.replace(re, '-blurred$1');
				var beyondSrc = origSrc.replace(re, '-beyond$1');
				var nextSrc = $(imagesBeyond.images[0]).attr('src').replace(/-beyond(\.[a-z]+)$/, '$1');

				if (parseInt($('#promo-slide .about').css('top')) < 200) {
					$('#promo-slide .about').animate({ top: '+=600px' }, 400, 'swing');
				}

				// Animace rozmazaneho obrazku (rychly pohyb)
				$(bigImg).attr('src', blurredSrc).animate({ left: '+=960px' }, 250, 'swing', function() {

					$(this).css({
						visibility: 'hidden',
						float: 'left',
						padding: '40px 0 0 20px',
						left: imagesBeyond.totalWidth() + 'px',
						position: 'static'
							
					}).css('visibility', 'visible').appendTo('#promo-slide .imgs-beyond').attr('src', beyondSrc);

					$('#promo-slide .imgs-beyond').children('img:first')
					.animate({ marginLeft: '-=800px'}, 400, 'swing', function() {
						$(this).css({
							position: 'absolute',
							bottom:   '30px',
							left:     '-800px',
							padding:  '0',
							margin:   '0'
						}).appendTo('#promo-slide .big-img a')
						.attr('src', nextSrc).animate({ left: '60px' }, 400);

						// rekurze
						naradiasistUI.promoSlide();
						return;
					});
				});

			}
			
			// ZOBRAZENI popisku a ceny zbozi na popredi
			about = bigImg.attr('title').split('•');
			$('#promo-slide .about').empty().css('top', '600px').append('<p class="title">' + about[0] + '<br /><small>' + about[1] + '</small></p>')
				.append('<p class="price">' + about[2] + '</p>')
				.animate({ top: '183px' }, 400, 'swing');
			
			// ZOBRAZENI zbozi na popredi (je-li schovano)
			if (parseInt($(bigImg).css('left')) < 0) {
				$(bigImg).animate({ left: '60px' }, 400);
			}
			
			// Casovac slidu
			$('#promo-slide .timebar').css('width', '1px').animate({ width: '960px' }, 5000, 'linear', function() {
				$(this).slideUp(100, function() { $(this).hide(); });

				rotate(false);
			});
						
		},
		
		
		
		timebar: function() {
			var tb = $('slide-timebar');
			tb.css('width', tb.css('width') + 1);
		},
		
		

		colours: function() {
			$('#colour-card .classic').click(function() {
				$('#colour-card a').removeClass('active');
				$('a', this).addClass('active').trigger('blur');
				$('.classic-scheme').addClass('hidden');
				$('div[title="' + $(this).text() + '"]').removeClass('hidden');
			});

			$('#colour-card .classic, #colour-card .trend').click(function() {
				$('select[name=colour]').val($(this).attr('title')).after(
					$('<div id="ajax-spinner"></div>').css({
						position: 'absolute',
						right: -22,
						top: 2
					})
				);
				$('#ajax-spinner').fadeOut(1200, function() { $(this).remove(); });
			});
		},
		


		fixFormLabels: function() {
			$('label img').click(function() {
				$(this).parent().trigger('click');
			});
		},
		


		homepageBubble: function() {
			$('#logo a').bind('mouseenter', function() {
				$('#homepage-title').html('úvodní stránka');
			}).bind('mouseleave', function() {
				$('#homepage-title').html('&nbsp;');
			});
		},



		articlesHover: function() {
			$('.product').bind('mouseover', function() {
				$('.small-photo', this).addClass('hover');
			}).bind('mouseout', function() {
				$('.small-photo', this).removeClass('hover');
			});
		},



		searchFieldStyling: function() {
			var text = 'Hledat…';
			$('#q').attr('value', text)
				.css('color', '#777')
				.bind('focus', function() {
					if ($(this).attr('value') != text)
						return;
					$(this).attr('value', '').css('color', '#000');
				})
				.bind('blur', function() {
					if ($(this).attr('value'))
						return;
					$(this).attr('value', text).css('color', '#777');
				});
			$('#search form').bind('submit', function() {
				if ($('#q').attr('value') == '' || $('#q').attr('value') == text) {
					alert('Zadejte ' + text);
					return false;
				}
			});
		},



		addInCartDone: function() {
			$('li.basket a.ajax').bind('click', function() {
				$(this).bind('ajaxSuccess', function() {
					$(this).attr('href', jQueryData.link)
						.attr('class', '')
						.addClass('done')
						.html('v&nbsp;košíku')
						.attr('title', 'Přejít do košíku');
				}).trigger('blur');
			});
		},
		
		
		
		fadeOutFlashes: function() {
			setTimeout(function() {
				$('.flash').fadeOut(750);
			}, 4000);
		},
		
		
		
		requireMarks: function() {
			// hvezdicka za povinne polozky ve form
			$('form label.required').each(function () {
				if ($(this).text().indexOf('*') == -1) {
					$(this).html($(this).text() + '<span>*</span>');
				}
			});
		}

	};
}();
