var menuEls, actual, body, scrollingEls, bio, bioLayer, bioNavigation, photosCarrousel, photosCarrouselMore, photosCarrouselLess, photosCarrouselIndex, photosCarrouselMax, newsDownload, newsCarrousel, newsImageContainer, newsCarrouselMore, newsCarrouselLess, newsCarrouselIndex, newsCarrouselMax, cinemaNow, cinemaImg, theaterImg, tvImg, cinemaContent, theaterContent, tvContent, downloadsContainer, downloadsWallpapersTitle, downloadsWallpapersItems, iframe, contactForm, contactEmail, contactText, contactEmailLabel, contactTextLabel, contactButton, contactLoader, showNewsImage = function(path) {
	newsImageContainer.empty();
	newsDownload.css('display', 'none');
	var img = $('<img src="' + path + '"/>').css('opacity', 0).appendTo('body').bind('load', function() {
		this.width = this.width * 340 / this.height;
		this.height = 340;
		newsImageContainer.animate({
			height: this.height,
			width: this.width,
			left: 620 - this.width / 2,
			top: 10,
			opacity: 0.9
		}, 400, 'easeOutCirc', function() {
			img.appendTo(newsImageContainer).animate({opacity: 1}, 300, 'easeOutCirc').lightbox();
			var p = newsImageContainer.position();
			newsDownload.attr('href', img[0].src).css({top: p.top + 40, left: p.left, display: 'block'}).animate({left: p.left + img[0].offsetWidth}, 300, 'easeOutCirc')
		});
	});
}, showSection = function(section) {
	if (actual == section) return;
	menuEls.removeClass('selected');
	$('#link-' + section).addClass('selected');
	var fn = sections[section + 'Show'];
	if (actual)
		$('#' + actual).fadeOut(200, fn);
	else
		fn();
	actual = section;
}, sections = {
	homeShow: function() {
		$('#home').fadeIn(300);
	},
	bioShow: function() {
		bioNavigation.hide();
		bioLayer.css({
			top: 0,
			left: 0,
			width: 910,
			height: 5250,
			opacity: 0
		});
		scrollingEls[0].top.hide();
		if ($.browser.msie)
			scrollingEls[0].top.css('visibility', 'hidden');
		scrollingEls[0].bottom.show();
		scrollingEls[0].container.css('marginTop', 0);

		bio.fadeIn(300, function() {
			bioLayer.animate({
				opacity: 0.5,
				width: 610,
				height: 476,
				left: 20,
				top: 20
			}, 400, 'easeOutCirc', function() {
				bioNavigation.fadeIn();
				if ($.browser.msie)
					scrollingEls[0].top.css('visibility', 'visible');
			});
		});
	},
	photosShow: function() {
		photosCarrousel.empty().css({
			bottom: -150,
			marginLeft: 0
		});
		photosCarrouselMore.hide();
		photosCarrouselLess.hide();

		$('#photos').fadeIn(300, function() {
			photosCarrouselIndex = 0;
			var container = photosCarrousel.animate({bottom: 0}, 300, function() {
				$.getJSON('images.php?id=1&folder=images', function(data) {
					var j = data.length, width = Math.ceil(j / 10) * 950;
					container.css('width', width);
					photosCarrouselMax = width / 950 - 1;
					$.each(data, function() {
						var item = this, img = $('<img src="' + item.thumb + '" alt="' + item.image + '" style="display:none"/>').bind('load', function() {
							var a = $('<li><a href="' + item.image + '" title="' + item.title + ': ' + item.description + '"/></li>').appendTo(container).children().lightbox();
							img.appendTo(a).fadeIn(300, function() {
								img.reflect().next().css('opacity', 0).animate({opacity:1});
							});
						});
					});
				});
				photosCarrouselMore.fadeIn();
			});
		});
	},
	newsShow: function() {
		newsImageContainer.empty().css({
			height: 340,
			width: 400,
			top: 10,
			left: 420,
			opacity: 0
		});
		newsCarrousel.empty().css({
			bottom: -150,
			marginLeft: 0
		});
		newsCarrouselMore.hide();
		newsCarrouselLess.hide();
		newsDownload.hide();

		$('#news').fadeIn(300, function() {
			newsCarrouselIndex = 0;
			var container = newsCarrousel.animate({bottom: 0}, 300, function() {
				$.getJSON('images.php?id=5&folder=news', function(data) {
					var j = data.length, width = Math.ceil(j / 10) * 950;
					container.css('width', width);
					newsCarrouselMax = width / 950 - 1;
					$.each(data, function() {
						var item = this, img = $('<img src="' + item.thumb + '" alt="' + item.image + '" style="display:none"/>').bind('load', function() {
							var a = $('<li><a href="' + item.image + '" title="' + item.title + ': ' + item.description + '"/></li>').appendTo(container).children().click(function(e) {
								e.preventDefault();
								showNewsImage(item.image);
							});
							img.appendTo(a).fadeIn(300, function() {
								img.reflect().next().css('opacity', 0).animate({opacity:1});
							});
						});
					});
				});
				newsCarrouselMore.fadeIn();
			});
		});
	},
	cinemaShow: function() {
		cinemaNow = null;
		scrollingEls[1].n = 0;
		scrollingEls[1].container.css('marginTop', 0);
		scrollingEls[1].top.hide();
		scrollingEls[1].bottom.show();
		scrollingEls[2].n = 0;
		scrollingEls[2].container.css('marginTop', 0);
		scrollingEls[2].top.hide();
		scrollingEls[2].bottom.show();
		scrollingEls[3].n = 0;
		scrollingEls[3].container.css('marginTop', 0);
		scrollingEls[3].top.hide();
		scrollingEls[3].bottom.show();
		if ($.browser.msie) {
			scrollingEls[1].top.css('visibility', 'hidden');
			scrollingEls[2].top.css('visibility', 'hidden');
			scrollingEls[3].top.css('visibility', 'hidden');
		}
		cinemaImg.removeClass('over').css({left: 633, zIndex: 0});
		theaterImg.removeClass('over').css({left: 316, zIndex: 0});
		tvImg.removeClass('over').css({left: -1, zIndex: 0});
		cinemaContent.hide();
		theaterContent.hide();
		tvContent.hide();

		$('#cinema').fadeIn(300);
	},
	blogShow: function() {
		iframe.attr('src', 'iframe.php');

		$('#blog').fadeIn(300, function() {
			iframe.attr('src', 'http://hs-223.dedicated.hostalia.com/~dpalazon/wordpress/');
		});
	},
	downloadsShow: function() {
		downloadsWallpapersTitle.hide();
		downloadsWallpapersItems.empty();
		downloadsContainer.css({
			right: 275,
			top: 250,
			width: 0,
			height: 0
		});

		$('#downloads').fadeIn(300, function() {
			downloadsContainer.animate({
				right: 20,
				top: 20,
				width: 550,
				height: 480
			}, 300, 'easeOutCirc', function() {
				downloadsWallpapersTitle[$.browser.msie ? 'show' : 'fadeIn']();
				$.getJSON('images.php?id=2&folder=wallpapers', function(data) {
					$.each(data, function() {
						var item = this, img = $('<img src="' + item.thumb + '" alt="' + item.image + '" style="display:none" class="thumb"/>').appendTo('body').bind('load', function() {
							var a = $('<li><a href="' + item.image + '" title="' + item.title + ': ' + item.description + '" target="new" /></li>').appendTo(downloadsWallpapersItems).children();
							img.appendTo(a).fadeIn(300, function() {
								img.reflect().next().css('opacity', 0).animate({opacity:1});
								img.css('marginTop', 0).before('<span>' + item.title + '</span>');
							});
						});
					});
				});
			});
		});
	},
	contactShow: function() {
		contactForm.hide();
		contactEmailLabel.css('color', 'black');
		contactTextLabel.css('color', 'black');
		contactEmail.css('borderColor', 'black');
		contactText.css('borderColor', 'black');

		$('#contact').fadeIn(300, function() {
			contactForm.css({
				display: 'block',
				opacity: 0
			}).animate({opacity: 0.9});
			contactEmail.focus();
		});
	}
};
jQuery(document).ready(function($) {
	var winHeight = $(window).height(), all = $('#all');
	if (winHeight > 632)
		all.css('marginTop', (winHeight - 632) / 2);
	all.css({opacity: 0, visibility: 'visible'}).animate({opacity: 1}, 700);

	$('<img src="images/bio_background.jpg"/><img src="images/cinema_background.jpg"/><img src="images/contact_background.jpg"/><img src="images/downloads_background.jpg"/><img src="images/home_background.jpg"/><img src="images/news_background.jpg"/><img src="images/photos_background.jpg"/>');
	var front = $('#front').click(function() {
		if (winHeight < 686)
			all.animate({marginTop: 0});
		front.css('backgroundImage', 'none').animate({
			height: 600,
			width: 950
		}, 300, 'easeOutCirc', function() {
			bio = $('#bio');
			bioLayer = $('#bio-layer');
			bioNavigation = $('#bio-navigation');
			photosCarrousel = $('#photos-carrousel');
			newsImageContainer = $('#news-big-image').css('opacity', 0);
			newsDownload = $('#news-download');
			newsCarrousel = $('#news-carrousel');
			downloadsContainer = $('#downloads-container');
			downloadsWallpapersTitle = $('#downloads-wallpapers-title');
			downloadsWallpapersItems = $('#downloads-wallpapers-items');
			iframe = $('#blog-iframe');
			contactForm = $('#contact-form');
			contactEmail = $('#contact-email');
			contactText = $('#contact-text');
			contactEmailLabel = $('#contact-email-label');
			contactTextLabel = $('#contact-text-label');
			contactLoader = $('#contact-loader');

			$('#body').show();
			front.hide();
			menuEls = $('#menu').click(function(e) {
				var li = e.target, i = $.inArray(li, menuEls);
				if (i >= 0) {
					var section = li.id.replace('link-', '');
					showSection(section);
				}
			}).fadeIn(200, function() {
				showSection('home');
			}).children().hover(function(e) {
				menuEls.slice(actual).removeClass('active');
				$(e.target).addClass('active');
			}, function() {
				menuEls.slice(actual).removeClass('active');
			});

			var bioText = $('#bio-text'),  scrollMe = function(i, m, scroll) {
				scrollingEls[i].container.animate({marginTop: m}, 600, 'easeOutCirc');
				scrollingEls[i].top[m >= 0 ? 'fadeOut' : 'fadeIn']();
				scrollingEls[i].bottom[Math.abs(m) + scroll > scrollingEls[i].container.height() ? 'fadeOut' : 'fadeIn']();
			};
			scrollingEls = [{
				container: bioText,
				top: $('#bio-navigation-top').click(function() {
					var m = parseInt(bioText.css('marginTop'), 10) + 420;
					scrollMe(0, m, 420);
				}),
				bottom: $('#bio-navigation-bottom').click(function() {
					var m = parseInt(bioText.css('marginTop'), 10) - 420;
					scrollMe(0, m, 420);
				})
			}];

			photosCarrouselMore = $('#photos-carrousel-more').click(function() {
				var n = parseInt(photosCarrousel.css('marginLeft'), 10);
				photosCarrousel.animate({marginLeft: n - 950}, 500, 'easeOutCirc');
				photosCarrouselIndex++;
				if (photosCarrouselIndex > 0)
					photosCarrouselLess.fadeIn();
				if (photosCarrouselIndex == photosCarrouselMax)
					photosCarrouselMore.fadeOut();
			});
			photosCarrouselLess = $('#photos-carrousel-less').click(function() {
				var n = parseInt(photosCarrousel.css('marginLeft'), 10);
				photosCarrousel.animate({marginLeft: n + 950}, 500, 'easeOutCirc');
				photosCarrouselIndex--;
				if (photosCarrouselIndex == 0)
					photosCarrouselLess.fadeOut();
				if (photosCarrouselIndex < photosCarrouselMax)
					photosCarrouselMore.fadeIn();
			});

			newsCarrouselMore = $('#news-carrousel-more').click(function() {
				var n = parseInt(newsCarrousel.css('marginLeft'), 10);
				newsCarrousel.animate({marginLeft: n - 950}, 500, 'easeOutCirc');
				newsCarrouselIndex++;
				if (newsCarrouselIndex > 0)
					newsCarrouselLess.fadeIn();
				if (newsCarrouselIndex == newsCarrouselMax)
					newsCarrouselMore.fadeOut();
			});
			newsCarrouselLess = $('#news-carrousel-less').click(function() {
				var n = parseInt(newsCarrousel.css('marginLeft'), 10);
				newsCarrousel.animate({marginLeft: n + 950}, 500, 'easeOutCirc');
				newsCarrouselIndex--;
				if (newsCarrouselIndex == 0)
					newsCarrouselLess.fadeOut();
				if (newsCarrouselIndex < newsCarrouselMax)
					newsCarrouselMore.fadeIn();
			});

			tvContent = $('#tv-content');
			theaterContent = $('#theater-content');
			cinemaContent = $('#cinema-content');
			tvImg = $('#tv-img').hover(function() {
				tvImg.addClass('over');
			}, function() {
				if (cinemaNow != 'tv')
					tvImg.removeClass('over');
			}).click(function() {
				if (cinemaNow == 'tv') {
					cinemaImg.animate({left: 633}, 500, 'easeOutCirc');
					theaterImg.animate({left: 316}, 500, 'easeOutCirc');
					cinemaNow = null;
				}
				else {
					tvContent.show();
					cinemaContent.hide();
					theaterContent.hide();
					tvImg.css('zIndex', 3).addClass('over');
					theaterImg.removeClass('over').css('zIndex', 2).animate({left: -1}, 500, 'easeOutCirc');
					cinemaImg.removeClass('over').css('zIndex', 1).animate({left: -1}, 500, 'easeOutCirc');
					cinemaNow = 'tv';
					if ($.browser.msie)
						scrollingEls[1].top.css('visibility', 'visible');
				}
			});
			theaterImg = $('#theater-img').hover(function() {
				theaterImg.addClass('over');
			}, function() {
				if (cinemaNow != 'theater')
					theaterImg.removeClass('over');
			}).click(function() {
				if (cinemaNow == 'theater') {
					theaterImg.animate({left: 316}, 500, 'easeOutCirc');
					cinemaImg.animate({left: 633}, 500, 'easeOutCirc');
					cinemaNow = null;
				}
				else {
					theaterContent.show();
					tvContent.hide();
					cinemaContent.hide();
					theaterImg.addClass('over').css('zIndex', 3).animate({left: -1}, 500, 'easeOutCirc');
					cinemaImg.removeClass('over').css('zIndex', 2).animate({left: -1}, 500, 'easeOutCirc');
					tvImg.removeClass('over').css('zIndex', 1).animate({left: -1}, 500, 'easeOutCirc');
					cinemaNow = 'theater';
					if ($.browser.msie)
						scrollingEls[2].top.css('visibility', 'visible');
				}
			});
			cinemaImg = $('#cinema-img').hover(function() {
				cinemaImg.addClass('over');
			}, function() {
				if (cinemaNow != 'cinema')
					cinemaImg.removeClass('over');
			}).click(function() {
				if (cinemaNow == 'cinema') {
					theaterImg.animate({left: 316}, 500, 'easeOutCirc');
					tvImg.animate({left: -1}, 500, 'easeOutCirc');
					cinemaNow = null;
				}
				else {
					cinemaContent.show();
					theaterContent.hide();
					tvContent.hide();
					cinemaImg.addClass('over').css('zIndex', 3);
					theaterImg.removeClass('over').css('zIndex', 2).animate({left: 633}, 500, 'easeOutCirc');
					tvImg.removeClass('over').css('zIndex', 1).animate({left: 633}, 500, 'easeOutCirc');
					cinemaNow = 'cinema';
					if ($.browser.msie)
						scrollingEls[3].top.css('visibility', 'visible');
				}
			});
			$.each(['tv', 'theater', 'cinema'], function(i) {
				var c = $('#' + this + '-content-container'), els = c.find('h2');
				scrollingEls.push({
					n: 0,
					container: $('#' + this + '-content-container'),
					top: $('#' + this + '-navigation-top').click(function() {
						var m = parseInt(c.css('marginTop'), 10) - els.eq(--scrollingEls[i + 1].n).position().top + 20;
						scrollMe(i + 1, m, 500);
					}),
					bottom: $('#' + this + '-navigation-bottom').click(function() {
						var m = parseInt(c.css('marginTop'), 10) - els.eq(++scrollingEls[i + 1].n).position().top + 20;
						scrollMe(i + 1, m, 500);
					})
				});
			});
			$('#cinema a.image').lightbox();
			$('#cinema a.video').click(function(e) {
				e.preventDefault();
				Videobox.open(this.href, this.rel, '');
			});

			contactButton = $('#contact-button').click(function(e) {
				e.preventDefault();
				contactButton.attr('disabled', true);
				contactLoader.fadeIn();
				$.post('email.php', contactForm.serialize(), function(data) {
					contactButton.attr('disabled', false);
					contactLoader.fadeOut();
					contactEmailLabel.css('color', 'black');
					contactTextLabel.css('color', 'black');
					contactEmail.css('borderColor', 'black');
					contactText.css('borderColor', 'black');
					if (data == 'OK')
						contactForm.animate({height: 15, top: 170}, 300, 'easeOutCirc').html('Email enviado correctamente');
					else {
						if (data == 2) {
							contactTextLabel.css('color', 'red');
							contactText.css('borderColor', 'red');
						}
						else {
							contactEmailLabel.css('color', 'red');
							contactEmail.css('borderColor', 'red');
						}
						contactForm.animate({right: 160}, 100).animate({right: 100}, 100).animate({right: 160}, 100).animate({right: 100}, 100).animate({right: 160}, 100).animate({right: 100}, 100).animate({right: 130}, 100);
					}
				});
			});
		});
	});
});