var point=function(){
	return{
		enabled:false,
		enable:function(settings){
			if(!this.enabled){
				$.extend(this,settings);
				this.map=$(this.id+' > img').attr('usemap');
				this.build();
				this.enabled=true;
			}
		},
		build:function(){
			var obj=this;
			if($('#experiences .point').length<1){
				$('#experiences').append('<div class="point"><div class="carat"></div><div class="body"></div></div>');
				$('#experiences .point').css({opacity:0})
				.bind('mouseover',function(){
					obj.displayPoint();
					$(this).addClass('hover');
				}).bind('mouseout',function(){
					obj.hidePoint();
					$(this).removeClass('hover');
				}).children().css({opacity:0.9}).filter('.carat').fillPolygon([0,10,10],[0,0,10],{color:'#959494'});
			}
			this.bind();
		},
		bind:function(){
			var obj=this;
			$(this.map+' area')
			.bind('select',function(){
				obj.displayPoint();
				var i=$(obj.map+' area').index(this);
				$('#experiences .point p').remove();
				$(obj.id+' .blurbs p').eq($(obj.map+' area').index(this)).clone().appendTo('#experiences .point').css({opacity:1});
				$('#experiences .point').css({left:obj.coords[i][0]+'px',top:obj.coords[i][1]+'px'})
				.add('#experiences .point .body').css({height:parseFloat($('#experiences .point p').height())+15+'px'});
			})
			.bind('mouseover',function(e){
				$(this).trigger('select');
			})
			.bind('unselect',function(){
				obj.hidePoint();
			})
			.bind('mouseout',function(){
				$(this).trigger('unselect');
			})
			.bind('click',function(){
				$(this).blur();
			});
		},
		displayPoint:function(){
			$('#experiences .point').stop().show().animate({opacity:1},(1-$('#experiences .point').css('opacity'))*1000);
		},
		hidePoint:function(){
			$('#experiences .point').stop().animate({opacity:0},1000,function(){$(this).hide().children('p').remove()});
		},
		displayBlurb:function(i){
			$(this.map+' area').eq(i).trigger('select');
		},
		hideBlurb:function(){
			$(this.map+' area:last').trigger('unselect');	
		}
	}
}
var blurb=function(){
	return{
		enabled:false,
		enable:function(settings){
			if(!this.enabled){
				$.extend(this,settings);
				this.map=$(this.id+' > img').attr('usemap');
				this.build();
				this.enabled=true;
			}
		},
		build:function(){
			var obj=this;
			if($('#experiences .bubble').length<1){
				$('#experiences').append('<div class="bubble"><div class="carat"></div><div class="body"></div></div>');
				$('#experiences .bubble').css({opacity:0})
				.bind('mouseover',function(){
					obj.displayBubble();
					$(this).addClass('hover');
				}).bind('mouseout',function(){
					obj.hideBubble();
					$(this).removeClass('hover');
				}).children().css({opacity:0.9}).filter('.carat').fillPolygon([0,5,10],[10,0,10],{color:'#959494'});
			}
			this.bind();
		},
		bind:function(){
			var obj=this;
			$(this.map+' area')
			.bind('select',function(){
				obj.displayBubble();
				obj.posCarat(obj.coords[$(obj.map+' area').index(this)]);
				$('#experiences .bubble p').remove();
				$(obj.id+' .blurbs p').eq($(obj.map+' area').index(this)).clone().appendTo('#experiences .bubble').css({opacity:1});
			})
			.bind('mouseover',function(e){
				$(this).trigger('select');
			})
			.bind('unselect',function(){
				obj.hideBubble();
			})
			.bind('mouseout',function(){
				$(this).trigger('unselect');
			})
			.bind('click',function(){
				$(this).blur();
			});
		},
		displayBubble:function(){
			$('#experiences .bubble').stop().show().animate({opacity:1},(1-$('#experiences .bubble').css('opacity'))*1000);
		},
		hideBubble:function(){
			$('#experiences .bubble').stop().animate({opacity:0},1000,function(){$(this).hide().children('p').remove()});
		},
		displayBlurb:function(i){
			$(this.map+' area').eq(i).trigger('select');
		},
		hideBlurb:function(){
			$(this.map+' area:last').trigger('unselect');	
		},
		posCarat:function(x){
			$('#experiences .bubble .carat').css({left:x+'px'});
		}
	}
}
var holidayDressCode={
	step:1,last:3,enabled:false,
	enable:function(){
		if(!this.enabled){
			$('#black_tie, #cocktail_party, #office_event').css({opacity:0});
			$('#splash > div')
			.bind('mouseover mouseout',function(){
				$(this).toggleClass('hover');
			})
			.bind('select',function(){
				holidayDressCode.occasion($('#splash > div').index(this));
			})
			.bind('click',function(){
				$(this).trigger('select');
				clearTimeout(holidayDressCode.auto);
			}).children('span.bg').css({opacity:0.7});
			$('#holiday_dress_code .nav > a')
			.bind('click',function(){
				holidayDressCode.occasion($('#holiday_dress_code .nav > a').index(this));
				clearTimeout(holidayDressCode.auto);
			});
			$('#holiday_dress_code img[usemap]').each(function(){
				$($(this).attr('usemap')+' area').bind('mouseover',function(){
					clearTimeout(holidayDressCode.pv);
					holidayDressCode.pv=setTimeout(function(){
						cmCreatePageviewTag($('#'+$(this).parent().attr('name')).attr('alt')+' > '+$(this).attr('alt'),null,'0~2376777~2374609~6025501');
					},1500);
				}).bind('mouseout',function(){
					clearTimeout(holidayDressCode.auto);
				});
			});
			this.blackTieBlurbs=new blurb();
			this.blackTieBlurbs.enable({id:'#black_tie',coords:[110,290]});
			this.cocktailBlurbs=new blurb();
			this.cocktailBlurbs.enable({id:'#cocktail_party',coords:[130,330]});
			this.officeEventBlurbs=new blurb();
			this.officeEventBlurbs.enable({id:'#office_event',coords:[155,330]});
			this.auto=setTimeout(function(){holidayDressCode.steps()},6000);
			this.enabled=true;
		}
	},
	occasion:function(i){
		if(i!=this.last&&!$('#holiday_dress_code > div').is(':animated')){
			clearTimeout(this.pv);
			$('#holiday_dress_code .floating_col.nav > a').children('span').remove().end().eq(i).append('<span>&gt;</span>');
			$('#holiday_dress_code > div').not('.nav').eq(this.last).animate({opacity:0},1000,function(){$(this).hide()});
			$('#holiday_dress_code > div').not('.nav').eq(i).show().animate({opacity:1},1000);
			$('#holiday_dress_code .floating_col.nav').animate({opacity:0},1000,function(){$(this).remove()}).clone(true).appendTo($('#holiday_dress_code > div').not('.nav').eq(i)).animate({opcaity:1},1000);
			this.last=i;
		}
	},
	steps:function(){
		switch(this.step){
			case 1: case 4: case 7:
				this.blackTieBlurbs.hideBubble();
				$('#splash > div').eq(Math.floor(this.step/3)).trigger('select');
				break;
			case 2: case 3:
				this.blackTieBlurbs.displayBlurb(Math.floor(this.step/3));
				this.blackTieBlurbs.posCarat((this.step==2)?100:305);
				break;
			case 5: case 6:
				this.cocktailBlurbs.displayBlurb(Math.floor(this.step/6));
				this.cocktailBlurbs.posCarat((this.step==5)?125:320);
				break;
			case 8: case 9:
				this.officeEventBlurbs.displayBlurb(Math.floor(this.step/9));
				this.officeEventBlurbs.posCarat((this.step==8)?130:310);
				break;
		}
		this.step+=1;
		if(this.step==10){
			this.auto=setTimeout(function(){
				holidayDressCode.blackTieBlurbs.hideBubble();
				$('#splash > div').eq(0).trigger('select');
				clearTimeout(holidayDressCode.auto);
			},7000);
		}else{
			this.auto=setTimeout(function(){holidayDressCode.steps()},7000);	
		}
	}
}
var mixMaster={
	S:-1,T:-1,enabled:false,
	enable:function(){
		if(!mixMaster.enabled){
			$('#shirts img').bind('click',function(){
				mixMaster.shirt($('#shirts img').index(this));
			});
			$('#ties img').bind('click',function(){
				mixMaster.tie($('#ties img').index(this));
			});
			$('#combos img, #blurbs p').css({opacity:0,visibility:'visible'});
			mixMaster.enabled=true;
		}
	},
	shirt:function(i){
		mixMaster.S=i;
		$('#shirts img').removeClass('selected').eq(i).addClass('selected');
		mixMaster.display();
	},
	tie:function(i){
		mixMaster.T=i;
		$('#ties img').removeClass('selected').eq(i).addClass('selected');
		mixMaster.display();
	},
	display:function(){
		if(mixMaster.S>-1&&mixMaster.T>-1){
			$('#combos img.selected, #blurbs p.selected').removeClass('selected').animate({opacity:0},1000);
			$('#combos img').eq((mixMaster.S*$('#shirts img').length)+mixMaster.T).addClass('selected').animate({opacity:1},1000);
			$('#blurbs p').eq((mixMaster.S*$('#shirts img').length)+mixMaster.T).addClass('selected').animate({opacity:1},1000);
			cmCreatePageviewTag('Mix Master > Combo '+(((mixMaster.S*$('#shirts img').length)+mixMaster.T)+1),null,'0~2376777~2374609~6025501');
			// writeCookie();
		}
	}
}
var onesuit4Ways={
	enabled:false,
	enable:function(){
		if(!onesuit4Ways.enabled){
			var suitBlurbs=new point();
			suitBlurbs.enable({
				id:'#one_suit_four_ways',
				coords:[[90,260],[225,260],[330,260],[395,260]]
			});
			$('#one_suit_four_ways img[usemap]').each(function(){
				$($(this).attr('usemap')+' area').bind('mouseover',function(){
					clearTimeout(onesuit4Ways.pv);
					onesuit4Ways.pv=setTimeout(function(){
						cmCreatePageviewTag($('#'+$(this).parent().attr('name')).attr('alt')+' > '+$(this).attr('alt'),null,'0~2376777~2374609~6025501');
					},1500);
				}).bind('mouseout',function(){
					clearTimeout(onesuit4Ways.auto);
				});
			});
			onesuit4Ways.enabled=true;
		}
	}
}
var interface={
	last:-1,
	ready:function(){
		interface.bind();
		$('#experiences > div').css({opacity:0});
		// readCookie();
		$('.archive > div:first').trigger('select');
	},
	bind:function(){
		$('.archive.bg').css({opacity:0.4});
		$('.archive > div')
		.each(function(){
			if($(this).text().length>0){
				$(this).css({cursor:'pointer'});	
			}
		})
		.bind('select',function(){
			$('.archive > div.selected').removeClass('selected').children('span').animate({opacity:0},1000).remove();
			$(this).addClass('selected').append('<span></span>').children('span').css({opacity:0}).animate({opacity:0.5},1000);
			interface.experience($($('.archive > div')).index(this));
		})
		.bind('click',function(){
			if(!$(this).is('.selected')&&$(this).text().length>0){
				$(this).trigger('select');
			}
			clearTimeout(holidayDressCode.auto);
			holidayDressCode.blackTieBlurbs.hideBubble();
		});
	},
	experience:function(i){
		$('#experiences > div').eq(interface.last).stop().animate({opacity:0},1000,function(){$(this).hide()});
		$('#experiences > div').eq(i).stop().show().animate({opacity:1},1000);
		var temp;
		switch(i){
			case 0:
				holidayDressCode.enable();
				temp='Holiday Dress Code';
				break;
			case 1:
				mixMaster.enable();
				// readCookie();
				if(mixMaster.S==-1&&mixMaster.T==-1){
					mixMaster.shirt(2);
					mixMaster.tie(2);
				}
				temp='Mix Master';
				break;
			case 2:
				onesuit4Ways.enable();
				temp='1 Suit 4 Ways';
				break;
		}
		cmCreatePageviewTag('Mens Style Guide > '+temp,null,'0~2376777~2374609~6025501');
		interface.last=i;
	}
};
$(function(){
	interface.ready();
});