$(document).ready(function(){
	
	$("#intro_content, #main-content").hide();
	$("#intro_teaser").click(function(e){
		e.preventDefault();
		e.stopPropagation();
		$("#intro_content").slideToggle();
		e.target.blur();                 // most browsers 
		e.target.hideFocus = true;       // internet explorer
		e.target.style.outline = 'none';
		});
	$(".about_bis").live('click',function(e){
		$("#intro_teaser").click();
		e.stopPropagation();
		});
	//content completion
	$("span.getbrand_1").append("Jean Paul Gaultier");
	$("var").each(function(){
		if($(this).html().match(/([^,\s]{2,30})\s?,\s?([^,v]{2,30})\s?,\s?([^,\s]{2,7})/gi)){
			var mail = RegExp.$1 +'@' + RegExp.$2 + '.' + RegExp.$3;
			$(this).html("<a href='MailTo:"+mail+"'>"+mail+"</a>");
		}
	});
	
	if($.browser.msie && $.browser.version <=7){
		$("#main-content").show();
		$(".links_images a").fancybox({
				'titlePosition'	: 'inside'
			});
		return;
	}
	var menu = new Menu("#main-content article");

});



function Menu(string_to_content){
	if(!window.jQuery) alert("Menu doesnt work without jQuery");
	var that = this;


	$('body').append('<div id="playground"></div>');
	
	var num_items=$(string_to_content).size(),
	col_width = (num_items>0) ? Math.floor($("#playground").width() / num_items) : 0,
	col_buffer = (num_items>0) ? $("#playground").width()%num_items : 0,
	current_index=-1;
	;
	
	
	function get_next_index(offset){
		var next_index=current_index;
		if(offset==1){
			if(next_index==Number(num_items)-1){
				next_index=0;
				}
			else{
				next_index=Number(next_index)+1;
			}
		}else if(offset==-1){
			if(next_index==0){
				next_index=Number(num_items)-1;
				}
			else{
				next_index=Number(next_index)-1;
				}
			}
		return next_index;
	}
	
	function set_current_index(index){
		if(Number(index)<0){
			current_index=0;
		}else if(Number(index)>Number(num_items - 1)){
			current_index=num_items-1;
		}else{
			current_index=index;
		}
		return current_index;
	}
	
	function get_current_index(){
		return current_index;
	}
	
	
	this.anim_article2article=function(offset){
		var next_index=get_next_index(offset);
		
		if(!document.getElementById('playgroundArticleTransition')){
			$("#playground").append('<div id="playgroundArticleTransition"></div>');
		}
		$("#playgroundArticleTransition").html($("#playgroundArticle").html());
		that.remove_navigation();
		that.setarticle(next_index);
		$("#playgroundArticleTransition").fadeOut(500, function(){
			$("#playgroundArticleTransition").remove();
		});		
	}
	
	this.setarticle=function(index){
		var safe_index = set_current_index(index);
		if(!document.getElementById('playgroundArticle')){
			$("#playground").append('<div id="playgroundArticle"></div>');
		}
		$("#playgroundArticle").html($(string_to_content).eq(safe_index).html());
		$("#playgroundArticle .links_images a").fancybox({
				'titlePosition'	: 'inside'
			});
		var t=setTimeout(function(){
			that.add_navigation();
		},250);
	}
	
	
	
	this.remove_navigation=function(){
		$("#playgroundNavigation").remove();
	}
	this.remove_article=function(){
		$("#playgroundArticle").remove();
	}
	
	
	
	this.add_navigation=function(){
		if(!document.getElementById('playgroundNavigation')){
			$("#playground").append('<div id="playgroundNavigation"></div>');
		}
		else{
			return;
		}
		var nav = '<div id="bt_previous" class="bt"><div><span>Previous</span></div></div>';
		nav += '<div id="bt_next" class="bt"><div><span>Next</span></div></div>';
		nav += '<div id="bt_menu" class="bt"><div><span>Back to</span><br /><span>the menu</span></div></div>';
		
		$("#playgroundNavigation").append(nav);
		
		$("#bt_previous").click(function(){
			that.anim_article2article(-1);
		});

		$("#bt_next").click(function(){
			that.anim_article2article(1);
		});

		$("#bt_menu").click(function(){
			$('#bt_previous,#bt_next,#bt_menu').css('display', 'none');
			that.set_menu();
		});

		$(".bt").hover(
			function(){
				$(this).children("div").animate({opacity:1},200);
			},
			function(){
				$(this).children("div").animate({opacity:0},200);
			}
		);


		
	}
	
	this.set_menu=function(){
		if(!document.getElementById('playgroundMenu')){
			$("#playground").append('<div id="playgroundMenu"></div>');
		}
		this.remove_navigation();
		this.remove_article();
		
		var menu_insert="";
		$(string_to_content).each(function(i){
			var h1=$('h1:first',this).text(),
			img=$('img:first',this).attr('src');
			var width=(i==(num_items-1)) ? col_width + col_buffer :col_width ;
			
			menu_insert+='<div class="menu_item" id="menu_item'+i+'" style="width:'+width+'px;"><h1 class="goes_up"><span>'+h1+'</span></h1><img src="'+img+'" /></div>';
			});
			menu_insert+= '<div id="portfolio_intro_baseline">'+$("#portfolio_intro").html()+'</div>';
		$("#playgroundMenu").append(menu_insert).find('div.menu_item').click(function(){
				var index = this.id.replace('menu_item','');
				that.setarticle(index);
				$(this).siblings().andSelf().addClass('exit');
				var t=setTimeout(function(){
						$("#playgroundMenu").find('div.menu_item').remove();
						$("#playgroundMenu").remove();
					},1000);
			});
		
		
		
	}
	
	

	
	this.set_menu();
}




















