var Core = {

	_controller : '',
	_action : '',
	_web_view : '',
	_web_host : '',
	_date_time : '',
	_lang : '',

	init : function() {

		// Fancybox.
		$('.fancybox').fancybox({
			'titlePosition'	: 'inside'
		});

		// Poleć znajomemu.
		Core.recommend();
	},

	recommend: function() {

		var dlgRecommend = $('#dlgRecommend'),
			dlgRecommendForm = dlgRecommend.find('form'),
			dlgRecommendMessageList = dlgRecommendForm.find('.messageList');

		dlgRecommend.submit(function(){

			dlgRecommendMessageList.html('<p>Proszę czekać...</p>').show();

			$.fancybox.resize();

			$.post(dlgRecommendForm.attr('action'), dlgRecommendForm.serialize(), function(data){

				var i, html = '';

				if (data.status == 'OK') {

					for (i in data.messages) {
						html += '<p class="ok">' + data.messages[i] + '</p>';
					}
				}
				else {

					for (i in data.messages) {
						html += '<p class="error">' + data.messages[i] + '</p>';
					}
				}

				dlgRecommendMessageList.html(html);

				$.fancybox.resize();

			}, 'json');

			return false;
		});

		$('#btnOpenRecommend').fancybox({
			modal: true,
			titleShow: false,

			onStart: function() {
				dlgRecommendForm.find('input.text, textarea').val('');
				dlgRecommendMessageList.hide();
			}
		});

		$('#btnCloseRecommend').click(function(){
			$.fancybox.close();

			return false;
		});
	},

	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]();
		}
	}

	//banner

	var flashvars = {};
	var params = {'wmode':'transparent'};
	var attributes = {};

	$('#banner img').each(function(i, el) {
		var randID = 'rand'+Math.floor(Math.random()*1000);
		$(el).attr('id', randID);
		swfobject.embedSWF("app/views/flash/banner_"+Core._lang+".swf", randID, "850", "270", "7", "#FFFFFF", flashvars, params, attributes);
	})
	// górne menu
	if ($('#menu a.current img').attr('src')) {
		$('#menu a.current img').attr(
			'src',
			$('#menu a.current img').attr('src').replace('.gif', '_on.gif')
		);
	}
	$('#menu a').hover(
		function() {
			if (!$(this).hasClass('current')) {
				$(this).find('img').attr('src', $(this).find('img').attr('src').replace('.gif', '_on.gif'))
				$(this).addClass('active');
			}
		},
		function() {
			if (!$(this).hasClass('current')) {
				var src = $(this).find('img').attr('src');
				src = src.replace('_on.gif', '.gif');
				$(this).find('img').attr('src', src);
				$(this).removeClass('active');
			}
		}
	)

	// preload obrazków w głównym menu
	// (przenieść tę funkcję gdzieś indziej)
	jQuery.preloadImages = function()
	{
		for(var i = 0; i<arguments.length; i++)
		{
			jQuery("<img />").attr("src", arguments[i]);
		}
	}

	var mip = 'app/views/img/'+Core._lang+'/'; //menu images path
	$.preloadImages(mip+'menu_home_on.gif', mip+'menu_produkty_on.gif',
		mip+'menu_wydarzenia_on.gif', mip+'menu_firma_on.gif', mip+'menu_kontakt_on.gif');



	// menu boczne
	$('#left_menu .menuItem.current').parent().parent().addClass('current');
	$('#left_menu .menuItem.current').parent().parent().parent().parent().addClass('current');

	// search form
	$('.search a').click(function(e) {
		e.preventDefault();
		$('#features').css('width', '265px');
		$('.search_form').show('fast');
		$(this).hide('fast');
	})
	

});

