var currentPageArrayPosition = 0;

var menuColor = Array();

var timeout;
$(document).ready(function () {
    updateState(location.hash);
    positionSite();

	$('.csCi').scroll(function() {
		$('.csCi').css('top', $('.csCi').css('top')-1);
	});
	
	$( "#slider-vertical" ).slider({
		orientation: "vertical",
		range: "min",
		min: 0,
		max: 100,
		value: 100,
		slide: function( event, ui ) {
			var iH = $('.csCi').height()-400;
			
			var scroll = (100-ui.value)*iH/100;
			 $('.csCi').css('top', -scroll);
			//$( "#amount" ).val( ui.value );
		}
	});
		
	var searchOnThisPage = new Imtech.SearchOnThisPage();
    searchOnThisPage.startSearchingLength = 3;
    searchOnThisPage.init();
    
    $(window).resize(positionSite);
    
    $('#c1 a, #c2 a').mouseover(function() {
    	$('.iBloc').stop().hide().css('opacity',  '');
    	
    	clearTimeout(timeout);
    	var elm = this;
    	timeout = setTimeout(function() { $('#i'+$(elm).attr('rel')).fadeIn();}, 200);
    });
    
    $('.arrowLeft').click(function () {
    	if (currentPageArrayPosition == 999) {
    		scrollPageTo(pagesArray[pagesArray.length-1]);
    		return;
    	}
    
    	currentPageArrayPosition--;
    	if (currentPageArrayPosition < 0) {
    		currentPageArrayPosition++;
    		return;					
    	}
    	scrollPageTo(pagesArray[currentPageArrayPosition]);
    }).mouseover(function() {
    	$(this).css('backgroundImage', 'url(/images/leftOver.png)');
    }).mouseout(function() {
    	$(this).css('backgroundImage', 'url(/images/left.png)');
    });
     
    $('.arrowRight').click(function () {
    	if (currentPageArrayPosition == 999) return;
    	currentPageArrayPosition++;
    	if (currentPageArrayPosition > pagesArray.length-1) {
    		currentPageArrayPosition--;
    		return;
    	}
    	scrollPageTo(pagesArray[currentPageArrayPosition]);
    }).mouseover(function() {
    	$(this).css('backgroundImage', 'url(/images/rightOver.png)');
    }).mouseout(function() {
    	$(this).css('backgroundImage', 'url(/images/right.png)');
    });
    
    $('.menu, .submenu').mouseover(function () {
    	$(this).addClass('menuSelectedOver');
    }).mouseout(function () {
    	$(this).removeClass('menuSelectedOver');
    });
    
    $('.menu').click(function() {
    	scrollPageTo($(this).attr('rel'));
    });
    $('.submenu').click(function() {
    	scrollPageTo($(this).attr('id').replace(/submenu_/g, ''), true);
    });
    
    $('#searchField').focus(function() {
    	if($(this).val() == 'Recherche' || $(this).val() == 'Suchen' || $(this).val() == 'Search') {
    		$(this).attr('rel', $(this).val());
    		$(this).val(''); 
    	}
    }).blur(function() {
    	if ($(this).val() == '') {
    		$(this).val($(this).attr('rel'));
    	}	
    });
    
    $('#logo').click(function() {
    	scrollPageTo(2);
    })
    
    $('.lineDate').live('mouseover', showDateLine).live('mouseout', function() {
    	$('#dateCursor').hide();				
    });
    
    $('.portalItem').live('mouseover', function() {
    	var img = $(this).children('a').children('img');
    	img.attr('src', img.attr('src').replace(/_c/g, '_nb'));
    }).live('mouseout', function() {
    	var img = $(this).children('a').children('img');
    	img.attr('src', img.attr('src').replace(/_nb/g, '_c'));
    });
});

function showDateLine() {
	
    var a = $(this).attr('rel');
    var p = (a-1895) * 719 / 120;
		
    if (p < 0) p = 0;
    $('#dateCursor').css('left', p).show();
}

function getPageArrayPosition(page) {
    var p = 0;
    for(var i = 0; i < pagesArray.length; i++){
    	if (pagesArray[i] == page) p = i;
    }
    
    return p;
}

function showPortal() {
    var e = $('.portalItem:hidden').get(0);
    
    $(e).fadeIn(400);
    
    setTimeout("showPortal()", 400);
}

function showTimeline() {
    $('#timelineContainer').fadeIn();
}

function showFlash() {
    	$('#practice').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="350" height="350" id="practice2" align="middle">\
    						 <param name="allowScriptAccess" value="sameDomain" />\
    						 <param name="allowFullScreen" value="false" />\
    						 <param name="movie" value="/flash/practice_EN.swf" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" />	<embed src="/flash/practice_EN.swf" quality="high" wmode="transparent" bgcolor="#ffffff" width="350" height="350" id="practice22" name="practice2" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />\
    						 </object>');
    	$('#practiceFR').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="350" height="350" id="practice2" align="middle">\
    						 <param name="allowScriptAccess" value="sameDomain" />\
    						 <param name="allowFullScreen" value="false" />\
    						 <param name="movie" value="/flash/practice_FR.swf" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" />	<embed src="/flash/practice_FR.swf" quality="high" wmode="transparent" bgcolor="#ffffff" width="350" height="350" id="practice22" name="practice2" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />\
    						 </object>');

}

