// JavaScript Document

var theTimer2, theTimer, scrollTimer, contentHeight, scrollStop, currentMenu;
var thumbsOpen = 0;
var playerState = true;
var player;

// AJAX INIT STUFF -- START
var xmlhttp=null;

try
{
	xmlhttp = new XMLHttpRequest();
}
catch (error)
{
	try
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (error)
	{
		xmlhttp = null;
	}
}
//  END

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function slideThumbs(aTag){
	var slideTo = (aTag.id - 1) * -602;
	var slideTo = slideTo + 'px';  //alert(slideTo);
	$("div.thumbs").animate( { left:slideTo }, 1000 );
	
	return false; }

function changeBack(whichMenu){ //console.log(whichMenu);
	theTimer = setTimeout("hideMenu('"+whichMenu+"')", 800);
} 

function showMenu(whichMenu){
	clearTimeout(theTimer);
	$('#' + whichMenu).slideDown('fast');
}

function hideMenu(whichMenu){		
	$('#' + whichMenu).slideUp('fast');
}

function dropNav(){
	$("div.navigation > ul > li").hover(
		function () { //alert('fire');
			if($(this).find('ul').attr('id') != currentMenu) $('#' + currentMenu).slideUp('fast');
			
			currentMenu = $(this).find('ul').attr('id'); //console.log(currentMenu);
			showMenu(currentMenu);
		}, 
		function () {
			changeBack(currentMenu);
		}
	);
	$("div.navigation ul li ul").hover(
		function () {
			clearTimeout(theTimer);
		}, 
		function () {
			changeBack(currentMenu);
		}
	);
}

function checkScrollHeight(contentHeight, containerHeight){
	if(contentHeight < containerHeight){
		$('#scrollarrows').css('display','none');
	}
}

function scrollCell2(){
	contentHeight = $('#scrollcell').height(); //alert(contentHeight);
	containerHeight = $('#scrollcontainer').height(); //alert(contentHeight);
	checkScrollHeight(contentHeight, containerHeight);
	scrollStop = contentHeight * -1 + 60; //alert(Math.abs(contentHeight) * 15);
	
	$("a.scrollup2").mousedown(function(){
		$('#scrollcell').animate({ top: "0" }, Math.abs(contentHeight) * 10);
	}).mouseup(function(){
		$('#scrollcell').stop();
	});
	
	$("a.scrolldown2").mousedown(function(){
		$('#scrollcell').animate({ top: scrollStop }, Math.abs(contentHeight) * 10);
	}).mouseup(function(){
		$('#scrollcell').stop();
	});
}

function scrollCell(){
	contentHeight = $('#scrollcell').height();
	scrollStop = contentHeight * -1;
	
	$("a.scrollup").mousedown(function(){
		$('#scrollcell').animate({ top: "0" }, Math.abs(contentHeight) * 4);
	}).mouseup(function(){
		$('#scrollcell').stop();
	});
	
	$("a.scrolldown").mousedown(function(){
		$('#scrollcell').animate({ top: scrollStop }, Math.abs(contentHeight) * 4);
	}).mouseup(function(){
		$('#scrollcell').stop();
	});
}

function moveScroll(action){
	var scrollCellTop = parseInt($('#scrollcell').css('top')); //alert(s
	if(action == 'up'){
		if(scrollCellTop >= scrollStop) $('#scrollcell').css('top', (scrollCellTop - 8) + 'px');
	}else{
		if(scrollCellTop < 0){
			$('#scrollcell').css('top', (scrollCellTop + 8) + 'px');
		}else{
			$('#scrollcell').css('top', '0px');
		}
	}
}

function moveScroll2(action){
	var scrollCellTop = parseInt($('#scrollcell').css('top')); //alert(s
	if(action == 'up'){
		if(scrollCellTop >= scrollStop) $('#scrollcell').css('top', (scrollCellTop - 4) + 'px');
	}else{
		if(scrollCellTop < 0){
			$('#scrollcell').css('top', (scrollCellTop + 4) + 'px');
		}else{
			$('#scrollcell').css('top', '0px');
		}
	}
}

function getArt(artID){
	xmlhttp.open("GET", "/ajaxHandler.php?action=getArt&artID=" + artID);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('getArt').innerHTML = xmlhttp.responseText;
			$("a.imgbox").imgbox({'alignment':'center'});
		}
	}
	xmlhttp.send(null);
	return false; }
	
function joinNewsletter(){
	var email  = document.getElementById('gb_email').value;
	xmlhttp.open("GET", "/ajaxHandler.php?action=newsletter&email=" + email);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			alert('Thank you for joining our newsletter');
			//alert(xmlhttp.responseText);
		}
	}
	xmlhttp.send(null);
	return false; }

function showEvents(whichEvent){
	if(whichEvent == 'upcoming-events'){
		$('#eventTitle').html('Upcoming Events');
		$('#past-events').fadeOut('medium', function(){
			$('#upcoming-events').fadeIn('medium');
		});
	}else{
		$('#eventTitle').html('Past Events');
		$('#upcoming-events').fadeOut('medium', function(){
			$('#past-events').fadeIn('medium');
		});
	}
	
	return false; }

