// JavaScript Document

var theTimer2, theTimer, scrollTimer, contentHeight, scrollStop;
var photoThumbsTimer, photoThumbsTimerOn;

// 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 showMenu(div){
	clearTimeout ( theTimer );
	$(div).show('fast');
}

function showMenu2(div){
	clearTimeout ( theTimer2 );
	$(div).slideDown('fast');
}

function doSlideUp(div){
	$(div).hide('fast');
}

function hideMenu(div){ //alert(div);
	theTimer = setTimeout( "doSlideUp('"+ div +"')", 500 );
}

function hideMenu2(div){ //alert(div);
	theTimer2 = setTimeout( "doSlideUp('"+ div +"')", 500 );
}

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 showbio(){
	$('#contentFrame').fadeOut('medium', function(){
		$('#bio').fadeIn('medium');
		$('.showartwork').css('color','#a48d72');
		$('.showbio').css('color','#701717');
		scrollCell2();
	});
}

function showartwork(){
	$('#bio').fadeOut('medium', function(){
		$('#contentFrame').fadeIn('medium');
		$('.showbio').css('color','#a48d72');
		$('.showartwork').css('color','#701717');
	});
}

function setPricingLabel(thisLabel){
	var innerHTML = $(thisLabel).find("span").html();
	$('#chosen-price').find("span:first").html(innerHTML);
	hideMenu('div.pricingDropMenu');
}

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 filterStore(whichArt){
	$('#shop div').css('display','none');
	$('#shop div.clear').css('display','block');
	
	switch(whichArt){
		case 'limiteds':
			$('div.limiteds').css('display','block');
			$('#shop-filter').html('Limiteds');
			break;
		case 'unlimiteds':
			$('div.unlimiteds').css('display','block');
			$('#shop-filter').html('Unlimiteds');
			break;
		case 'originals':
			$('div.originals').css('display','block');
			$('#shop-filter').html('Originals');
			break;
		case 'all-artwork':
			$('#shop div').css('display','block');
			$('#shop-filter').html('All Artwork');
			break;
	}
	contentHeight = $('#scrollcell').height(); //alert(contentHeight);
	scrollStop = contentHeight * -1; //alert(scrollStop);
	
	$('#scrollcell').css('top','0px');
}

function shopCatalog(){
	var page = location.href;
	var pageParts = page.split('#'); //alert(pageParts);
	if(pageParts[1] == 'limiteds') filterStore('limiteds');
	if(pageParts[1] == 'unlimiteds') filterStore('unlimiteds');
	if(pageParts[1] == 'originals') filterStore('originals');
}

function shortSite(){
	var wHeight = $(window).height(); //alert(wHeight);
	if(wHeight < 620){
		$('#sidebar').css({'padding-top':'346px','background-position':'18px 70px'});
		$('#contentArea').css({'padding-top':'63px'});
	}
}

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 joinNewsletter(){
	if(errorCheckForm()){
		var email = document.getElementById('email').value;
		
		xmlhttp.open("GET", "/ajaxHandler.php?action=joinNews&email=" + email );
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				response = xmlhttp.responseText;
				document.getElementById('joinNewsletter').innerHTML = '<p style="text-align:center;"><em><strong>Thank you for joining our newsletter</strong></em></p>';
			}
		}
		xmlhttp.send(null);
	}
}

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 sortShop(sortType){ //alert(window.location.hash);
	switch(sortType){
		case 'title':
			window.location = '/shop.html' + window.location.hash;
			break;
		case 'lowest':
			window.location = '/shop-lowest.html' + window.location.hash;
			break;
		case 'highest':
			window.location = '/shop-highest.html' + window.location.hash;
			break;
	}
}

function photos(){
	$("div.thumbs div.container a").click(function () {
		var photoID = $(this).attr('id');
		$('div.photo').html('<div class="loading"><img src="/images/common/loading.gif" /></div>');
		getPhoto(photoID);
		return false;
	});
}