function scrollPageTo(page, ommitSubmenus) {
    if (page == 3) {
    	scrollPageTo(1);
    	return;
    }
    
    currentPageArrayPosition = getPageArrayPosition(page);
    
    $('.submenus').hide();
    $('#submenus_'+page).show();
    $('#submenus_'+$('#submenu_'+page).attr('rel')).show();	
    
    var offset 		= $('#page'+page).position();

    $('.menu').removeClass('menuSelected');
    $('.submenu').removeClass('menuSelected');
    
    location.hash = "p"+page;
    $('#menu_'+page).addClass('menuSelected');
    
    if (page == 1 || page == 7) {
    	$('#menu_3').addClass('menuSelected');
    }
    
    $('#submenu_'+page).addClass('menuSelected');
    
    if ($('#submenu_'+page).attr('rel') != undefined) {
    	$('#menu_'+$('#submenu_'+page).attr('rel')).addClass('menuSelected');	
    }
    
    $("#pages").animate({ 
    	left: -offset.left
    }, 1000 );
    
    if (page == 5) {
    	$('.portalItem').show();
    	//setTimeout("showPortal()", 1000);
    }
    
    if (page == 4) {
    	$('#practice').html('');
    	setTimeout("showFlash()", 1100);
    }
    
    if (page == 8 || page == 9 || page == 10 || page == 11) {
    	if ($('#timelineContainer').is(':hidden')) setTimeout("showTimeline()", 750);
    	if (page == 8) {
    		$('#timelineCursor').animate({
    			left: 0,
    			width: 181
    		}, 1000);
    		
    		$('#timelineLegend').fadeOut(500, function() {
    			$('#timelineLegend').css('left', 0);
    			$('#timelineImage').attr('src', '/images/1stgeneration'+currentLang+'.png');
    			$('#timelineLegend').fadeIn(500);
    		});

    	}
    	if (page == 9) {
    		$('#timelineCursor').animate({
    			left: 181,
    			width: 239
    		}, 1000);

    		$('#timelineLegend').fadeOut(500, function() {
    			$('#timelineLegend').css('left', 169);
    			$('#timelineImage').attr('src', '/images/2ndgeneration'+currentLang+'.png');
    			$('#timelineLegend').fadeIn(500);
    		});
    	}
    	if (page == 10) {
    		$('#timelineCursor').animate({
    			left: 358,
    			width: 227
    		}, 1000);

    		$('#timelineLegend').fadeOut(500, function() {
    			$('#timelineLegend').css('left', 358);
    			$('#timelineImage').attr('src', '/images/3rdgeneration'+currentLang+'.png');
    			$('#timelineLegend').fadeIn(500);
    		});
    	}
    	if (page ==11) {
    		$('#timelineCursor').animate({
    			left: 578,
    			width: 140
    		}, 1000);

    		$('#timelineLegend').fadeOut(500, function() {
    			$('#timelineLegend').css('left', 578);
    			$('#timelineImage').attr('src', '/images/4thgeneration'+currentLang+'.png');
    			$('#timelineLegend').fadeIn(500);
    		});
    	}
    } else {
    	if ($('#timelineContainer').is(':visible')) $('#timelineContainer').fadeOut();				
    }
} 

function showLightBox(id) {
    //var elm = $('#l'+id).detach();
    $('#l'+id).appendTo('body');
    
    $('#blackBox').css('width', $(document).width()).css('height', $(document).height()).css('opacity', 0.85).click(function() {
    	$('#l'+id).fadeOut();
    	$('#blackBox').fadeOut();
    
    }).fadeIn();
    
    $('#l'+id+' img').click(function() {
    	$('#l'+id).fadeOut();
    	$('#blackBox').fadeOut();
    
    })
    
    var s = $(window);
   	
    $('#l'+id).fadeIn().css('left', s.width()/2 - ($('#l'+id).width()+80) /2).css('top', s.height()/2 -100- $('#l'+id).height() /2) ;
}

var lastHash = '';
function updateState(hash) {
    if (lastHash != hash) {
    	scrollPageTo(hash.replace(/#p/g, ''));
    }
    
    lastHash = hash;
}

function private() {
    scrollPageTo(999);
    currentPageArrayPosition = 999;
}

function positionSite() {
	var offset = $('#site').offset();
	
	if (offset.left  < 25) {
	    $('#site').css('left', '25px').css('margin-left', '0px');
	}
	
	if (offset.top  < 25) {
	    $('#site').css('top', '25px').css('margin-top', '0px');
	}
	
	var wWidth  = $(window).width()
	var sWidth  = $('#site').width()
	var wHeight = $(window).height()
	var sHeight = $('#site').height()
	
	if (wWidth > sWidth) {
	    $('#site').css('left', '50%').css('margin-left', '-466px');    	
		var offset = $('#site').offset();
		if (offset.left  < 25) {
		    $('#site').css('left', '25px').css('margin-left', '0px');
		}
	}
	
	if (wHeight > sHeight) {
		$('#site').css('top', '46%').css('margin-top', '-360px');
		var offset = $('#site').offset();
		if (offset.top  < 25) {
	    	$('#site').css('top', '25px').css('margin-top', '0px');
		}
	}
}

