$.clientCoords = function() {
     var dimensions = {width: 0, height: 0};
     if (document.documentElement) {
         dimensions.width = document.documentElement.offsetWidth;
         dimensions.height = document.documentElement.offsetHeight;
     } else if (window.innerWidth && window.innerHeight) {
         dimensions.width = window.innerWidth;
         dimensions.height = window.innerHeight;
     }
     return dimensions;
}

function openVideoDialog(flvPath,path) {
	var dialog = '#flvdialog'+path.substr(1);
	if($(dialog).html() == null) {
		var insideHTML = '<div class="videoUnder"><div class="videoDate">'+$(path+' .videoDate').html()+'</div><img src="'+$(path).find('.videoUnder').find('img').attr('src')+'" style="position: relative; top: -4px;" /></div>';
		var insideButtons = $(path+' .videoButtons').html();
		$(path).append('<div id="flvdialog'+path.substr(1)+'" class="videoDialog"><div class="videoDialogInner clear-block"><center><div class="videoDialogBox"><div class="videoBorder"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="650" height="360" id="flash video test" align="middle">'
		+'<param name="allowScriptAccess" value="sameDomain" />'
		+'<param name="FlashVars" value="flvsource='+flvPath+'&skin=/themes/fightclub/flash/skin.swf"/>'
		+'<param name="allowFullScreen" value="false" />'
		+'<param name="movie" value="/themes/fightclub/flash/videoPlayer.swf" />'
		+'<param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />'
		+'<embed src="/themes/fightclub/flash/videoPlayer.swf" flashvars="flvsource='+flvPath+'&skin=/themes/fightclub/flash/skin.swf" quality="high" wmode="gpu" bgcolor="#000000" width="650" height="360" name="flash video test" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />'
		+'</object></div><div class="videoDialogText">'+insideHTML+'</div></div></center><div class="videoDialogButtons form-submit">'+insideButtons+'</div></div></div>');	
	}
	$(dialog).dialog({
		bgiframe: true,
		height: 500,
		width:700,
		modal: true
	});
	$(dialog).dialog('open');
	return false;
}
function closeVideo(ident){
	$(ident).dialog("destroy");
}
var imgLoaded = new Array();

function finishImageOpen(imgPath,dialog,img) {
	var clientSize = $.clientCoords();
	if(!imgLoaded[imgPath]) {
		var width = $(img).width();
		imgLoaded[imgPath] = width;
	} else {
		var width = imgLoaded[imgPath];
	}
	if(clientSize['width'] < width) {
		width = clientSize['width']-50;
	} 
	
	
	$(imgPath).width(width);
	var height = $(imgPath).height();
	$(dialog).dialog({
		bgiframe: true,
		modal: true,
		width: width+50,
		height: 'auto',
		position:'center'
	});
	$(dialog).dialog('open');
	$('.imageNavi').width(width);
	
	
}
//'<a href="javascript:newImage(1420,\''+dialog+'\')">TEST</a>
var galleryTrans = false;
var galLength = 0;

function openImageDialog(flvPath,path) {
	var dialog = '#flvdialog'+path.substr(1);
	var id = path.substr(4);
	var imgPath = '#flvdialog'+path.substr(1)+' #imageViewed';
	if(!galleryTrans) {
		galleryTrans = new Array();
		var ct = 0;
		for ( var i in galleryItems)
		{
			galleryTrans[ct] =  galleryItems[i];
			ct = ct +1;
		}
		galLength = galleryTrans.length;
	}

	if($(dialog).html() == null) {
		if(galleryItems[id]['index'] == 0) {
			pid = galleryTrans[galLength-1]['id'];
		} else {
			pid = galleryTrans[galleryItems[id].index-1]['id'] 
		}
		if(galleryItems[id]['index'] == galLength -1) {
			nid =  galleryTrans[0]['id'];
		} else {
			nid = galleryTrans[galleryItems[id].index+1]['id'];
		}
		
		  var blank = "";
		  
		  if (typeof videogallery_blank_image != 'undefined')
		  {
			   blank = videogallery_blank_image;
		  }
		  else
		  {
			   blank = "/themes/fightclub/img/gallery/nextlabel.gif";
		  }

		var onClickPrev = '<div onClick="newImage('+pid+',\''+dialog+'\')" class="imageNaviCont leftGo"><img src="'+blank+'" /></div>';
		var onClickNext = '<div onClick="newImage('+nid+',\''+dialog+'\')" class="imageNaviCont" style="text-align:right"><img src="'+blank+'" /></div>';
		var naviButtons = '<div class="imageNavi">'+onClickPrev+onClickNext+'</div>';

		var insideHTML = '<div class="videoUnder"><div class="videoDate">'+$(path+' .videoDate').html()+'</div>'+$(path+' .videoUnder').html()+'</div>';
		var insideButtons = $(path+' .videoButtons').html();
		$(path).append('<div id="flvdialog'+path.substr(1)+'" class="videoDialog"><div class="videoDialogInner clear-block"><center><div class="videoDialogBox">'
		+'<div class="videoBorder"><center>'+naviButtons+'<img id="imageViewed" src="'+flvPath+'"/></center>'
		+'<div class="videoDialoTgext">'+insideHTML+'</div></div></center><div class="videoDialogButtons form-submit">'+insideButtons+'</div></div></div>');	
		$(imgPath).load(function(){ finishImageOpen(imgPath,dialog,this);});
	} else {
		finishImageOpen(imgPath,dialog,this);
	}
	return false;
}
function closeImage(ident){
	$(ident).dialog("destroy");
}

$(document).ready(function(){
	$('#showmesel').change(function() {
		$('#showme').get(0).submit();
	});
	if(typeof showID != 'undefined') {
		if(galleryType == 'image') {
			openImageDialog(galleryItems[showID]['path'],'#flv'+galleryItems[showID]['id']);
		} else {
			openVideoDialog(galleryItems[showID]['path'],'#flv'+galleryItems[showID]['id']);
		}
	}
	 $('.imageNaviCont').live('mouseover',function(){
		  var new_src = "";
		  
		  if($(this).hasClass('leftGo'))
		  {
			   if (typeof videogallery_prev_image != 'undefined')
			   {
					new_src = videogallery_prev_image;
			   }
			   else
			   {
					new_src = "/themes/fightclub/img/gallery/prevlabel.gif";
			   }
		  }
		  else
		  {
			   if (typeof videogallery_next_image != 'undefined')
			   {
					new_src = videogallery_next_image;
			   }
			   else
			   {
					new_src = "/themes/fightclub/img/gallery/nextlabel.gif";
			   }
		  }
		  $(this).find('img').attr('src', new_src);
	 });
	 $('.imageNaviCont').live('mouseout',function() {
		  var new_src = "";
		  
		  if (typeof videogallery_blank_image != 'undefined')
		  {
			   new_src = videogallery_blank_image;
		  }
		  else
		  {
			   new_src = "/themes/startrek/img/gallery/blank.png";
		  }
		  $(this).find('img').attr('src', new_src);
	 });
});

function newImage(id,old) {
	 var new_src = "";
		  
	 if (typeof videogallery_blank_image != 'undefined')
	 {
		  new_src = videogallery_blank_image;
	 }
	 else
	 {
		  new_src = "/themes/fightclub/img/gallery/nextlabel.gif";
	 }
	 
	 $('.imageNaviCont').find('img').attr('src', new_src);
	 $(old).dialog("destroy");
	 openImageDialog(galleryItems[id]['path'],'#flv'+galleryItems[id]['id']);
}
