// JavaScript Document


//when everything such as images are loaded
$(document).ready(function(){	
	/**********************Current Page Functions**************************/
	var page = location.pathname;
	page = page.substring(page.lastIndexOf('/')+1,page.lastIndexOf('.html')).toLowerCase();
	markCurrentPage(page);
	/**********************Three columns**********************************/
	if (!Modernizr.csscolumns){
		$('.threecolumn').each(function(){
			var items = $(this).find('li').length;
			items = Math.round(items/3);
			for(var n=1; n<3; n++){
				$(this).find('ul:last').after('<ul />');
				var i = items;
				while (i < items*2){				
					$(this).find('ul:eq(0) li:eq('+items+')').detach().appendTo($(this).find('ul:eq('+n+')'));
					i++;
				}
			}
		});		
	}
	adjustBackgroundHeight();
	/***********************Gallery*************************************/
	if (!Modernizr.borderradius){
		$('.gallery-content').before('<div class="gallery-top"><!--stuff--></div>').after('<div class="gallery-bottom"><!--stuff--></div>');
	}
	$('.gallery-content a').click(expandPic);
});
$(window).load(function(){
	adjustBackgroundHeight();
});
/*Make sure left and right parts of background are the same height as middle*/
function adjustBackgroundHeight(){
	$('#back-left, #back-right').removeAttr('style');
	var height = $('#mainback').outerHeight(true);
	if (height != $('#back-left').outerHeight()){
		$('#back-left, #back-right').css('height',height-$('#wrapper').offset().top+'px');
	}
}
/*Mark the current pages in links*/
function markCurrentPage(page){
	var category = (page.indexOf('-') != -1 ? page.substring(0,page.indexOf('-')):page);
	switch (category){
		case 'services':
			$('#link1').addClass('current');
			break;
		case 'projects':
			$('#link2').addClass('current');
			break;
		case 'brochures':
			$('#link3').addClass('current');
			break;
		case 'galleries':
			$('#link4').addClass('current');
			break;
		case 'contact':
			$('#link5').addClass('current');
			break;
		default:
			break;
	}
	//do subnav & gallery
	$('#subnav, .gallery-links').each(function(){


			$(this).find('a[href="'+page+'.html"]').addClass('current');
				
	});
}
/*Expands a pictures in lightbox*/
function expandPic(){
	var img = $(this).attr('href');
	var title = $(this).find('img').attr('alt');
	var piccontent = 
	'<div id="screen"></div>'+
	'<div id="fullsize">'+
		'<div id="closearea"><span>'+title+'</span><a href="#" id="closepic">close</a></div>'+
		'<a href="#" id="prev">Previous</a>'+
		'<a href="#" id="next">Next</a>'+
		'<img src="'+img+'" alt="pic" id="fullpic" />'+
		'<div class="clear"></div>'+
	'</div>';
	$('body').append(piccontent);
	$('#screen').css({height:$('#mainback').outerHeight(true)+'px'});
	centerFullSize();
	//set previous and next
	if ($(this).prev().length){
		$('#prev').attr('title', $(this).prev().attr('href'));
	}
	if ($(this).next().length){
		$('#next').attr('title', $(this).next().attr('href'));
	}
	$('#prev, #next').click(changePic);
	$('#closepic').click(closePic);
	$('#fullpic').load(adjustFullSize);
	return false;
}
function closePic(){
	$('#fullsize, #screen').remove();
	$('#mainback').removeAttr('style');
	adjustBackgroundHeight();
	return false;
}
function changePic(){
	var action = $(this).attr('id');
	var currentpic = $('#fullpic').attr('src');
	var newpic = $(this).attr('title');
	if (newpic != ''){
		$('#fullpic').hide().attr('src',newpic);
		centerFullSize();
		$('#fullpic').load(adjustFullSize);
		
		var thumb = '.gallery-content a[href="'+newpic+'"]';
		if ($(thumb).prev().length){
			$('#prev').attr('title', $(thumb).prev().attr('href'));
		}else{
			$('#prev').removeAttr('title');
		}		
		if ($(thumb).next().has('img').length){
			$('#next').attr('title', $(thumb).next().attr('href'));
		}else{
			$('#next').removeAttr('title');
		}
		$('#closearea span').text($(thumb + ' img').attr('alt'));
	}
	
	return false;
}
function adjustFullSize(){	
	$(this).show();
	var picheight = $('#fullsize').outerHeight(true);
	if (picheight > $('#mainback').outerHeight(true)){			
		$('#mainback').css({height:picheight+'px'});
		$('#screen').css({height:picheight+'px'});
		adjustBackgroundHeight();
	}
	centerFullSize();
}
function centerFullSize(){
	if ($('body').hasClass('ie6')){
		var padding = Number($('#fullsize').css('padding-left').replace('px',''));
		var picwidth = $('#fullpic').width();
		$('#fullsize').css('width',(picwidth + 2*padding)+'px');
	}
	$('#fullsize').css({
		left : ($(window).width() - $('#fullsize').outerWidth())/2+'px'
	});
}