function setDefault(input, defaultValue){
	var inputValue = trim(input.value);
	if(inputValue == ''){
		input.value = defaultValue;
		input.style.color = '#999999';
		input.style.fontStyle = 'italic';
	}
}

function clearInput(input){ //alert(input.value);
	var value = input.value;
	switch(value){
		case 'Enter Email Address':
			input.value = '';
			input.style.color = 'black';
			input.style.fontStyle = 'normal';
			break;
	}
}

function errorCheckForm(){
	var passedCheck = true;
	var email = document.getElementById('email').value;

	if(email == '' || email == 'Email Address'){
		document.getElementById('email').style.color = 'red';
		passedCheck = false;
	}	
	
	return passedCheck; }


function checkForm(){
	var passedCheck = true;
	
	if(document.getElementById('name').value == ''){
		passedCheck = false;
		document.getElementById('nameField').style.color = 'red';
		document.getElementById('nameField').style.fontStyle = 'italic';
		document.getElementById('nameField').style.fontWeight = 'bold';
	}else{
		document.getElementById('nameField').style.color = 'black';
		document.getElementById('nameField').style.fontStyle = 'normal';
		document.getElementById('nameField').style.fontWeight = 'normal';
	}
	
	if(document.getElementById('email_address').value == ''){
		passedCheck = false;
		document.getElementById('emailField').style.color = 'red';
		document.getElementById('emailField').style.fontStyle = 'italic';
		document.getElementById('emailField').style.fontWeight = 'bold';
	}else{
		document.getElementById('emailField').style.color = 'black';
		document.getElementById('emailField').style.fontStyle = 'normal';
		document.getElementById('emailField').style.fontWeight = 'normal';
	}
	
	return passedCheck; }

function sortMe(sortType){
	document.getElementById('sorter').value = sortType;
	document.forms[0].submit();
}

function galleryMenu(){
	$('div.thumbs h3:eq(0)').find('span').html('&raquo; ');
	$('div.thumbs div:eq('+thumbsOpen+')').slideDown('fast');
	
	$('div.thumbs h3').click( function(){
		$('div.thumbs h3').find('span').html('');
		$(this).find('span').html('&raquo; '); //alert();
		var newOpen = $('div.thumbs h3').index(this);
		$('div.thumbs div:eq('+thumbsOpen+')').slideUp('fast', function (){
			$('div.thumbs div:eq('+newOpen+')').slideDown('medium');
			thumbsOpen  = newOpen;
		});
	});
	
	$('div.thumbs a').click( function (){ //alert(this);
		var options = {};
		var timestamp = Number(new Date());
		
		var galleryID = $(this).attr('id');
		var ID = galleryID.split('-');
		
		switch(ID[0]){
			case 'photo':
				var ajaxURL = '/ajaxHandler.php?action=getPhoto&photoID=' + ID[1] + '&time=' + timestamp;
				break;
			case 'gallery':
				var ajaxURL = '/ajaxHandler.php?action=galleryArt&galleryID=' + ID[1] + '&time=' + timestamp;
				break;
		} //alert(ajaxURL);
		
		//$('div.artdiv img').hide('drop', options, 400, function (){
		$('div.art').fadeOut(700, function (){
			$.ajax({
				url: ajaxURL,
				type: 'GET',
				dataType: 'html',
				timeout: 1000,
				error: function(){
						//alert('Error loading page document');
				},
				success: function(html){ //alert(html);
					$('div.art').html(html);
					
				},
				complete: function(){
					//$('div.artdiv img').show('drop', options, 900);
					$('div.art').fadeIn(600, function (){
						$('div.artdiv img').fadeIn(700);
					});
				}
			});																								 
		});
		
		return false;
	});
}

function playStop() {
	player = document.getElementById('mpl');
	player.sendEvent("play");
	
	if(playerState){
		$('#sound').html('off');
		playerState = false;
	}else{
		$('#sound').html('on');
		playerState = true;
	}

	return false; }

$(document).ready(function() {
	galleryMenu();
	dropNav();
	
	$('#store-entries').jScrollPane({showArrows:true, scrollbarWidth: 12});
	$('div.events').jScrollPane({showArrows:true, scrollbarWidth: 12});
	$('div.generic').jScrollPane({showArrows:true, scrollbarWidth: 12});
	$('div.bio-text').jScrollPane({showArrows:true, scrollbarWidth: 12});
	$('div.press').jScrollPane({showArrows:true, scrollbarWidth: 12});
	
	$("a.press").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic'
	});
	$(".enlarge").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic'
	});
	$(".info-links").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic'
	});
	$(".fullnews").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic'
	});

	/*$("a.imgbox").imgbox({'alignment':'center'});
	scrollCell();
	scrollCell2();
	shopCatalog();
	shortSite();*/
});
