var durTimer,themeColor,useTheme,useDuration,useFade,numViews,showNav,runOnce=0;
var zI=2;
var params=parseQuery($('script:last')[0].src.replace(/^[^\?]+\??/,''));
function parseQuery(query){
	var Params=new Object();
	if(!query)return Params;
	var Pairs=query.split(/[;&]/);
	for(var i=0;i<Pairs.length;i++){
		var KeyVal=Pairs[i].split('=');
		if(!KeyVal||KeyVal.length!=2)continue;
		var key=unescape(KeyVal[0]);
		var val=unescape(KeyVal[1]);
		val=val.replace(/\+/g,' ');
		Params[key] = val;
	}
	return Params;
}
function flashVisible(){
	$('#cover').css({visibility:'visible'});
}
function flashComplete(){
	if(runOnce==0){
		$('#coverFlash').remove();
		$('#storyShop').css({display:'block'});
		$('#storyChapters div').css({opacity:0,visibility:'visible'});
		$('#storyChapters > div').css({visibility:'visible'}).bindBook(useTheme,useDuration,useFade,numViews,showNav);
		runOnce=1;
	}
}
function createName(str,html){
	var name=html.replace(/[^a-zA-Z0-9 :_|-]+/g,function(char){
		var ltr='';
		if(char=='&'||char==';') return char;
		var fragment=encodeURIComponent(char).split('%');
		if(fragment[1]=="C3"){
			switch(fragment[2]){
				case '92': case '93': case '94': case '95': case '96':ltr='O'; break;
				case 'B2': case 'B3': case 'B4': case 'B5':ltr='o'; break;
				case 'BB': case 'B9': case 'BA': case 'BC':ltr='u'; break;
				case '89':ltr='E'; break;
				case 'A8': case 'A9':ltr='e'; break;
				case '91':ltr='N'; break;
				case 'B1':ltr='n'; break;
				case 'A7':ltr='c'; break;
				case '9F':ltr='B'; break;
				case 'A6':ltr='ae'; break;
			}
		}
		return ltr;
	});
	return str+name;
}
function applyColor(theme){
	var elem=['storyBook','indexBack','indexNext'];
	switch(theme){
		case 'designer':
			path=['',getImageURL()+'store/category/designer/2009/_constant/dlpBack.gif',getImageURL()+'store/category/designer/2009/_constant/dlpNext.gif'];
			themeColor=['','#B1B1B1','#CCCCCC'];
			break;
	}
	$(elem).each(function(i){
		$('#'+this).css({backgroundImage:'url('+path[i]+')',backgroundRepeat:'no-repeat'});
	});
	$('.center_rightColumn').css({background:themeColor[0]});
	$('#storyIndex a')
	.filter(':first').css({borderRight:'1px solid '+themeColor[2]}).end()
	.filter(':last').css({borderLeft:'1px solid '+themeColor[2]}).end()
	.not(':first').not(':last').css({border:'1px solid '+themeColor[2]});
}
$.fn.bindBook=function(color,duration,fadespeed,views,nav){
	$('#storyBook').append('<div id="storyIndex"><a id="indexBack"></a></div>');
	this.each(function(i){
		$(this).css({opacity:0}).attr({viewsLeft:views})
		.bind('readable',function(e){
			$('#storyChapters > div').not(e.target).children().not('a:has(img),div').animate({opacity:0},fadespeed*100);
			$('*',this).animate({opacity:1},fadespeed*100);
			$(this).animate({opacity:1},fadespeed*100,function(){
				$('#storyChapters > div').not(e.target).trigger('unreadable');
			}).css({zIndex:zI}); zI++;
			var n=parseFloat($(this).attr('viewsLeft'))-1;
			$(this).attr({viewsLeft:n});
			if(n<=-1){clearTimeout(durTimer);}
		})
		.bind('unreadable',function(e){
			$(this).animate({opacity:0},fadespeed*100,function(){
				$(this).children().css({opacity:1});
			});
		})
		.children('img[src$=gif]').css({margin:'36px 0 5px 0'}).end()
		.children('img[src$=jpg]').wrap($(this).children('p').children('a[rel=main]').clone(true).html('').removeAttr('rel')).end()
		.children('div').mouseup(function(e){
			clearTimeout(durTimer);
		});
		$('a', this).each(function(){
			if($(this).children('img').length>0){
				$(this).attr('name',createName('Feature Link '+(i+1)+': ',$('img', this).eq(0).attr('alt')));
			}else{
				$(this).attr('name',createName('Feature Link '+(i+1)+': ',$(this).text()));
			}
		});
		$('#storyIndex').append('<a><img src="'+getImageURL()+'store/common/transpixel.gif" alt="" /></a>').children('a:last').bind('readChapter',{dur:duration[i]},function(e,autoPush){
			if(autoPush){durTimer=setTimeout(function(){$('#storyIndex a:last').trigger('nextChpt',true);},(e.data.dur*1000)+(fadespeed*100));}
			var n=$('#storyIndex a').index(this);
			$('#storyIndex a').not(':first').not(':last').css({background:'none'}).attr({current:'false'}).end().end().eq(n).css({background:themeColor[1]}).attr({current:'true'});
			$('#storyChapters > div').eq(n-1).trigger('readable');
		});
		if(nav==0)$('#storyIndex').hide();
	});
	$('#storyIndex').append('<a id="indexNext"></a>').children('a')
	.filter(':first').bind('prevChpt',function(e,bool){
		var n=$('#storyIndex a').index($('#storyIndex a[current=true]'))-1;
		if(n==0)n=$('#storyChapters > div').length;
		$('#storyIndex a').not(':first').not(':last').eq(n-1).trigger('readChapter',bool);
	}).end()
	.filter(':last').bind('nextChpt',function(e,bool){
		var n=$('#storyIndex a').index($('#storyIndex a[current=true]'))-1;
		if(n>=$('#storyChapters > div').length-1)n=-1;
		$('#storyIndex a').not(':first').not(':last').eq(n+1).trigger('readChapter',bool);
	}).end()
	.attr('href','#').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		$('#storyBook *:animated').stop(true);
		clearTimeout(durTimer);
		$(this).trigger('readChapter',false).trigger('prevChpt',false).trigger('nextChpt',false);
		clearTimeout(durTimer);
	});
	applyColor(color);
	$('#storyIndex a').eq(1).trigger('readChapter',true);
}
$(document).ready(function(){
	useTheme=(typeof params.theme=="undefined")?'designer':params.theme;
	useDuration=(typeof params.dur=="undefined")?function(){
		var myArr=[];
		for(var i=0;i<$('#storyChapters > div').length;i++){
			myArr.push('6');
		}
		return myArr;
	}:function(){
		if(params.dur.length==1){
			var myArr=[];
			for(var i=0;i<$('#storyChapters > div').length;i++){
				myArr.push(params.dur);
			}
			return myArr;
		}else{
			return params.dur.split(',');
		}
	};useDuration=useDuration();
	useFade=(typeof params.fade=="undefined")?5:params.fade;
	numViews=(typeof params.cycle=="undefined")?9999:params.cycle;
	showNav=(typeof params.nav=="undefined")?1:params.nav;
	if($('#intro').length>0){
		if(readCookie("designer")){
			flashComplete();
			flashVisible();
		}else{
			$('#coverFlash').css({visibility:'visible'});
			createCookie("designer",true);
		}
	}else{
		flashComplete();
		flashVisible();
	}
});