var Core = {

	_controller : '',
	_action : '',
	_web_view : '',
	_web_host : '',
	_date_time : '',

	init : function() {

		$.ajaxSetup({
			cache: false
		});

		$('.fancybox').fancybox({
			autoDimensions: true,
			autoScale: true,
			cyclic: true,
			titlePosition: 'inside'
		});

		// Wyszukiwarka.
		$('#search .search').focus(function(){
			if ($(this).val() == '   szukaj...') {
				$(this).val('');
			}
		}).blur(function(){
			$(this).val('   szukaj...');
		});

		// Poleć znajomemu.
		var recommendBox = $('#recommendBox'), recForm = recommendBox.find('form');

		recForm.submit(function(){

			recForm.find('.messagesList').html('<p>Proszę czekać...</p>');
			recForm.find('.messagesList').show();

			$.fancybox.resize();

			$.post(recForm.attr('action'), recForm.serialize(), function(data){

				var i, html = '';

				if (data.status == 'OK') {

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

					recForm.find('.messagesList').html(html);
					recForm.find('.messagesList').show();

					recForm.find('input, textarea').val('');
				}
				else {

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

					recForm.find('.messagesList').html(html);
					recForm.find('.messagesList').show();
				}

				$.fancybox.resize();

			}, 'json');

			return false;
		});

		$('#btnRecommend').click(function(){
			recForm.find('input, textarea').val('');
			recForm.find('.messagesList').hide();

			return false;
		});

		$('#btnRecommend').fancybox({
			autoScale: false,
			modal: true,
			titleShow: false
		});

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

			return false;
		});

		// Nagłówki flash.
		var flashvars = {
			tekst: $('#flashHeader').text()
		};

		var params = {
			wmode: 'transparent',
			scale: 'noscale',
			menu: false,
			salign: 'lt'
		};

		swfobject.embedSWF(
			Core._web_view + 'flash/textbox.swf',
			'flashHeader', '450', '30', '9.0.0', '', flashvars, params, { }
		);

		// Nagłówki w menu.
		flashvars = {
			tekst: $('#menuFlashHeader').text()
		};

		params = {
			wmode: 'transparent',
			scale: 'noscale',
			menu: false,
			salign: 'lt'
		};

		swfobject.embedSWF(
			Core._web_view + 'flash/textbox_menu.swf',
			'menuFlashHeader', '220', '30', '9.0.0', '', flashvars, params, { }
		);

		// IE Hover Hack
		$('#content .columns img, .newsList img, .gallery img').mouseenter(function(){
			$(this).addClass('.hover');
		}).mouseleave(function(){
			$(this).removeClass('.hover');
		});
	},

	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]();
		}
	}
});

