var current_image;

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(){
	$$('.gallery-item-thumb').each(function(thumb){
		thumb_cssid=thumb.id;
		gid=parseInt(thumb_cssid.substr(4));
		Event.observe(thumb,'click',show_gallery_1.bind(this,gid));
	});
}

function show_gallery_1(gid){
	new Ajax.Request('multimedia-fetch.php', {
		method:'get',
		parameters: { id:gid },
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport){
			gallery=transport.responseText.evalJSON(true);
			if(gallery.items.length<1) return;
			$$('object').each(function(obj){ obj.style.visibility='hidden'; });
			$$('embed').each(function(obj){ obj.style.visibility='hidden'; });
			$('overlay').style.display='';
			new Effect.Appear('overlay-transparence', { duration: 0.5,from:0,to:0.8,afterFinish:show_gallery_2.bind(this,gallery) });
		}
	});
}

function show_gallery_2(gallery){
	if(gallery.type=='img'){
		current_image=0;
		images_count=gallery.items.length;
		photo=gallery.items[current_image];
		if(photo.title=='') title=(current_image+1)+'/'+images_count;
		else title=photo.title+' ('+(current_image+1)+'/'+images_count+')';
		new_html='<div id="overlay-frame-loader" style="width:690px;padding:10px 30px 10px;"><div style="display:none;" id="overlay-sx"></div><div style="display:none;" id="overlay-dx"></div><img id="overlay-close" src="img/overlay-x.gif" alt="X" />';
		new_html+='<img style="visibility:hidden;" id="inserted-media" src="'+photo.img_url+'" alt="'+title+'" title="'+title+'" />';
		new_html+='<div id="caption">'+title+'</div>';
		new_html+='</div>';
		$('overlay-content').update(new_html);
		Event.observe('overlay-sx','click',show_image.bind(this,gallery,-1));
		Event.observe('overlay-dx','click',show_image.bind(this,gallery,1));
		resize_image();
	}else{/*vid*/
		video=gallery.items[0];
		new_html=
			'<div id="overlay-frame" style="width:640px;padding:10px;"><img id="overlay-close" src="img/overlay-x.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="comicon-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='+video.video_url+'&img_name='+video.img_url+'" />'+
				'<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="comicon-player.swf" flashvars="flv_name='+video.video_url+'&img_name='+video.img_url+'" type="application/x-shockwave-flash"/>'+
			'</object></div>';
		if(video.title!='') new_html+='<div id="caption">'+video.title+'</div>';
		new_html+='</div>';
		$('overlay-content').update(new_html);
	}
	
	Event.observe('overlay-close', 'click', x_clicked_1);	
}

function resize_image(){
	var new_img=new Image();
	new_img.src=$('inserted-media').src;
	
	if(new_img.complete){
		win_size=getWindowSize();
		max_w=win_size[0];if(max_w<1) max_w=1; if(max_w>690) max_w=690;
		max_h=win_size[1]-80;if(max_h<1) max_h=1;
//		media_w=parseInt($('inserted-media').width);
//		media_h=parseInt($('inserted-media').height);
		media_w=parseInt(new_img.width);
		media_h=parseInt(new_img.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-loader').style.width=(media_w/ratio)+'px';
		$('overlay-sx').style.top=$('overlay-dx').style.top=(media_h/(2*ratio)+3)+'px';
		$('overlay-frame-loader').style.backgroundPosition=(media_w/(2*ratio)+14)+'px '+(media_h/(2*ratio)-6)+'px';
		$('inserted-media').style.visibility='';
		$('overlay-sx').style.display=$('overlay-dx').style.display='';
	}else setTimeout(resize_image,100);
}

function show_image(gallery,offset){
	$('inserted-media').style.visibility='hidden';
	$('overlay-sx').style.display=$('overlay-dx').style.display='none';
	images_count=gallery.items.length;
	current_image+=offset;
	if(current_image<0) current_image=images_count-1;
	if(current_image>=images_count) current_image=0;

	photo=gallery.items[current_image];
	if(photo.title=='') title=(current_image+1)+'/'+images_count;
	else title=photo.title+' ('+(current_image+1)+'/'+images_count+')';
	$('inserted-media').src=photo.img_url;
	$('inserted-media').alt=$('inserted-media').title=title;
	$('caption').update(title);
	setTimeout(resize_image,100);
}

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';
	$$('object').each(function(obj){ obj.style.visibility=''; });
	$$('embed').each(function(obj){ obj.style.visibility=''; });

}

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