var Core = {

	controller: '',
	action: '',
	webHost: '',
	webView: '',
	webImg: '',
	dateTime: '',
	lang: '',

	init : function() {

		// Fancybox.
		$('.fancybox').fancybox({
			'titlePosition'	: 'inside'
		});

		$(".carousel").jCarouselLite({
		       btnNext: ".imageNext",
		      btnPrev: ".imagePrev"
		 });
		// Poleć znajomemu.
		Core.dialog('dlgRecommend', '#btnOpenRecommend');

		// Newsletter.
		Core.newsletter('dlgNewsletter', '#btnOpenNewsletter');

		// Zakładki.
		Core.tabs();

		// Banery reklamowe.
		Core.banners();

		// Zmiana zdjęcia po wskazaniu kursorem.
		Core.hoverImage();

		// Drukowanie stron.
		Core.print();
	},

	print: function() {

		$('.btnPrint').click(function(){

			xferWindow = Core.openWindow(Core.webHost + 'print,index.html', 640, 280);
			xferWindow.opener = self;

			return false;
		});
	},

	hoverImage: function() {

		$('img.hoverImage, input.hoverImage').mouseenter(function(){

			if ($(this).hasClass('active') === false) {
				$(this).attr('src', $(this).attr('src').replace('.gif', '_on.gif'));
				$(this).attr('src', $(this).attr('src').replace('.jpg', '_on.jpg'));
			}

		}).mouseleave(function(){

			if ($(this).hasClass('active') === false) {
				$(this).attr('src', $(this).attr('src').replace('_on.gif', '.gif'));
				$(this).attr('src', $(this).attr('src').replace('_on.jpg', '.jpg'));
			}

		});
	},

	inputDescription: function(formName, elementName, description) {

		var form = $(formName), element = $(elementName);

		if (element.val() === '')
			element.val(description);

		element.focus(function(){

			if ($(this).val() === description)
				$(this).val('');

		}).blur(function(){

			if ($(this).val() === '')
				$(this).val(description);

		});

		form.submit(function(){

			if (element.val() === description)
				element.val('');

		});

	},

	banners: function() {

		$('div.advImageContent div.advImagePlaceholder').each(function(){

			swfobject.embedSWF(
				$(this).children('.path').text(), 'b' +
				$(this).attr('id').substr(1),
				$(this).children('.width').text(),
				$(this).children('.height').text(), '9.0.0', '', { }, { wmode: 'opaque' }, { }
			);

		});

		if ($.cookie('advTopLayer') !== '1') {

			var advTopLayer = $('#advTopLayer'),
				advImagePlaceholder = advTopLayer.find('.advImagePlaceholder');

			if (advTopLayer.length > 0 && advImagePlaceholder.length > 0) {

				$.fancybox({
					autoScale: true,
					enableEscapeButton: false,
					hideOnOverlayClick: false,
					height: parseInt(advSwf.children('.height').text()),
					href: advSwf.children('.path').text(),
					margin: 0,
					overlayOpacity: 0,
					padding: 0,
					scrolling: 'no',
					swf: { wmode: 'opaque' },
					titleShow: false,
					type: 'swf',
					transitionIn: 'none',
					transitionOut: 'none',
					width: parseInt(advSwf.children('.width').text()),

					onStart: function() {

						$.cookie('advTopLayer', 1);

					}
				});

			}
			else if (advTopLayer.length > 0) {

				$.fancybox({
					autoScale: true,
					enableEscapeButton: false,
					hideOnOverlayClick: false,
					href: '#advTopLayer',
					margin: 0,
					overlayOpacity: 0,
					padding: 0,
					scrolling: 'no',
					titleShow: false,
					transitionIn: 'none',
					transitionOut: 'none',

					onStart: function() {

						$.cookie('advTopLayer', 1);

					}
				});
			}
		}

	},

	dialog: function(dialogName, buttonOpenName) {

		var dialog = $('#' + dialogName),
			form = dialog.find('form'),
			messages = dialog.find('.messageList'),
			inputs = dialog.find('input.text, textarea');

		dialog.submit(function(){

			// Komunikat lub animacja ładowania.
			messages.html('<p>Proszę czekać...</p>');
			messages.show();
			
			// Jeżeli to newsletter - odeślij do mailpro
			if (dialogName == 'dlgNewsletter') {
				form.submit();
				return;
			}

			// Przeskaluj okno.
			$.fancybox.resize();

			// Wyślij żądanie.
			$.post(form.attr('action'), form.serialize(), function(data){

				if ('OK' === data.status) {
					inputs.not('input[type="checkbox"]').val('');
					inputs.removeAttr('checked');
				}

				var html = '';

				for (var i in data.messages) {
					html += '<p class="' + data.status + '">' + data.messages[i] + '</p>';
				}

				messages.html(html);

				// Przeskaluj okno.
				$.fancybox.resize();

			}, 'json');

			return false;
		});

		$(buttonOpenName).fancybox({
			href: '#' + dialogName,
			titleShow: false,

			onStart: function() {

				// Usuń poprzednio wpisane wartości.
				inputs.not('input[type="checkbox"]').val('');
				inputs.removeAttr('checked');

				// Usuń poprzednie komunikaty.
				messages.html('');
				messages.hide();
			}
		});

		form.find('.btnClose').click(function(){
			$.fancybox.close();

			return false;
		});
	},

	newsletter: function(dialogName, buttonOpenName) {

		Core.dialog(dialogName, buttonOpenName);

		var dialog = $('#' + dialogName),
			form = dialog.find('form'),
			messages = dialog.find('.messageList'),
			inputs = dialog.find('input.text, textarea');

		$('#btnSignout').click(function(){

			// Komunikat lub animacja ładowania.
			messages.html('<p>Proszę czekać...</p>');
			messages.show();

			// Przeskaluj okno.
			$.fancybox.resize();

			// Wyślij żądanie.
			$.post(Core.webHost + 'newsletters_ajax,signout.html', form.serialize(), function(data){

				if ('OK' === data.status) {
					inputs.not('input[type="checkbox"]').val('');
					inputs.removeAttr('checked');
				}

				var html = '';

				for (var i in data.messages) {
					html += '<p class="' + data.status + '">' + data.messages[i] + '</p>';
				}

				messages.html(html);

				// Przeskaluj okno.
				$.fancybox.resize();

			}, 'json');

			return false;
		});

		$(buttonOpenName).fancybox({
			href: '#' + dialogName,
			titleShow: false,

			onStart: function() {

				// Usuń poprzednio wpisane wartości.
				inputs.not('input[type="checkbox"]').val('');
				inputs.removeAttr('checked');

				// Usuń poprzednie komunikaty.
				messages.html('');
				messages.hide();
			}
		});

		form.find('.btnClose').click(function(){
			$.fancybox.close();

			return false;
		});
	},

	tabs: function () {
		// Zakładki.
		$('.buttons.ajax a').click(function() {

			// Pobierz zapamiętane zakładki.
			var activeTabs = $.cookie('ACTIVE_TABS');

			if (activeTabs)
				activeTabs = activeTabs.split(',');
			else
				activeTabs = new Array();

			var id = activeTabs.indexOf($(this).parent().children('a.active').attr('href'));

			if (typeof id != 'undefined')
				activeTabs.splice(id, 1);

			// Zmień zakładki.
			$(this).parent().next('.tabs').children('div.tab').hide();
			$($(this).attr('href')).show();

			$(this).parent().children('a').removeClass('active');
			$(this).addClass('active');

			// Zapamiętaj aktywane zakładki.
			activeTabs.push($(this).attr('href'));

			$.cookie('ACTIVE_TABS', activeTabs.join(','));

			return false;
		});

		// Przywróć zakładki.
		var activeTabs = $.cookie('ACTIVE_TABS');

		if (activeTabs)
			activeTabs = activeTabs.split(',');
		else
			activeTabs = new Array();

		for (var i in activeTabs) {

			var button = $('.buttons.ajax a[href="' + activeTabs[i] + '"]');

			button.parent().next('.tabs').children('div.tab').hide();
			$(button.attr('href')).show();

			button.parent().children('a').removeClass('active');
			button.addClass('active');
		}
	},

	openWindow : function( href, width, height ) {
		return window.open(href, 'window', 'width=' + (width | 650) + ', height=' + (height | 650) +
			', menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=no, status=no');
	}
}

$(document).ready(function() {

	// Zamień pierwszą literę oraz każdą przed "_" na wielkie.
	var controller = Core.controller.replace(/(^|_)([a-z])/g, function(m, p1, p2) {
		return p1 + p2.toUpperCase();
	});

	Core.init();

	if (window[controller] != undefined) {

		if (typeof window[controller]['init'] == 'function') {
			window[controller]['init']();
		}

		if (typeof window[controller][Core.action] == 'function') {
			window[controller][Core.action]();
		}
	}
});

