/*--FUNCTIONS.JS--*/

function pngFix(){
	$$(".pngFix").each(function(el){
		$(el).firstChild.style.visibility = "hidden";
		$(el).style.display = "block";
		$(el).style.width = $(el).firstChild.width+"px";
		$(el).style.height = $(el).firstChild.height+"px";
		$(el).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+$(el).firstChild.src+"', sizingMethod='crop');";
	});
}

function expandCollapseCharacter(mode, element){
	switch(mode){
		case 'expand':
			$(element).removeClassName('active');
			$(element).nextSiblings()[0].addClassName('active');
			var calcHeight = $('illustration').descendants()[0].getHeight();
			$$('#top, #top .container').each(function(el){
				el.setStyle({'height':(calcHeight+90)+'px'});
			});
			new Effect.Morph('illustration', {
				duration: 0.5,
				transition: Effect.Transitions.sinoidal,
				style: 'height:'+calcHeight+'px'
			});
			break;
		case 'collapse':
			$(element).removeClassName('active');
			$(element).previousSiblings()[0].addClassName('active');
			new Effect.Morph('illustration', {
				duration: 0.5,
				transition: Effect.Transitions.sinoidal,
				style: 'height:492px',
				afterFinish: function(){
					$$('#top, #top .container').each(function(el){
						el.setStyle({'height':'492px'});
					});
					$('top').scrollTo();
				}
			});
			break;
		default :
			alert('mff!');
			break;
	}
}

//Fonctions de gestion de la Greybox
function greyBoxShow(page, close, rel_path){
	if(!$('overlay')){
		$('bottom').insert({after:'<div id="overlay"></div>'});
	}
	if(!$('#greyBox')){
		$('overlay').insert({after:'</div><div id="greyBox"><div class="top"></div><div class="content" id="greyBoxContent"></div><div class="bottom"></div><div onclick="greyBoxHide();" class="close">'+close+' <img src="'+rel_path+'medias/img/greybox/close.gif" alt="" /></div>'});
	}
	var navigatorVersion = Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
	if(navigatorVersion){
		var totalHeight = $('top').getHeight()+$('bottom').getHeight();
		$('overlay').setStyle({'display':'block','height':totalHeight+'px'});
		new Ajax.Request(page, {
			onComplete: function(transport){
				pngFix();
				$('greyBox').setStyle({'display':'block'});
				$('greyBoxContent').update(transport.responseText);
				$$('select').each(function(el){el.setStyle({'display':'none'});});
				$('top').scrollTo();
			}
		});
	}
	else{
		$('overlay').setOpacity(0);
		$('overlay').setStyle({'display':'block'});
		$('overlay').fade({
			duration: 0.5,
			from: 0,
			to: 0.75,
			afterFinish: function(){
				new Ajax.Request(page, {
					onComplete: function(transport){
						$('greyBox').setStyle({'display':'block'});
						$('greyBoxContent').update(transport.responseText);
						$('top').scrollTo();
					}
				});
			}
		});
	}
	return false;
}
function greyBoxHide(){
	var navigatorVersion = Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
	if(navigatorVersion){
		$('overlay').setStyle({'display':'none'});
		$('greyBox').setStyle({'display':'none'});
		$('select').each(function(el){el.setStyle({'display':'block'});});
	}
	else{
		$('greyBox').fade({
			duration: 0.5,
			from: 1,
			to: 0,
			afterFinish: function(){
				$('overlay').fade({
					duration: 0.3,
					from: 0.75,
					to: 0
				});
			}
		});
	}
}