function getPhoto(photoID){
	xmlhttp.open("GET", "/ajaxHandler.php?action=getPhoto&photoID=" + photoID);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var response = xmlhttp.responseText; //alert(response);
			$('div.photo').html(response);
			
			$("a.imgbox").imgbox({'alignment':'center', 'overlayShow':'true','speedIn':'500'});
		}
	}
	xmlhttp.send(null);
	return false; }

function scrollAlbums(){
	
	$("a.albums-up").click(function () {
		$('div.albums div.container div.scroll').animate({
			 top: '+=225'
		  }, 500, function (){
				$("div.upDown a.albums-down").css('visibility','visible');
				
				if($('div.albums div.container div.scroll').css('top') == '0px'){
					$("div.upDown a.albums-up").css('visibility','hidden');
				}
		  });
		return false;
	});
	
	$("a.albums-down").click(function (){
		$('div.albums div.container div.scroll').animate({
			 top: '-=225'
		  }, 500, function (){
			  var top = parseInt($('div.albums div.container div.scroll').css('top'));
			  top = (top * -1) + 225;
				$("div.upDown a.albums-up").css('visibility','visible');
				
				if(top > $('div.albums div.container div.scroll').height()){
					$("div.upDown a.albums-down").css('visibility','hidden');
				}
		  });
		return false;
	});
}

function scrollPhotos(){
	$('div.thumbs').mousemove(function(e){
		var div = $("div.thumbs");
		var offset = div.offset();

		var y = e.pageY - offset.top;
		//$('div.caption').html("X Axis : " + e.pageX + " | Y Axis " + y);
		
		if(y < 125){
			if(photoThumbsTimerOn != 'up'){
				photoThumbsTimerOn = 'up';
			}
			moveThumbs(true);
		}else if( y >= 125 && y <= 300){
			photoThumbsTimerOn = 'off';
			$('div.thumbs div.container').stop();
			$('div.caption').html(photoThumbsTimerOn);
			clearTimeout(photoThumbsTimer);
		}else{
			if(photoThumbsTimerOn != 'down'){
				photoThumbsTimerOn = 'down';
				moveThumbs(false);
			}
		}
	});
	
	$('div.thumbs').mouseout(function(){ //alert('stop');
		photoThumbsTimerOn = 'off';
		clearTimeout(photoThumbsTimer);
		$('div.thumbs div.container').stop();
	});
}

function moveThumbs(upDown){
	var top = parseInt($('div.thumbs div.container').css('top'));
	if(upDown){  // Move up
		$('div.thumbs div.container').animate({
			top: '-=50px'
		}, 500);
		photoThumbsTimer = setTimeout('moveThumbs(true)', 500);
	}else{	//Move down
		$('div.thumbs div.container').animate({
			top: '+=50px'
		}, 500, function (){
			moveThumbs(false);
		});
	}
	$('div.caption').html(photoThumbsTimerOn);
}

function h1Click(){
	$("h1").click(function () { 
		window.location = "/";
	});
}

function scrollArtists(){
	
	if(420 < $('div.artists-scroll div.container').height()){
		$("div.upDown a.artists-down").css('visibility','visible');
	}
	
	$("a.artists-up").click(function () {
		$('div.artists-scroll div.container').animate({
			 top: '+=420'
		  }, 500, function (){
				$("div.upDown a.artists-down").css('visibility','visible');
				
				if($('div.artists-scroll div.container').css('top') == '0px'){
					$("div.upDown a.artists-up").css('visibility','hidden');
				}
		  });
		return false;
	});
	
	$("a.artists-down").click(function (){
		$('div.artists-scroll div.container').animate({
			 top: '-=420'
		  }, 500, function (){
			  var top = parseInt($('div.artists-scroll div.container').css('top'));
			  top = (top * -1) + 420;
				$("div.upDown a.artists-up").css('visibility','visible');
				
				if(top > $('div.artists-scroll div.container').height()){
					$("div.upDown a.artists-down").css('visibility','hidden');
				}
		  });
		return false;
	});
}

