// JavaScript Document
var timer, pictimer;
var menuid, pic;
//get menu ready
$(document).ready(function(){

	//prepare dropdown menu
	$('#menuList > li > a').each(function(){
		$(this).mouseover(imgOn);
		$(this).mouseout(menuOut);
	});
	$('#menuList > li ul').hover(keepAlive, menuOut);
	
	if(!Modernizr.boxshadow){
		/*Add to topMain*/
		$('#topMain').prepend(
			'<div id="leftshadeTop"><!--stuff--></div><!--/leftshadeTop-->'+
            '<div id="rightshadeTop"><!--stuff--></div><!--/rightshadeTop-->');
		/*lowerhalf*/
		$('#lowerHalf .center, #lowerHalf .center2').prepend(
			'<div id="leftshadeBottom"><!--stuff--></div><!--/leftshadeBottom-->'+
		    '<div id="rightshadeBottom"><!--stuff--></div><!--/rightshadeBottom-->');
	}
	if(!Modernizr.multiplebgs){
		$('#stripes').remove();
		/*Deal with the stripes*/
		$('#topHalf .center').prepend(
			'<div id="topBackLeft"><div id="topLeftStripe"><!--stuff--></div></div><!--/topBackLeft-->'+
            '<div id="topBackRight"><div id="topRightStripe"><!--stuff--></div></div><!--/topBackRight-->');
		$('#footer').prepend('<div id="footerback"><!--stuff--></div>');
	}
	if(!Modernizr.borderradius){
		//The Sidebars of the home and other pages
		$('#imageBar').wrapInner('<div id="imageBarInside"><div id="imageBarMid">'+
			'</div></div>').prepend('<div id="imageBarTop"><!--stuff--></div>');
		$('#submenu').wrapInner('<div id="submenuMid"></div>').prepend('<div id="submenuTop"><!--stuff--></div>');
		//Any of the White box classes
		$('.whitebox').wrapInner('<div class="whiteboxL"><div class="whiteboxR"><div class="whiteboxMid">'+
			'</div></div></div><!--/whitebox middle-->').prepend('<div class="whiteboxTL"><div class="whiteboxTR">'+
			'<div class="whiteboxT"><!--stuff--></div></div></div><!--/top of box-->').append('<div class="whiteboxBL">'+
			'<div class="whiteboxBR"><div class="whiteboxB"><!--stuff--></div></div></div><!--/bottom of box-->');		
	}
	if ($('#galleryphoto').length > 0){
		openGallery();
	}
	//report form fixes for the stupider browsers
	if ($("#reportform label[for='details']").width() < 600){		
		$("#reportform label[for='details']").width('630px');
	}
	if (($("#reportform input[type='text']").width() < 355) && ($("#reportform input[type='text']").css('border-color')!='#b4b4b4')){
		$("#reportform input[type='text']").addClass('reportfield');
	}
});
//open menu on mouse over
function imgOn(){
    clearTimeout(timer);
    
    $('#menuList > li > ul').hide();
	
	$(this).next('ul').show();	
}

//menu closing on mouseout
function menuOut(){
    timer = setTimeout("closeDropDown()",1000);
}

//close dropdown menu
function closeDropDown(){
	$('#menuList > li > ul').hide();
}
//keep menu open
function keepAlive(){
    clearTimeout(timer);
}
/****************************************PHOTO GALLERY STUFF*********************************/
/*First step in opening gallery*/
var pagesize = 12;
var currentthumb;
function openGallery(){
	var galleryindex = '';
	$.ajax({
		type: 'GET',
		url: 'gallery/gallery.xml',
		dataType: 'xml',
		beforeSend: function(){
			$('#galleryphoto').html('Loading...');
			$('#galleryindex').html('Loading...');
		},
		success: function(xml){
			$('#galleryindex').html('');
			$(xml).find('pic:lt('+pagesize+')').each(function(n){ //get 1st 12 pictures
				var filename = $(this).attr('file');							
				
				var picid = 'gallpic'+n; //pic id
				galleryindex = '<a href="gallery/' + filename + '" id="'+picid+'">'
				+'<img src="gallery/thumbs/' + filename + '" alt="' + filename + '" /></a>'; //add gallery picture
				
				if (n==0){					
					$('#galleryphoto').html('<img src="gallery/' + filename + '" alt="' + filename + '" />'); //show first picture
					currentthumb = filename;
				}	
				
				$('#galleryindex').append(galleryindex);
				
			});			
			$('#galleryindex a').click(viewpic);
			$('#prevbutton, #nextbutton').click(switchpage); //add listeners to previous and next buttons
			$('#galleryindex').attr('title','page1'); //set title of index to know which page we are on
			$('#galleryindex img[src$="'+currentthumb+'"]').addClass('current'); //set first pic as current
		}
	});
}
/*view a pic*/
function viewpic(){		
	var path = $(this).attr('href').split('/');
	var filename = path[path.length-1];

	currentthumb = filename;
	$('#galleryphoto').html('<img src="gallery/' + filename + '" alt="' + filename + '" />');
	$('#galleryindex .current').removeClass('current');
	//$('#'+picid+' img').addClass('current');
	$(this).find('img').addClass('current');
	return false;
}
/*go to a page*/
function switchpage(){
	var button = this.id;
	var pagenum = parseInt ($('#galleryindex').attr('title').replace('page',''),10);
	if ((pagenum==1)&&(button=='prevbutton')){
		return false;
	}
	var galleryindex;
	$.ajax({
		type: 'GET',
		url: 'gallery/gallery.xml',
		dataType: 'xml',
		beforeSend: function(){
			galleryindex = $('#galleryindex').html();
			$('#galleryindex').html('Loading...');
		},
		success: function(xml){
			var newpage;
			if (button=='nextbutton'){
				newpage = pagenum+1;
			}else{
				newpage = pagenum-1;
			}
			
			if ((newpage-1)*pagesize > $(xml).find('pic').length){
				$('#galleryindex').html(galleryindex);
				return false;
			}
			galleryindex = '';
			var lowerbound = (newpage-1)*pagesize-1;
			var bounds;
			if (lowerbound > -1)
				bounds = 'gt('+ lowerbound +'):lt('+pagesize+')';
			else
				bounds = 'lt('+pagesize+')';
			$('#galleryindex').html('');
			$(xml).find('pic:'+bounds).each(function(n){
				var filename = $(this).attr('file');
				
				if (n<pagesize){					
					var picid = 'gallpic'+n; 
					var classname = '';
					if (currentthumb==filename) classname = 'class="current"'; //check if current
					
					galleryindex = '<a href="gallery/' + filename + '" id="'+picid+'">'
					+'<img src="gallery/thumbs/' + filename + '" alt="' + filename + '" '+classname+' /></a>';
				}
				
				$('#galleryindex').append(galleryindex);
			});			
			$('#galleryindex a').click(viewpic);
			$('#galleryindex').attr('title','page' + newpage);			
		}
	});
}
