$(document).ready(function() {

$("#logo").tipsy({gravity: "w"});
$("#fb_button").tipsy({gravity: "s"});
$("#fb_fanbox").tipsy({gravity: "e", fade: true});
$("#author").tipsy({gravity: "se"});
$("#change_avatar").tipsy({gravity: "s"});

$("#comments_box > table").tsort("",{order:"desc",attr:"id"});

if ($("#comments_box > table").size() == 0) {
	$("#comments_box").html("Nėra komentarų.");
}

$("a[rel=example_group]").fancybox({
	'titlePosition'	: 'over',
	'padding'	: 0,
	'overlayColor'	: '#000',
	'overlayOpacity': 0.5,
	'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
	    return '<span id="fancybox-title-over">' +  (currentIndex + 1) + ' / ' + currentArray.length + ' - ' + title + '</span>';
	}
});

$("#gMaps").fancybox({
	'width'		: '75%',
	'height'	: '75%',
	'autoScale'	: false,
	'padding'	: 0,
	'type'		: 'iframe',
	'overlayColor'	: '#000',
	'overlayOpacity': 0.5
});

$(".youtube").click(function() {
	$.fancybox({
	'overlayColor'		: '#000',
	'overlayOpacity'	: 0.5,
	'padding'		: 0,
	'autoScale'		: false,
	'transitionIn'		: 'none',
	'transitionOut'		: 'none',
	'title'			: this.title,
	'width'			: 640,
	'height'		: 390,
	'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
	'type'			: 'swf',
	'swf'			: {
		'wmode'		: 'transparent',
		'allowfullscreen'	: 'true'
	}
	});

	return false;
});

$(".login_field").focus(function () {
	if($(this).val() == this.defaultValue) {
		$(this).val("");
	}
});

$(".login_field").blur(function () {
	if($(this).val().length == 0) {
		$(this).val(this.defaultValue);
	}
});

$("#question_box").focus(function () {
	if($(this).val() == this.defaultValue) {
		$(this).val("");
	}
});

$("#question_box").blur(function () {
	if($(this).val().length == 0) {
		$(this).val(this.defaultValue);
	}
});

$("#comment_box").focus(function () {
	if($(this).val() == this.defaultValue) {
		$(this).val("");
	}
});

$("#comment_box").blur(function () {
	if($(this).val().length == 0) {
		$(this).val(this.defaultValue);
	}
});

$("#home_score").focus(function () {
	if($(this).val() == this.defaultValue) {
		$(this).val("");
	}
});

$("#home_score").blur(function () {
	if($(this).val().length == 0) {
		$(this).val(this.defaultValue);
	}
});

$("#away_score").focus(function () {
	if($(this).val() == this.defaultValue) {
		$(this).val("");
	}
});

$("#away_score").blur(function () {
	if($(this).val().length == 0) {
		$(this).val(this.defaultValue);
	}
});

$("#home_score").keydown(function(event) {
	if (event.keyCode == 46 || event.keyCode == 8  || event.keyCode == 9) {

	} else {

		if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) {

		} else {
			event.preventDefault();
		}
	}
});

$("#away_score").keydown(function(event) {
	if (event.keyCode == 46 || event.keyCode == 8  || event.keyCode == 9) {

	} else {

		if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) {

		} else {
			event.preventDefault();
		}
	}
});

$("#post_question").click(function() {
	if($("#question_box").val() != document.getElementById('question_box').defaultValue) {

	$("#post_question").val("Siunčiama...");

	$.ajax({
	type: "POST",
	url: "question.php",
	data: "text=" + $("#question_box").val() + "&conference=" + $("#conf").val(),
	cache: false,
	success: function(html){
		$("#question_form").html(html);
	}
	});

	} else {
		alert("Neužpildėte klausimo.");
	}
});

$(".game_url").click(function() {
	$("#game_video").attr("src", $(this).attr("href"));
	$("#game_name").text($(this).text().replace("- ", ""));
	window.location = "#game_name";
	return false;
});

$("#via_web").click(function() {
	window.location = "http://www.utenosjuventus.lt/registracija-iprasta";
});

$("#via_fb").click(function() {
	window.location = "http://www.utenosjuventus.lt/registracija-facebook";
});

