// JavaScript Document

/* This section is reserved to load or instancy components */


	// Exclude framesetting
	//if (window!=top) {top.location=window.location;}
	
	
	//Init Cooking session controler <CookieJar> // expires in seconds !
	if( typeof(CookieJar) != 'undefined' ){ jar = new CookieJar({ expires:3600, path: '/' }); };


	var UI_MINIMAL_HEIGHT_SIZE = 524; // px
	var UI_FOOTER_LINK_INIT_HEIGHT_SIZE = 0; // px
	var UI_SELECTED_TABPANE_HEIGHT_SIZE = 0; // px
	var UI_EXPANDED_TABPANE_HEIGHT_SIZE = 220; // px
	var UI_VERTICAL_TABCONTENT_HEIGHT_SIZE = 0; // px
	var UI_VERTICAL_TABS_HEIGHT_SIZE = 0; // px

	var scrolling = false;
	var UI_RAPIDLINK_FX_SCROLLING = null; // Morph Fx obj
	var UI_RAPIDLINK_FX_EXPANDING = null; // Morph Fx obj
	var I_NAVBAR_TIMER = null;
	
	
	
 

/* The following section allows to init and launch instancied components (only if DOM il completly loaded) */

	// When DOM is ready.
	Event.observe(document, 'dom:loaded', function(event) {
		
		
//console.log("twm Some inits... !");
		UI_FOOTER_LINK_INIT_HEIGHT_SIZE = $('area_rapidlink').getHeight();
		new Effect.Morph($('area_rapidlink'), { style: "height:0px;", duration: 0.3, transition:Effect.Transitions.EaseFromTo });


		// Manage cookie garbage
//console.log("twm CookieJar !");
		if( typeof(CookieJar) != 'undefined' )
		{
			/*
			ui_data = jar.get('ui_data');
			if(ui_data == null) { jar.put('ui_data', {mode: 'ui_data_mode'}); }
			*/
		};



		// Make UI beautiful to prevent any problems.
//console.log("twm Make UI beautiful !");
		if( parseInt($$('#content').length) == 1 )
		{
			if( $('header').getHeight() + $('content').getHeight() < UI_MINIMAL_HEIGHT_SIZE ){ $('content').setStyle( {'height': UI_MINIMAL_HEIGHT_SIZE - $('header').getHeight() +'px'}); };
		};

		
		
		// URL Request Listener
		(function(){ 
			if( gan('Nous_Contacter')!="" ){

				 Shadowbox.open({
					  content:	"/Essai_Gratuit_Contact.html",
					  player:	"iframe",
					  title:		"Contactez-nous pour un conseil gratuit",
					  height:	558,
					  width:		650
				 });
				 
			}
		}).delay(0.2);



		// URL Request Listener
		(function(){ 
			if( gan('Inscription_Newsletter')!="" ){

				 Shadowbox.open({
					  content:	"/Inscription_Newsletter.html",
					  player:	"iframe",
					  title:		"Inscrivez-vous gratuitement &agrave; la newsletter BOOST !",
					  height:	310,
					  width:		650
				 });
				 
			}
		}).delay(0.3);






		// stopbubling
//console.log("twm stopbubling !");
		$$('.overlighted').each(function(o,i)
		{
			o.observe('click',function(e)
			{
				if( o.hasClassName('stopbubling') ){ Event.stop(e); }
				
				
			});
		});

		
		
		

		// Rapid Links handle expand/collapse
//console.log("twm Search Focus !");
		if( parseInt( $$('#handle_rl').length ) == 1 ) // This control exists !
		{
			$('handle_rl').observe('click', function(e){
				
				Event.stop(e);
				
				s = $(Event.element(e)).readAttribute('rel'); s = (s=='')? 0 : parseInt(s); s = 1 - s; // s=0:collapse,s=1:expand
				styleheight = (s==0)? "height:0px;" : "height:"+UI_FOOTER_LINK_INIT_HEIGHT_SIZE+"px;";
				$(Event.element(e)).writeAttribute('rel',s);
				
				var anchor = String($('handle_rl').readAttribute('href')).replace(/#/,"");
				UI_RAPIDLINK_FX_EXPANDING = new Effect.Morph($('area_rapidlink'), { 
					style: styleheight, 
					duration: 0.3, 
					transition:Effect.Transitions.EaseFromTo, 
					afterFinish: function(){
						scrollToEasing(anchor,e);
					}
				});
				
			});
		}
		
		
		
		
		// Handle behavior on embeded search engine
//console.log("twm Search Focus !");
		$('txt_search').observe('focus',function(e){
			Event.stop(e);
			/*Try.these(
				this.select(),
				this.focus()
			);/**/
			$('txt_search').up('#area_searchbox').addClassName('active');
			if( $('txt_search').value=="Rechercher" ){ $('txt_search').value=""; }
		});

		$('txt_search').observe('blur',function(e){
			Event.stop(e);
			/*Try.these(
				this.select(),
				this.focus()
			);/**/
			$('txt_search').up('#area_searchbox').removeClassName('active');
			if( $('txt_search').value=="" ){ $('txt_search').value="Rechercher"; }
		});



		// Disable search engine.
	/*
		$('txt_search').addClassName('disabled')
		$('txt_search').disabled = 'disabled';
		$('txt_search').disabled = true;
		
		$('btn_search').disabled = 'disabled';
		$('btn_search').disabled = true;
	/**/
		
		
		
		
		// Adjust the minimal height size for vertical tab content.
//console.log("twm pane_infodetail !");
		if( parseInt($$('#pane_infodetail').length) == 1 ){
			
			(function(){ 
				
				UI_VERTICAL_TABS_HEIGHT_SIZE = $$('.vertical_tab_bar ul li').length * ((parseInt($$('.vertical_tab_bar ul li').length)>0)? $($$('.vertical_tab_bar ul li')[0]).getHeight() : 0);

				UI_VERTICAL_TABCONTENT_HEIGHT_SIZE = parseInt($('pane_infodetail').getHeight()) + parseInt($('pane_infodetail').getStyle('marginTop').replace(/px/,'')) + parseInt($('pane_infodetail').getStyle('marginBottom').replace(/px/,''));
				$($('pane_infodetail').up('div.embedbox_shortwhite_content')).setStyle({ overflow: 'hidden' });

				new Effect.Morph($('pane_infodetail'), { 
					style: 'height:'+UI_VERTICAL_TABS_HEIGHT_SIZE+'px;', 
					duration: 0.1, 
					transition:Effect.Transitions.EaseFromTo,
					afterFinish: function(){
						
						UI_VERTICAL_SWINGPANEL_HEIGHT_SIZE = Math.max(UI_VERTICAL_TABCONTENT_HEIGHT_SIZE,UI_VERTICAL_TABS_HEIGHT_SIZE);

						new Effect.Morph($('pane_infodetail'), { 
							style: 'height:'+UI_VERTICAL_SWINGPANEL_HEIGHT_SIZE+'px;', 
							duration: 0.3, 
							transition:Effect.Transitions.EaseFromTo
						});
						
					}
					
				});
				
			}).delay(0.4);
			

		}
		
				
		
		
		// Compute the max height size for a tab pane (horizontal module)
		if( parseInt($$('#pane_infodetail').length) == 1 ){
			$('pane_infodetail').select('.tab_pane').each(function(p){
				UI_EXPANDED_TABPANE_HEIGHT_SIZE = Math.max( UI_EXPANDED_TABPANE_HEIGHT_SIZE , 20 + parseInt(p.getHeight()) + parseInt(p.getStyle('marginTop').replace(/px/,'')) + parseInt(p.getStyle('marginBottom').replace(/px/,'')) );
			});
		}
		
		

		// Vertical Tab controler.
//console.log("twm vertical_tab_bar !");
		$$('.vertical_tab_bar ul li a').each(function(vtab){										 
			
			var items = $$('.vertical_tab_bar ul li');
			
			vtab.observe('click', function(e){
				
				Event.stop(e);
			
				$A(items).map(function(elm){ elm.removeClassName('current'); });
				
				var current = Event.element(e);
				li = (current.tagName.toLowerCase() == "li")? current : current.up('li') ;
				li.addClassName('current');
				
				// Pane controler (show/hide tab_pane)
				var ph = $(li.down('a').readAttribute('rel'));
				var p = $$('#'+li.down('a').readAttribute('href').replace(/#/,'')+'.tab_pane')[0];

				new Effect.Morph(ph, { 
					style: 'height:'+UI_EXPANDED_TABPANE_HEIGHT_SIZE+'px;', 
					duration: 0.8, 
					transition:Effect.Transitions.SwingFromTo, 
					afterFinish: function(){
						
						// Hide all tab_pane
						ph.select('.tab_pane').each(function(pane){ pane.addClassName('hidded'); });
						
						// Show it with minimal visibility (force DOM to exists and allows to retreive box model on tab_pane)
						p.setOpacity(0.01);
						p.removeClassName('hidded');
						
						// Render efect to show the selected tab_pane
						new Effect.Appear(p, { 
							duration: 0.6, 
							transition: Effect.Transitions.EaseFromTo,
							afterFinish: function(){
								
								UI_SELECTED_TABPANE_HEIGHT_SIZE = parseInt(p.getHeight()) + parseInt(p.getStyle('marginTop').replace(/px/,'')) + parseInt(p.getStyle('marginBottom').replace(/px/,''));
								UI_VERTICAL_TABS_HEIGHT_SIZE = $$('.vertical_tab_bar ul li').length * ((parseInt($$('.vertical_tab_bar ul li').length)>0)? $($$('.vertical_tab_bar ul li')[0]).getHeight() : 0);
								UI_VERTICAL_SWINGPANEL_HEIGHT_SIZE = Math.max(UI_SELECTED_TABPANE_HEIGHT_SIZE,UI_VERTICAL_TABS_HEIGHT_SIZE);

								new Effect.Morph(ph, { 
									style: 'height:'+UI_VERTICAL_SWINGPANEL_HEIGHT_SIZE+'px;', 
									duration: 0.4, 
									transition:Effect.Transitions.EaseFromTo, 
									afterFinish: function(){
										
										window.location.hash = '#/'+li.down('a').readAttribute('href').replace(/#/,'');
										
										
									}
								});

							}
						});
						
					}
				});


			});
			
		});
	
		// Pane controler (show/hide tab_pane)
		// ... map behavior on each related panes (synchronized with its tab item)
//console.log("twm showPane !");
		var showPane = function(p,ph)
		{
			ph.select('.tab_pane').each(function(pane){
			//
				pane.addClassName('hidded');
			});
			p.setOpacity(0.01);
			p.removeClassName('hidded');
			
			new Effect.Appear(p, { duration: 1.3,  transition: Effect.Transitions.EaseFromTo });
		};
		
		
		
		
		
		
		// twm Accordion !
		// TODO : Create an expressive javascript Class for Prototype...
//console.log("twm Accordion !");
		$$('.accordion_handle').each(function(o){
		
			o.observe('click',function(){
		
				if( $(o.readAttribute('rel')).getStyle('height') == '1px' ){
				
					if($$('.accordion_handle').length > 1)
					{
						$$('.accordion_handle').each(function(elm){
						
							$(elm).addClassName('collapsed');
							$(elm).removeClassName('expanded');
							
							if( $(elm.readAttribute('rel')).getStyle('height') != '1px' )
							{
								_duration = (parseInt(String($(elm.readAttribute('rel')).getStyle('height')).replace(/px/,'')) > 1500)?  1.80 : ((0.25)*(parseInt( String($(elm.readAttribute('rel')).getStyle('height')).replace(/px/,'') )/300));
								$(elm.readAttribute('rel')).morph("height:1px;" , {duration: _duration });
							}
						
						});
					}
				  
					$(o.readAttribute('rel')).hide();
					$(o.readAttribute('rel')).setStyle({'height':'auto'});
					var h = $(o.readAttribute('rel')).getHeight();
					$(o.readAttribute('rel')).setStyle({'height':'1px'});
					$(o).addClassName('expanded');
					$(o).removeClassName('collapsed');
					$(o.readAttribute('rel')).show();
					_duration = (h > 1500)? 1.80 : ((0.20)*(h/300));
					$(o.readAttribute('rel')).morph('height:'+h+'px' , {duration: _duration});
				
				}
				else{
					if($$('.accordion_handle').length > 1)
					{
						$$('.accordion_handle').each(function(elm){
							$(elm).addClassName('collapsed');
							$(elm).removeClassName('expanded');
						});
					}
				
					$(o).addClassName('collapsed');
					$(o).removeClassName('expanded');
					_duration = (parseInt(String($(o.readAttribute('rel')).getStyle('height')).replace(/px/,'')) > 1500)?  1.80 : ((0.25)*(parseInt( String($(o.readAttribute('rel')).getStyle('height')).replace(/px/,'') )/300));
					$(o.readAttribute('rel')).morph("height:1px;" , {duration: _duration });
				}
		
			});
		
		});		
		




		// twm Src Rollup !
		// TODO : Create an expressive javascript Class for Prototype...
//console.log("twm srcRollUp !");
		if( typeof(srcRollUp) == 'object' || typeof(srcRollUp) == 'function' )
		{
			srcRollUp();
		}

		
		
		

		// hide RSS Feed icon.
		$$('a.rssfeed').each(function(o){ o.hide(); });
		
		
		
		


		// FBLike
		$("fbs").observe('click', function(e){
			Event.stop(e);
			$(Event.element(e)).writeAttribute('target','_blank');
			u	= location.href;
			t	= document.title;
			n	= "sharer";
		//	su	= "http://www.facebook.com/sharer.php?u="+encodeURIComponent(u)+"&t="+encodeURIComponent(t);
			su = $(Event.element(e)).readAttribute('href');
			options = { height:740, width:1000, scrollbars:'yes', toolbar:0, status:0 };
			window[n] = openCTKFORMPopup(su,n,options);
		});


		// Tweet
		$("tws").observe('click', function(e){
			Event.stop(e);
			u	= location.href;
			t	= document.title;
			n	= "sharer";
			maxLength = 140 - (u.length + 1);
			if (t.length > maxLength) { t = t.substr(0, (maxLength - 3))+'...'; }

			su	= "http://twitter.com/home?status="+encodeURIComponent(t + ' ' + u);
			options = { height:550, width:1000, scrollbars:'auto', toolbar:0, status:0 };
			window[n] = openCTKFORMPopup(su,n,options);
		});


		// LinkedIn
		$("lis").observe('click', function(e){
			Event.stop(e);
			u	= location.href;
			t	= document.title;
			n	= "sharer";

			su	= "http://www.linkedin.com/company/boost-cloud#"+encodeURIComponent(t);
			window[n] = window.open(su);
		});







	});

	// End DocumentReady



	// Script to scroll to the required anchor with easing transition.
	var scrollToEasing = function(anchor,event)
	{
		selectedPane = $(anchor);
		if(scrolling){ return false;}
		scrolling = true;
		
		UI_RAPIDLINK_FX_SCROLLING = new Effect.ScrollTo(selectedPane,{
			duration:0.8,
			transition:Effect.Transitions.EaseFromTo,
			afterFinish:function(){
				scrolling=false;
				window.location.hash = '#'+anchor;
			}
		});
		//Event.stop(e);
		return false;	
	};
 
 
 
	// Function to setup 
	function setupDemos()
	{
		 Shadowbox.setup('a.swfvideo', {
			  width: 340,
			  height: 356,
			  flashParams: {
					AllowScriptAccess: "always",
					allowFullScreen: "true"
			  }
		 });
	}



	Shadowbox.init({
		 overlayOpacity: 0.8,
		 handleOversize: "drag",
		 handleUnsupported: "remove"
	}, setupDemos);
	
	
	
	// Internal Search Engine
	var handleDoSearch = function()
	{
		if($('txt_search').value.strip()!="")
		{
			if( $('txt_search').hasClassName('disabled') ){return false;}
			
			
			domain = ($('txt_search').readAttribute('data') == null)? window.location.hostname : ( ($('txt_search').readAttribute('data').isJSON() == true)? ($('txt_search').readAttribute('data').evalJSON().target):"www.boost-cloud.com" );
			window.location = "http://" + domain + "/Rechercher.html?q="+encodeURIComponent( $('txt_search').value.strip() );
		}
	}
	
	var handleEnterSearch = function(evt)
	{
		if($('txt_search').value.strip()!="")
		{
			if( $('txt_search').hasClassName('disabled') ){return false;}
			//IE fix
			if (typeof evt == "undefined"){ evt = window.event; }
			
			var keycode = evt.keyCode || evt.which;
			if (keycode == 13)
			{
				domain = ($('txt_search').readAttribute('data') == null)? window.location.hostname : ( ($('txt_search').readAttribute('data').isJSON() == true)? ($('txt_search').readAttribute('data').evalJSON().target):"www.boost-cloud.com" );
				window.location = "http://" + domain + "/Rechercher.html?q="+encodeURIComponent( $('txt_search').value.strip() );
			}
			
		}
	}





	
	
	
	/*{twm}, 2009. Packed GUP*/function gup(name){name=name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");var regexS="[\\?&]"+name+"=([^&#]*)";var regex=new RegExp(regexS);var results=regex.exec(window.location.href);if(results==null)return "";else return results[1];}
	
	/*{twm}, 2009. Packed GAN*/function gan(name){name=name.replace("#","");var regexS=""+name+"";var regex=new RegExp(regexS);var results=regex.exec(window.location.hash);if(results==null)return "";else return results[1];}
	
	
	/*****************************************************
	* popup
	* 19/08/2001
	*
	* Open a simple nice popup window with url,width, height, centered
	*
	* --Sébastien Brémond--
	* http://www.tocra.org
	*****************************************************/
	function openCTKFORMPopup(sURI,sWinName,o){
		FULLSCREEN_const = 'no';
		sh = parseInt(screen.availHeight || screen.width);
		sw = parseInt(screen.availWidth || screen.height);
		pH = o.height;
		pW = o.width;
		hasSB = o.scrollbars;
		hasTB = o.toolbar;
		hasS	= o.status;
		return open(sURI,sWinName,'top='+((sh-pH)/2)+',left='+((sw-pW)/2)+',width='+pW+',height='+pH+',scrollbars='+hasSB+',toolbar='+hasTB+',status='+hasS+',fullscreen='+FULLSCREEN_const);
	}
	
