var autoRotate;
var curStory=-1;
var zI=1;
var rate=750;
$.fn.buildNavigation=function(){
	$('#coverNav span').before('<img src="'+getImageURL()+'store/common/transpixel.gif" class="divider" />').after('<img src="'+getImageURL()+'store/common/transpixel.gif" class="divider" />');
	return this.each(function(i){
		$('#coverNav span').append('<a href="javascript:void(0)"><img src="'+getImageURL()+'store/common/transpixel.gif" alt="Slide '+(i+1)+' Nav" /></a>').children('a:last')
		.bind('readChapter',function(){
			var n=$('#coverNav span a').index(this); curStory=n;
			$(this).addClass('sel').siblings().removeClass('sel');
			$('#coverImages > div').eq(n).trigger('readable');
		});
		if($('#coverImages > div').index(this)!=0)$(this).css({opacity:0});
		$('#coverCopy > div').not($('#coverNav')).css({visibility:'visible',opacity:0});
		$(this).css({visibility:'visible'})
		.bind('readable',function(e){
			var n=$('#coverImages > div').index(this);
			if($('#coverCopy > div').length>2){
				$('#coverCopy > div').eq(n).animate({opacity:1},rate).css({zIndex:zI});
				$('#coverImages > div').not(e.target).each(function(){
					var n=$('#coverImages > div').index(this);
					$('#coverCopy > div').eq(n).css({opacity:0});
				});
			}else{
				$('#coverCopy > div').css({opacity:1});
			}
			$(this).css({zIndex:zI}).animate({opacity:1},rate,function(){
				$('#coverImages > div').not(e.target).trigger('unreadable');
			}); zI++;
		})
		.bind('unreadable',function(e){
			$(this).animate({opacity:0},rate);
		});
	});
}
$(document).ready(function(){
	$('#coverNav').prepend('<a href="javascript:void(0)"><img src="'+getImageURL()+'store/category/designer/2009/_constant/back.gif" alt="BACK" /></a><span></span><a href="javascript:void(0)"><img src="'+getImageURL()+'store/category/designer/2009/_constant/next.gif" alt="NEXT" /></a>');
	$('#coverImages > div').buildNavigation();
	$('#coverNav > a,#coverNav > span > a')
	.filter(':first').bind('prevChpt',function(){
		if(curStory==0){
			$('#coverNav > span > a').eq($('#coverImages > div').length-1).trigger('readChapter');
		}else{
			$('#coverNav > span > a').eq(curStory-1).trigger('readChapter');
		}
	}).end().filter(':last').bind('nextChpt',function(){
		if(curStory==$('#coverImages > div').length-1){
			$('#coverNav > span > a').eq(0).trigger('readChapter');
		}else{
			$('#coverNav > span > a').eq(curStory+1).trigger('readChapter');
		}
		autoRotate=setTimeout(function(){$('#coverNav > a:last').trigger('nextChpt');},5000);
	}).end().click(function(e){
		e.preventDefault();
		$('#cover *:animated').stop(true);
		clearTimeout(autoRotate);
		$(this).trigger('readChapter').trigger('prevChpt').trigger('nextChpt');
		clearTimeout(autoRotate);
	}).trigger('nextChpt');
	$('#cover 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());
			}
		}
	});
});