function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;
    }
    return new Array(xScroll,yScroll)
}

function overlayInit() {
	//handle the artwork overlay
		$('#artwork-overlay').show();
		var overlayHeight = $('#artwork-overlay').height();
		var overlayMargin = overlayHeight * -1;
		$('#artwork-overlay').css('margin-top',overlayMargin);
		
		$('#artwork-large').mouseenter(function() {
			$('#artwork-overlay').slideUp('slow');
		});
		$('#artwork-large').mouseleave(function() {
			$('#artwork-overlay').slideDown('slow');
		});
}

$(document).ready(function() {
	//handle the header image
		$('#header').mouseenter(function() {
			$('#header').css('background-image','url(/images/header-on.jpg)');
		});
		$('#header').mouseleave(function() {
			$('#header').css('background-image','url(/images/header-off.jpg)');
		});
	
	overlayInit();

	//handle the scrolling artwork column
		$(window).scroll(function () { 
			if ($('#artwork-frame').length && !$('#artwork-frame').hasClass('noscroll')) {
				var headerPosition = $('#artwork-left').offset();
				headerPosition=headerPosition.top;
				var footerPosition = $('#footer').offset();
				footerPosition=footerPosition.top;
				var leftOffset=getPageScroll();
				leftOffset=15 + leftOffset[1];
				var frameHeight=$('#artwork-frame').height();
				bottomBound=leftOffset+frameHeight;
				if (leftOffset>headerPosition && bottomBound<footerPosition) {
            		$('#artwork-frame').css('top',leftOffset+'px');
            	}
			}
		});
});

function show_artwork(id,title) {
	if ($("#slide-"+title).is(':visible')) {
		$("#slide-"+title).slideToggle("slow");
	}
	else {
		$('#artwork-frame').slideUp('fast', function() {
			$('.artwork-description').hide();
			$("#slide-"+title).slideToggle("slow");
			$.ajax({
				type: "GET",
				url: "/includes/artwork-left.php",
				data: ({id : id}),
				dataType: "html",
				success: function(data) {
					$('#artwork-frame').html(data);
					$('#artwork-frame').slideDown('fast',function(){
						overlayInit();
						
					//check to see if the image is greater than the height of the body
					var artwork_height=$('#artwork-frame').height();
					var body_height=$('#artwork').height();
					if (artwork_height>body_height) {
						var difference=artwork_height-body_height;
						$('#artwork').height(body_height+difference+30);
					}

					});
				}
			});
		});
	}
}

function change_artwork_image(id,i) {
	if ($('#artwork-frame').hasClass('noscroll')) cancel_larger_image();
	$('#artwork-large').slideUp('fast', function() {
		$.ajax({
			type: "GET",
			url: "/includes/artwork-change.php",
			data: ({id:id,i:i}),
			dataType: "html",
			success: function(data) {
				$('#artwork-large').html(data);
				$('#artwork-large').slideDown('fast',function(){
					overlayInit();

					//check to see if the image is greater than the height of the body
					var artwork_height=$('#artwork-frame').height();
					var body_height=$('#artwork').height();
					if (artwork_height>body_height) {
						var difference=artwork_height-body_height;
						$('#artwork').height(body_height+difference+30);
					}
				});
			}
		});
	});
}

var rightcolumnheight=0;

function show_larger_image(image) {
	if ($('#artwork-frame').hasClass('noscroll')) {
		cancel_larger_image();
	}
	else {
		rightcolumnheight=$('#artwork-right').height();
		$('html, body').animate({scrollTop:'245px'}, 'fast');
		$('#artwork-frame').css('position','relative');
		$('#artwork-frame').addClass('noscroll');
		$('#artwork-frame').css('top','0px');
		$("#large-image").attr("src", image);
		$("#large-image").attr("title", "Click to close large image.");
		$('#large-image-menu').show();
		$('#artwork-right').animate({ width: '0px', height: '0px'}); 
		$('#artwork-left').animate({ width: '800px' }); 
		$('#large-image').animate({ width: '800px' }); 
		$('#artwork-overlay').animate({ width: '800px' });
		$('.artwork-overlay-text').animate({ width: '780px' }); 
	}
}

function cancel_larger_image() {
	$('#large-image-menu').hide();
	$('#artwork-frame').css('position','absolute');
	$('#artwork-frame').css('top','267px');
	$('#large-image').animate({ width: '358px' });
	$('#artwork-right').animate({ width: '420px', height: rightcolumnheight+'px' });
	$('#artwork-left').animate({ width: '370px' }); 
	$('#artwork-overlay').css('width','358px');
	$('.artwork-overlay-text').css('width','338px');
	$("#large-image").attr("title", "Click to view a larger image.");
	$('#artwork-right').css('height','auto');
	$('#artwork-frame').removeClass('noscroll');
}

var slideshowCount = 1;

function slideShow(slideCount) {
	var slideWidth = 500;
	$('#index-rotate-content').css('width',Math.round(slideWidth*(slideCount + 1)));
	var slideMargin = slideshowCount * slideWidth * -1;
	$('#index-rotate-content').animate({marginLeft:slideMargin}, 500, function(){
		if (slideshowCount==(slideCount)){
			$('#index-rotate-content').css('margin-left','0px');
			slideshowCount=1;
		}
		else slideshowCount++;
	});
}
