var gallerySlideWidth=932;
var gallerySlideMargin=14;
var gallerySlideIndexCur=0;
var gallerySlideIndexMax=0;
$(document).ready(function(){
	setupFeatureActions();
	setupGallerySlides();
	$(".more-button").click(function(){
		var numMore=String($(this).attr('href')).substr(1);
		loadMoreThumbs(numMore);
		return false;
	});
});
function setupFeatureActions(){
	//load feature
	$("#portfolio-thumbs").delegate(".thumb a", "click", function(){
		var id=String($(this).attr('class')).substr(5);
		loadFeature(id);
		return false;
	});
	//close feature
	$("#portfolio-feature").delegate(".feature-close", "click", function(){
		$("#portfolio-feature").fadeOut('slow');
	});
	//collapse feature details
	$("#portfolio-feature").delegate(".summary-collapse", "click", function(){
		if($("#portfolio-feature .feature-details").hasClass('feature-details-collapsed')){
			$("#portfolio-feature .feature-details").removeClass('feature-details-collapsed');
		}else{
			$("#portfolio-feature .feature-details").addClass('feature-details-collapsed');
		}
	});
	//change purchase option
	$("#portfolio-feature").delegate(".item-options-list", "change", function(){
		var siid=$(".purchase-options .item-options-list").val();
		var soldout=String($(".item-options-list option:selected").text()).indexOf('(S/O)');
		$(".order-description").hide();
		$("#order-description-"+siid).show();
		$(".price-label").hide();
		$("#price-label-"+siid).show();
		if(soldout>=0){
			$(".purchase-options .purchase-button").hide();
		}else{
			$(".purchase-options .purchase-button").show();
		}
	});
	//add to cart
	$("#portfolio-feature").delegate(".purchase-button", "click", function(){
		var siid=$(".purchase-options .item-options-list").val();
		var url=$(this).attr('href');
		$("#portfolio-feature .purchase-button").hide();
		$.getJSON(url, {id:siid, form_action:"item_add", ajax:1}, function(data) {
			$("#portfolio-feature .purchase-button").show();
			if(data.success){
				$("#cart-item-count").html(data.items);
				$("#portfolio-feature .cart-added").fadeIn('slow',function(){
					$("#portfolio-feature .cart-added").fadeOut('slow');
				});
			}
		});
		return false;
	});
}
function setupGallerySlides(){
	$("#portfolio-feature").delegate(".paginate-left", "click", function(){
		viewGallerySlide(-1);
	});
	$("#portfolio-feature").delegate(".paginate-right", "click", function(){
		viewGallerySlide(1);
	});
}
function gallerySlidesInit() {
	gallerySlideIndexCur=0;
	gallerySlideIndexMax=0;
	createGallerySlides(1);
}
function createGallerySlides(dir) {
	var numSlides=$(".slide-pool img").length;
	var addSlides=$(".slide-pool").html();
	if(dir>0){
		$(".feature-slides").append(addSlides);
	}else{
		$(".feature-slides").prepend(addSlides);
		gallerySlideIndexCur=gallerySlideIndexCur+numSlides
		shiftGallerySlides();
	}
	gallerySlideIndexMax=gallerySlideIndexMax+numSlides;
	extendGallerySlideContainer();
}
function extendGallerySlideContainer() {
	var newWidth=gallerySlideIndexMax*(gallerySlideWidth+gallerySlideMargin);
	$(".feature-slides").css('width',newWidth+'px');
}
function viewGallerySlide(dir){
	//create new slides if needed
	if(dir>0){
		if(gallerySlideIndexCur>=gallerySlideIndexMax-1){
			createGallerySlides(dir)
		}
		gallerySlideIndexCur++;
	}else{
		if(gallerySlideIndexCur==0){
			createGallerySlides(dir)
		}
		gallerySlideIndexCur--;
	}
	//get position and animate
	var posX=getSlidePositionByIndex(gallerySlideIndexCur);
	gallerySlideTransition(posX);
}
function shiftGallerySlides() {
	//shift slides for to offset for new left-inserted slides
	var curPosXObj=$("#portfolio-feature .feature-slides").position();
	var curPosX=curPosXObj.left;
	var allSlidesWidth=$(".slide-pool img").length*(gallerySlideWidth+gallerySlideMargin)
	curPosX=curPosX-allSlidesWidth;
	$("#portfolio-feature .feature-slides").css('left',curPosX+'px');
}
function getSlidePositionByIndex(i) {
	var posX=0-(i*(gallerySlideWidth+gallerySlideMargin));
	return posX;
}
function gallerySlideTransition(posX) {
	var timeSlide=250;
	$("#portfolio-feature .feature-slides").animate({
		left: posX+'px'
	},
	{
		duration:timeSlide,
		queue:false,
		complete:function() {
		}
	});
}
function loadFeature(id){
	//remove content
	$("#portfolio-feature").html('');
	//get data
	$.get('/work/fragments/feature.php',{ajax:true,pid:id},function(data){
		if(data!='error'){
			setFeature(data);
			showFeature();
		}
	});
	$("#portfolio-feature").fadeIn('slow');
}
function setFeature(data){
	$("#portfolio-feature").html(data);
}
function showFeature(){
	if($(".slide-pool img").length>1){
		$("#portfolio-feature .feature-details .feature-pagination").fadeIn('slow');
	}
	$("#portfolio-feature .feature-summary").fadeIn('slow');
	gallerySlidesInit()
}
function loadMoreThumbs(num){
	var entryItemSelector=".portfolio-entry-thumb";
	var entryContainer=$("#portfolio-thumbs");
	//get most recent id
	var displayOffset=String($(entryItemSelector).length);
	//get tag
	var tag=String($(".more-button").attr('id')).substr(15);
	//hide button
	$(".more-button").hide();
	//show loading
	$(".more-button").after('<div class="more-loading"></div>');
	//get data
	$.get('/work/fragments/thumbnails.php',{ajax:true,count:num,displayOffset:displayOffset,tag:tag},function(data){
		if(data=='empty'){
			$(".more-button").hide();
			$(".more-loading").remove();
		}else if(data!='error'){
			$(".more-loading").remove();
			//entryContainer.append(data);
			$("#portfolio-thumb-last").before(data);
			$(".more-button").show();
		}
	});
}
