// default.js
var mapA;
var currentGallery;
var currentPage;
var currentHash;

var currentHash = '';

var calDay 		= '';
var calMonth	= '';
var calYear 	= '';
var calGallery 	= '';

function checkForHash(){
	if(window.location.hash.substring(1,window.location.hash.length) != currentHash){
		if(window.location.hash.length > 0){
			window.location = window.location.hash.substring(1,window.location.hash.length); 	
		}
	}
}
var HashCheckInterval = setInterval("checkForHash()", 500);

$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};

$(function(){
	
	checkForHash();
	
	$('.navigation>ul>li.navcalendar').hide();
	$('#footer>.navigation>ul>li').hide();
	$('#footer>.navigation>ul>li.navcalendar').show();
	$('#info').hide();
	
	$('#image>.credit').livequery(function(){
		$(this).hide();
		$(this).parent('div').each(function(){
			$(this).mouseover(function(){
				$(this).children('.credit').stop().show();					   
			});
			$(this).mouseout(function(){
				$(this).children('.credit').hide();					   
			});	
		});
		
		$(this).mouseover(function(){
			$(this).stop().show();					   
		});
		$(this).mouseout(function(){
			$(this).hide();							   
		});
	}, function(){});
	
	$('a').livequery(function(){
		if($(this).attr('onclick')	){
			$(this).click(function(){
				window.location.hash = $(this).attr('href');
				currentHash = $(this).attr('href');
				return false; 
			});
		}
	}, function(){});
	
	$('.left').find('a').livequery(function(){
			$(this).click(function(){
				$('.left').find('a').removeClass('sel');
				$(this).addClass('sel');
				
				$(this).parent('li').parent('ul').parent('li').children('ul').children('li').children('ul').slideUp();
				$(this).parent('li').children('ul').stop().slideDown();
				return false; 
			});
	
	}, function(){});
	
	$('.open').find('a[target!=_blank]').livequery(function(){
			$(this).click(function(){
				if($('.left').find('a[href='+ $(this).attr('href') +']').text()){
					$('.left').find('a').removeClass('sel');
					$('.left').find('a[href='+ $(this).attr('href') +']').addClass('sel');
				}
				
				return false; 
			});
	
	}, function(){});
	
	$('.events>.flow>.left>ul>li>ul>li>ul:not(:first)').livequery(function(){
		$(this).hide();
		
	}, function(){});
	
	$('#map').livequery(function(){
		if (GBrowserIsCompatible()) {
			
			var building = $('#map>.bld').html();
			var sTaddress = $('#map>.st').html();
			var sFaddress = $('#map>.sf').html();
			var sTdirections = 'http://www.google.com/maps?f=d&hl=en&saddr=' + '' + '&daddr=' + sTaddress;
			var sFdirections = 'http://www.google.com/maps?f=d&hl=en&saddr=' + sTaddress + '&daddr=';

			var sTinfo = '<div style="text-align: left;"><a href="http://www.ubartgalleries.com/">' + building + '</a><br>' + sFaddress + '<br><br>Get directions: <a href="' + sTdirections + '">To here</a> - <a href="' + sFdirections + '">From here</a><br><br></div>';
			$(this).empty();
			
        	mapA = new GMap2(this);
			mapA.addControl(new GSmallMapControl());
			mapA.addControl(new GMapTypeControl());

        	geocoder = new GClientGeocoder();

			showAddress(sTaddress, sTinfo);
		}	
	},function(){});
	
	$('#monthArea').livequery(function(){
		$(this).mousewheel(function(event, delta) {
			if (delta > 0)
				$(this).scrollTop($(this).scrollTop() - (delta*18));
			else if (delta < 0)
				$(this).scrollTop($(this).scrollTop() - (delta*18));
			return false; // prevent default
		}); }, function(){ $(this).unmousewheel(); });
	
	$('#image>img').livequery(function(){
		var preLoad = new Image();
		preLoad.src = $(this).attr('src');
		preLoad.onload = $('#image').slideDown(1000, function(){$(this).removeAttr('style');});
	}, function(){});
	
});

