// For all pages, may need to move?

function newWin(url) {
	var day = new Date();
	var id=day.getTime();
	var params = 'toolbar,location,status,menubar,scrollbars,resizable,width=900,height=600';
	open(url,id,params);
	return false;
}


///////////////////////// Set it up on document.ready.  ///////////////////////

$(function(){

	// Used for lightboxes on video
	$("a[rel^='prettyPhoto']").prettyPhoto({
		show_title: false,
		theme: 'dark_rounded',
		opacity: 0.5
	});
	
	var linkids =	['reputation',	'passion',		'relation',		'practice'];
	var hiddens = 	['rep-hidden',	'pass-hidden',	'rel-hidden',	'prac-hidden'];
	var head_bgs = 	[
					'/wp-content/themes/brown-law/images/reputation-hd.gif',
					'/wp-content/themes/brown-law/images/passion-hd.gif',
					'/wp-content/themes/brown-law/images/relation-hd.gif',
					'/wp-content/themes/brown-law/images/practice-hd.gif'
					];
	var curr_ind=0;
	var timeout = null;
	var duration = 7000;
	var restart = 5000;
	
	// Stop all animations when the links are clicked and set the current to active.
	$('#sidebar ul li a').click(function(event){
		var $this_id=this.id;
		var this_index=0;
		$('#sidebar ul li a').each(function (i) {
			$(this).stop();
			if (this.id == $this_id) { 
				$(this).addClass('current'); 
				this_index=i;
			}
			else { $(this).removeClass('current'); }
		});
		set_current(this_index,hiddens,head_bgs);
		clearTimeout(timeout);
		event.preventDefault();
	});
	timeout = setInterval( function() { curr_ind = slideSwitch(curr_ind,linkids,hiddens,head_bgs) }, duration);
});

///////////////////////// The meat and potatoes, the image cross fade and callout fades. ///////////////////////

function slideSwitch(index,links,hiddendivs,heads) {

	// The images.
    var $active = $('#middle-content IMG.active');
    if ( $active.length == 0 ) { $active = $('#middle-content IMG:last'); }
    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next() : $('#middle-content IMG:first');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
	});
    $active.addClass('last-active');

	// First one is already loaded, increment before beginning.
	index=((index+1)>=links.length)?0:index+1;
	var prev_index;
	prev_index = ((index-1)>=0)?index-1:links.length-1;
	$('#'+links[prev_index]).css({opacity: 1.0}).animate({opacity: 0.0}, 250,
		function() {
		fade_in_nav(links[prev_index],0);
	});
	$('#'+links[index]).css({opacity: 1.0}).animate({opacity: 0.0}, 250,
		function() {
		fade_in_nav(links[index],1);
	});
	
	// Callout boxes, h1's and side navs.
	var $callout = $('#callout-box');
	if ((index)=='undefined') { index=0; }
	var hidden	= hiddendivs[index];
	var heading	= heads[index];
    $callout.css({opacity: 1.0}).animate({opacity: 0.0},500,
	function() {
		swap_content(hidden,heading); 
	});

	return index;
}

function fade_in_nav(obj,addClass) {
	if (addClass==1) { $('#'+obj).addClass('current'); }
	else { $('#'+obj).removeClass('current'); }
	$('#'+obj).css({opacity: 0.0}).animate({opacity: 1.0}, 500);
}

///////////////////////// Set the text for the callouts, bg head images, fading jazz ///////////////////////

function swap_content (hidden,head) {
	var $hidden	= $('#'+hidden);
	var $nxt	= $hidden.html();
	$('#callout-box').html($nxt);
	$('#callout-box > h1').css('backgroundImage','url('+head+')');
	$('#callout-box').css({opacity: 0.0}).animate({opacity: 1.0}, 500);
}

///////////////////////// If one of the tabs are clicked, stop everything and open it. /////////////////////

function set_current(index,hiddens,heads) {

	var $hidden	= $('#'+hiddens[index]);
	var $nxt	= $hidden.html();
	$('#callout-box').html($nxt);
	$('#callout-box > h1').css('backgroundImage','url('+heads[index]+')');
	$('#middle-content img').each(function (i) {
		if (i==index) { 
			// make sure last active is not already assigned
			$(this).removeClass('last-active');
			$(this).addClass('active');
		}
		else { 
			$(this).removeClass('active');
			$(this).addClass('last-active'); 
		}
	});
}

