$(function() {
	jQuery.fn.uniqueId = function(suffix, clobber) {
		if ( suffix == null )
			suffix = "UniqueId";
		return this.each(function(){
			if ( !this.id || clobber )
				this.id = "n"+ (++jQuery.fn.uniqueId.index) + suffix;
		});
	};
	jQuery.fn.uniqueId.index = 0;
	var fancyboxDefaults = {
		centerOnScroll: true,
		overlayColor: '#000',
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		easingIn: 'easeOutBack',
		easingOut: 'easeInBack',
		titlePosition: 'inside'
	};
	$('a[href$=.jpg], a[href$=.png], a[href$=.gif], .fb').fancybox(fancyboxDefaults);
	$('form a.actionFormSubmit').click(function(event) {
		event.preventDefault();
	  $(this).parents('form').submit();
	});
	$('form a.actionOrderMultiProduct').click(function(event) {
		event.preventDefault();

		var form = $(this).parents('form'),
			name = $(this).attr('href').split('#')[1];
		$('<input>', { type: 'hidden', name: name }).appendTo(form).val('true')
		form.submit();

	});
	$('form a.actionOrderProduct').click(function(event) {
		event.preventDefault();
		var form = $(this).parents('form'),
			action = form.attr('action') + '#' + $(this).parents('li').attr('id'),
			name = $(this).attr('href').split('#')[1];
		$('<input>', { type: 'hidden', name: name }).appendTo(form).val('true')
		$(this).parents('form').attr('action', action).submit();

	});
	$('form a.actionCheckAll').click(function(event) {
		event.preventDefault();
		$(this).parents('form').find('input[type=checkbox]').attr('checked', 'checked');
	});
	$('form a.actionUncheckAll').click(function(event) {
		event.preventDefault();
		$(this).parents('form').find('input[type=checkbox]').attr('checked', '');
	});
	$('form .actionClickRelaoad').change(function(event) {
		var form = $(this).parents('form');
		$('<input>', { 'name': 'zmien', 'value': 'zmien' }).appendTo(form);
		$(form).submit();
	});
	$('table.cardProduct tr:odd, table.transactions tr:odd, table.simpleTable tr:odd').addClass('odd');
	$('table.hoverRow tr').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
	$('input[title]').each(function() {
		$(this).wrap('<span class="defaultValue" />');
		$('<div>', { text: $(this).attr('title') })
			.appendTo($(this).parents('.defaultValue'))
			.css({ width: $(this).outerWidth()-10, height: $(this).outerHeight(), 'line-height': $(this).outerHeight() + 'px', 'padding-left': '5px'})
			.click(function(event) {
				event.preventDefault();
				$(this).hide();
				$(this).parent().find('input').focus();
			});
		$(this)
			.focus(function() {
				$(this).parent().find('div').hide();
			})
			.blur(function() {
				if ($(this).val() == '')
					$(this).parent().find('div').show();
			});
		if ($(this).val() != '') {
			$(this).parent().find('div').hide();
		}
	});
	$('ul.a-d-s a[rel]').each(function() {
		var data = {
			banners: $(this).attr('rel').split(','),
			current: 0
		};
		$(this).uniqueId().data('animation', data);
		setInterval('bannerAnim("' + $(this).attr('id') + '")', 1500);
	});
	$('#content ul.topics .description').each(function() {
		var content = $(this).html();
		$(this).html(content.replace(/\r\n|\r|\n/g, '<br />'));
	});
	$('a.actionOverlay').click(function(event){
		event.preventDefault();
		$.sfWindow.overlay();
	});
	var markedProducts = $('#orderForm ul.products li.productMarker');
	if (markedProducts.length) {
		$('#markedProducts').removeClass('hidden');
		markedProducts.each(function(){
			$(this).appendTo('#markedProducts ul.products');
		});
		if ($('#orderForm ul.products li').length) $('#markedProducts h3.otherProducts').removeClass('hidden');
	}
	if ($('#promotions').length) promoScroller.init();
	$('input.qtt').change(function() {
		var val = Math.abs(parseInt($(this).val()));
		$(this).val((val)?val:1);
	});
});

var promoScroller = {
	slide: [],
	width: 0,
	height: 0,
	position: 0,
	dir: 1,
	animated: false,
	speed: 'slow',
	timer: null,
	interval: 4000,
	init: function() {
		var self = this,
			source = $('#promotions').attr('id', '').hide();
		$('<div id="promotions">' +
				'<a href="#" class="preview"></a>' +
				'<a href="#" class="next"></a>' +
				'<div class="wrapper">' +
				'<div class="current" />' +
				'<div class="next" />' +
				'</div>' +
			'</div>').insertAfter(source);
		self.width = $('#promotions div.wrapper').width();
		self.height = $('#promotions div.wrapper').height();
		$(source).find('li').each(function() { self.slide.push('<div class="innerContent">' + $(this).html() + '</div>'); });
		$(source).remove();
		$('#promotions div.wrapper div.current, #promotions div.wrapper div.next').css({ width: self.width, height: self.height }).html(self.slide[self.position]);
		$('#promotions a.next').click(function(event) { event.preventDefault(); promoScroller.next(); });
		$('#promotions a.preview').click(function(event) { event.preventDefault(); promoScroller.prev(); });
		$('#promotions').mouseover(function() { promoScroller.stop(); }).mouseout(function() { promoScroller.start(); });
		this.start();
	},
	next: function() {
		if (!this.animated) {
			this.dir = 1;
			this.stop();
			this.animate();
		}
	},
	prev: function() {
		if (!this.animated) {
			this.dir = -1;
			this.stop();
			this.animate();
		}
	},
	animate: function() {
		this.animated = true;
		var next = this.element(this.position + this.dir);
		$('#promotions div.wrapper div.current').html(this.slide[this.position]);
		$('#promotions div.wrapper div.next')
			.html(this.slide[next])
			.hide()
			.fadeIn(this.speed, function() { promoScroller.after(); })
		this.position = this.element(this.position + this.dir);
	},
	after: function() {
		$('#promotions div.wrapper div.current').html(this.slide[this.position]);
		this.animated = false;
		this.start();
	},
	start: function() {
		if (this.timer) this.stop();
		this.timer = setTimeout('promoScroller.next()', this.interval);
	},
	stop: function() {
		clearTimeout(this.timer);
		this.timer = null;
	},
	element: function(position) {
		if (position < 0) position = this.slide.length - 1;
		if (position >= this.slide.length) position = 0;
		return position;
	}
};

function bannerAnim(id) {
	var self = $('#' + id),
		data = self.data('animation');
	data.current++; if (data.current >= data.banners.length) data.current = 0;
	self.find('img').attr('src', '/imp/images/b/' + data.banners[data.current]);
}

