DD = {
  Browser: {
    IE:     !!(window.attachEvent &&
      navigator.userAgent.indexOf('Opera') === -1),
    Opera:  navigator.userAgent.indexOf('Opera') > -1,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 &&
      navigator.userAgent.indexOf('KHTML') === -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/),
		FF2: (navigator.userAgent.search(/Firefox\/2.0.0/) > -1)
  }
};

$(function(){
	var headings = $('#product_detail h2,#rider_details h2,#product_detail .content h3'),
		current_navigation = $('#navigation .active'),
		timer = null;
	
	headings.css('opacity',0);
	
	//activate subnavigation
	$('#navigation>li').mouseover(function(){
		timer && clearTimeout(timer);
		$('#navigation>.active').removeClass('active');
		$(this).addClass('active');
	});
		
	//reset navigation
	$('#navigation').mouseout(function(){
		timer = setTimeout(function(){
			$('#navigation li.active').removeClass('active');
			current_navigation.addClass('active');}, 300);
	});
	
	//add hover elements for sIFR hover state
	if(hasFlash !== false){
		$('#product_detail h3 span').each(function(){
			$(this.parentNode).append($(this).clone(true).addClass('hover').removeClass('std'));
		});
	}
	
	//display name of technology
	$('#product_detail>.content>div:not(#product_highlights)>h3').click(function(e){
		if(!$(this.parentNode).hasClass('active')){
			var old_active = $('#product_detail>.content>div.active').removeClass('active');
				new_active = $(this.parentNode).addClass('active'),
				new_description = $('div.description', new_active),
				old_description = $('div.description', old_active).css({display:'block'});
				
			$({height:0}).animate({height:212}, {
				duration: 500,
				step: function(){
					new_description.css('height',parseInt(this.height)+'px');
					old_description.css('height',(212-parseInt(this.height))+'px');
				},
				complete: function(){
					new_description.css('height','212px');
					old_description.css('height','0');
				}
			});
		}
		e.preventDefault();
	});
	
	//display large product image
	$('#product_images li').bind('click mouseover',function(){
		$('#product_image_large').attr('src',$('img.web',this)[0].src);
	});

	
	function showHeadings(){
		if(headings.hasClass('sIFR-replaced') || hasFlash === false){
			//IE does not render flash correctly if opacity is set to 1
			headings.css('opacity',DD.Browser.IE?'auto':1);
		} else {
			setTimeout(showHeadings,50);
		}
	}
	showHeadings();

});

//set paper height and width
$(function(){
	$(window).resize(function(){
		set_paper_height();
	});
	
	var paper = $('#paper'),
		body = $('body'),
		wrapper = $('#wrapper');
	
	
	function set_paper_height(){
		paper.css({
			height:	(body.height()<wrapper.height()? wrapper.height()+'px' : '100%'),
			width:	(body.width()<wrapper.width()? wrapper.width()+'px' : '100%')});
	}	
	
	set_paper_height();
});


//team tabs
$(function(){
	$('#rider_details h3').bind('mouseover focus', function(){
		switch(this.parentNode.id){
			case 'bio':
				set_background_left_to(8);
				break;
			case 'interview':
				set_background_left_to(-392);
				break;
			case 'products':
				set_background_left_to(-792);
				break;
		}
		$('#rider_details .active').removeClass('active').find('div');
		$(this.parentNode).addClass('active');
	});
	
	var rider_details_list = $('#rider_details ul');
	
	set_background_left_to = function(position){
		rider_details_list.css('backgroundPosition',position+'px bottom');	
	};
});

//Product highlights title
$(function(){
	var technology_title = $('#technology_title').hide();
	
	$('#product_highlights li').mouseover(function(){
		var left = $(this)[0].offsetLeft;
		technology_title.html($('span',this).text()).css({left:left,right:'auto'}).slideDown();
		var width = technology_title[0].offsetWidth;
		if(left+width>324){
			technology_title.html($('span',this).text()).css({left:'auto',right:0});
		}		
	});
	$('#product_highlights ul').mouseout(function(){
		technology_title.html('').hide();
	});
});

//women background
$(function(){
	$(window).resize(function(){
		set_background_dimensions();
	});
	
	var background = $('#paper_and_background'),
		body = $('body');
	
	
	function set_background_dimensions(){
		var body_height = body.height(),
			body_width = body.width();
		background.css({
			height:	Math.min(1280,Math.max(800,body_height))+'px',
			width:	Math.min(1920,Math.max(1065,body_width))+'px',
			bottom: Math.min(0,Math.max(-480, 800-body_height))	+'px',
			right: Math.min(0,Math.max(-875, 1065-body_width))	+'px'});			
	}	
	
	set_background_dimensions();
});

// slide in language_selection
$(function(){
	var language_selection = $('#language_selection');
	language_selection.css('top','-1000px');
	if(language_selection){
		$(window).load(function(){
			language_selection.animate({top:'20px'},500).animate({top:'-30px'},100).animate({top:'5px'},100).animate({top:'-15px'},50).animate({top:'0px'},50);
		});
	}
});

