var loginOn = 0;
var formOn = 0;
var replyOn = 0;
var editOn = 0;

$(document).ready(function(){

	$(function() {
		$('a[rel=lightbox]').lightBox();
		$('a[rel=lightbox[gallery]]').lightBox();
	});

	$(function() {
		if($('div#fcontent').length > 0){
			initForum();
		}
	});

//  	$('#themebox').slideshow({ 
// 		timeout: '5000',
// 		type: 'sequence',
// 		tag: 'img'
// 	});

// 	$("div#logform").css('display', 'none');
// 	$("a#loglog").click(function(){
// 		openLogin();
// 		this.blur();
// 		return false;
// 	;})
;});

function initForum(){

	$("div#forumform").css("display", "none").appendTo($("#fph"));
	$("div#forumform form").submit(function(){
		return checkForm("forumform");
	;})
	$(".openform").click(function(){
		openForumForm(this);
		this.blur();
		return false;
	;})
	$(".openreply").click(function(){
		openReplyForm(this);
		this.blur();
		return false;
	;})
	$(".openedit").click(function(){
		openEditForm(this);
		this.blur();
		return false;
	;})
	$('div#forumform .flf2').markItUp(mySettings);

	if($('div#replyform').length == 0) {
		$("div#forumform").clone().attr('id','replyform').appendTo($("#fph"));;
		$("div#replyform .flf2").markItUpRemove().markItUp(mySettings);
		$("div#replyform form").submit(function(){
			return checkForm("replyform");
		;})
		$("div#replyform input.openform").click(function(){
			openReplyForm(this);
			this.blur();
			return false;
		;})
	}

	if($('div#editform').length == 0) {
		$("div#forumform").clone().attr('id','editform').appendTo($("#fph"));;
		$("div#editform h3").html("Uprav príspevok");
		$("div#editform .flf2").markItUpRemove().markItUp(mySettings);
		$("div#editform .fsub").attr('name','act[edittopic]');
		$("div#editform form").submit(function(){
			return checkForm("editform");
		;})
		$("div#editform input.openform").click(function(){
			openEditForm(this);
			this.blur();
			return false;
		;})
	}

	resizeForumImages();
}

function resizeForumImages(){

	var imgs = $("DIV.fmsgmsg IMG");

	for(i = 0; i < imgs.length; i++){
		if(imgs[i].width > 400) imgs[i].width = 400;
	}
}

function openForumForm(){

	if (formOn==0) {
		formOn = 1;
		$("div.ftlist").css('opacity','0.5');
		$("div#forumform").css("display","block");
	}
	else {
		formOn = 0;
		$("div.ftlist").css('opacity','1');
		$("div#forumform").css("display","none");
	}
}

function openReplyForm(node){
	var id = node.id.replace(/[^0-9]/g, '');
	var pid = '#post'+id;
	if(id > 0){
		if(editOn > 0) {
			replyOn = 0;
			$("div#editform").css("display", "none");
			$("div.flist").css('opacity','1');
		}

		if(replyOn == id) {
			replyOn = 0;
			$("div#replyform").css("display", "none");
			$("div.flist").css('opacity','1');
		}
		else {
			replyOn = id;
			$(pid).append($('div#replyform'));
			$("div#replyform input.openform").attr('id','opr' + id)
			$("div#replyform .flf1").attr("value","re: "+($(pid + ' h3').html()));
			$("div#replyform").css("display", "block");
			$("div.flist").css('opacity','0.5');
			$(pid).css('opacity','1');
		}
	}
}

function openEditForm(node){
	var id = node.id.replace(/[^0-9]/g, '');
	var pid = '#post'+id;

	if(replyOn > 0) {
		replyOn = 0;
		$("div#replyform").css("display", "none");
		$("div.flist").css('opacity','1');
	}

	if(editOn == id) {
		editOn = 0;
		$("div#editform").css("display", "none");
		$("div.flist").css('opacity','1');
	}
	else {
		editOn = id;
		$(pid).append($('div#editform'));
		$("div#editform input.openform").attr('id','opr' + id);
		$("div#editform form").attr("action",$("div#editform form").attr("action").replace(/\.html/g, '-edit' + id + '\.html'));
		$("div#editform .flf1").attr("value",($(pid + ' h3').html()));
		$("div#editform .flf2").attr("value",getTopic(id));
		$("div#editform").css("display", "block");
		$("div.flist").css('opacity','0.5');
		$(pid).css('opacity','1');

	}
}


function getTopic(id){
	$.ajax( {
		type: 'POST',
		async: false,
		url: '/app/topic.php',
		data: 'id='+id,
		success: function(data) {
			phtml = data; 
		}
	} );
	return phtml;
}

function checkForm(fid){
	var done = true;
	var fSub = $("#" + fid + " .flf1").attr("value");
	if(fSub == null || fSub == ""){
		done = false;
		$("#" + fid + " .flf1").css("border-color","#CC0000");
	}
	else $("#" + fid + " .flf1").css("border-color","#D6D6D6");
	var fMsg = $("#" + fid + " .flf2").attr("value");
	if(fMsg == null || fMsg == ""){
		done = false;
		$("#" + fid + " .flf2").css("border-color","#CC0000");
	}
	else $("#" + fid + " .flf2").css("border-color","#D6D6D6");
	return done;
}

function showBanner(pos,ban){
	var seen = $.cookie('bp' + pos);
	if(seen) seen = seen + '~' + ban;
	else seen = ban;
	$.cookie('bp' + pos, seen, { path: '/' });
}

function resetBanner(pos){
	$.cookie('bp' + pos, '', { path: '/' });
}

function openLogin(){
	if (loginOn==0) {
		loginOn = 1;
		$("div#logform").animate({ 
			height: "100px"
		}, 500 );
	}
	else {
		loginOn = 0;
		$("div#logform").animate({ 
			height: "0px"
		}, 500 );
	}
}




function regJazdec(checkb){
	if(checkb.checked){
		document.getElementById('regform').style.visibility = 'visible';
		document.getElementById('regform').style.display = 'block';
	}
	else {
		document.getElementById('regform').style.visibility = 'hidden';
		document.getElementById('regform').style.display = 'none';
	}
}
function regJazdecCheck(){
	regJazdec(document.getElementById('regj'))
}


/* order menu */

function showOrder(){
	document.getElementById('oul').style.display = "block";
	orderOpen = true;
}
function hideOrder(){
	orderOpen = false;
	setTimeout('hideOrderMenu()',2000);
}
function onOrder(){
	orderOpen = true;
	//alert('on');
}
function offOrder(){
	orderOpen = false;
	setTimeout('hideOrderMenu()',2000);
}

function hideOrderMenu(){
	if(typeof(orderOpen) != 'undefined') {
		if(orderOpen) return false;
		else document.getElementById('oul').style.display = "none";
	}
	else document.getElementById('oul').style.display = "none";
}


var ajaxRequest = "/app/vote.php";
var poll;

function vote(node){

	var numId = node.id.replace(/[^0-9]/g, '');
	$.ajax( {
		type: 'POST',
		async: false,
		url: ajaxRequest,
		data: 'id='+numId,
		success: function(data) {
			document.getElementById('anketa').innerHTML = data;
		}
	} );

}

function showVote(){
	document.getElementById('pollbox').innerHTML = poll.response;
}







