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'
	});

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

	$("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 > 710) imgs[i].width = 710;
	}
}

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 );
	}
}
















/* auto */

// var ajaxModelRequest = "/app/ajax_model.php";
// var ajaxModel;
// 
// function changeModel(node){
// 
// 	if(node.selectedIndex == 1){
// 		document.getElementById('new_znacka').style.display="block";
// 	}
// 	else {
// 		var params = "?" + node.value + "/";
// 		ajaxModel = new sack();
// 		ajaxModel.requestFile = ajaxModelRequest + params;
// 		ajaxModel.onCompletion = function() { showModel() };	// Specify function that will be executed after file has been found
// 		ajaxModel.runAJAX();	// Execute AJAX function*/
// 		checkAutoForm();
// 	}
// }
// 
// function changeModelNo(node){
// 
// 	var params = "?" + node.value + "~false";
// 	ajaxModel = new sack();
// 	ajaxModel.requestFile = ajaxModelRequest + params;
// 	ajaxModel.onCompletion = function() { showModelNo() };	// Specify function that will be executed after file has been found
// 	ajaxModel.runAJAX();	// Execute AJAX function*/
// 	checkAutoForm();
// }
// 
// function showModel(){
// 	if(ajaxModel.response) document.getElementById('model').innerHTML = '<select name="model" class="ac_me" onchange="checkModel(this)">' + ajaxModel.response + '</select>*';
// }
// 
// function showModelNo(){
// 	if(ajaxModel.response) document.getElementById('model').innerHTML = '<select name="model">' + ajaxModel.response + '</select>';
// }
// 
// function checkModel(node){
// 	//alert(node.selectedIndex);
// 	if(node.selectedIndex == 1){
// 		document.getElementById('new_model').style.display="block";
// 	}
// 	else {
// 		checkAutoForm();
// 	}
// }
// 
// function newZnacka(){
// 	
// }
// function newModel(){
// 	
// }
// 
// function checkAutoForm(){
// 	//alert(document.getElementById('model').firstChild.selectedIndex);
// 	//alert(document.getElementById('znacka').firstChild.selectedIndex);
// 
// 	//if(!document.getElementById('new_model').firstChild.value) document.getElementById('new_model').style.display = "none";
// 	if(document.getElementById('model').firstChild.selectedIndex != 1) document.getElementById('new_model').style.display = "none";
// 	//if(!document.getElementById('new_znacka').firstChild.value)  document.getElementById('new_znacka').style.display = "none";
// 	if(document.getElementById('model').firstChild.selectedIndex != 1) document.getElementById('new_znacka').style.display = "none";
// }




// var checkBan = 0;
// 
// function banconfirm(node,id){
// 	var bl = document.getElementById('bl'+id).value;
// 	var bt = document.getElementById('bt'+id).value;
// 	node.href = node.href.replace(/\.html/g, '-bl' + bl + '-bt' + bt + '\.html');
// 	return confirm('naozaj ban?');
// }
// 
// function showban(id){
// 	if(checkBan == id) {
// 		document.getElementById('ban' + checkBan).style.display = 'none';
// 		checkBan = 0;
// 		return;
// 	}
// 	if(checkBan > 0) {
// 		document.getElementById('ban' + checkBan).style.display = 'none';
// 	}
// 	var ban = document.getElementById('ban' + id);
// 	ban.style.display = 'block';
// 	checkBan = id;
// }