//products loupe zoom functionality
$(function(){
	if($('body').hasClass('products_detail')){

		$('#loupe').click(function(){
			overlay.show();
		});

		$('#overlay, #close').live('click',function(){overlay.hide()});
		
		overlay = {
			body_height: document.documentElement.clientHeight || $('body').height(),
			is_board: $('body').hasClass('board'),
		
			show: function(){
				this.overlay ? this.unhide() : this.build();
			},
			
			build: function(){
				this.initialize_layout();
				this.initialize_graphics();
				this.initialize_thumb_actions();
				this.unhide();
			},
			
			initialize_layout: function(){
				this.retrieve_meta_information();	
				$('body').append('<div id="overlay"></div><div id="popup"><div id="close"></div><h2>'+this.title+'</h2><div id="popup_thumbs"></div></div><img id="dimension_getter" />');
				if(typeof(DD_belatedPNG)!='undefined') {DD_belatedPNG.fix('#close');}
				this.overlay = $('#overlay').css({width:$(document).width()+'px',height:$(document).height()+'px'});
				this.popup = $('#popup').css({width:this.width, height:this.height,marginLeft:this.get_margin_left(this.width),marginTop:this.get_margin_top(this.height)});
				this.thumbs = $('#popup_thumbs');
			},
			
			initialize_graphics: function(){
				var images = $('img.overlay').removeClass('fullsize overlay');
				this.first_image_height = $(images[0]).height();
				this.top_position = this.get_half(this.height)+'px';
				if(this.is_board) {
					this.popup.append('<div id="current_image_container"></div>');
					$('#current_image_container').append(images).css({'height':this.height-36+'px',overflow:'scroll'});
				} else {
					this.thumbs.append(images);
					this.popup.append('<div id="current_image_container"><img id="current" /></div>');
					$('#current_image_container').css('lineHeight',this.height-70+'px');
					this.initialize_thumb_actions();
					$(images[0]).trigger('click');
				}
			},
			
			initialize_thumb_actions: function(){
				$('#popup_thumbs img').live('click',function(){
					if($(this).hasClass('active')) { return; }
					var image_src = this.src;
					$('#dimension_getter').attr('src', image_src);
					var height = $('#dimension_getter').height();
					$('#popup_thumbs .active').removeClass('active');
					$(this).addClass('active');
					$('#current_image_container').fadeOut(250, function(){
						if((height+72) > overlay.body_height){
							$('#current').css({height: overlay.body_height-72+'px'});
						} else {
							$('#current').css({height: 'auto'});
						}
						$('#current').attr('src', image_src);
						$(this).fadeIn(250);
					});
				});		
			},
			
			retrieve_meta_information: function(){
				this.title = $('#product_detail h2').text();
				max_image_dimensions = this.get_max_image_dimensions();
				this.width = max_image_dimensions.width;
				this.height = max_image_dimensions.height;
			},
			
			get_max_image_dimensions: function(){
				width = height = 0;
				$('img.overlay').each(function(el){
					if($(this).width()>width){ width = $(this).width()}
					if($(this).height()>height){ height = $(this).height()}
				});
				this.max_height = height+30+40;
				if(this.is_board){ 
					this.max_height = Math.min(600, height);
					width = width*2+40; 
				}		
				return {width:width+80+40, height:Math.min(this.max_height,this.body_height)};
			},
			
			get_margin_left: function(width){
				return (-Math.min(this.get_half($('body').width()),this.get_half(width))+'px');
			},

			get_margin_top: function(height){
				return (-Math.min(this.get_half(document.documentElement.clientHeight || $('body').height()),this.get_half(height))+'px');
			},
			
			get_half: function(dimension){
				return parseInt(dimension/2);
			},
			
			hide: function(){
				$('#popup').slideUp(500,function(){
					$('#overlay').fadeOut(250);
					showAllByTag('embed');
					showAllByTag('object');
				});
			},
			
			unhide: function(){
				$('#overlay').show();
				$('#popup').slideDown(500);
				hideAllByTag('embed');
				hideAllByTag('object');
			},
			
			resize: function(){
				this.body_height = document.documentElement.clientHeight || $('body').height();
				this.height = Math.min((this.is_board?600:this.max_height),this.body_height);
				this.overlay.css({height:$(document).height()+'px', width:(document.documentElement.clientWidth || $('body').width())+'px'});
				this.popup.css({height:this.height+'px',marginTop:this.get_margin_top(this.height)});
				if(!this.is_board) {
					$('#current_image_container').css({lineHeight:this.height-24+'px',height:this.height-24+'px'});
				} else {
					$('#current_image_container').css({height:this.height-36+'px'});
				}
				$('#current').css({height: Math.min($('#dimension_getter').height(), this.height-60)+'px'});
			}
		};
		$(window).resize(function(){
			//if($('body').hasClass('products_detail')){overlay.resize();}});
			if ( $("#overlay").length > 0 ){overlay.resize();}
		});	
	}
});


//team nationals countries
$(function(){
	$('#countries>li').hover(
		function(){
			$('ul',this).fadeIn('fast');
		},
		function(){
			$('ul',this).fadeOut();
		}
	);
});

$(function(){
 if(DD.Browser.FF2){
	$('#product_images li,#product_perfect_match li').css('float','left');
 }
});

//edding contest gallery
$(function(){
	$('#edding_contest_gallery a').click(function(e){
		e.preventDefault();
		//$('#fullsize').html('<img src="'+this.href+'" alt="" />');
		$('#fullsize').html('<a id="thumb" href="'+this.title+'.jpg"><img src="'+this.href+'" alt="" /></a>');
		$('#thumb').fancybox({callbackOnStart: function(){hideAllByTag('embed');hideAllByTag('object');}, callbackOnClose: function(){showAllByTag('embed');showAllByTag('object');}}); 
	});
});