jQuery(document).ready(function($) {
	$body = $('body');	
	$body.bind('orientationchange', updateOrientation).trigger('orientationchange');
	init();
	maxImage();
});

function init(){
	$('html, body').css({overflow: 'hidden'});
	$('ul#nav li:not(:last-child) a').after(' //');
	if ($('#pg_home').length != 0) introMovie();	
	if ($('body#pg_gallery').length != 0) gallery();	
	if ($('body#pg_about').length != 0 || $('body#pg_news').length != 0 || $('body#pg_affilations').length != 0) panels();
	if($('#affilations').length != 0) newWindow();
	
	dyadcredit();
}

function updateOrientation(){
    $body.removeClass();
	scrollTo(0,0);
		
    switch(window.orientation){                     
        case 0:
		case 180:
        $body.addClass("portrait");
        break;  
        
        case -90:
		case 90:
        $body.addClass("landscape");
        break;
    }
   
}

function dyadcredit(){
	$('a#credit').hover(function(){
		$(this).animate({
			'width': '157px',
			'background-position': '0px 0px'
		}, 200);
	}, function(){
		$(this).animate({
			'width': '12px',
			'background-position': '-155px 0px'
		}, 200);
	});
}

function maxImage(){
	$('#maxImage').maxImage({
		isBackground: true,
		overflow: 'auto'
	});
}


function panels(){
	var numOfPages = $('.page').length;	
	var width = 900;
	var totalW = width*numOfPages;	
	var speed = 1500;
		
	$('#pane').css({width: totalW});
	
	if($('.page').length==1) $('#btn-next').fadeOut(0);
	
	$('#btn-prev').fadeOut(0);
	
	$('#btn-next').click(function(){		
		$('#pane').stop(true,true);
		var marginLeft = $('#pane').css('margin-left');
		if(marginLeft=='auto')marginLeft = '0px';		
		var nextPane = parseFloat((marginLeft).replace("px",""))-width;
		
		if(nextPane<=0 && nextPane != -totalW){
			$('#btn-prev').fadeIn(1000);
			$('#pane').animate({marginLeft: nextPane}, speed, 'easeOutCubic', function(){
				if(nextPane==(-totalW+width)) $('#btn-next').fadeOut(0);
			});
		}		
		return false;
	});
	
	$('#btn-prev').click(function(){
		$('#pane').stop(true,true);
		var nextPane = parseFloat(($('#pane').css('margin-left')).replace("px",""))+width;
		if(nextPane<=0 && nextPane != totalW){
			$('#btn-next').fadeIn(1000);
			$('#pane').animate({marginLeft: nextPane}, speed, 'easeOutCubic', function(){
				if($('#pane').css('margin-left')=='0px')  $('#btn-prev').fadeOut(100);
			});
		}

		return false;
	})
}

function newWindow(){
	$('#affilations a').each(function(){
		$(this).attr('target', '_blank');
	});
}

function introMovie(){
	var speed = 800;
	var delay = 1500;	
	 
	$(window).ready(function() {
		if(jQuery.support.opacity) {
			$('#wrapper').fadeIn(speed);
		}
		else $('#wrapper').show();
		$('a#logo-lg').delay(delay).fadeIn(speed);
		$('#intro-img').delay(delay+1000).fadeIn(speed);
		$('#greenBar, #intro nav').delay(delay+2000).fadeIn(speed);
		$('#intro nav ul').delay(delay+3000).fadeIn(speed);
	});	
}

function gallery(){
	headerCollapse();
	
	$('#gallery ul').cycle({ 
	    fx:     'fade', 
	    speed:  'fast', 
	    timeout: 0, 
	    next:   '#btn-next', 
	    prev:   '#btn-prev',
		before: onBefore
	});
	
	$('#gallery').click(function(){
		$('#gallery ul').cycle('next');
	});
	
	$('ul#nav li a').each(function(){
		$(this).click(function(){
			var url = $(this).attr('href');
			headerExpand(url);
			return false;
		});
	});
}

function onBefore(){
	$('.caption').html($(this).find('img').attr('alt'));
}

function headerExpand(url){
	$('body#pg_gallery section#content header').delay(0).animate({
		top: '0px'
	}, 1500, function(){
		window.location = url;
	});
}

function headerCollapse(){
	$('body#pg_gallery section#content header').delay(2000).animate({
			top: '-140px'
		}, 1500);
}
