var theThread={
	loadedStory:-1,zI:2,
	enable:function(){
		this.build().bind().loadImages();
	},
	loadImages:function(){
		var $imageToLoad=$('#threadIndex img');
		var loadedImage=[];
		$imageToLoad.each(function(i,val){
			$(this).bind('load onreadystatechange',function(){
				if($.inArray(i, loadedImage)==-1){
					loadedImage.push(val);
					$imageToLoad=$.grep($imageToLoad,function(n,i){return n!=val;});
					if($imageToLoad.length<=0){
						theThread.spaceEqually().loadStory(0);
					}
				}
			}).each(function(){
				if(this.complete||this.complete===undefined){ 
					var holder=this.src;
					this.src='';
					this.src=holder;
				}
			});
		});
		return this;
	},
	build:function(){
		$('.threadArticle').css({opacity:0,visibility:'visible'});
		$('.threadCopy')
		.jScrollPane({
			scrollbarWidth:15,
			dragBlockHeight:15,
			reinitialiseOnImageLoad:true
		});
		$('.threadLinks')
		.before('<img src="'+getImageURL()+'store/boutique/the_thread/_constant/storyend.gif" style="display:block; margin-top:5px;" alt="" />')
		.append('<p><a href="javascript:void(0)" name="Back To All Stories">BACK TO ALL STORIES</a> &#160;&#183;&#160; <a href="javascript:O_LC(\'thethread.nordstrom.com\')" name="Feedback">FEEDBACK</a></p>');
		$('#threadIndex a,.threadArticle a')
		.each(function(){
			if($(this).attr('name')===undefined||$(this).attr('name').length<=0){
				if($('> img',this).length>0){
					$(this).attr('name',$('> img',this)[0].alt);
				}else{
					$(this).attr('name',$(this).text());
				}
			}
		});
		return this;
	},
	bind:function(){
		$('#threadIndex > a')
		.bind('mouseover',function(){
			theThread.loadStory($('#threadIndex > a').index(this));
		})
		.bind('click',function(){
			theThread.selectStory($('#threadIndex > a').index(this),false);
		});
		$('#threadNew > a')
		.bind('mouseover',function(){
			$(this).addClass('active');
		})
		.bind('mouseout',function(){
			$(this).removeClass('active');
		});
		$('.threadLinks p a:even')
		.bind('click',function(){
			theThread.selectStory(-1,false);
		});
		return this;
	},
	spaceEqually:function(){
		var space=$('#threadIndex').height()-$('#threadIndex p').outerHeight();
		var group=$('#threadIndex > a,#threadIndex > img,#threadNew');
		$(group).each(function(){
			space-=$(this).height();
		});
		var margin=(space/(group.length)<=40)?space/(group.length):40;
		$(group).each(function(){
			$(this).css({marginBottom:margin});
		});
		$('#threadIndex img').css({visibility:'visible'});
		return this;
	},
	playFlash:function(){
		try{
			$('#'+$('.threadImg').eq(this.loadedStory).find('object').attr('id'))[0].nordPlay();
		}catch(error){
			
		}
		return this;
	},
	pauseFlash:function(){
		try{
			$('#'+$('.threadImg').eq(this.loadedStory).find('object').attr('id'))[0].nordPause();
		}catch(error){
			
		}
		return this;
	},
	stopFlash:function(){
		try{
			$('#'+$('.threadImg').eq(this.visibleStory).find('object').attr('id'))[0].nordStop();
		}catch(error){
			
		}
		return this;
	},
	loadStory:function(num){
		if(num!=this.loadedStory&&!this.isAnimated()){			
			$('#threadIndex > a').removeClass('front').eq(num).addClass('front');
			try{
				$('.threadArticle').eq(num).find('.threadCopy')[0].scrollTo(0);
			}catch(error){
				
			}
			$('#threadIndex').css({zIndex:this.zI+1});
			$('.threadArticle').eq(num).css({zIndex:this.zI})
			.animate({opacity:1},500,function(){
				$('.threadArticle').not(this).css({opacity:0});
			});
			this.zI+=1;
			this.loadedStory=num;
		}
	},
	selectStory:function(num,play){
		if(num!=this.visibleStory&&!this.isAnimated()){
			if(num==-1){
				theThread.stopFlash();
				$('#threadIndex').animate({top:'0px'},750);
				$('.threadContent').eq(theThread.loadedStory).children().animate({top:'-567px'},750);
			}else{
				this.loadStory(num);
				cmCreatePageviewTag('The Thread > '+$('#threadIndex > a').eq(num).attr('name'),null,'0~2377475~6011270');
				$('#threadIndex').animate({top:'567px'},750);
				$('.threadContent').eq(num).children().css({position:'relative',top:'-567px'}).animate({top:'0px'},750,function(){
					if(play===undefined){
						theThread.playFlash();
					}
				});
			}
			this.visibleStory=num;
		}else if(num==this.visibleStory){
			theThread.playFlash();
			theThread.playFlash();
		}
		return this;
	},
	isAnimated:function(){
		return ($('#threadIndex:animated').length!=0)?true:false;
	}
};
$(function(){
	theThread.enable();
});