var Core = {

	controller: '',
	action: '',
	webHost: '',
	webView: '',
	webImg: '',
	dateTime: '',
	lang: '',

	init : function() {
		/*Core.background();*/
		//Core.backgroundErr();
		Core.fitBackground();
		
		// atrybuty title dla hiperłączy, które nie mają title, ale mają name = dla fancyboxa
		$('a.fancybox').each( function() {
		
			if ($(this).attr('name') != '' && $(this).attr('title') == '') {
				$(this).attr('title', $(this).attr('name')) ;
			}
			
		})
		
		// Fancybox.
		$('.fancybox').fancybox({
			'titlePosition'	: 'inside'
		});
		// Galeria zdjęć na podstronie
		Core.galleryCarousel();
		
		// Poleć znajomemu.
		Core.dialog('dlgRecommend', '#btnOpenRecommend');

		// Newsletter.
		Core.newsletter('dlgNewsletter', '#btnOpenNewsletter');
		
		// Zmiana języków
		Core.langChanger();

		// Zakładki.
		Core.tabs();

		// Banery reklamowe.
		Core.banners();
		
		// Animacje na menu - hover
		Core.hoverMenu();
		
		// Podświetlane panele do pobierania
		Core.hoverDownloadList();
		
		// Zmiana zdjęcia po wskazaniu kursorem.
		Core.hoverImage();

		// Drukowanie stron.
		Core.print();
	},
	
	fitBackground : function() {
	
		var width = $(window).width();
		var top;
		//var left;
		
		if ($('#background_container').find('img').height() > $(window).height()) {
			top = ($(window).height() - $('#background_container').find('img').height())/1.5;
		} else {
			top = 0;
		}
		//left = (( ( width * 1.10 ) - width ) / 2 ) * (-1);
		
		$('#background_container').width( width * 1.10 );
		$('#background_container').height( $('#background_container').width() * 66 / 100 );
		$('#background_container').css( 'top', top );
		//$('#background_container').css( 'left', left );
		
		
		$(window).resize( function() {
			width = $(window).width();
			if ($('#background_container').find('img').height() > $(window).height()) {
				top = ($(window).height() - $('#background_container').find('img').height())/1.5;
			} else {
				top = 0;
			}
			//left = (( ( width * 1.10 ) - width ) / 2 ) * (-1);
			
			$('#background_container').width( width * 1.10 );
			$('#background_container').height( $('#background_container').width() * 66 / 100 );
			$('#background_container').css( 'top', top );
			//$('#background_container').css( 'left', left );
		})
		
	},
	
	hoverDownloadList: function(){
		$('.downloadsListItem').bind({
			mouseover: function() {
				$(this).css('background-color','#a2b446');
				$(this).find('.downloadsListItemTitle').find('a,span').css('color','#ffffff');
				$(this).find('.downloadsListItemDwonload').find('a').css('color','#ffffff');
				$(this).find('.downloadsListItemDwonload').find('img').attr('src',Core.webHost+'app/views/img/arrowBottomWhite.png');
			},
			mouseleave: function() {
				$(this).css('background-color','#5f5d5e');
				$(this).find('.downloadsListItemTitle').find('a,span').css('color','#c7c0bf');
				$(this).find('.downloadsListItemDwonload').find('a').css('color','#c7c0bf');
				$(this).find('.downloadsListItemDwonload').find('img').attr('src',Core.webHost+'app/views/img/arrowBottomGrey.png');
			}
		});
	},
	
	hoverMenu: function(){
		$('#headerMenuLinks ul li').bind({
			mouseover: function() {
				$(this).children('a').css('color','#b8cf2c');
				$(this).children('ul').children('li').bind({
					mouseover: function() {
						$(this).children('a').css('background',"url(app/views/img/arrowMenuGreen.jpg)");
						$(this).children('a').css('background-repeat',"no-repeat");
						$(this).children('a').css('background-position',"left center");
					},
					mouseleave: function() {
						$(this).children('a').css('background',"url(app/views/img/arrowMenuWhite.jpg)");
						$(this).children('a').css('background-repeat',"no-repeat");
						$(this).children('a').css('background-position',"left center");
					}
				});
				$(this).children('ul').show();
			},
			mouseleave: function() {
				$(this).children('a').css('color','#f9f5ec');
				$(this).children('ul').hide();
			}
		});
	},
	
	galleryCarousel: function(){
		// Główna galeria zdjęć.
		var mainPhoto = $('#mainPhoto'), photoNumber = 0;

		var thumbs = $('.pageGallery .pageCarousel a.photo').map(function(){
			return { href: $(this).attr('href'), title: $(this).attr('title') };
		}).get();

		$('.pageGallery .pageCarousel a.photo').each(function(k, v){
			$(this).attr('rel', k);
		});

		$('#mainPhoto').click(function(){
			$.fancybox(thumbs, {
				'cyclic': true,
				'type': 'image',
				'index': photoNumber,
				'titlePosition': 'inside'
			});

			return false;
		});

		$(document).ready( function() {
			// Karuzela.
			if ($('.pageGallery .pageCarousel').find('a.photo').length > 4) {

				$('.pageGallery .pageCarousel').find('.inner').jCarouselLite({
					btnNext: $('.pageGallery .pageCarousel').find('.btnPrevious'),
					btnPrev: $('.pageGallery .pageCarousel').find('.btnNext'),
					mouseWheel: true
				});

				$('.pageGallery .pageCarousel').find('.btnPrevious, .btnNext').show();
			}
		})

		// Ładowanie karuzeli na widocznej zakładce.
		$('.buttons a.visible').click();

		// Galerie w zakładkach.
		$('.tabs .pageCarousel a.photo').fancybox({
			'cyclic': true,
			'type': 'image',
			'titlePosition': 'inside'
		});

		// Zmiana dużego zdjęcia.
		$('.pageGallery .pageCarousel a.photo').click(function(){
			mainPhoto.attr('href', $(this).attr('href'));
			mainPhoto.children('img').attr('src', $(this).children('img').attr('src').replace('small_', ''));
			photoNumber = $(this).attr('rel');
			return false;
		});
	},
	
	background: function() {
		var height = screen.height;
		if(height > 1024){
			var heightResult = parseInt(height) / 1024;
			var h = $('#contentMainTitle').css("height");
			heightResult = parseInt(heightResult) * parseInt(h);
			$('#contentMainTitle').css({
				'height' : heightResult 
			});
		}else{
			var heightResult = 1024 / parseInt(height);
			var h = $('#contentMainTitle').css("height");
			h = h.substr(0,3);
			heightResult = heightResult * h;
			heightResult = Math.round((parseInt(heightResult) * 100) / 100);
			$('#contentMainTitle').css({
				'height' : heightResult
			});
		}
	},
	
	backgroundErr: function() {
		/* BACKGROUND */
		/* Zmienne */
		var scrollBrowser = 0;
		var width = screen.width;
		var height = screen.height;
		var w = $(window);
		var heightBrowser = 50;
		/* Rozróżnianie przeglądarek */
		$.each(jQuery.browser, function(i, val) {
			if(i=='opera' || i=='safari'){
			  /* Opera */
				var version = navigator.appVersion;
				version = version.substr(0,3);
				if(i=='opera'){
					scrollBrowser = 34;
				}else if(i=='safari'){
					scrollBrowser = 17;
				}
			
				/* Domyślna wielkość tła */		
				$('#background').css({
					'width' : parseInt(width)-parseInt(scrollBrowser),
					'height' : parseInt(height)-parseInt(heightBrowser)
				});
				/* Wielkość tła zmieiająca się poprzez zmianę okna przeglądarki */
				/*$(window).wresize(function(){
					var wHeight = w.height(); 
					var wWidth = w.width();
					wWidth = parseInt(wWidth)+parseInt(scrollBrowser);
					wHeight = parseInt(wHeight)+parseInt(heightBrowser);
					var sizeBackground = $('#background').css('width');
					
					$('#background').css({
						'width' : wWidth,
						'height' : wHeight 
					});
				});*/
			};
		});
	},
	
	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'));
				$(this).attr('src', $(this).attr('src').replace('.png', '_on.png'));
			}
		}).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'));
				$(this).attr('src', $(this).attr('src').replace('_on.png', '.png'));
			}
		});
	},

	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: false,
					autoDimensions: true,
					enableEscapeButton: false,
					hideOnOverlayClick: false,
					href: advSwf.children('.path').text(),
					margin: 0,
					overlayOpacity: 0,
					padding: 0,
					scrolling: 'no',
					swf: { wmode: 'opaque' },
					titleShow: false,
					type: 'swf',
					transitionIn: 'none',
					transitionOut: 'none',

					onStart: function() {
						$.cookie('advTopLayer', 1);
					}
				});

			}
			else if (advTopLayer.length > 0) {

				$.fancybox({
					autoScale: false,
					autoDimensions: 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();

			// 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');
				}
				else if ('ERROR' === data.status) {
					$.each(data.fields, function(k, v){
						dialog.find('[id*=' + k + ']').addClass('error');
					});
				}

				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;
		});

		inputs.blur(function(){
			$(this).removeClass('error');
		});

		$(buttonOpenName).fancybox({
			href: '#' + dialogName,
			titleShow: false,
			margin: 0,
			padding: 0,

			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,
			margin: 0,
			padding: 0,

			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 = 0;

			for (var i = 0; i < activeTabs.length; ++i) {
				if ($(this).parent().children('a.active').attr('href') === activeTabs[i]) {
					id = i;
					break;
				}
			}

			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');
		}
	},
	
	// Zmiana języków
	langChanger : function() {
		
		$('#headerNavigationLangs .changer a').click(
			function(e) {
				
				e.preventDefault();
				
				$(this).parent().parent().find('.list').show();
				
			}
		)
		
		$('#headerNavigationLangs').mouseleave(
			function() {
				
				$(this).find('.list').hide();
				
			}
		)
		
	},

	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]();
		}
	}
});

