//Ilic Goran 21.01.2008:16:46
function archiveSwitcher(self,id, plus, moins){
	var El=document.getElementById(id);
	
	if(El.style.display=="none"){
		self.src=moins;
		El.style.display='block';
		
	} else {
		self.src=plus;
		El.style.display='none';
		
	}
}


function switcher(o){
	
	
	//GET ELEMENT if id is in object
	
	var ElLaucher=$(o.laucher);
	
	ElLaucher.removeAttribute('onmouseover');
	
	if(ElLaucher.getAttribute('isInit')!=1){
		
		ElLaucher.setAttribute('isInit', 1);
		
		var ElBox=$(o.box);
		
		//bind 
		var bind=this;
		
		if(ElLaucher.getAttribute('key')=='plus'){
			ElLaucher.setAttribute('key','moins');
		} else {
			ElLaucher.setAttribute('key','plus');
		}
		//mk event for button
		Event.observe(ElLaucher, 'click', function(event) {
				var children=ElBox.immediateDescendants();
				
				children.each(function(v){
				  if(v.classNames()=='bordurePetit'){
				    test = v.immediateDescendants();
				    test.each(function(asd) {
              if(asd.classNames()==o.hideClass){
    						if(asd.style.display=='block'){
    							asd.style.display='none';
    						} else {
    							asd.style.display='block';
    						}
    					}
            });
				 
          }
					if(v.classNames()==o.hideClass){
						if(v.style.display=='block'){
							v.style.display='none';
						} else {
							v.style.display='block';
						}
					}
					
				});
				
				
				
				var key=ElLaucher.getAttribute('key');

				if(key=='plus'){
					ElLaucher.src=o.img.normal.moins;
					ElLaucher.setAttribute('key','moins');
				} else {
					ElLaucher.src=o.img.normal.plus;
					ElLaucher.setAttribute('key','plus');
				}

		});
		
		Event.observe(ElLaucher, 'mouseover', function(event) {
			
			var key=ElLaucher.getAttribute('key');
			
			if(key=='plus'){
				ElLaucher.src=o.img.hover.plus;
			} else {
				
				ElLaucher.src=o.img.hover.moins;
			}
		});
		
		Event.observe(ElLaucher, 'mouseout', function(event) {
			var key=ElLaucher.getAttribute('key');
			
			if(key=='plus'){
				ElLaucher.src=o.img.normal.plus;
			} else {
				ElLaucher.src=o.img.normal.moins;
			}	
		});
		
	}
}

function btnHover(El, isHover){
	var regex="\-hover\.(.){3}$";
	var src=El.src;
	
	if(src.match(regex)){
		
		var ext=src.substring(src.length-4,src.length);
		var normalPath=src.substring(0,src.length-10);
		
		El.src=normalPath+ext;
	
	} else {
		
		var ext=src.substring(src.length-4,src.length);
		var normalPath=src.substring(0,src.length-4);
		src=normalPath+'-hover'+ext;	
		El.src=src;

	}

}

function appendScript(src, charset){
	var date = new Date();
	var id='append-'+date.getTime();
	var head = document.getElementsByTagName('head')[0];
	var script = document.createElement('script');
	
	
	script.type = 'text/javascript';
	script.id = id;
	if (charset) script.charset = charset;
	script.src = src;
	head.appendChild(script);
	
	return id;
}


