/**
 * @author MacDonald
 */
$(function(){
	var date = new Date();
	$('#year').text(date.getFullYear());
	//alert($('#wrapper').height()+', '+$(window).height());
	if ($('#wrapper').height() < $(window).height()){
		$('#wrapper').css('margin-top', ($(window).height()-$('#wrapper').height())/2+'px')
	}
	$('#slider-trigger').click(function(){
		if ($(this).hasClass('collapsed')) {
			$('#slider1').animate({
				left: 0
			}, 'slow');
			$('#slider2').animate({
				left: 0
			}, 'slow')
			$(this).removeClass('collapsed');
			$('#gallery-up, #gallery-down').hide();
			$('#sitemap #menu ul ul').show();
		}
		else {
			$('#slider1').animate({
				left: (40-parseInt($('#slider1').css('width')))+'px'
			}, 'slow');
			$('#slider2').animate({
				left: '-156px'
			}, 'slow')
			$(this).addClass('collapsed');
			$('#gallery-up, #gallery-down').show();
			$('#sitemap #menu ul ul').hide();
		}
		return false;
	});
// SERVICES
	if ($('body').attr('id') == 'services'){
		if (window.location.search != ''){
			$(window.location.search.replace(/\?/,'#')).show();
			$(document).scrollTop(0);
		}
		else {
			$('#landing').show();
		}
		$('#menu li.selected ul a, .services-trigger a').click(function(){
			$('.services-panel').hide();
			$($(this).attr('href').replace(/\?/,'#')).show();
			return false;
		});
	}
// GALLERY
	if ($('body').attr('id') == 'gallery') {
		var shown = 17;
		var firstshown = 0; //index of first item visible
		var lastshown = 16; //index of last item visible
    var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
		if ($('#gallery-list a:first').is('[rel=video]')) {
			var url = $('#gallery-list a[rel=video]:first').attr('href');
		  swfobject.embedSWF(url+"?enablejsapi=1&playerapiid=ytplayer", "player", "480", "385", "8", null, null, params, atts);
		}
		else {
			$('#slider-trigger img').attr('src', $('#gallery-list a:first').attr('href'));
		}
		$('#gallery-list li:first').addClass('selected');
		$('#gallery-list a').click(function(){
			triggerGallery($(this).parent());
			return false;
		});
		$('#gallery-up, #arrow-up').click(function(){
			if ($('#gallery-list li:first').hasClass('selected')){
				anchor = $('#gallery-list li:last');
				lastshown = $('#gallery-list li').length-1;
				firstshown = lastshown - shown + 1;
			}
			else {
				anchor = $('#gallery-list li[class=selected]').prev();
			}
			triggerGallery(anchor);
			return false;
		});
		$('#gallery-down, #arrow-down').click(function(){
			if ($('#gallery-list li:last').hasClass('selected')){
				anchor = $('#gallery-list li:first');
				firstshown = 0;
				lastshown = shown - 1;
			}
			else {
				anchor = $('#gallery-list li[class=selected]').next();
			}
			triggerGallery(anchor);
			return false;
		});
		
		function triggerGallery($item){
			if ($item.find('a').attr('rel') == 'video') {
				$('#video').html('<div id="player"></div>');
				var url = $item.find('a').attr('href');
			  swfobject.embedSWF(url+"?enablejsapi=1&playerapiid=ytplayer", "player", "480", "385", "8", null, null, params, atts);
				$('#slider-trigger img').attr('src', '/img/gallery/gallery_movie.jpg');
			}
			else {
				$('#video').html('<div id="player"></div>');
				$('#slider-trigger img').attr('src', $item.find('a').attr('href'));
			}
			$item.siblings().removeClass('selected');
			$item.addClass('selected');
			if ($('#gallery-list li').index($item) <= firstshown+1 && firstshown > 0){
				firstshown--;
				lastshown--;
			}
			if ($('#gallery-list li').index($item) >= lastshown-1 && lastshown < $('#gallery-list li').length-1){
				firstshown++;
				lastshown++;
			}
			$('#gallery-list ul').css('top', $('#gallery-list li').height()*(-firstshown));
		}
	}
});