$("#post_comment").click(function() {
	if($("#comment_box").val() != document.getElementById('comment_box').defaultValue) {

	$("#post_comment").val("Siunčiama...");

	$.ajax({
	type: "POST",
	url: "comment.php",
	data: "text=" + $("#comment_box").val() + "&id=" + $("#newsid").val(),
	cache: false,
	success: function(html){
		$("#comments_box").prepend(html);
		$("#comments_box table:first").fadeOut('fast').fadeIn('fast');
	}
	});

	$("#comment_box").val(document.getElementById('comment_box').defaultValue);
	$("#post_comment").val("Komentuoti");

	} else {
		alert("Neparašėte komentaro.");
	}
});

$("#comment_web").click(function() {
	document.getElementById("web_type").style.display = "block";
	document.getElementById("fb_type").style.display = "none";
});

$("#comment_fb").click(function() {
	document.getElementById("web_type").style.display = "none";
	document.getElementById("fb_type").style.display = "block";
});

$("#logo").click(function() {
	window.location = "http://www.utenosjuventus.lt";
});

$("#close").click(function() {
	window.location = "http://www.utenosjuventus.lt/isjungti";
});

$("#fb_button").click(function() {
	$("#fb_button").text("Vyksta prisijungimas...");
	window.location = "http://www.utenosjuventus.lt/facebook-prisijungimas";
	return false;
});

$("#box").click(function() {
	window.location = "registracija";
});

$(".fadein img:gt(0)").hide();

var focused = 1;

function bindEvent(el, eventName, eventHandler) {
	if (el.addEventListener){
		el.addEventListener(eventName, eventHandler, false); 
	} else if (el.attachEvent){
		el.attachEvent('on'+eventName, eventHandler);
	}
}

bindEvent(window, 'focus', function() {
	focused = 1;
});

bindEvent(window, 'blur', function() {
	focused = 0;
});

var sponsid = 0;
var sponsors = new Array("PAGRINDINIAI RĖMĖJAI", "DIDIEJI RĖMĖJAI", "RĖMĖJAI", "INFORMACINIAI RĖMĖJAI", "DRAUGAI");
var prefix = "http://www.utenosjuventus.lt/images/remejai/";
$("#sponsors").html("<a href='http://www.utena.lt/'><div class='sponsor' style='background-image: url(" + prefix + "utena.gif);'></div></a><a href='http://www.utenosalus.lt/lt'><div class='sponsor' style='background-image: url(" + prefix + "utenos_alus.gif);'></div></a><a href='http://www.rokiskio.com/'><div class='sponsor' style='background-image: url(" + prefix + "rokiskio_suris.gif);'></div></a>");
$("#sponsor_text").html(sponsors[0]);

function change_sponsors(sponsorid) {

	switch (sponsorid)
	{
	case 0:
		source = "<a href='http://www.utena.lt/'><div class='sponsor' style='background-image: url(" + prefix + "utena.gif);'></div></a><a href='http://www.utenosalus.lt/lt'><div class='sponsor' style='background-image: url(" + prefix + "utenos_alus.gif);'></div></a><a href='http://www.rokiskio.com/'><div class='sponsor' style='background-image: url(" + prefix + "rokiskio_suris.gif);'></div></a>";
		break;
	case 1:
		source = "<a href='http://www.vytrolma.lt/'><div class='sponsor' style='background-image: url(" + prefix + "vytrolma.gif);'></div></a><a href='http://www.duventa.lt/'><div class='sponsor' style='background-image: url(" + prefix + "duventa.gif);'></div></a><a href='http://www.utenosrvk.lt/'><div class='sponsor' style='background-image: url(" + prefix + "koop.gif);'></div></a><a href='http://www.visalietuva.lt/imones/info/utenos-duona-uab'><div class='sponsor' style='background-image: url(" + prefix + "utenos_duona.gif);'></div></a><a href='http://www.alausa.lt/'><div class='sponsor' style='background-image: url(" + prefix + "alausa.gif);'></div></a>";
		break;
	case 2:
		source = "<a href='http://www.kestoreklama.lt/'><div class='sponsor' style='background-image: url(" + prefix + "kesto_reklama.gif);'></div></a><a href='http://www.sportas.utena.lm.lt/'><div class='sponsor' style='background-image: url(" + prefix + "kksc.gif);'></div></a><a href='http://www.arkprekyba.lt/'><div class='sponsor' style='background-image: url(" + prefix + "arkas.gif);'></div></a>";
		break;
	case 3:
		source = "<a href='http://www.utenos-diena.lt/'><div class='sponsor' style='background-image: url(" + prefix + "utenos_diena.gif);'></div></a><a href='http://www.utenoszinios.lt/'><div class='sponsor' style='background-image: url(" + prefix + "utenos_zinios.gif);'></div></a><a href='http://www.indrosradijas.lt/ir.php'><div class='sponsor' style='background-image: url(" + prefix + "ir.gif);'></div></a>";
		break;
	case 4:
		source = "<a href='http://www.alausoslenis.lt/'><div class='sponsor' style='background-image: url(" + prefix + "alauso_slenis.gif);'></div></a>";
		break;
	default:
		source = "<a href='http://www.utena.lt/'><div class='sponsor' style='background-image: url(" + prefix + "utena.gif);'></div></a><a href='http://www.utenosalus.lt/lt'><div class='sponsor' style='background-image: url(" + prefix + "utenos_alus.gif);'></div></a><a href='http://www.rokiskio.com/'><div class='sponsor' style='background-image: url(" + prefix + "rokiskio_suris.gif);'></div></a>";
	}

	$("#sponsors").fadeOut("fast");
	$("#sponsors").html(source);
	$("#sponsors").fadeIn("fast");

	$("#sponsor_text").html(sponsors[sponsorid]);
}

