// JavaScript Document

var WindowSize;

var addthis_config = {"data_track_clickback":true};

// *****************************************************************************************

function showLang() {
	
	l = $('lang');
	if (!l.visible()) {
		l.show();
	}
	
}

// *****************************************************************************************

function hideLang() {
	
	l = $('lang');
	if (l.visible()) {
		l.hide();
	}
	
}

// *****************************************************************************************

function hideBanners() {
	$$('[class="banner"]').each(function(e) { 
		e.hide();
	});
}

// *****************************************************************************************

function showBanners() {
	$$('[class="banner"]').each(function(e) { 
		e.show();
	});
}

// *****************************************************************************************

function showPreview() {
	if (!$('preview').visible()) {
		$('preview').setStyle( { 'height' : $$('body')[0].getHeight() + 'px' } );
		hideBanners();
		new Effect.Appear('preview', { duration : 1.0 });
	}
}

// *****************************************************************************************

function clearHash() {
	window.location = window.location.href.replace( /#.*/, "");
	return false;
}

// *****************************************************************************************

function hidePreview() {
	if ($('preview').visible()) {
		new Effect.Fade('preview', { duration : 1.0 });
//	clearHash();
		showBanners();
	}
}

// *****************************************************************************************

function getOptimalImageHeight() {
	vh = document.viewport.getDimensions().height;
	if (vh<600) { oh = 480; }
	else if (vh>900) { oh = 750; }
	else { oh = 600; }
	return oh;
}

// *****************************************************************************************

function clickPhoto(e) {
	previewPhoto($('image_' + e).name, true);
}

// *****************************************************************************************

function showPhotoData(data, okolni) {
//	alert(data);
	$('gallery_name').update(data[4]);
	$('gallery_name').href = '/cz/fotogalerie/' + data[5];
	
	if (okolni && data[0]) {
		$('image_previous').src = 'http://i.autosportmedia.eu/'+data[0]+'/h/75';
		$('image_previous').name = data[0];
		$('image_previous').show();
		$('arl').show();
	} else {
		$('image_previous').hide();
		$('arl').hide();
	}
	
	if (okolni && data[1]) {
		$('image_next').src = 'http://i.autosportmedia.eu/'+data[1]+'/h/75';
		$('image_next').name = data[1];
		$('image_next').show();
		$('arr').show();
	} else {
		$('image_next').hide();
		$('arr').hide();
	}
	pi = '#' + data[2] + ' &bull; ' + data[6] + 'x';
	if (data[3]) { pi += ' &bull; ' + data[3]; }
	$('photo_info').update(pi);
}

// *****************************************************************************************

function getPhotoData(cislo, okolni) {
	new Ajax.Request('/ajax/photodata.php?id=' + cislo, {
		onSuccess: function(t) {
			showPhotoData(t.responseText.split('|'), okolni);			
			}
		}
	);
}

// *****************************************************************************************

function getPhotoTags(cislo, tag) {
//	alert(cislo + ' : ' + tag);
	new Ajax.Request('/ajax/phototags.php?id=' + cislo + '&j=' + encodeURIComponent(tag), {
		onSuccess: function(t) {
			$('taginput').value = '';
			res = t.responseText;
			if (res) {
				$('tags').update(res);
				$('tags').show();
			} else {
				$('tags').hide();
			}
			}
		}
	);
}

function setErrorTag(id) {
	if (confirm(TEXT_DOTAZ_JEZDEC_NENI_NA_FOTOGRAFII)) {
		new Ajax.Request('/ajax/seterrortag.php?id=' + id, {
			onSuccess: function(t) {
				alert(TEXT_PRIPOMINKA_ODESLANA);
				}
			}
		);
	}
}

// *****************************************************************************************

function previewPhoto(cislo, okolni) {
	showPreview();
	$('image').src = '/design/dovrchu.cz/loading15.gif';
//	$('image').hide();
	$('image').src = 'http://i.autosportmedia.eu/' + cislo + '/h/' + getOptimalImageHeight();
	$('preview_center').setStyle({ 'top' : (document.viewport.getScrollOffsets().top + 10) + 'px' });
	$('image').show();
	$('image').name = cislo;
	
	getPhotoData(cislo, okolni);
	getPhotoTags(cislo, '');
	document.location.hash = cislo;
}

// *****************************************************************************************

function checkHash() {
	h = document.location.hash.substring(1, 10);
	if (h && !isNaN(h)) {
		previewPhoto(h, true);
	}
}

// *****************************************************************************************

function startOnLine(fu) {
	new PeriodicalExecuter(function(peOnline){
								
		new Ajax.Request('/ajax/online.php?fu=' + fu, {
			onSuccess: function(t) {
				$('online').update(t.responseText);
				new Effect.Highlight('online');
				}
			}
		);
								
	}, 120);
}

// *****************************************************************************************

function logoColor(e) {
	$(e.id).src = 'http://i.autosportmedia.eu/partneri/' + e.id + '.png';
}

// *****************************************************************************************

function logoBW(e) {
	$(e.id).src = 'http://i.autosportmedia.eu/partneri/' + e.id + '_bw.png';
}

// *****************************************************************************************
// *****************************************************************************************

Event.observe(window, 'load', function() { 

	checkHash();
	new Effect.Fade('lang', { duration : 3.0 } );
	new Ajax.Autocompleter("taginput", "tagselectfromlist", "/ajax/pilotbystring.php", { minChars: 4 });


});