var ActuP={
	colLeft:'root_col_left',
	colRight:'root_col_right',
	content:{
		left:'content_left',
		right:'content_right'
	},
	filter:{
		archive_small:'mbArchivePetit',
		archive_big:'mbArchiveGrand'
	},
	ajax_loader:'loader-ajax',
	proxy:(window.parent.document.location.protocol=='https:' ? 'https://layout-tsr-new.xobix.ch/tsr_fre_layout/index.html?&siteSect=100035&isSnippet=true&url=':'http://www.tsr.ch/tsr/index.html?siteSect=100035&isSnippet=true&url='),
	//proxy:(window.parent.document.location.protocol=='https:' ? 'https://layout-tsr.xobix.ch/tsr_fre_layout/index.html?&siteSect=100035&isSnippet=true&url=':'http://www.tsr.ch/tsr/index.html?siteSect=100035&isSnippet=true&url='),
	//proxy:'http://www.tsr.ch/tsr/index.html?siteSect=100035&isSnippet=true&url=',
	//proxy:'https://layout-tsr-video.xobix.ch/tsv_fre_layout/index.html?&siteSect=20213&isSnippet=true&url=',
	//proxy:'https://layout-tsr.xobix.ch/tsr_fre_layout/index.html?&siteSect=100035&isSnippet=true&url=',
	
	
	Request:function(url, transporter){
		
		new Ajax.Request(url,{
			method:'get',
			onSuccess:transporter
		});
	},
	
	getVideo:function(o){
		$(this.ajax_loader).style.display="block";
		//création de l'url d'origin
		var origin='http://www3.tsr.ch/tsr/index.html?siteSect=20211&block=Bvideo&isSnippet=true';
		//on encode l'url
		origin=escape(origin);
		//on recupère le context de l'objet
		var bind=this;
		//on execute la requête et la réponse est gérée par dspVideo
		
		this.Request(this.proxy+(origin), function(T){ bind.dspVideo(T, o); });
		
	},
	
	getStory:function(id){
		window.scrollTo(0,0);
		$(this.ajax_loader).style.display="block";
		var ElLeft=$(this.root_col_left);
		var ElRight=$(this.root_col_right);
		
		var story='http://www3.tsr.ch/tsr/index.html?siteSect=20210&isSnippet=true&sid=';
		var moreInfo='http://www3.tsr.ch/tsr/index.html?siteSect=20212&isSnippet=true&sid=';
		
		story=this.proxy+escape(story+id);
		moreInfo=this.proxy+escape(moreInfo+id);
		
		var bind=this;
		this.Request(story, function(T){bind.dspStory(T, 'left')});
		this.Request(moreInfo, function(T){bind.dspStory(T, 'right')});
	},
	getGallery:function(id, idCurrentImg){
		var origin='http://www3.tsr.ch/tsr/index.html?siteSect=20211&block=Bgallery&isSnippet=true';
		var jsFile='http://www.tsr.ch/scripts/galleryLinker/init_gallery.js';
		
		this.galleryScriptId=appendScript(jsFile);
		
		origin=escape(origin);
		
		var bind=this;
		
		this.Request(this.proxy+origin, function(T){ bind.getGalleryTemplate(T) });
		
		new PeriodicalExecuter(function(pe) {
			
			var jsInitReady=(typeof ajax_init=='function');
			
			if(jsInitReady && bind.galleryTemplate){
				bind.defaultContent({
					action:'hide',
					parent_id:bind.content.left,
					classFilter:bind.filter.archive_big
				});
				
				bind.defaultContent({
					action:'hide',
					parent_id:bind.content.right,
					classFilter:bind.filter.archive_small
				});
				
				new Insertion.Top(bind.colLeft, '<div id="dynamicEl-left" >'+bind.galleryTemplate+'</div>');
				new Insertion.Top(bind.colRight, '<div id="dynamicEl-right" >'+bind.dspGoBack()+'</div>');
				
				var position=($(idCurrentImg).firstChild.nodeValue);
				ajax_init(id,position);
				
				pe.stop();
			}
			
		}, 0.3);
	},
	getGalleryTemplate:function(T){
		this.galleryTemplate=T.responseText;
	},
	
	storyBack:function(){
		
		this.deleteAjaxContent();
		
	
		this.defaultContent({
							
			action:'display',
			parent_id:this.content.left,
			classFilter:this.filter.archive_small
		});
		
		this.defaultContent({
							
			action:'display',
			parent_id:this.content.right,
			classFilter:this.filter.archive_big
		});

		var element = document.getElementsByTagName('style');
		for (var i=0 ; i<element.length ; i++)
			element[i].style.display='none';
		
		if($('goBack')){
			
			$('goBack').remove();
		}
	
	},
	goBack:function(){
		this.storyBack();
		if(this.galleryScriptId){
			$(this.galleryScriptId).remove();
		}
		
	},
	getAudio:function(o){
		
		//url to origin server
		var origin='http://www3.tsr.ch/tsr/index.html?siteSect=20211&block=Baudio&isSnippet=true';
		//encode the url
		origin=escape(origin);
		this.mkLoader();
		
		//bind current content
		var bind = this;
		this.Request(this.proxy+origin, function(T){bind.dspAudio(T, o)});
	},
	
	
	dspAudio:function(T, o){
		var template=new Template(T.responseText);
		var result = template.evaluate(o);
		this.insertion(result, this.colLeft);
	},
	
	dspVideo:function(T, o){
		
		//création du template
		var template=new Template(T.responseText);
		o.height=o.height+20;
		
		//application de variable au template
		var result = template.evaluate(o);
		
		this.defaultContent({
			action:'hide',
			parent_id:this.content.left,
			classFilter:this.filter.archive_big
		});
		
		this.defaultContent({
			action:'hide',
			parent_id:this.content.right,
			classFilter:this.filter.archive_small
		});
		
		this.insertion(result, this.colLeft);
		new Insertion.Top(this.colRight, this.dspGoBack());
		
		//content_right
		$('goBack').style.display='block';
		$(this.ajax_loader).style.display="none";

	},
	dspGoBack:function(){
		var html='';

		html+='<div id="goBack" class="storyMoreInfoHeader" style="margin-bottom:5px">';
		html+='<p><a href="javascript:ActuP.goBack();">Retour</a></p>';
		html+='<table cellspacing="0" cellpadding="0" border="0" class="tbl_print"/>';
		
		
		html+='</div>';
		if(!$('goBack')) return html;
		
	},
	dspStory:function(T, position){
		
		
		if(position!='left' && position!='right') position='left';
		
		if(position=='left'){
			this.storyLeft=T.responseText
			
		} else {
			this.storyRight=T.responseText
			
		}
		if(this.storyRight && this.storyLeft){
			
			
			
			
			
			this.defaultContent({
				action:'hide',
				parent_id:this.content.left
			});
			
			this.defaultContent({
				action:'hide',
				parent_id:this.content.right
			});
			
			this.deleteAjaxContent();
			
			new Insertion.Top(this.colLeft, '<div id="dynamicEl-left" >'+this.storyLeft+'</div>');
			new Insertion.Top(this.colRight, '<div id="dynamicEl-right" ><table>'+this.storyRight+'</table></div>');
			
			this.storyRight='';
			this.storyLeft='';
			
			
			$(this.ajax_loader).style.display="none";
			
			$('goBack').remove();
			
			
		}

	},
	defaultContent:function(o){
		//o -> param: parent_id, action, classFilter
		if(!o.parent_id) return;
		
		if(o.action=='display'){
			var cssDisplay='block';
		} else {
			var cssDisplay='none';
		}
		/*
		if(this.IE6()){
			var children= new Array();
			var i=1;
			var noeuds=$(o.parent_id).firstChild;
			children[0]=noeuds;
			
			while (noeuds!=null) {
				alert(noeuds.nodeName);
				noeuds = noeuds.nextSibling;
				children[i]=noeuds;
				i++;
				
			}
			
		}else {
			
			var children=$(o.parent_id).immediateDescendants();
		}*/
		
		var children=$(o.parent_id).immediateDescendants();
		children.each(function(v){
			
			if(!v.hasClassName(o.classFilter)){
				
				if(v.nodeName!='SCRIPT'){
					
					v.style.display=cssDisplay;
				} else {
					
				}	
			}
		});
		
		
		
	},
	IE6:function(){
		var is_major = parseInt(navigator.appVersion);
		var agt=navigator.userAgent.toLowerCase();
		var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
		
		return (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
	},
	insertion:function(str){
		
		
		
		var insertEl=$(this.colLeft);
		
		if(insertEl.firstChild!=null){
			var insertEl=insertEl.firstChild;
				
		}
		
		if(insertEl.nextSibling!=null){
			//si ie on ignore le nextSibiling du a des diférance entre les arbres dom ff et ie
			if(!(!!(window.attachEvent && !window.opera))){
				var insertEl=insertEl.nextSibling;
			}
		}
		//div
		this.deleteAjaxContent();
		
		new Insertion.Top(this.colLeft, '<div id="dynamicEl-left" >'+str+'</div>');
		
		
	},
	
	deleteAjaxContent:function(){
		
		if($('dynamicEl-left')){
			//$('dynamicEl-left').innerHTML='';
			//$('dynamicEl-left').remove();
			$(this.colLeft).removeChild($('dynamicEl-left'));
			
			if($('dynamicEl-right')){
				//$('dynamicEl-right').innerHTML='';
				//$('dynamicEl-right').remove();
				$(this.colRight).removeChild($('dynamicEl-right'));
			}
		}
		
	}
}