$(document).ready(function(){

preparePopUps();

// sideBar nav toggle -------------------------------------

var $sideNav = $('#nav');
$('#nav ul').addClass('hide');

$('#nav a[@href="#"]').click(function() {	
	var me = $(this).parent().find('ul');
	me.slideToggle('fast',navCallBack($(this)));
})

function navCallBack(a) {
	var me = a;
	me.toggleClass('cls');
}

// PopUps ------------------------------------------------

function preparePopUps() {
	var headLinks = $('#head a');
	
	headLinks.each( function() {
		if ( $(this).hasClass('popUp') ) {
			$(this).click( function() { 
			popUp( $(this).attr('href') );
			return false;
			}); 
		};
	});
}

function popUp(href) {
	window.open(href, "", "width=550, height=600, scrollbars");
}


// image Viewer ------------------------------------------

var imgViewer = document.getElementById('imgViewer');

// check if the page needs the viewer script
if(imgViewer) {

	var viewer = $('#imgViewer');

	var thumbs = $('.thumbs a');

	var i = 1;

	var imgs = Array();

	thumbs.each( function() {

		var img = $(this).find('img');
		img.attr('rel',i);
	
		if (i !==1) {	
			$('#imgDisplay').append('<li><img src="'+ $(this).attr('href')+ '" /></li>')
		}	
		i++;
	
		$(this).click(function(){
		
		// check if the curent displayed img is clicked
		
			if ($(this).hasClass('current')) {
				return false;			
			}
			var links = $(this).parent().parent();
		
			links.find('a').each( function() { 
				$(this).removeClass('current')
				})
		
			$(this).addClass('current');
		
			var href = $(this).attr('href');
		
			$('#imgDisplay li img').each( function() {
		
				var li = $(this).parent();		
			
				if (li.hasClass('active')) {
					li.removeClass('active').addClass('lastActive');
				}	else {
					li.removeClass('lastActive');
				}			
				if ($(this).attr('src') == href) {
			
					li
						.css('opacity', '0')
						.addClass('active')
						.animate({opacity: 1.0}, 1500);				
				}
			})

			return false;
		})
	
	})

}

});