// JavaScript Document

var theTimer2, theTimer, scrollTimer, contentHeight, scrollStop;

// 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).slideDown('fast');
}

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

function doSlideUp(div){
	$(div).slideUp('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() {
	$("div#makeMeScrollable").smoothDivScroll({scrollingSpeed: 12, mouseDownSpeedBooster: 3, startAtElementId: "startAtMe"});

	$("a.thumbnail").hover(
      function () {
				var imgSrc = $(this).find('img').attr('src');
				$(this).append($('<span><img src="'+ imgSrc +'" width="103" height="103" /></span>'));
		}, 
		function () {
			$(this).find("span:last").remove();
		}
	);
});

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;
	}
}

$(document).ready(function() {
	$("a.imgbox").imgbox({'alignment':'center'});
	scrollCell();
	scrollCell2();
	shopCatalog();
	shortSite();
});