var height;
var width;
var imgHeight;

function initBackgroundDiv() {
	$('#mainHolder').height($(window).height());
	$('#mainHolder').width($(window).width());
	height = $('#mainHolder').height();
	width = $('#mainHolder').width();
	imgHeight = Math.ceil(width * 0.75);
}

function loadStartGal() {
	$.ajax({
		url: "./ajax.php",
		type: 'POST',
		data: '_load=init',
		dataType: "json",
		success: function(data) {
			showImages(data, true);
		}
	});
}

function initShowFile() {
	$('.showFile').unbind('click');
	$('.showFile').click(function() {
		$('#dataHolder').hide();
		$.ajax({
			url: "./ajax.php",
			type: 'POST',
			data: '_load=file&_url=' + $(this).attr('href'),
			success: function(data) {
				$('#dataHolder div').html(data);
				$('#dataHolder').show(350);
				$('#imgSliderHolder').cycle('pause');
				Cufon.refresh();
			}
		});
		return false;
	});
}

function initLoadGallery() {
	$('.loadGallery').unbind('click');
	$('.loadGallery').click(function() {
		$('#dataHolder').hide();
		$.ajax({
			url: "./ajax.php",
			type: 'POST',
			data: '_load=dir&_url=' + $(this).attr('href'),
			dataType: 'json',
			success: function(data) {
				_gaq.push(['_trackPageview', '/' + data.url]);
				showImages(data, false);
			}
		});
		return false;
	});
}

function loadGallery(path) {
	$('#dataHolder').hide();
	$.ajax({
		url: "./ajax.php",
		type: 'POST',
		data: '_load=dir&_url=' + path,
		dataType: 'json',
		success: function(data) {
			showImages(data, false);
		}
	});
	return false;
}

function onAfter(currSlideElement, nextSlideElement, options, forwardFlag) {
	$('#infoContent2').html($('#imgSliderHolder li:visible .infoText').html());
	$('#logoHolder img').attr('src', $('#logoHolder img').attr('src').replace(/(.*)[0-9]\.png/, '$1' + $('#imgSliderHolder li:visible .logoType').html() + '.png'));
	$('#infoHolder div').html('');
	if($('#imgSliderHolder li:visible .infoPath').html().length > 1) {
		$('#infoLink').show();
	} else {
		$('#infoLink').hide();
	}

	Cufon.refresh();
} 

function showImages(data, init) {
	var template = '<li><img src="##SRC##" alt="##INFO##" /><div class="topPos">##TOP##</div><div class="infoText">##INFO##</div><div class="logoType">##LOGO##</div><div class="infoPath">##PATH##</div></li>';
	var len = data.img.length;
	if(typeof init == "undefined" || !init) {
		var hash = data.url.replace(/[\.\/]?(.*)\/?/, "$1");
		hash = hash.replace(/\//g, "|");
		$(location).attr('hash', hash);
		init = false;
	}
	
	$('#imgSliderHolder').cycle('destroy');
	$('#imgSliderHolder').html('');
	
	for(var i = 0; i < len; i++) {
		var help = template.replace('##SRC##', data.url + '/' + data.img[i].file + '.jpg');
		help = help.replace(/##INFO##/g, data.img[i].desc);
		help = help.replace(/##TOP##/g, data.img[i].top);
		help = help.replace(/##LOGO##/g, data.img[i].logo);
		if(data.img[i].info) {
			help = help.replace(/##PATH##/g, data.url.replace("fotos", "inc") + '/' + data.img[i].info);
		} else {
			help = help.replace(/##PATH##/g, '');
		}
		$('#imgSliderHolder').append(help);
	}
	
	if(len > 1) {
		if((typeof init != "undefined") && init) {
			$('#imgSliderHolder').cycle({
				fx: 'fade', 
				speed: 2500,
				timeout: 5000, 
				after: onAfter
			});
		} else {
			$('#prevNextHolder').show();
			$('.current').html('1');
			$('.total').html('/' + len);
			$('#imgSliderHolder').cycle({
				fx: 'fade', 
				speed: 2500,
				timeout: 0,
				prev: $('#prev'),
				next: $('#next'),
				after: onAfter,
				onPrevNextEvent: function(isNext, zeroBasedSlideIndex, slideElement) {
					$('.current').html((zeroBasedSlideIndex + 1));
					$('#transparent').click();
					$('#infoLink').hide();
				}
			});
		}
	}
	
	$('#imgSliderHolder img').each(function() {
		$(this).css('width', width);
		//var top = parseInt($('.topPos', $(this).parent()).html()) * -1;
		var screenHeight = $(window).height();
		if($('.topPos', $(this).parent()).html() == "B"){
			$(this).css('marginTop', (screenHeight - imgHeight + 80) + "px");
		}
	});
	
}

function initMouseEvents() {
	$('#dataHolderCloser').click(function() {
		$('#dataHolder div').html('');
		$('#dataHolder').hide(350);
		$('#imgSliderHolder').cycle('resume');
	});
	
	$('#infoItem2').mouseenter(function() {
		$('#infoContent2').show(350);
	}).mouseleave(function() {
		$('#infoContent2').hide(350);
	});
	
	$('#infoLink').click(function() {
		var url = $('#imgSliderHolder li:visible .infoPath').html();
		if($('#infoHolder div').html().length > 1) {
			$('#infoLink').hide('fast');
			$('#infoHolder').animate({
				width: '0px'
			}, 1000, function() {
				$('#infoHolder div').html('');
			});
		} else {
			$.ajax({
				url: url,
				type: 'GET',
				success: function(data) {
					$('#infoHolder div').html(data);
					$('#infoLink').hide('fast');
					$('#infoHolder').animate({
						width: '500px'
					}, 1000);
				}
			});
		}
	});
	
	$('#infoHolder').click(function() {
		if($('#imgSliderHolder li:visible .infoPath').html().length > 1) {
			$('#infoLink').show('fast');
			$('#infoHolder').animate({
				width: '0px'
			}, 1000, function() {
				$('#infoHolder div').html('');
			});
		}
	});
	
	$('#transparent').click(function() {
		if($('#dataHolderCloser').is(':visible')) {
			$('#dataHolderCloser').click();
		}
		if($('#infoHolder').is(':visible')) {
			$('#infoHolder').click();
		}
	});
	
	$('#footerNav > li').slice(0, 2).mouseenter(function() {
		$('#transparent').click();
	});
	
	$('#logoHolder').click(function() {
		$('#transparent').click();
	});
}

function initCufon() {
	Cufon.replace('h2', {
		fontFamily: 'Myriad Web Pro'
	});
	Cufon.replace($('#infoItem'), {
		fontFamily: 'Myriad Web Pro',
		fontSize: '14px'
	});
	Cufon.replace($('#footerNav'), {
		fontFamily: 'Myriad Web Pro',
		fontSize: '14px'
	});
	Cufon.refresh();
}

$('#transparent').click(function(event) {
	if(event.button == 2) {
		return false;
	}
});

$(document).ready(function() {
	initBackgroundDiv();
	if($(location).attr("hash")) {
		var path = $(location).attr("hash").substr(1);
		loadGallery(path.replace(/\|/g, "/"));
	} else {
		loadStartGal();
	}
	
	$(window).resize(function() {
		initBackgroundDiv();
		if($(location).attr("hash")) {
			var path = $(location).attr("hash").substr(1);
			loadGallery(path.replace(/\|/g, "/"));
		} else {
			loadStartGal();
		}
	});

	initShowFile();
	initLoadGallery();
	initMouseEvents();
	initCufon();
});
