FV = {
	'modules': {
		'main': {
			'__init__': function () {
			
				$('#link-top').find('span').hide()
				$('#brands ul li').animate({opacity: 0.4})
			
				$('#link-top').hover(
					function () { $(this).find('span').fadeIn() }
					, function () { $(this).find('span').fadeOut() }
				)
			
				$('#brands ul li').hover(
					function () { $(this).animate({opacity: 1}) }
					, function () { $(this).animate({opacity: 0.4}) }
				)
				
				$('#contact-name').val('Imię i nazwisko / nazwa firmy').focus(function() { $(this).val('') })
				$('#contact-email').val('Adres e-mail').focus(function() { $(this).val('') })
				$('#contact-text').val('Treść wiadomości').focus(function() { $(this).val('') })
				$('#contact-submit').val('')

				$('img.alpha').css({ opacity: .25 })
				$('img.alpha').hover(
					function () { $(this).animate({ opacity: 1 }) },
					function () { $(this).css({ opacity: .25 }) }
				)

				$("ul.lightbox li a").lightbox();
			
			} // main.__init__()
		},
		'index': {
			'__init__': function () {
				
				$('#box-newsletter dd form').submit(function () {
				
					var data = $(this).serializeArray();
					$.post($(this).attr('action'), data,
						function(data) {
							if (data == 'OK') {
								data = 'Dane zostały zapisane pomyślnie.'
								$('#email').val('')
								pageTracker._trackPageview("/newsletter_ok.html");
							}
								$('#newsletter-notify').animate({ opacity: 0 }, function () {
										$(this).remove()
									})
								$('<dd id="newsletter-notify">' + data + '</dd>')
									.hide()
									.appendTo('#box-newsletter')
									.fadeIn('slow')
									.animate({opacity: 1}, 2000)
						}
					);
				
					return false;
				})
				
				$.getJSON("/banner/get", function (json) {
					FV.modules.index.banners = json
					FV.modules.index.banner_count = json.length
					FV.modules.index.banner_current = 0
					
					$('<div id="banner-switcher"><ul></ul></div>').appendTo('#index-header')
				
					if (FV.modules.index.banners.length > 0) {	
						$.each(FV.modules.index.banners, function (i, item) {
							$('<li id="banner-'+parseInt(i+1)+'"><a href="#" title="'+ item.fields.name +'"><span>'+parseInt(i+1)+'</span></a></li>')
								.appendTo('#banner-switcher ul')
							$('#banner-'+parseInt(i+1)).click(function () {
								FV.modules.index.banner_current = parseInt(i)
								FV.modules.index.switch_banner(parseInt(i))
								clearTimeout(FV.modules.index.timeout)
								FV.modules.index.timeout = setTimeout('FV.modules.index.banner_loop()', 17000)
								return false
							})
						})
						
						$('#banner-switcher li').animate({ opacity: .7 }).hover(function () { $(this).animate({ opacity: 1 }) }, function () { $(this).animate({ opacity: .7 }) })
						
						FV.modules.index.banner_loop()
					}
				})
			}, // index.__init__()
			'banner_loop': function () {
				FV.modules.index.switch_banner(FV.modules.index.banner_current)
				FV.modules.index.timeout = setTimeout('FV.modules.index.banner_loop()', 17000)
			},
			'switch_banner': function (id) {
				if (FV.modules.index.banner_current == FV.modules.index.banner_count-1) {
					FV.modules.index.banner_current = 0
				} else {
					FV.modules.index.banner_current += 1
				}
				$('#banner').animate({ opacity: 0 }, 300, function () {
					$('#banner-switcher ul li').removeClass('active')
					$('#banner').html(FV.modules.index.banners[id].fields.content).animate({ opacity: 1 }, 300)
					$('#index-header h1.title').css({ marginTop: '30px', marginLeft: '30px'})
					$('#index-header p.description').css({ marginTop: '10px', marginLeft: '30px'})
					$('#banner-'+parseInt(id+1)).addClass('active')
				})
			}
		}, // index
		'kontakt': {
			'__init__': function () {
				$('#contact-form button[type=submit], #contact-form input[type=image]').click(function () {
					var _data = $("#contact-form").serialize()
					
					$.ajax({
						type: "POST",
						url: "/contact_message",
						data: _data + '&rand='+Math.random(),
						dataType: "json",
						success: function (result) {
							$('#contact-form input, #contact-form textarea').removeClass('error')
							if (result['result']) {
								$('#contact-form').html('<p>Twoja wiadomość została wysłana.</p><p>Dziękujemy za zainteresowanie.</p>')
							} else {
								$.each(result, function (i, item) {
									$('#contact_'+i).addClass('error')
								})
							}
							$("#contact-form button").removeAttr('disabled')
							pageTracker._trackPageview("/kontakt_ok.html");
						},
						beforeSend: function() {
							$("#contact-form button").attr('disabled', 'disabled')
						}
					});
					return false
				})
			},
			'error_message_': function (msg) {
				$('#error-message').fadeOut(400, function () {
					$('#error-message').html(''+msg+'')
					$('#error-message').fadeIn(400)
				})
			}
		},
		'oferta': {
			'__init__': function () {
				$('#contact-form button[type=submit], #contact-form input[type=image]').click(function () {
					var _data = $("#contact-form").serialize()
					var _dataArray = $("#contact-form").serializeArray()
					
					$.ajax({
						type: "POST",
						url: "/offer_contact_message",
						data: _data + '&rand='+Math.random(),
						dataType: "json",
						success: function (result) {
							$('#contact-form input, #contact-form textarea').removeClass('error')
							if (result['result']) {
								$('#contact-form').html('<p>Twoja wiadomość została wysłana.</p><p>Dziękujemy za zainteresowanie.</p>')
							} else {
								$.each(result, function (i, item) {
									$('#contact_'+i).addClass('error')
								})
							}
							$("#contact-form button").removeAttr('disabled')
							$.each(_dataArray, function (i, field) {
								if (field.name == 'offer_uri') {
									if (field.value == 'Pozycjonowanie') {
										pageTracker._trackPageview("/kontakt_pozycjonowanie_ok.html");
									} else if (field.value == 'Aplikacje internetowe') {
										pageTracker._trackPageview("/kontakt_oferta_ok.html");
									} else if (field.value == 'Strona WWW') {
										pageTracker._trackPageview("/kontakt_oferta_ok.html");
									}
								}
							})
							return false
						},
						beforeSend: function() {
							$("#contact-form button").attr('disabled', 'disabled')
							return false
						}
					});
					return false
				})
				FV.modules.oferta.highlights()
				FV.modules.oferta.keywords()
			},
			'error_message_': function (msg) {
				$('#error-message').fadeOut(400, function () {
					$('#error-message').html(''+msg+'')
					$('#error-message').fadeIn(400)
				})
			},
			'highlights': function () {
				var items = $('div.highlights ul.links li')
				items.each(function (i, item) {
					$(item).hover(function () {
						items.each(function (i, itm) {
							$(itm).removeClass('active')
						})
						$(item).addClass('active')
						$('div.highlights div.hi-content').removeClass('active')
						var act = item.id.substring(8, item.id.length)
						$('#hi-' + act).addClass('active')
					}, function () {})
					$(item).click(function() {return false;})
				})
			},
			'keywords': function () {
				$('<div class="holder"><ul></ul></div>').appendTo('#keywords-container')
								
					$.ajax({
						type: "GET",
						url: "/static/seo/raport.xml",
						dataType: "xml",
						success: function (response) {
						var container = $('#keywords-container ul')
						var xmlDoc = response.documentElement
				
						for (i = 0; i < xmlDoc.getElementsByTagName("wyniki")[0].childNodes.length; i++)
						{
							element = xmlDoc.getElementsByTagName("wyniki")[0].childNodes[i]
							if (element.nodeName == 'slowo')
							{
								$('<li id="'+element.getAttribute('id')+'"><span class="name">'+element.getAttribute('txt')+'</span> <span class="position">'+element.getAttribute('pozycja')+'</span></li>').appendTo(container)
							}
						}
					}
				});

			}
		}
	}
}

$(function () {

	var current_page = $('body').attr('class')
	$('#navbar ul li.'+current_page).addClass('active')

	FV.modules.main.__init__()
	if (FV.modules[current_page]) {
		FV.modules[current_page].__init__()
	}
})
