/* gallery.js */
var pageGalleryId, pageGalleryName;

function listGallery(galleryId, galleryName, pageNumber)
{
	var displayBox = $('#result-content');
	pageGalleryId = galleryId;
	pageGalleryName = galleryName;
	$.ajax({
		url: 'gallery_list.php',
		type: 'get',
		data: 'id=' + galleryId + '&type=' + galleryName + '&page=' + pageNumber,
		dataType: 'json',
		beforeSend: function(){},
		success: function(data){
			displayBox.html(data.result);
			$('.match-date').html(data.information['date']);
			$('.match-name').attr('title', data.information['title']).html(data.information['title']);
			$('.match-description .data').html(data.information['detail']);
			
			$("a[rel=album] img").each(function(){
				var imgHeight = $(this).height();
				if(imgHeight<103){
					$(this).css({
						'margin' : imgHeight/4
					});
				}else if(imgHeight>103){
					$(this).css({
						'margin' : -(imgHeight-100)/2
					});
				}
			});
			
			$("a[rel=album]").fancybox({
				showCloseButton: false,
				showNavArrows: false,
				titleShow: 	false,
				cyclic:		true,
				overlayColor: 	'#000',
				overlayOpacity:	0.5,
				padding: 0,
				onComplete: function(){
					/* 
					if(galleryName=='media'){
						var filePath = 'download.php?file=' + this.title;
						var modalImage = $('#fancybox-img');
						
						modalImage
							.height(modalImage.height()-6)
							.width(modalImage.width()-6)
							.css('border', 'solid 3px #FFF')
							.wrap('<div style="position: relative;"></div>')
							.after('<a href="' + filePath + '" target="_blank" class="button-download"></a>');
					} */
					
					var modalImage = $('#fancybox-img');
					modalImage
						// .height(modalImage.height()-6)
						// .width(modalImage.width()-6)
						// .css('border', 'solid 3px #FFF')
						.wrap('<div style="position: relative;"></div>')
						// .after('<a href="#" target="_blank" class="button-download"></a>');
						.after(function(){
							var result = '';
							
							result+= '<li><a class="goPrev" href="javascript:void(0)" onclick="goPrev()"><em>prev</em></a></li>';
							result+= '<li><a class="goPlay" href="javascript:void(0)" onclick="goPlay()"><em>play</em></a></li>';
							result+= '<li><a class="goNext" href="javascript:void(0)" onclick="goNext()"><em>next</em></a></li>';
							if(galleryName=='media'){
								var filePath = 'stocks/media/' + this.alt;
								// result+= '<li><a class="imgDownload" href="' + filePath + '" target="_blank"><em>download</em></a></li>';
								result+= '<li><a class="imgDownload" href="javascript:void(0)" onclick="window.open(\'' + filePath + '\', \'Download\')"><em>download</em></a></li>';
							}else{
								result+= '<li><a class="imgDownload" href="javascript:void(0)" style="cursor: default;">&nbsp;</a></li>';
							}
							result+= '<li><a class="goClose" href="javascript:void(0)" onclick="goClose()"><em>close</em></a></li>';
							
							return '<ul class="slideNav">' + result + '</ul>';
						});
						
					/* $('.slideNav em')
						.css({'opacity': 0})
						.hover(
							function(){ $(this).stop().css({'opacity': 1}); },
							function(){ $(this).stop().css({'opacity': 0}); }
						); */
				}
			});
		}
	});
}

var slide = false;
var clearSlide = function(){
	clearInterval(slide);
	slide = false;
};

var goPrev = function(){
	$.fancybox.prev();
	if(slide) clearSlide();
};

var goNext = function(){
	$.fancybox.next();
	if(slide) clearSlide();
};

var imgDownload = function(){
	
}

var goClose = function(){
	$.fancybox.close();
	if(slide) clearSlide();
};

var goPlay = function(){
	if(!slide){
		slide = setInterval("doSlide()", 4000);
	}else{
		clearSlide();
	}
};

var doSlide = function(){
	$.fancybox.next();
};

// ARE YOU READYYYYYYYYY ...
$(document).ready(function(){
	
	/* Gallery: navigation effect */

	
	/* Select album */
	$('#album-selector').bind({
		'change': function(){
			var galleryId = parseInt($(this).val());
			var galleryName = $(this).attr('name');
			listGallery(galleryId, galleryName, 1);
		}
	});
	
});