function getPress(pressID){ //alert(pressID)
	$('div.container a').each( function (){
		$(this).removeClass('on');
	});
	
	$('#pressID-' + pressID).addClass('on');
	
	xmlhttp.open("GET", "/ajaxHandler.php?action=getPress&pressID=" + pressID);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var artHTML = xmlhttp.responseText; //alert(artHTML);
			document.getElementById('press-image').innerHTML = artHTML;
					
			var $pancontainer2=$('div.pancontainer2')
			$pancontainer2.each(function(){
				var $this=$(this).css({position:'relative', overflow:'hidden', cursor:'move'})
				var $img=$this.find('img:eq(0)') //image to pan
				var options={$pancontainer:$this, pos:$this.attr('data-orient'), curzoom:1, canzoom:$this.attr('data-canzoom'), wrappersize:[$this.width(), $this.height()]}
				$img.imgmover(options)
			});
		}
	}
	xmlhttp.send(null);
	return false; }

function scrollEvents(){ //alert($('div.events-scroll div.container').height());
	if(465 > $('div.events-scroll div.container').height()){
		$("div.upDown a.events-up").css('visibility','hidden');
		$("div.upDown a.events-down").css('visibility','hidden');
	}else{
		if(420 < $('div.events-scroll div.container').height()){
			$("div.upDown a.events-down").css('visibility','visible');
		}
	}
	
	$("a.events-up").click(function () { //alert('fire');
		$('div.events-scroll div.container').animate({
			 top: '+=285'
		  }, 500, function (){
				$("div.upDown a.events-down").css('visibility','visible');
				
				if($('div.events-scroll div.container').css('top') == '0px'){
					$("div.upDown a.events-up").css('visibility','hidden');
				}
		  });
		return false;
	});
	
	$("a.events-down").click(function (){
		$('div.events-scroll div.container').animate({
			 top: '-=285'
		  }, 500, function (){
			  var top = parseInt($('div.events-scroll div.container').css('top'));
			  top = (top * -1) + 285;
				$("div.upDown a.events-up").css('visibility','visible');
				
				if(top > $('div.events-scroll div.container').height()){
					$("div.upDown a.events-down").css('visibility','hidden');
				}
		  });
		return false;
	});
	
	$("#remember a").click(function (){ 
		document.cookie = 'scrollPosition=' + $('div.events-scroll div.container').css('top') + '|' + $('#doc-path').val();	
	});
}

function getArtistArt(artID){
	xmlhttp.open("GET", "/ajaxHandler.php?action=getArtistArt&artID=" + artID);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var artHTML = xmlhttp.responseText; //alert(artHTML);
			document.getElementById('art-div').innerHTML = artHTML;
			$("a.imgbox").imgbox({'alignment':'center', 'overlayShow':'true','speedIn':'500'});
		}
	}
	xmlhttp.send(null);
	return false; }

function getShopArt(artID){
	xmlhttp.open("GET", "/ajaxHandler.php?action=getShopArt&artID=" + artID);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var artHTML = xmlhttp.responseText; //alert(artHTML);
			document.getElementById('shop-main').innerHTML = artHTML;
			$("a.imgbox").imgbox({'alignment':'center', 'overlayShow':'true','speedIn':'500'});
			$('html, body').animate({
				scrollTop: $("div.banner").offset().top
			}, 650);

		}
	}
	xmlhttp.send(null);
	return false; }

$(document).ready(function() {
	h1Click();
	photos();
	scrollAlbums();
	scrollArtists();
	scrollEvents();
	
	$("a.imgbox").imgbox({'alignment':'center', 'overlayShow':'true','speedIn':'500'});
	
	//artistsDD();
});
