function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

function gallery_init(){
	$$('.news-photo-gallery').each(function(gallery){
		news_id=parseInt(gallery.id.substr(19));
		gallery_length=$$('#news-photo-gallery-'+news_id+' .photo-gallery-box').length;
		gallery_pages=Math.ceil( gallery_length / 3);
		$$('#news-photo-gallery-'+news_id+' .gallery-inner')[0].style.width=(gallery_pages*564+10)+'px';
		if(gallery_length>3){
			$('photo-gallery-nav-dx-'+news_id).style.display='';
			Event.observe('photo-gallery-nav-sx-'+news_id,'click',nav_sx_clicked.bind(this,news_id,'photo'));
			Event.observe('photo-gallery-nav-dx-'+news_id,'click',nav_dx_clicked.bind(this,news_id,'photo'));
		}
		for(i=0;i<$$('#news-photo-gallery-'+news_id+' .photo-gallery-thumb').length;i++){
			thumb=$$('#news-photo-gallery-'+news_id+' .photo-gallery-thumb')[i];
			Event.observe(thumb,'click',show_gallery_1.bind(this,news_id,i,'photo'));
		}
	});
	$$('.news-video-gallery').each(function(gallery){
		news_id=parseInt(gallery.id.substr(19));
		gallery_length=$$('#news-video-gallery-'+news_id+' .video-gallery-box').length;
		gallery_pages=Math.ceil( gallery_length / 2);
		$$('#news-video-gallery-'+news_id+' .gallery-inner')[0].style.width=(gallery_pages*572+10)+'px';
		if(gallery_length>2){
			$('video-gallery-nav-dx-'+news_id).style.display='';
			Event.observe('video-gallery-nav-sx-'+news_id,'click',nav_sx_clicked.bind(this,news_id,'video'));
			Event.observe('video-gallery-nav-dx-'+news_id,'click',nav_dx_clicked.bind(this,news_id,'video'));
		}
		for(i=0;i<$$('#news-video-gallery-'+news_id+' .video-gallery-thumb').length;i++){
			thumb=$$('#news-video-gallery-'+news_id+' .video-gallery-thumb')[i];
			Event.observe(thumb,'click',show_gallery_1.bind(this,news_id,i,'video'));
		}
	});
}

function show_gallery_1(news_id,index,type){
	$('overlay').style.display='';
	new Effect.Appear('overlay-transparence', { duration: 0.5,from:0,to:0.8,afterFinish:show_gallery_2.bind(this,news_id,index,type) });
}

function show_gallery_2(news_id,index,type){
	file_to_display=$$('#news-'+type+'-gallery-'+news_id+' .'+type+'-gallery-thumb')[index].alt;
	if(type=='photo'){
		new_html='<div id="overlay-frame" style="width:960px;"><img id="overlay-close" src="img/closebox.gif" alt="X" /><img id="inserted-media" src="'+file_to_display+'" alt="" /></div>';
		$('overlay-content').update(new_html);
		show_gallery_3();
	}else{
		new_html=
			'<div id="overlay-frame" style="width:640px;height:400px;"><img id="overlay-close" src="img/closebox.gif" alt="X" /><div id="inserted-media"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+
				'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"'+
				'width="640"'+
				'height="400"'+
				'id="flash-video">'+
				'<param name="movie" value="graphilm-player.swf" />'+
				'<param name="quality" value="high" />'+
				'<param name="bgcolor" value="#000000" />'+
				'<param name="allowscriptaccess" value="sameDomain" />'+
				'<param name="allowfullscreen" value="false" />'+
				'<param name="flashvars" value="flv_name='+file_to_display+'&img_name=img/video-standard-bg.gif" />'+
				'<param name="wmode" value="transparent" />'+
				'<embed id="flash-video_" width="640" height="400"'+
					'allowscriptaccess="sameDomain" allowfullscreen="false" quality="high" bgcolor="#000000" wmode="transparent" name="videoswf"'+
					'src="graphilm-player.swf" flashvars="flv_name='+file_to_display+'&img_name=img/video-standard-bg.gif" type="application/x-shockwave-flash"/>'+
			'</object></div></div>';
		$('overlay-content').update(new_html);
	}
	
	Event.observe('overlay-close', 'click', x_clicked_1);	
}

function show_gallery_3(){
	if($('inserted-media').complete){
		win_size=getWindowSize();
		max_w=win_size[0];if(max_w<1) max_w=1; if(max_w>940) max_w=940;
		max_h=win_size[1]-80;if(max_h<1) max_h=1;
//		media_w=parseInt($('inserted-media').naturalWidth);
//		media_h=parseInt($('inserted-media').naturalHeight);
		media_w=parseInt($('inserted-media').width);
		media_h=parseInt($('inserted-media').height);
		ratio_w=media_w/max_w;
		ratio_h=media_h/max_h;
		ratio=(ratio_w>ratio_h) ? ratio_w : ratio_h;
		if(ratio<1) ratio=1;
		$('inserted-media').style.height=(media_h/ratio)+'px';
		$('overlay-frame').style.width=(media_w/ratio)+'px';
	}else setTimeout(show_gallery_3,100);
}

function nav_sx_clicked(news_id,type){
	hide_navs(news_id,type);
	offset=(type=='video' ? 572 : 564);
	inner=$$('#news-'+type+'-gallery-'+news_id+' .gallery-inner')[0];
	new Effect.Move(inner, {x:offset,y:0,duration:0.5, afterFinish:set_navs.bind(this,news_id,type)});
}

function nav_dx_clicked(news_id,type){
	hide_navs(news_id,type);
	offset=(type=='video' ? 572 : 564);
	inner=$$('#news-'+type+'-gallery-'+news_id+' .gallery-inner')[0];
	new Effect.Move(inner, {x:-offset,y:0,duration:0.5, afterFinish:set_navs.bind(this,news_id,type)});	
}

function hide_navs(news_id,type){
	$(type+'-gallery-nav-sx-'+news_id).style.display='none';
	$(type+'-gallery-nav-dx-'+news_id).style.display='none';
}

function set_navs(news_id,type){
	offset=(type=='video' ? 572 : 564);
	inner=$$('#news-'+type+'-gallery-'+news_id+' .gallery-inner')[0];
	gallery_pages=(parseInt(inner.style.width)-10)/offset;
	active_page=-parseInt(inner.style.left)/offset;
	if(active_page>0) $(type+'-gallery-nav-sx-'+news_id).style.display='';
	if(active_page<gallery_pages-1) $(type+'-gallery-nav-dx-'+news_id).style.display='';
}

function x_clicked_1(){
	$('overlay-content').update('');
	new Effect.Fade('overlay-transparence', { duration:0.5,from:0.8,to:0,afterFinish:x_clicked_2 });
}

function x_clicked_2(){
	$('overlay').style.display='none';
}

Event.observe(window, 'load', gallery_init);