function gallery(title){
	if(currentGallery == 'home'){
		$('#intro').stop().animate({'height' : '35px'}, 1000, function(){
			$(this).removeClass().addClass('spacer').removeAttr('style').empty();
		});
		$('#image').stop().animate({'height' : '235px'}, 1000, function(){
			$(this).removeClass().addClass('medium').removeAttr('style');
		});
	}
	
	if(currentGallery != title){
		
		tempHeight = $('.flow').height();
		slideSpeed = (tempHeight / 470) * 1000;
		
		$('.flow').slideUp(slideSpeed, function(){ 
			
			infos = true;
			
			switch(title){
				case 'events':
					events('', '', '');
					break;
				case 'anderson':
					page(title, 'background');
					break;
				case 'art':
					page(title, 'background');
					break;
				case 'calendar':
					events('calendar', '', '');
					title = 'events';
					infos = false;
					break;
			}
			
			$('#content').removeClass().addClass(title); 
			
			navigation(title, infos);
			currentGallery = title;
		});
	} else {
			switch(title){
				case 'events':
					events('', '', '');
					break;
				case 'anderson':
					page(title, 'background');
					break;
				case 'art':
					page(title, 'background');
					break;
				case 'calendar':
					events('calendar', '', '');
					title = 'events';
					break;
			}
	}
	
	$('#footer>.navigation>ul>li').show();
	$('#footer>.navigation>ul>li.nav'+title).hide();
	$('#footer>.navigation>ul>li.navcalendar').hide();
	
	$('.navigation>ul>li>a').removeClass('sel');
	$('.navigation>ul>li.nav'+title+'>a').addClass('sel');

}

function imageSwap(gallery, eventID){
		
		tempHeight = $('#image').height();
		slideSpeed = (tempHeight / 470) * 1000;
		
		$('#image').slideUp(slideSpeed, function(){
			$('#image').empty();
			if(eventID){ $(this).removeClass().addClass('large'); }else{ $(this).removeClass().addClass('medium'); }
			$.get('ajax.php',{'select':'image', 'gallery':gallery, 'eventID':eventID},function(data){
				if(data){ 
					$('#image').html(data);
				}	
			});
		});
}

function navigation(gallery, infos){
	$.get('ajax.php',{'select':'navigation', 'gallery':gallery},function(data){
		$('.left').html(data);	
		
		tempHeight = $('.flow').height();
		slideSpeed = (tempHeight / 470) * 1000;
		
		$('.flow').wait(1000).slideDown(slideSpeed);

	});
	
	if(infos){
		showInfo(gallery);
	}
	
	if(gallery != currentGallery && (currentGallery != 'home' || gallery != 'events')){
		if(gallery == 'events'){ gallery = ''; }
		imageSwap(gallery, '');
	}
}

function calendar(){
	if($('#info').find('#monthArea').html() && $('#info').height() == 185){
		$('#info').animate({'height' : '0px'}, 1000, function(){
			$(this).empty();
		});
	} else if($('#info').find('.message').html()){
		$('#info').animate({'height' : '0px'}, 1000, function(){
			$(this).empty();
			calendar();
		});
	}
	
	if($('#info').height() <= 35){
		$.get('ajax.php',{'select':'calendar'},function(data){
			$('#info').html(data);
			$('#info').animate({'height' : '185px'}, 1000, function(){});
			
			setCalReset();
		});
	}
}

function hideCalendar(){
	if($('#info').find('#monthArea').html() && $('#info').height() == 185){
		$('#info').animate({'height' : '0px'}, 1000, function(){
			$(this).empty();
		});
	}
}

function showInfo(gallery){
	$('#info').animate({'height' : '0px'}, 1000,function(){
		$(this).empty();
		$.get('ajax.php',{'select':'info', 'gallery':gallery},function(data){
			if(data){ 
				$('#info').html(data).wait(1000).animate({'height' : '185px'}, 1000, function(){});
			}									   
		});
	});
}

function hideInfo(gallery){
	$.get('ajax.php',{'select':'hideInfo', 'gallery':gallery},function(){	
		$('#info').animate({'height' : '0px'}, 1000, function(){}).empty();
	});
}

function hideIntro(){
	$('#intro').stop().animate({'height' : '35px'}, 1000, function(){
			$(this).removeClass().addClass('spacer').removeAttr('style').empty();
	});	
}

function eventDetails(gallery, eventID){
	
	$.get('ajax.php',{'select':'event', 'eventID':eventID},function(data){
		$('.open').html(data);			 
	});
	
	imageSwap(gallery, eventID);
	hideInfo(gallery);
	currentPage = 'event';
	
	_gaq.push(['_trackPageview','/ajax/event/'+gallery+'/'+eventID]);
}

function events(type, gallery, range, year, letter, img){
	
	if(!year){year = ''}
	if(!letter){letter = ''}
	if(!img){img = ''}
	
	if(type != 'calendar'){
		$.get('ajax.php',{'select':'events', 'type':type, 'gallery':gallery, 'range':range, 'year':year, 'letter':letter, 'img':img},function(data){
			$('.open').html(data);			 
		});
		
		hideCalendar();
	} else {
		calendar();	
	}
	
	if(currentPage == 'event'){
		imageSwap(gallery, '');
		currentPage = 'events';
	}
	
	_gaq.push(['_trackPageview','/ajax/events/'+gallery+'/'+type+'/'+range]);
}