setInterval(function(){
	if (focused == 1) {
	$(".fadein :first-child").fadeOut().next("img").fadeIn(1000).end().appendTo(".fadein");

	if (sponsid == 4) {
		sponsid = 0;
	} else {
		sponsid++;
	}

	change_sponsors(sponsid);
}}, 5000);

$("#show_back").click(function() {

if (sponsid == 0) {
	sponsid = 4;
	change_sponsors(sponsid)
} else {
	sponsid--;
	change_sponsors(sponsid)
}

return false;
});

$("#show_next").click(function() {

if (sponsid == 4) {
	sponsid = 0;
	change_sponsors(sponsid)
} else {
	sponsid++;
	change_sponsors(sponsid)
}

return false;
});

var clickCount = 0;
var loaded = 1;
var title = document.title;

$("#loader").click(function() {

if (clickCount >= 5) {
document.location = "naujienos";
} else {
$(this).html("<img style='margin-top: 10' src='images/loading.gif'>");
document.title = 'Kraunama...';

var klase = $("#last_new").val();

$("#last_new").remove();
$("#loader").before("<input type='hidden' id='last_new' name='last_new' value='" + (klase - 3) + "' />");

$.ajax({
type: "POST",
url: "news_load.php",
data: "lastmsg=" + klase,
cache: false,
success: function(html){
$("#loader").before("<div id='cc" + clickCount + "' style='width: 0; height: 0'></div>");
$("#loader").before(html);
$('html, body').animate({
    scrollTop: $("#cc" + clickCount).offset().top
}, 750);
$("#loader").html("Rodyti daugiau naujienų...");
document.title = title;
if (clickCount == 5) {
$("#loader").html("Nerandi naujienos? Peržiūrėk visas!");
}
}
});

clickCount = clickCount + 1;
}
});

function get_albums()
{
var ID = $('.album').size();
$('div#album_loader').html('<img src="images/loading.gif">');
$.post("gallery_load.php?rowstart="+ID,

function(data){
if (data != "") {
$(".album:last").after(data);
loaded = 1;
$('div#album_loader').empty();
} else {
$('div#album_loader').html('Daugiau galerijų nebėra. <a href="#">Grįžti į viršų...</a>');
loaded = 0;
}
document.title = title;
});
};

$('#box').bounceBox();
var logged_in = (document.cookie.indexOf('fusion_user') == -1) ? false : true;

function string_contains(haystack, needle) {

if (haystack.indexOf(needle) == -1) {
	return false;
} else {
	return true;
}

} 


$(window).scroll(function(){
if ($(document).scrollTop() == 0 && !logged_in) {
	$('#box').bounceBoxToggle();
} else {
	$('#box').bounceBoxHide();
}

if (loaded == 1) {
if ($(window).scrollTop() > ($(document).height() - $(window).height()) - 250 && string_contains(document.location.href, "/galerija")) {
	document.title = 'Kraunama...';
	get_albums();
	loaded = 0;
}
}
});

});
