$(document.documentElement).addClass('has-js');

$(function(){
	
	/*------------------------------------------------------------------------------------------- DEFINE GLOBALS */
	var url = window.location.hash.toString(),
		speed = 300,
		speedSlow = speed * 2,
		speedSlower = speed * 3,
		win = $(window),
		html = $(document.documentElement),
		body = $(document.body),
		page = ($.browser.safari) ? body : html,
		email = $('#email'),
		label = $('label[for='+email[0].id+']'),
		festival = $('#the-festival'),
		festival_box = festival.find('.wrapper'),
		program = $('#program'),
		program_wrapper = program.find('h2 + .wrapper'),
		intro_height = $('.intro').outerHeight(),
		maxim = $('#maxim-eshkenazy'),
		maxim_height = maxim.height(),
		maxim_more = maxim.find('.box>div'),
		afb = $('#america-for-bulgaria'),
		afb_height = afb.height(),
		both = $('#maxim-eshkenazy,#america-for-bulgaria'),
		menu = $('.page-menu'),
		menu_height = menu.outerHeight(),
		links = menu.find('a'),
		active_link = program_link = links.filter('[href="#program"]'),
		maxim_link = links.filter('[href="#maxim-eshkenazy"]'),
		afb_link = links.filter('[href="#america-for-bulgaria"]'),
		performers_link = links.filter('[href="#performers"]'),
		opened = false;
	
	/*--------------------------------------------------------------------------------------------- THE FESTIVAL */
	festival.append('<a href="#" class="open" />').insertBefore(program);
	var opener = festival.find('.open');
	opener.click(function(e){
		if(opened==true){
			festival_box.fadeOut(speed);
			opener.css({cursor:'pointer'});
			opened = false;
		} else {
			var top = (win.height()/2 - festival_box.outerHeight()/2) + (page.scrollTop() - festival.offset().top) + menu_height/2;
			festival_box.css({top:top+'px'}).fadeIn(speed);
			opener.css({cursor:'default'});
			opened = true;
		}
		e.preventDefault();
		e.stopPropagation();
	});
	
	festival_box.append('<a href="#" class="close" />')/*.fadeIn(speed)*/
		.click(function(e){
			e.stopPropagation();
		})
		.find('.close').add(body).click(function(e){
			festival_box.fadeOut(speed);
			opener.css({cursor:'pointer'});
			opened = false;
			if(this.href) e.preventDefault();
		}
	);
	
	/*------------------------------------------------------------------------------------------- URL NAVIGATION */
	html.removeClass('loading');
	page.scrollTop(program.offset().top);
	if(url!=''){
		setTimeout(function(){
			active_link.removeClass('active');
			active_link = links.filter('[href="'+url+'"]');
			if(active_link.length<=0){
				if(url.match(/#september\-[0-9]+/)) {
					active_link = program_link;
					active_link.addClass('active');
				}
				else active_link = links.filter('[href="#performers"]');
				$('a[href="'+url+'"]:first').click();
			} else {
				active_link.click();
			}
		},500);
	}
	active_link.addClass('active');
	
	/*----------------------------------------------------------------------------------------------------- MENU */
	menu.parents('.menu').appendTo(body);
	$('a[href^="#"]').not('[href="#"]').click(function(){
		var href = this.href.toString(), elem = $(this), newheight;
		href = href.substr(href.indexOf('#'));
		var target = $(href);
		
		if(target.length>0){
			target[0].id += '-fake';
         page.animate({scrollTop:(target.offset().top - menu_height)+'px'},speedSlower,'easeInOutCubic',function(){
            target[0].id = target[0].id.replace('-fake','');
            if(target[0].id=='the-festival')
               //festival_box.fadeIn(speed);
               opener.click();
            else festival_box.hide();
         });
         active_link.removeClass('active');
			active_link = links.filter('[href="'+href+'"]');
			if(active_link.length<=0){
				if(href.match(/#september\-[0-9]+/)) {
					active_link = program_link;
					active_link.addClass('active');
				}
				else active_link = links.filter('[href="#performers"]');
			}
			active_link.addClass('active');
		}
	});
	
	/*-------------------------------------------------------------------------------------------------- PROGRAM */
	var calendar_links = $('.calendar a'),
		active_date = calendar_links.filter('.active'),
		active_program = $('.program .active'),
		intro_links = $('.intro .lead a'),
		h1_offset = $('h1').offset().top - 20;
		
	intro_links[0].onclick = function(){ maxim_link.click(); }
	intro_links[1].onclick = function(){ afb_link.click(); }
	
	program.find('.program a').click(function(){
		var elem = $(this), href = elem.attr('href');
		
		if(href!='#maxim-eshkenazy'){
			setTimeout( function () {
				var target = $(href),
					prev = target.prev().prev();
				
				if(target.length>0){
					target.find('h3').click();
				}
			}, 1000);
		}
	});
	
	if(label.length>0){
		if(email.val()!='') label.hide();
		email.focus(function(){
			label.hide();
		}).blur(function(){
			if(email.val()=='') label.show();
		});
	}
	
	$('form').submit(function(){
		$.ajax({
			type:'POST',
			url:this.action,
			data:email[0].name+'='+email.val(),
			success:function(msg){
				alert(msg);
			}
		});
		return false;
	});
	
	/*----------------------------------------------------------------------------------------------- PERFORMERS */
	var overlay = $('<div id="overlay" class="overlay" />'),
		opened_photo = false,
		active_h3;
	
	overlay.appendTo(body).click(function(){
		opened_photo = false;
		var photo = $('.photo'),
			data = photo.data('properties');
			
		this.style.display = 'none';
		photo.animate({
			width:'50px',
			height:'50px',
			left:data.left+'px',
			top:data.top+'px'
		},speed,'swing',function(){ photo.remove(); });
		
		if(active_h3.parents('.expanded').length<=0)
			active_h3.removeClass('hover');
	});
	
	$('#performers .performer').hover(
		function(){
			$(this).find('h3 img').animate({width:'show'},speed).data('hovered','true');
		},
		function(){
			var elem = $(this),
				img = elem.find('h3 img');
			if(!elem.hasClass('expanded') && opened_photo == false)
				img.animate({width:'hide'},speed);
			else var int = setInterval(function(){
				if(!elem.hasClass('expanded') && opened_photo == false){
					if(img.data('hovered')=='false') img.animate({width:'hide'},speed);
					clearInterval(int);
				}
			},speed);
			img.data('hovered','false');
		}
	).find('h3').click(function(e){
		var elem = $(this),
			performer = elem.parents('.performer'),
			desc = performer.find('.description');
			
		$('.performer.expanded h3').not(this).removeClass('hover')
			.next().slideUp(speedSlow,function(){ $(this).parents('.performer').removeClass('expanded'); });
		
		elem.toggleClass('hover');
		desc.slideToggle(speedSlow,function(){
			performer.toggleClass('expanded');
		});
	}).children('a').click(function(e){
		
		opened_photo = true;
		
		var photo = $('<img src="'+this.href+'" style="width:50px; height:50px;" class="photo" />'),
			elem = $(this),
			avatar = elem.offset(),
			height = body.height(),
			w = win.width(),
			h = win.height(),
			st = page.scrollTop();
			
		overlay.css({display:'block',height:height+'px',background:'#f2f2f2',opacity:0});
		photo.appendTo(body).css({left:avatar.left+'px',top:avatar.top+'px',display:'block'})
			.animate({
				width:'770px',
				height:'700px',
				left:(w/2 - 385)+'px',
				top:((h/2 - 350)+st)+'px'
			},speed,'easeOutBack')
			.data('properties',{top:avatar.top,left:avatar.left})
			.click(function(){
				overlay.click();
			}
		);
		
		active_h3 = elem.parent();
		active_h3.addClass('hover');
		
		return false;
		
	}).each(function(){
		var img = new Image();
		img.src = this.href;
	})
	.end()
	.next().next().find('a').click(function(e){
		var href = this.href.toString(),
			elem = $(this),
			performer = elem.parents('.performer');
		
		elem.parents('ul').prev().slideUp(speedSlow,function(){
			performer.removeClass('expanded');
		});
	});
	
});


