/**
 * Executed when document is finished loading elements into DOM
 */
$(document).ready(function() {

	$('.confirm').click(function(){
		return confirm('Vai tiešām vēlaties turpināt?');
	});

	// replace all emails to js
	$('.email').each(function(){
		var email = $(this).text().replace('[at]', '@');
		$(this).html('<a href="mailto:'+email+'">'+email+'</a>');
	});

	if(typeof sIFR == "function"){
		if (lang == 'ru') {
			var font = 'font-ru.swf';
		} else {
			var font = 'font-all.swf';
		}

		sIFR.replaceElement('h1', named({
			sFlashSrc: webroot+'swf/'+font,
			sWmode:'transparent',
			sBgColor:'#f2eee2',
			sColor:'#666666'
		}));
	}

	$('input[type=text], textarea').focus(function(){
		if ($(this).attr('rel') == $(this).val()) {
			$(this).val('');
		}
	});

	$('input[type=text], textarea').blur(function(){
		if ($(this).val() == '' && $(this).attr('rel') != '') {
			$(this).val($(this).attr('rel'));
		}
	});


		$('.pop').fancybox({
			padding: 15
		});


	if ($('#video').length > 0) {
		var filename = $('#video input').val();

		var flashvars = {
			videoURL:webroot+'videos/'+filename
		};

		var params = {
			menu: 'true',
			scale: 'noScale',
			allowFullscreen: 'true',
			allowScriptAccess: 'always',
			bgcolor: '#000000'
		};

		var attributes = {
			id:'videoPlayer'
		};

		swfobject.embedSWF(
			webroot+'swf/player.swf',
			'video',
			'538',
			'353',
			'9.0.15',
			webroot+'swf/expressInstall.swf',
			flashvars,
			params,
			attributes
		);
	}

	if ($('#history-timeline').length > 0) {

		var flashvars = {
			xmlURL:webroot+lang+'/history/timeline'
		};

		var params = {
			menu: 'false',
			scale: 'noScale',
			allowFullscreen: 'true',
			allowScriptAccess: 'always',
			bgcolor: '#f2f0e1'
		};

		var attributes = {
			id:'historyTimeline'
		};

		swfobject.embedSWF(
			webroot+'swf/timeline.swf',
			'history-timeline',
			'550',
			'100%',
			'9.0.15',
			webroot+'swf/expressInstall.swf',
			flashvars,
			params,
			attributes
		);
	}

	$('map area').hover(
		function(){
			if (!$(this).hasClass('active')) {
				if ($.browser.msie) {
					$('#'+$(this).attr('rel')).show();
				} else {
					$('#'+$(this).attr('rel')).stop(false, true).fadeIn('fast');
				}
			}
		},
		function(){
			if (!$(this).hasClass('active')) {
				if ($.browser.msie) {
					$('#'+$(this).attr('rel')).hide();
				} else {
					$('#'+$(this).attr('rel')).stop(false, true).fadeOut('fast');
				}
			}
		}
	);

	$('area').focus(function(){
		$(this).blur();
	})

});

/**
 * Print email link (protection from spambots)
 * If javascript is disabled, nothing is visible
 *
 * @param string p1 Part 1 - this is username
 * @param string p2 Part 2 - this is domain and TLD
 * @param string name (optional) Display value
 */
function writemail(p1, p2, name) {
	if (typeof(name) == "undefined") {
		name = p1+"@"+p2;
	}

	document.write("<a href='mailto:"+p1+"@"+p2+"'>"+name+"</a>");
}

/**
 * Redirect user to specified page
 *
 * @param string url Address to redirect to
 */
function redirect(url) {
	document.location = url;
}

/**
 * Load/Show gallery item
 *
 * @param int id GalleryItem ID
 */
function showGalleryItem(id) {
	$('.gallery-item').hide();
	$('#gallery-item-'+id).show();
	$('.gallery .thumbs a').removeClass('active');
	$('.inline-gallery .thumbs a').removeClass('active');
	$('#gallery-thumb-'+id).addClass('active');
}

function nextGalleryItem() {
	var full_id = $('.thumbs .active ~ a').eq(0).attr('id');
	if (full_id) {
		var id = full_id.substr(14);
		showGalleryItem(full_id.substr(14));
	}
}


function prevGalleryItem() {
	var full_id = $('.thumbs .active').prev().attr('id');
	if (full_id) {
		var id = full_id.substr(14);
		showGalleryItem(full_id.substr(14));
	}
}

function printview(el) {
	window.open($(el).attr('href'));
	return false;
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}

	return IsNumber;
}

function resizeSWF(height) {
	if (IsNumeric(height)) {
		height = (height+20)+'px';
	}
	$('#history-container').css('height', height);
}