function playMusic(sUrl, sTitle, sId, sColor, sLang) {
	var options = {
		swf: '/imp/swf/muza.swf',
		expressInstaller: '/imp/swf/expressinstall.swf',
		width: 136,
		height: 30,
		hasVersion: 8,
		params: {
			bgcolor: '#f5f5f5',
			quality: 'high',
			wmode: 'opaque',
			allowScriptAccess: 'sameDomain'
		},
		flashvars: {
			pSrc: sUrl,
			pName: sTitle,
			parametr_losowy: Math.round(Math.random()*(999)),
			pAutoplay: true,
			leadColor: sColor,
			lang: sLang
		}
	};
	$('#' + sId).html('').flash(options);
}

function showMessage (message) {
	$.fancybox('<p>' + message + '</p>', { transitionIn: 'elastic', transitionOut: 'elastic' });
}

function checkNewsletterForm(sInfo) {
	if (!$('#zgoda:checked').length) {
		showMessage(sInfo);
		return false;
	}
	else return true;
}

function submitPlatnosciPL() {
	if (!$('#platnosciPL input:checked').length)
		showMessage('Proszę zaznaczyć sposób płatności.');
}

$(function() {
	$.fn.equals = function(compareTo) {
		if (!compareTo || !compareTo.length || this.length!=compareTo.length)
			return false;
		for (var i=0; i<this.length; i++)
			if (this[i]!==compareTo[i])
				return false;
		return true;
	};

	$('.DHTMLTree').each(function() {
		var self = this;
		var activeNode = parseInt($(this).attr('rel'));
		$(self).find('a')
			.click(function() {
				var parent = $(this).parent();
				$(parent).parent().children('li').each(function() {
					var ul = $(this).find('ul:first');
					$(parent).css('font-weight', 'bold');
					if ($(ul).parent().equals($(parent)))
						$(ul).slideDown('fast');
					else
						$(ul)
							.slideUp('fast')
							.find('ul').hide();
				});
			});
		if (activeNode)
			$('#nodeId' + activeNode)
				.addClass('marker')
				.parents('ul').slideDown('fast');
	});

	$('a.actionShowInWindow').click(function() {
		var content = '#' + $(this).attr('href').split('#')[1];
		$.fancybox({
			href: content,
			titleShow: false,
			transitionIn: 'elastic',
			transitionOut: 'elastic',
			autoDimensions: false,
			onStart: function() {
				$(content).show();
				$('#sibelius').css('visibility', 'hidden');
			},
			onClosed: function() {
				$(content).hide();
				$('#sibelius').css('visibility', 'visible');
			}
		});
		return false;
	});
	$('a.YTVideo')
		.click(function(event) {
			event.preventDefault();
			var settings = {
				centerOnScroll: true,
				titlePosition: 'outside',
				overlayColor: '#000',
				transitionIn: 'elastic',
				transitionOut: 'elastic',
				autoScale: false,
				width: 640,
				height: 385,
				orig: $(this),
				href: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '&fs=1&autoplay=1&rel=0',
				type: 'swf',
				swf: {
					wmode: 'opaque',
					allowfullscreen: 'true'
				}
			};
			$.fancybox(settings);
		})
		.each(function() {
			var video = this.href.match("[\\?&]v=([^&#]*)");
			$(this)
				.attr('title', 'YouTube: ' + $(this).text())
				.text('')
				.append($('<img src="http://img.youtube.com/vi/' + video[1] + '/2.jpg" />'))
				.append($('<img src="/imp/images/layout/YTPlay.png" class="play" />'));
		});

	$('a[rel=lightbox], a.actionAjaxWindow').fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		onStart: function() {
			$('#sibelius').css('visibility', 'hidden');
		},
		onClosed: function() {
			$('#sibelius').css('visibility', 'visible');
		}
	});
	$('a.actionAjaxWindow').fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		onStart: function() {
			$('#sibelius').css('visibility', 'hidden');
		},
		onClosed: function() {
			$('#sibelius').css('visibility', 'visible');
		},
		onComplete: function() {
			$('#reviewForm').submit(function() {
				var action = $(this).attr('action');
				$.fancybox.showActivity();
				$.ajax({
					type: 'post',
					cache: false,
					url: action,
					data: $(this).serialize(),
					success: function(data) {
						if(data.indexOf('<!-- SUCCESS -->') > 0)
							$.fancybox('<p>Recenzja zapisana i czeka na akceptacje</p>');
						else
							$.fancybox('<p>Nie zapisano recenzji</p>');
					}
				});
				return false;
			});
		}
	});
	$('span.actionUnits').each(function() {
		var i = 0,
			count = parseInt($(this).text()),
			lang = $(this).attr('lang'),
			units = {
				'polski': ['pozycji', 'pozycje', 'pozycja'],
				'english': ['element', 'elements']
			};
		if (lang == 'polski') {
			if (count < 15)
				switch(count) {
					case 1: i = 2; break;
					case 2: case 3: case 4: i = 1;
				}
			else {
				var num = count % 10;
				switch (num) {
					case 2: case 3: case 4: i = 1;
				}
			}
		} else if (count > 1)
			i = 1;
		$(this).text(count + ' ' + units[lang][i]);
	});
	$('a.actionShowInstanceGroup').click(function() {
		$('#' + $(this).attr('href').split('#')[1]).slideDown('fast');
		return false;
	});
	$('input.actionRadioSubmit').change(function() {
		$('#paymentShipment')
			.attr('action', '/cart.php?action=showPaymentAndShipment')
			.submit();
	});

	$.tablesorter.addParser({
		id: 'dates',
		is: function(s) {
			// return false so this parser is not auto detected
			return false;
		},
		format: function(s) {
			numbers = s.split('.');
			return numbers[2] + '.' + numbers[1] + '.' + numbers[0];
		},
		type: 'text'
	});


	var pagerOptions = {
		container: $("#pager"),
		output: '{startRow} ' + i18n.to + ' {endRow} (' + i18n.all + ': {totalRows})',
		updateArrows: true,
		positionFixed: false
	};

	$("table.sortable").tablesorter({
		sortList: [[0,0]],
		headers: {
			0: {
				sorter:'dates'
			}
		},
		sortLocaleCompare: true
	});
	$("table.sortable").each(function(){
		var headers = new Array();
		$(this).find("thead th").each(function(i){
			headers.push('<a href="' + i + '" rel="0">'+ i18n.sortBy + ' ' + i18n[$(this).text()] + '</a>');
		});
		$(this).before('<p class="r sortBy jsSort">' + headers.join('') + '</p>');
		$(this).prev('.jsSort').find('a[href="0"]').addClass('desc');
		$(this).find("thead th").click(function(){
			$(this).parents('table.sortable:first').prev('.jsSort').find('a[href="' + $(this).index() + '"]').click();
		});
	});
	$('.jsSort a').live('click', function(e){
		var table = $(this).parents('.jsSort:first').next('table.sortable');
		e.preventDefault();
		var sorting = [[parseInt($(this).attr('href')), parseInt($(this).attr('rel'))]];
		$(table).trigger("sorton", [sorting]);
		var direction = ($(this).attr('rel') == '0' ? '1' : '0');
		$(this).attr('rel', direction);
		$(this).parent('.jsSort').find('a').removeClass('asc').removeClass('desc');
		$(this).addClass(direction == '0' ? 'asc' : 'desc');
	});

	$("table.pagable").tablesorterPager(pagerOptions);

	$('#head .social')
		.hover(function() {
			$(this).addClass('hover');
			var links = $(this).find('.links'), width = links.css('width', 'auto').width() + 1;
			links.css('width', '80px').stop().animate({ width: width + 'px' });
		}, function() {
			$(this).find('.links').stop().animate({ width: '80px' }, { complete: function() { $(this).parent().removeClass('hover'); } });
		});
});

