// JavaScript Document
//Requires JQUERY

$(window).resize(function() {
  resizeWin();
});


function displayPopup(content, popwidth, showBorder, id){
	
	if ($("#frame").length == 0){
		$('body').append('<div id="frame"><div id="closeButton"><a href="javascript:closePop();"><img src="assets/images/misc/closebox.png" width="30" height="30" alt="Close" /></a></div><div id="frameContent"><div id="frameLoading">Loading...</div></div></div><a href="javascript:closePop();" id="coverLink"><div id="cover" class="blackout"> </div></a>');
	
		if(popwidth!=null){
			$("#frame").css('width', popwidth + "px");	
			$("#closeButton").css('left', (popwidth-16) + "px");
		}
	
		if(showBorder==false){
			$("#frameContent").addClass('noBorder');	
		}
	
		$.ajax({
			url: "popups/"+content+".php",
			data: "id="+id,
			success: function(msg) {
				$("#frameContent").html(msg);
			}
		});
	
		//$("#frameContent").html('<div style="text-align:center; padding:50px;">Coming Soon</div>');
		
		$("#cover").width($(window).width()).height($(window).height());
		$("#cover").show();
		
		centerDiv("frame");
	}
	
	
}

function centerDiv(myDiv){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	var popupHeight = $("#"+myDiv).height();
	var popupWidth = $("#"+myDiv).width();
	var popupLeft = windowWidth/2-popupWidth/2;
	if(popupLeft<0){ popupLeft = 0; }
	//centering
	
	if(windowHeight<popupHeight + 100){
		var myPos = "absolute";
	}else{
		var myPos = "fixed";	
	}
	
	$("#"+myDiv).css({
		"position": myPos,
		"left": popupLeft
	});
	
}

function resizeWin(){
	if ($("#frame").length > 0){
		centerDiv("frame");
		$("#cover").width($(window).width()).height($(window).height());
	}
}
  
function closePop(){
	$("#frame").remove();
	$("#cover").remove();
	$("#coverLink").remove();
}


/* SLIDESHOW */
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});


function showBio(bioID){
	$(".bioArea .bio").hide();
	$(".bioArea #" + bioID).show();
	
}
