// jQuery

// Panel scrolling variable
var panelNum = 1;
var intervalID = 0;

$(function () {
	// Test for IE 6
	var ieSix = ($.browser.msie && $.browser.version.slice(0,1) == '6')? true : false;

	// External links open in new window
	$('a[href^="http://"]').click(function () {
		window.open(this.href); return false;
	});
	
	// Quote changes over time
	if(jQuery().delayedfade) {
		var options = { duration: 7000, pause: 1000, fadeOut: 1000, fadeIn: 1000 };
		$('ul#quote li').delayedfade(options);
	}
	
	// Begin image swapping over time
	function startTransition() {
		if(intervalID == 0) {
			intervalID = setInterval(function() {
				if(panelNum > 4) panelNum = 1;
				var next = $('li#panel_'+panelNum+' li.active').next();
				if(next.length == 0) {
					next = $('li#panel_'+panelNum+' li').first();
				}
				$('li#panel_'+panelNum+' li.active').fadeTo(1000, 0).removeClass('active');
				
				next.fadeTo(1000, 1).addClass('active');
				panelNum++;
			}, 5000);
		}
	}
	// Stop image swapping
	function stopTransition() {
		clearInterval(intervalID);
		intervalID = 0;
	}
	
	// Initially start image swapping
	startTransition();
	
	// Link mousenter to fading the object and adding active to another
	function fadeLink(source, target) {
		$(source).mouseenter(function () {
			stopTransition();
			if (!ieSix) $(this).stop().fadeTo(1, 0.50);
			$(target).addClass('active');
		}).mouseleave( function () {
			startTransition();
			if (!ieSix) $(this).fadeTo(100, 1);
			$(target).removeClass('active');
		});
		// Send source and target in reverse order to setup fade redundancy
		reverseLink(source, target);
	}
	// Fade redundancy
	function reverseLink(target, source) {
		$(source).mouseenter(function () {
			stopTransition();
			if (!ieSix) $(target).stop().fadeTo(1, 0.50);
		}).mouseleave( function () {
			startTransition();
			if (!ieSix) $(target).fadeTo(100, 1);
		});
	}
	
	// Setup fading areas
	fadeLink('li#panel_1', '#nav_body_the-ensemble');
	fadeLink('li#panel_2', '#nav_body_box-office');
	fadeLink('li#panel_3', '#nav_body_support');
	fadeLink('li#panel_4', '#nav_body_multi-media');
	
	// Setup initial active states for image swapping 
	$('ul#panel li').each( function () {
		$(this).find('li').first().addClass('active');
	});
	
	// Block click events
	$('#nav_body>li>a').click(function() {
		return false;
	});
	
	// Add hover for IE 6
	if (ieSix) {
		$('#nav_body li').hover(function() {
			$(this).addClass('active');
		},
		function() {
			$(this).removeClass('active');
		});
	}
	
	// Default to label
	$('input[title!=]')
		.focus(function () {
			if (this.value == this.title) {
				this.value = '';
				$(this).removeClass('default');
			}
		})
		.blur(function () {
			if (this.value == '') {
				this.value = this.title;
				$(this).addClass('default');
			}
		})
		.blur();
	
	$('form#form_mailing-list').submit(function () {
		
		// E-mail Address Validator
		function emailValidator(email) {
			var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
			if (email.match(emailExp)) {
				return true;
			} else { 
				return false;
			}
		}

		if (!emailValidator(this.email.value) || this.email.title == this.email.value) {
			alert('A valid e-mail address is required');
			return false;
		}
	});
	
	// Konami
	if ($('body.page_the-team"')[0]) {
		var keys = [];
		var konami  = '38,38,40,40,37,39,37,39,66,65';
		$(document).keydown(function(e) {
			keys.push(e.keyCode);
			if (keys.toString().indexOf(konami) >= 0){
				$('div#site_container').css('background','transparent url(/images/body/t.jpg) repeat-y center top');
				keys = [];
			}
		});
	}
});
////////////////////////////////////////////////
$(document).ready(function() {	

		var id = '#dialog';
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 	
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});		
	
});

