/*******************************************************************
*
*	Creative + Communications Portfolio
*	
*	The individual portfolio piece	
*	
*	Andrew Karpenko
*	206-221-4564
*	akarpy@u.washington.edu	
*
********************************************************************/

function Piece(id) {
	/* 
	 *	Setup the main variables
	 */
	this.ident 			= "piece";
	//our own index, and total pieces created
	if ( typeof Piece.total == 'undefined' ) {
        this.index = Piece.total = 0;
    }else{
    	this.index = ++Piece.total;
    }
    //the id of the HTML element representing this object
	this.htmlID = (id != "" ? id : "portfolio_piece_"+this.index);
	//the amount of pixels we need to offset from the left of the container
	this.offsetLeft		= 0;
	
	this.currentState  	= {
		slide		: 0, 		//the current slide
		active		: false		//is it active?
	}
	
	/*
	 *	An array of pointers to the slide objects that are part of 
	 *	this piece.
	 */
	this.slides			= [];
	// a pointer to the currently displayed slide
	this.slide			= "";
	
	//pointer to the jquery image for this piece
	this.img			= "";
	
	/*
	 *	Sets the slide at the specified index
	 *	Passing an index equal to the number of slides will append the slide
	 *	Returns the passed index if successful
	 *	Returns -1 if the specified index is out of bounds
	 *	Returns -2 if the passed object is not a slide
	 */
	this.setSlideAtIndex = function(slide, index){
		var p = this;
		if(slide==null || slide.ident==null || slide.ident!="slide"){
			return -2;
		}
		if(index<0 || index>p.slides.length){
			return -1;
		}
		if(index==p.slides.length){
			p.slides.push(slide);
		}else{
			p.slides[index] = slide;
		}
		//setup the first slide
		if(p.slide==""){
			p.slide = slide;
		}
		
		return index;
	}	
	//appends the slide at the end
	this.addSlide = function(slide) {
		var p = this;
		return p.setSlideAtIndex(slide, p.slides.length);
	}

	/*
	 *	Sets the current active slide to display
	 *
	 */
	this.setCurrentSlide = function(to) {
		var p = this;
		
		p.currentState.slide = to;
		p.slide = p.slides[to];
		p.setActive(true);
	}
	
	/*
	 *	Sets the active state of the piece, as well as changes the display of 
	 *		the slides that are part of this piece
	 */
	this.setActive = function(to) {
		var p = this;
		
		if(to){
			$("#"+p.htmlID).stop(true,false).fadeIn("slow");
			/*$("#"+p.htmlID+" > .seemore").fadeIn("slow");*/
			$("#"+p.htmlID+" > .portfolio_slide:lt("+p.currentState.slide+")").add("#"+p.htmlID+" > .portfolio_slide:gt("+p.currentState.slide+")").fadeOut("slow");
			p.slides[p.currentState.slide].self.stop(true,false).fadeIn("slow");
		}else{
			$("#"+p.htmlID).fadeOut("slow");
			/*$("#"+p.htmlID+" > .seemore").fadeOut("slow");*/
			p.slides[p.currentState.slide].self.fadeOut("slow");
		}
		p.currentState.active = to;
		
		p.slide = p.slides[p.currentState.slide];
				
		return p.currentState.active;
	}
	
}
