$(document).ready(function() {
	var oldheight = 0;

	//HIDE STUFF (SETUP contact us...)
	$('#contact').removeClass('topicbox').addClass('popup_block');
	$('#contact').attr('id','contact_popup');		
	$('#contact_popup').hide();

	//TOGGLE BARS
	//Hide (Collapse) the toggle containers on load
	$(".topic_container").hide(); 

	//AUTOMATICALLY OPEN THE FIRST ONE THOUGH
	$('div#interests_box').next().toggle();

	//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
	$("div.topicbox div.head").click(function(){
		$(this).toggleClass("headactive").next().slideToggle("slow", function() {

			if($('div#filler').height() == 10 && $('div.topic_container:visible').size() == 0) {
				//$('div#filler').height(oldheight);
			} else {
				//if($('div#filler').height() > 10) {
				//	oldheight = $('div#filler').height();
				//	$('div#filler').height(10);
				//}
			}
		});
		
		
		
		$('#' + $(this).attr('id') + ' span.moreinfo').toggle();
	});
	$('a.menuopt').click(function() {
		var box = $(this).attr('href');
		$(box).addClass("headactive").next().slideDown("slow", function(){ 
			//scroll the window down
			$('html,body').animate({ scrollTop: $(box).offset().top}, 2500); 
		});

		//adjust the filler div so the height of the body box is valid
		if($('div#filler').height() > 10 && box != '#intro') {
			oldheight = $('div#filler').height();
			$('div#filler').height(10);
		}

		$(box + ' span.moreinfo').toggle();
	});

	//FADE property
/*$(function () { */
	$('.fade').hover(function() { $(this).fadeTo("fast", 0.3); }, 
				function() { $(this).fadeTo("fast", 1); });
	$('.fade_link_inner').hover(function() { $(this).fadeTo("fast", 0.5); }, 
				function() { $(this).fadeTo("fast", 1); });

	/*$('.fade_link_inner').hover(function() {
	  $(".fade_link_inner").animate({ 
	    border: solid 1px '#000000',
	    background-color: '#f3efef',
		opacity: 1.0}, 1500 ); },
		function() {
		$(".fade_link_inner").animate({ 
	    border: solid 1px '#d6d6d6',
	    background-color: '#d6d6d6',
		opacity: 0
	  }, 1500 ); 
		
	});*/



	//POPUP CODE FOLLOWS

	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
	    var popID = $(this).attr('rel'); //Get Popup Name
	    var popURL = $(this).attr('href'); //Get Popup href to define size

	    //Pull Query & Variables from href URL
	    var query= popURL.split('?');
	    var dim= query[1].split('&');
	    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

	    //Fade in the Popup and add close button
	    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/btn_close3.png" class="btn_close" title="Close Window" alt="Close" /></a>');

	    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
	    var popMargTop = ($('#' + popID).height() + 80) / 2;
	    var popMargLeft = ($('#' + popID).width() + 80) / 2;

	    //Apply Margin to Popup
	    $('#' + popID).css({
		'margin-top' : -popMargTop,
		'margin-left' : -popMargLeft
	    });

	    //Fade in Background
	    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
	    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

	    return false;
	});

	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	    $('#fade , .popup_block').fadeOut(function() {
		$('#fade, a.close').remove();  //fade them both out
	    });
	    return false;
	});

	//move all quote boxes tot he right:
	//$('div.quotes:hidden').css('left',250);

	/*rotateQuote = function() {
		var thisbox = $('div.quotes:visible');
		var nextsel;
		//transition the color boxes...
		if($(thisbox).attr('id') === 'lastquote') {
			nextsel = $('div#box1');
		} else {
			nextsel = $('div.activebox').next();
		}
		
		$('div.activebox').removeClass('activebox').addClass('box');
		//fade in/out the quote:
		$('div.quotes:visible').fadeOut("slow", function() { 
			if($(thisbox).attr('id') === 'lastquote') {
				$('div#firstquote').fadeIn("slow"); 
			} else {
				$(thisbox).next('div.quotes').fadeIn("slow");
			}
			$(nextsel).removeClass('box').addClass('activebox');
			//$(nextsel).fadeIn("slow");
		});
	};

	rotateSwitch = function() {
		
		play = setInterval(function() {
			rotateQuote();
		}, 7000);
	};

	//$('div#box1').removeClass('box').addClass('activebox');
	rotateSwitch();

	$('div.box').hover(function() {
		clearInterval(play); 
		$('div.activebox').removeClass('activebox').addClass('box');
		$(this).removeClass('box').addClass('activebox');			
	}, function() { rotateSwitch(); });	//resume timer...

	$('div#box1').hover(function() {
		$('div.quotes:visible').hide();
		$('div#firstquote').show();
		clearInterval(play); 
		$('div.activebox').removeClass('activebox').addClass('box');
		$(this).removeClass('box').addClass('activebox');
	});
	$('div#box2').hover(function() {
		$('div.quotes:visible').hide();
		$('div#quote2').show();
	});
	$('div#box3').hover(function() {
		$('div.quotes:visible').hide();
		$('div#quote3').show();
	});
	$('div#box4').hover(function() {
		$('div.quotes:visible').hide();
		$('div#quote4').show();
	});
	$('div#box5').hover(function() {
		$('div.quotes:visible').hide();
		$('div#quote5').show();
	});
	$('div#box6').hover(function() {
		$('div.quotes:visible').hide();
		$('div#lastquote').show();
	});*/

	//show more quotes
	$('div#more_quotes').show();
	$('div#more_quotes').click(function() {
		var thisbox = $('div.quotes:visible');
		
		$('div.quotes:visible').fadeOut("slow", function() { 
			if($(thisbox).attr('id') === 'lastquote') {
				$('div#firstquote').fadeIn("slow"); 
			} else {
				$(thisbox).next('div.quotes').fadeIn("slow");
			}
		});
	});

	/*$('blockquote.quote').click(function() {
		var thisbox = $('div.quotes:visible');
		$('div.quotes:visible').slideUp("slow", function() { 
			if($(thisbox).attr('id') === 'lastquote') {
				$('div#firstquote').slideDown("slow"); 
			} else {
				$(thisbox).next('div.quotes').slideDown("slow");
			}
		});
	});*/


});

