var zI=2;
function readyThread(){
	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){
					alignThread();
					$('#threadIndex img').css({visibility:'visible'});
				}
			}
		}).each(function(i, val){
			if(this.complete||this.complete===undefined){ 
				var holder=this.src;
				this.src='';
				this.src=holder;
			}
		});
	});
}
function alignThread(){
	var space=$('#threadIndex').height()-56;
	var group=$('#threadIndex > a,#threadIndex > img,#threadNew');
	$(group).each(function(){
		space-=$(this).height();
	});
	$(group).each(function(){
		$(this).css({marginBottom:(space/(group.length)<=40)?space/(group.length):40});
	});
}
$(document).ready(function(){
	readyThread();
	$('.threadArticle').each(function(){
		$(this).css({opacity:0,visibility:'visible'});
	}).eq(0).css({opacity:1});
	$('#threadIndex > a').bind('mouseover',function(){
		if(!$(this).hasClass('front')&&$('#threadIndex:animated').length==0){
			$('.center_singleColumn *:animated').stop(true);
			var n=$('#threadIndex > a').index(this);
			var article=$('.threadArticle').eq(n);
			$('#threadIndex > a').removeClass('front').eq(n).addClass('front');
			$('#threadIndex').css({zIndex:zI+1});
			try{$('.threadCopy', article)[0].scrollTo(0);}catch(e){}
			$(article).css({zIndex:zI}).animate({opacity:1},500,function(){
				$('.threadArticle').filter(function(){
					return $('.threadArticle').index(this)!=$('#threadIndex > a').index($('#threadIndex > a.front'));
				}).css({opacity:0});
			});zI++;
		}
	}).bind('click',function(e){
		if($('#threadIndex:animated').length==0){
			$(this).trigger('mouseover');
			e.preventDefault();
			
			//cmCreatePageviewTag(The Thread > {Story Title}, null,0~2377475~6011270);
			cmCreatePageviewTag('The Thread > '+$(this).attr('name'), null, '0~2377475~6011270');
			
			var n=$('#threadIndex > a').index(this);
			$('#threadIndex').animate({top:567},750);
			$('.threadContent').eq(n).children().css({position:'relative',top:-567}).animate({top:0},750);
		}
	}).eq(0).trigger('mouseover');
	$('#threadNew > a').bind('mouseover',function(){
		$(this).addClass('active');
	}).bind('mouseout',function(){
		$(this).removeClass('active');
	});
	$('.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>').each(function(){
		$('p a:first', this).bind('click',function(e){
			e.preventDefault();
			var n=$('#threadIndex > a').index($('#threadIndex > a.front'));
			$('#threadIndex').animate({top:0},750);
			$('.threadContent').eq(n).children().animate({top:-567},750);
		});
	});
	$('#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());
			}
		}
	});
});