$(function(){
	$('#productArwLft').click(browseLft).css({cursor:'pointer'});
	$('#productArwRgt').click(browseRgt).css({cursor:'pointer'});
});

var slideShowData = null;
var showAt = 0;

function showArtistsForCat(id) {
	showProductsByCat(id);
	
	$('#artistList').html('<p>Loading artists...</p>');

	$.get('artistlist.php', { cat: id}, function(data) {
		$('#artistList').html(data);
	});

	$.get('categorydetail.php', { id: id}, function(data) {
		$('#categoryDetail').html(data).show();;
	});

	return false;
}

function showSlide(n) {
	var ht = '';
	if(slideShowData[n].large.length)
		ht = ht + '<a href="/images/products/' + slideShowData[n].large + '" class="thickbox" >';
	ht = ht + 
				'<img class="productImg" src="/images/products/' + slideShowData[n].img + '" alt="' + slideShowData[n].name + '" />';
	if(slideShowData[n].large.length)
		ht = ht + '</a> ';
	ht = ht +
				' <div id="productDetail"> ' +
				'	<span class="productID"># ' + slideShowData[n].partnum + '</span> ' + 
				'	<span class="productInfo">' + slideShowData[n].name + '</span> ' +
				'	<span class="productPrice">';
	if(slideShowData[n].sold == 1 )
		ht = ht + 'SOLD';
	else
		ht = ht + '$ ' + slideShowData[n].price;
	ht = ht + '</span> ' +
				'</div>';
//	$('#currentProduct').html(ht).hide();
//	$('#currentProduct').fadeIn('slow');
	
	$('#priorProduct').html($('#currentProduct').html()).show();;
	$('#currentProduct').html(ht).css({opacity:0});	
	$('#currentProduct').animate({opacity:1},300);
	$('#priorProduct').animate({opacity:1},500);
	tb_init('a.thickbox');
	
}

function loadSlideShow(data) {
	var ht = "";
	$('#noproducts').hide();
	$('#products').hide();
	$('#productThumbs').html('');
	

	slideShowData = data;
	showAt = 0;
	updateArws();
	$.each(slideShowData, function(i,prod) {
		$('#productThumbs').css({marginLeft: '600px'}).append('<a href="#" onclick="showSlide(' + i + '); return false;"; ><img class="prodThumb" src="/images/products/' + prod.thumb + '" alt="' + prod.name + '" /></a>');
		//alert(prod.id);
	});
	if(data.length) {
		$('#products').show();
		showSlide(0);
	}	else {
		$('#noproducts').hide();
	}
	
	var thumbs = $('#productThumbs a');
	thumbs.css({opacity:'0.8'});
	
	thumbs.hover(function(){ $(this).stop(true,true).animate({opacity:'1'},100); }, function(){ $(this).stop(true,true).animate({opacity:'0.8'},400); });
	
	$('#productThumbs').animate({marginLeft: '0px'},1500);
	$('.prodThumb').click(function(){ $('.prodThumb').css({borderColor:'#903516'}); $(this).css({borderColor:'white'}); });
	
}

function updateArws() {
	if ((showAt + 1) * 5 < slideShowData.length) {
		$('#productArwLft').css({backgroundImage:'url(/images/productArwLft.gif)'});
	} else {
		$('#productArwLft').css({backgroundImage:'none'});
	}
	
	if ((showAt) > 0) {
		$('#productArwRgt').css({backgroundImage:'url(/images/productArwRgt.gif)'});
	} else {
		$('#productArwRgt').css({backgroundImage:'none'});
	}
	
}

function browseLft() {
	if ((showAt + 1) * 5 < slideShowData.length){
		$('#productThumbs').animate({marginLeft: '-=506px'},800);
		showAt++;
	}
	updateArws();	
}

function browseRgt() {
	if (showAt > 0){
		$('#productThumbs').animate({marginLeft: '+=506px'},800);
		showAt--;
	}
	updateArws();	
}


function showProductsByPrice(f,t) {
	$('#artistDetail').hide();
	$.getJSON('productlist.php', {from : f, to : t}, loadSlideShow);
}

function showProductsByCat(c) {
	$('#artistDetail').hide();
	$.getJSON('productlist.php', {cat : c}, loadSlideShow);
}


function showArtistDetail(id) {
	$('#categoryDetail').hide();
	$('#artistDetail').html('<p>Loading details...</p>');
	
	$.getJSON('productlist.php', {id : id}, loadSlideShow);

	$.get('artistdetail.php', { id: id}, function(data) {
		//alert('loaded:' + data);
		$('#artistDetail').html(data).hide();
		$('#artistDetail').slideDown('slow');
	});
	return false;
}
