// JavaScript Document

$(document).ready(function(){
	$(function() {
  
			$.ajax({
               	type: "GET",
                 url: "xml/data.xml",
                 dataType: "xml",
                 success: function(xml) {
                    $(xml).find('categoryentries').each(function(){
                         var title = $(this).attr('categorytitle');
                         //var article = $(this).find('entry').attr('entrytitle').text()
                         	$('<li id="' + title + '" ></li>')
                             	.appendTo('#nav1>ul');
							$('<span class="sub1" onclick=""></span>')
								.html(title)
								.appendTo('#'+title);
								$('<li class="arrow arrowUp" onclick=""></li>')
									 .appendTo('#'+title).hide();
						 	$('<ul id="ul_'+title+'" class="article_ul"></ul>').appendTo('#'+title).hide();
								$(this).find('entry').each(function(){
									var entrytitle = $(this).attr('entrytitle');
									var linkout = $(this).attr('entrylink');
									var post = $(this).text();
									var img = $(this).attr('entryurl');
									var today = new Date();
									today.setDate(today.getDate()+1);
									var entrydate = $(this).attr('entrydate');
									var upcomingentrydate = new Date(entrydate);
									if (upcomingentrydate >= today){
										$("#INTERVIEW").css('display','block')
										var dater = "upcoming";
									}else{
										var dater = "nope";
									}
									//var entrydatenow = new Date(entrydate);
									 $('<li class="article dater" onclick=""></li>')
										 .html(entrytitle +dater)
										 .data('src', img)
										 .data('txt',post)
										 .data('linkout',linkout)
										 .appendTo('#ul_'+title);
								});// close entry each
								$('<li class="arrow arrowDn" onclick=""></li>')
									 .appendTo('#'+title).hide();
						//alert(c);
                     }); //close each(
                 }

			}); //close $.ajax
		});   //close function()
					 
  //transition effects to make page fade in and fade out 
$("body").fadeIn(1000);	
	$('#right').css('display', 'none');
	$('#left').css('display', 'none');
	$('#right').fadeIn(1500);	
	$('#left').fadeIn(1000);
						   
	
//NAV1 functionality	
	$('.sub1').live('click',function(){
		$('.sub1').siblings().hide('slow');
		if($(this).siblings().is(':visible')){
			$(this).siblings().hide('slow');
		}else{
			$(this).siblings().show('slow');
		}
	});

	$(".arrowUp").live('hover',function(){
			//alert(height);
			$("#ul_NEWS li:first").animate({
				marginTop:(2)},
				500
				);
	});
	
	$(".arrowDn").live('hover',function(){
			total = $(this).prev().children().length;
			total++;
			length = (21 * total);
			//alert(length)
			mt = (300-length);
			//alert(mt)
			if (mt > 0){mt = 0}
			$(this).prev().children().filter(":first").animate({
				marginTop:(mt)},
				500
				);
	});
	
	//for iPads
		$(".arrowUp").live('click',function(){
			//alert(height);
			$("#ul_NEWS li:first").animate({
				marginTop:(10)},
				500
				);
	});
	
	$(".arrowDn").live('click',function(){
			total = $(this).prev().children().length;
			total++;
			length = (21 * total);
			//alert(length)
			mt = (300-length);
			//alert(mt)
			if (mt > 0){mt = 0}
			$(this).prev().children().filter(":first").animate({
				marginTop:(mt)},
				500
				);
	});
	
	
	
	
   $('.article').live('click',function(){
		/*linkLocation = $(this).parent().parent().attr('id').toLowerCase();
		linkLocation = linkLocation + ".php";
		post = $(this).html();
		redirectPage();	*/
		var src = $(this).data('src');
		var txt = $(this).data('txt')+"<br />";
		var linkout = $(this).data('linkout');
		$('#right').css('display','none');
		$('#img_box *').detach();
		$('#img_box').removeClass('whtbg');
		getImgSize(src, linkout);
		$('#right').fadeIn(1500);
			
			if (linkout){
				$('#text_box').html('<a href="' + linkout + '" target = "_blank" >'+txt+'</a>');
				$('<span class="tiny">Click here to see more.' +linkout+'</span>').appendTo('#text_box'); 	
			}else{
				$('#text_box').html(txt);
			}
	});
   
   
   
   
function getImgSize(imgSrc, linkout) {
    var newImg = new Image();

    newImg.onload = function() {
    var height = newImg.height;
    	var tbheight = Math.round(height*.8);
		tb_height = tbheight + "px";

	
	
	
	var width = newImg.width;
		var tbwidth = Math.round(width*.8);
		tb_width = tbwidth + "px";
		adj_tbwidth = tbwidth + 20;

		
		
	var right_width = $('#right').width();  
		if(right_width < width){
			var tb_margin = ((width - adj_tbwidth)/2);
		}else{
			var tb_margin = Math.round((right_width - adj_tbwidth)/2);
		}
		//alert("width; "+ width + " right_width: "+ right_width + " tbwidth: " + tbwidth + " tb_margin: " +tb_margin);

		
		$('#img_box').css({
			'width':width+'px', 
			'height':height+'px',
		});   
		
		$('#text_box').css({
			'display':'none',
			'margin-top':'-'+(height*.9)+'px', 
			'width':tb_width, 
			'margin-left': tb_margin+'px', 
			'padding':'10px',
			'max-height': tb_height

		});   
    }
    newImg.src = imgSrc; // this must be done AFTER setting onload
	if(linkout){
		$('#img_box').append('<a href="' + linkout + '" target = "_blank" ><img src="'+imgSrc+'" alt="'+imgSrc+'" /></a>');
	}else{
		$('#img_box').append('<img src="'+imgSrc+'" alt="'+imgSrc+'" />');
	}
}



$('#right').hover(
		function(){
			$('#text_box').css('background', 'rgba(255, 255, 255, 0.8)');
			$('#text_box').fadeIn('slow');
							   
		},
		function(){
			$('#text_box').fadeOut('slow');
		}
	);
	
//NAV 2 functionality
   $(".link").click(function(event){
        event.preventDefault();
        linkLocation = $(this).attr('id').toLowerCase();
		linkLocation = linkLocation + ".php";
		redirectPage(linkLocation);						   
	});
	
	$("#nav2 ul li").bind('mouseover',function(){
		$(this).siblings().addClass('blur');
	});
	$("#nav2 ul li").bind('mouseout',function(){
		$(this).siblings().removeClass('blur');
	});
					

	$("#quotes").cycle({
				fx: 'fade',
				speed:    2000, 
   				timeout:  2000
		});
						
    function redirectPage() {
        window.location = linkLocation;
    }						
});