function page(gallery, page){
	
	$.get('ajax.php',{'select':'page', 'gallery':gallery, 'page':page},function(data){
		$('.open').html(data);			 
	});
	
	if(currentPage == 'event'){
		imageSwap(gallery, '');
		currentPage = 'page';
	}
	
	_gaq.push(['_trackPageview','/ajax/page/'+gallery+'/'+page]);
}

function originalImage(imageID){
	$.get('ajax.php',{'select':'original', 'imageID':imageID},function(data){
		$('body').append(data);
		
		$('#imageOverlay').fadeIn(function(){
			if($(window).height() <= $('#imageContent>img:first').height() + 105){
				$('#imageContent>img:first').height(($(window).height() - 105));
			}
			
			$('#imageContent>img:first').load(function(){
				if($(window).height() <= $('#imageContent>img:first').height() + 105){
					$('#imageContent>img:first').height(($(window).height() - 105));
				}
			});
		});
	});
	
	_gaq.push(['_trackPageview','/ajax/image/'+imageID]);
}

function setCalDay(obj, day){
	calDay = day;
	
	$.get('ajax.php',{'select':'events', 'gallery':calGallery, 'range':'date', 'month':calMonth, 'day':calDay},function(data){
		$('.open').html(data);			 
	});
	
	$('.days>ul>li>a').removeClass('sel');
	obj.addClass('sel');
	
	_gaq.push(['_trackPageview','/ajax/calendar/'+calGallery+'/'+calMonth+'/'+calDay]);
}

function setCalMonth(obj, month){
	calMonth = month;
	calDay = '';
	
	$.get('ajax.php',{'select':'days', 'gallery':calGallery, 'month':calMonth},function(data){
		$('.days').html(data);			 
	});
	
	$.get('ajax.php',{'select':'events', 'gallery':calGallery, 'range':'date', 'month':calMonth},function(data){
		$('.open').html(data);			 
	});
	
	$('.months>div>ul>li>a').removeClass('sel');
	obj.addClass('sel');
	
	scrollPos = 18 * (parseFloat(month) - 3);
	$('#monthArea').animate({ 'scrollTop':scrollPos });
	
	_gaq.push(['_trackPageview','/ajax/calendar/'+calGallery+'/'+calMonth+'/'+calDay]);
}

function moveCalMonth(){
	
	var d	=	new Date();
	var month = d.getMonth();
	
	scrollPos = 18 * (parseFloat(month) - 2);
	$('#monthArea').animate({ 'scrollTop':scrollPos });
}

function setCalGallery(obj, gallery){
	
	$('.galleries>ul>li>a').removeClass('sel');
	
	if(calGallery != gallery){
		calGallery = gallery;
		obj.addClass('sel');
	} else {
		calGallery = '';
	}
	
	$.get('ajax.php',{'select':'days', 'gallery':calGallery, 'month':calMonth, 'day':calDay},function(data){
		$('.days').html(data);			 
	});
	
	$.get('ajax.php',{'select':'events', 'gallery':calGallery, 'range':'date', 'month':calMonth, 'day':calDay},function(data){
		$('.open').html(data);			 
	});

	_gaq.push(['_trackPageview','/ajax/calendar/'+calGallery+'/'+calMonth+'/'+calDay]);
}

function setCalReset(){
	calGallery = '';
	calMonth = '';
	calDay = '';	
	
	$.get('ajax.php',{'select':'events', 'range':'date'},function(data){
		$('.open').html(data);
		moveCalMonth();
	});
}

function showAddress(address, infos) {
	if (geocoder) {
		geocoder.getLatLng( address, function(point) {
			if (!point) {
				alert(address + " not found");
            } else {
				mapA.setCenter(point, 15);
				var marker = new GMarker(point);
				mapA.addOverlay(marker);
			  
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(infos);
				});
            }
		});
	}
}

function showBio(person){
	$('.bio').slideUp(1000);
	$('#bio_'+person).stop().slideDown(1000);
}

var step = 1;

/* scroll */
function scrollDivDown(id){
	document.getElementById(id).scrollTop+=step;
	timerDown	= setTimeout("scrollDivDown('"+id+"')",10);
}
		
function scrollDivUp(id){
	document.getElementById(id).scrollTop-=step;
	timerUp		= setTimeout("scrollDivUp('"+id+"')",10);
}

function fbs_click(u, t) {
	window.open('http://www.facebook.com/sharer.php?u='+u+'&t='+t,'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}