Event.observe(window,'load',function(){

	//Parsage du block images screenshots characters
	compteur = 0;
	$$('#parseImgs img').each(function(el){
		el.style.cursor = 'pointer';
		el.id = 'chracter_img_'+compteur;
		Event.observe(el, 'click', function(){
			var stockId = this.id.substr(13, this.id.length);
			var urlString = get_link.substr(0, get_link.length-5);
			if(mode == 'characters'){
				urlString = urlString+'_images_'+stockId+'.html';
			}
			else if(mode == 'videos'){
				urlString = urlString+'_'+childNode+'_videos_'+stockId+'.html';
			}
			else if(mode == 'images'){
				urlString = urlString+'_'+childNode+'_images_'+stockId+'.html';
			}
			document.location.href = urlString;
		});
		compteur ++;
	});
	compteur = 0;
	$$('#parseImgs img').each(function(el){
		if(mode == 'characters'){
			if(compteur == current_img){
				var futureSrc = el.src.replace('/_thumbs', '/'+identifier);
				$('characterBgImg').insert({bottom: '<img src="'+futureSrc+'" />'});
			}
		}
		else if(mode == 'images'){
			if(compteur == current_img){
				var futureSrc = el.src.replace('/thumbs', '');
				if($('downloadLink')){
					$('downloadLink').href = futureSrc;
				}
				$('characterBgImg').insert({bottom: '<img src="'+futureSrc+'" />'});
			}
		}
		else if(mode == 'videos'){
			if(compteur == current_video){
				var futureSrc = get_media_path+'medias/videos/'+identifier2+'_'+current_video+'.wmv';
				if($('downloadLink')){
					$('downloadLink').href = futureSrc;
				}
			}
		}
		compteur ++;
	});

	//illustration loader Hiding
	if($('illustration')){
		$('illustration').setStyle({'background':'none'});
	}

	//menuLang swapDepth
	if($('menuLang')){
		Event.observe('lang', 'mouseover', function(){
			$('commingSoon').setStyle({'zIndex':'3'})
		});
		Event.observe('lang', 'mouseout', function(){
			$('commingSoon').setStyle({'zIndex':'21'})
		});
	}

	//coming soon effect
	/*Effect.SlideUp('commingSoonContent', {
		duration: 0.5,
		transition: Effect.Transitions.sinoidal
	});
	Event.observe('expdClps', 'click', function(){
		if(!this.hasClassName('active')){
			this.addClassName('active');
			Effect.SlideDown('commingSoonContent', {
				duration: 0.5,
				transition: Effect.Transitions.sinoidal
			});
		}
		else{
			this.removeClassName('active');
			Effect.SlideUp('commingSoonContent', {
				duration: 0.5,
				transition: Effect.Transitions.sinoidal
			});
		}
	});*/
	
	//Custom scrolls creation
	compteur = 1;
	$$('.scroll').each(function(el){
	
		var heighCheck = el.nextSiblings()[0].className.substr(7, el.nextSiblings()[0].className.length-1);
		heighCheck = parseInt(heighCheck);
		
		if(el.nextSiblings()[0].getHeight() > heighCheck){
			currentScroller = 1;
		
			el.down(1).id = 'track_'+compteur;
			Event.observe(el.down(1), 'mouseover', function(){
				currentScroller = this.id.substr(6,this.id.length-1);
			});
			var trackHeight = heighCheck-18;
			el.down(1).setStyle({'height':trackHeight+'px'});
			el.down(2).id = 'slider_'+compteur;
			el.setStyle({'display':'block'});
			
			el.nextSiblings()[0].addClassName('scrollContent');
			el.nextSiblings()[0].setStyle({'height':heighCheck+'px'});
			el.nextSiblings()[0].descendants()[0].id = 'scrollContent_'+compteur;
			$('scrollContent_'+compteur).setStyle({'position':'relative'});
			
			new Control.Slider('slider_'+compteur, 'track_'+compteur, {
				axis: 'vertical',
				onSlide: function(value){
					var calc = Math.ceil(value * ($('scrollContent_'+currentScroller).getHeight()-$('scrollContent_'+currentScroller).ancestors()[0].getHeight()));
					calc = -calc;
					$('scrollContent_'+currentScroller).setStyle({'top':calc+'px'});
				},
				onChange: function(value){
					var calc = Math.ceil(value * ($('scrollContent_'+currentScroller).getHeight()-$('scrollContent_'+currentScroller).ancestors()[0].getHeight()));
					calc = -calc;
					$('scrollContent_'+currentScroller).setStyle({'top':calc+'px'});
				}
			});
			
			compteur++;
		}
		else{
			var trackHeight = el.nextSiblings()[0].getHeight()-18;
			el.down(1).setStyle({'height':trackHeight+'px'});
		}
		
	});
	
	//Accordions creation
	compteur = 1;
	$$('.accordion').each(function(el){
		el.id = 'accordion_'+compteur;
		compteur++;
	});
	$$('.accordion .title').each(function(el){
		if(!el.hasClassName('active')){
			el.nextSiblings()[0].setStyle({'height':'0px'});
		}
		else{
			el.nextSiblings()[0].setStyle({'height':el.nextSiblings()[0].descendants()[0].getHeight()+'px'});
		}
		Event.observe(el, 'click', function(){
			if(!this.hasClassName('active') || el.nextSiblings()[0].getHeight() == 0){
				$$('#'+this.ancestors()[0].id+' .title').each(function(el){
					if(el.hasClassName('active')){
						el.removeClassName('active');
						new Effect.Morph(el.nextSiblings()[0], {
							duration: 0.5,
							transition: Effect.Transitions.sinoidal,
							style: 'height:0px;'
						});
					}
				});
				this.addClassName('active');
				new Effect.Morph(this.nextSiblings()[0], {
					duration: 0.5,
					transition: Effect.Transitions.sinoidal,
					style: 'height:'+this.nextSiblings()[0].descendants()[0].getHeight()+'px;'
				});
				
				var currentClick = this.nextSiblings()[this.nextSiblings().length-1];
				if(currentClick.hasClassName('expdClpse')){
					currentClick.removeClassName('expand');
					if(Prototype.Browser.IE){
						new Effect.Morph(currentClick, {
							duration: 0.5,
							transition: Effect.Transitions.sinoidal,
							style: 'margin-top:0px;'
						});
					}
				}
			}
		});
	});
	
	//Expand / Collapse column button gestion
	$$('.col .expdClpse').each(function(el){
		el.setStyle({'display':'block'});
		Event.observe(el, 'click', function(){
			this.toggleClassName('expand');
			$$('#'+this.ancestors()[0].id+' .title').each(function(el){
				if(el.hasClassName('active')){
					if(el.nextSiblings()[0].getHeight() != 0){
						if(Prototype.Browser.IE){
							var currentClick = el.nextSiblings()[el.nextSiblings().length-1];
							new Effect.Morph(currentClick, {
								duration: 0.5,
								transition: Effect.Transitions.sinoidal,
								style: 'margin-top:-200px;'
							});
						}
						new Effect.Morph(el.nextSiblings()[0], {
							duration: 0.5,
							transition: Effect.Transitions.sinoidal,
							style: 'height:0px;'
						});
					}
					else{
						if(Prototype.Browser.IE){
							var currentClick = el.nextSiblings()[el.nextSiblings().length-1];
							new Effect.Morph(currentClick, {
								duration: 0.5,
								transition: Effect.Transitions.sinoidal,
								style: 'margin-top:0px;'
							});
						}
						new Effect.Morph(el.nextSiblings()[0], {
							duration: 0.5,
							transition: Effect.Transitions.sinoidal,
							style: 'height:'+el.nextSiblings()[0].descendants()[0].getHeight()+'px;'
						});
					}
				}
			});
		});
	});

});

