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

	$('.button').mouseup(function() {
		$(this).blur();
	});

	$('.scroll-pane').jScrollPane({
		showArrows: true
	});

	var newwindow = '';
	$('.ve a').click(function(){
		if (!newwindow.closed && newwindow.location) {
			newwindow.location.href = $('.ve a').attr('href');
		} else {
			newwindow=window.open($('.ve a').attr('href'), 'Ekskursija', 'height=700, width=700, titlebar=no, close=no');
			if (!newwindow.opener) newwindow.opener = self;
		}
		if (window.focus) {
			newwindow.focus()
		}
	});

	$('.language-links .a1, .language-links .a2, .language-links .a3').click(function(){
		var self = this;
		var ts = new Date();
		ts = ts.getTime();
		$.get(webroot + lang + '/textsize/' + $(self).attr('rel') + '?t=' + ts);
		$.doAfter(300, function() {
			document.location = $(self).attr('href');
		});
		return false;
	});
});

/**
 * Fix background height
 */
function fixBg() {
	if($('#container').height() < 520) $('#container').height(520);
}

/**
 * Bind Fancybox
 */
function bindFancybox() {
//	$('.pop').fancybox({
//		titlePosition:'inside'
//	});
}

/**
 * Video in new window
 */
var newvideowindow = '';

function openVideoWindow(el, title) {
	if (!newvideowindow.closed && newvideowindow.location) {
		newvideowindow.location.href = $(el).attr('href');
	} else {
		newvideowindow = window.open($(el).attr('href'), title, 'height=446, width=590, titlebar=no, close=no');
		if (!newvideowindow.opener) newvideowindow.opener = self;
	}
	if (window.focus) {
		newvideowindow.focus()
	}
}

/**
 * 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
 */
function writemail(p1, p2, span_id)
{
	name = p1+'@'+p2;

	$('#'+span_id).html('<a href="mailto:'+p1+'@'+p2+'">'+name+'</a>');
}

$.fn.pagination = function() {
	$(this).each(function() {
		var el = $(this);

		if ($(el).length) {
			$(el).find('#paginator a, .pagination-link a').each(function() {
				$(this).click(function(){
					if ($(this).attr('rel') > '') {
						el = $(this).attr('rel');
					}

					if ($(el).length) {
						$.get($(this).attr('href'), function(html){
							$(el).html(html)
								 .pagination();
						});

						return false;
					}

					return false;
				});
			});
		}
	});
};

(function($){
	$.doAfter = function(time,f) {
		$('body').animate({opacity: 1}, time, f);
	};
})(jQuery);
