var currLead = 0;
var lead = 0;

function fmail (domena, ime, zadeva, bodi)
{
	var zad = "";
	if (zadeva != "" && zadeva != undefined)
		zad = "?subject=" + zadeva;
	window.location = "mai" + "l" + "to" + ":" + ime + "@" + "" + domena + zad;
}

function viewLead(a)
{
	currLead = a;
	$(".gumb").removeClass("hover");
	$("#poudarekSlika").fadeOut("fast", function () {
		$("#poudarekSlika").html("<a href='"+lead[a][2]+"'><img src='"+lead[a][1]+"' alt=''/></a>");
	});
	$("#poudarekSlika").fadeIn("fast");
	$("#gumb_i"+a).addClass("hover");
}

function navLead(b)
{
	if (lead.length > 1)
	{
		if (b == 1)
		{
			//naprej
			if ((lead.length-1) <= currLead)
				viewLead(0);
			else
				viewLead(currLead+1);
		}
		else
		{
			//nazaj
			if (currLead == 0)
				viewLead(lead.length-1);
			else
				viewLead(currLead-1);
		}
	}
}

$(document).ready(function() {
		
	$("#iskalnikGo").click(function() {
		window.location = "/kamnik/iskanje/" + $("#iskalnik").val() + "/";
	});
	
	$("#iskalnik").keypress(function(e) {
		if (e.keyCode==13)
			$("#iskalnikGo").click();
	});
	
	$(".gumb").click(function(a) {
		//alert();
	});
	
	viewLead(0);
	
	$.timer(7000, function (timer) {
		navLead(1);
	});
	
	var delam = false;
	$("#komGo").click(function() {
		if (delam) {
			alert("Prejšnji zahtevek je še v delu. Prosimo počakajte nekaj trenutkov in poskusite ponovno.");
			return false;
		}
		if ($("#author").val().length == 0) {
			alert('NAPAKA: prosim vpišite vaš vzdevek!');
			$("#author").focus();
			return false;
		}
		if ($("#content").val().length == 0) {
			alert('NAPAKA: prosim vpišite komentar!');
			$("#content").focus();
			return false;
		}
		if ($("#koda").val().length == 0) {
			alert('NAPAKA: prosim prepišite varnostno kodo s slike na desni!');
			$("#koda").focus();
			return false;
		}
		
		// ok očitno mamo vse, request
		delam = true;
		$("#komLoad").css("visibility", "visible");
		
		$.post("/addComment.php", { author: $("#author").val(), content: $("#content").val(), koda: $("#koda").val(), vID: $("#vID").val(), barva: $("#barva").val() },
			function(data) {
				if (data == "koda")
				{
					alert("NAPAKA: prosim pravilno prepišite varnostno kodo s slike na desni!");
					return false;
				}
				if (data == "podatki")
				{
					alert("NAPAKA: niste pravilno vpisali vseh podatkov!");
					return false;
				}
				if (data == "napaka")
				{
					alert("NAPAKA: prosimo osvežite spletno stran in ponovno poskušajte dodati komentar!");
					return false;
				}
				
				$("#ajaxComments").prepend(data);
				if ($("#barva").val() == "0")
					$("#barva").val("1");
				else
					$("#barva").val("0");
				
				$("#author").val("");
				$("#content").val("");
				$("#koda").val("");
				$("#komLoad").css("visibility", "hidden");
				delam = false;
				$("#niKomentarjev").css("display", "none");
				window.location = "#zadnjiKomentar";
				
			});

		
	});
	
});

function videoVote(vid, num) {
	
	$.post("/vote.php", { vote: num, vID: vid },
		function(data) {
			if (data == "napaka")
			{
				return false;
			}
			alert (data);
		});
		
}

/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
 
 jQuery.timer = function (interval, callback)
 {
 /**
  *
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  * 
  */

	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };

