$(document).ready(function(){

	$(function() {
		$('a[rel=lightbox]').lightBox();
	});

	$(function() {
		initAjaxMenu();
		initSwitchInput();
		initLoginForm();
		initSharer();
		if($('div#fcontent').length > 0) initForum();
		
		$('input.fsb_addimages').click(
			function(){
				$(this).addClass('fsb_loading').attr('value','čakajte prosím...');
			}
		);
	});
	
	$('img.capimg').click(function(){
		d = new Date();
		this.src = "/app/captcha.php?"+d.getTime();
	;})
;});


var ajaxMenuRequest = "/app/menu.php";
var ajaxMenu = new Array();
var ajaxMenuAct = "";
var timeout	= 1000;
var closetimer	= 0;

function initAjaxMenu(){
	$("div#menu").append("<div class=\"ajaxbg\"></div>");
	$("div#menu").append("<div class=\"ajaxmenu\"></div>");
	$("div.ajaxbg").css({ opacity: 0.90 });
	$("a.ajmenu").hover(
		function(){
			openAjaxMenu(this);
		},
		function(){
			mclosetime();
		}	    
	);
	$("div.ajaxmenu").hover(
		function(){ 
			mcancelclosetime();
		}, 
		function(){
			mclosetime();
		}
	);
	$(".checkauto").change(function() {
		changeModel(this);
	});
}

function openAjaxMenu(node){
	
	var id = node.id.replace(/[^0-9]/g, '');
	
	mcancelclosetime();
	if($(ajaxMenuAct).length > 0) closeAjaxMenu();
	
	ajaxMenuAct = 'a#'+node.id;
	$(ajaxMenuAct).addClass("menuact");
	
	if(!ajaxMenu[id]) {
		$('div.ajaxbg').css("display","block").height("100px").addClass("loader");
		//alert('load');
		$.ajax( {
			type: 'GET',
			async: false,
			url: ajaxMenuRequest,
			data: 'id='+id,
			success: function(data) {
				ajaxMenu[id] = data; 
			}
		});
		//alert(ajaxMenu[id]);
	}
	
	$("div.ajaxmenu").html(ajaxMenu[id]).css("display","block");
	$("div.menuinfobox").html($("div.menuinfomain").html());
	$("div.ajaxmenu li").hover(
		function(){
			$("div.menuinfobox").html($("div.menuinfo",this).html());
		}, 
		function(){
			$("div.menuinfobox").html($("div.menuinfomain").html());
		}
	);
	$("div.ajaxbg").css("display","block").removeClass("loader").height($("div.ajaxmenu").height());
	
	return false;
}

function closeAjaxMenu(){
	
	if($(ajaxMenuAct).length > 0) {
		$(ajaxMenuAct).removeClass("menuact");
		$("div.ajaxmenu").css("display","none");
		$('div.ajaxbg').css("display","none");
		return false;
		
	}
}

function mclosetime(){
	
	closetimer = window.setTimeout(closeAjaxMenu, timeout);
}

// cancel close timer
function mcancelclosetime(){
	//alert('rusim');
	if(closetimer)	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}




function initSwitchInput(){
	
	$('.switchin').each(function(i) {
		
		if($(this).attr("type") == "password"){
			//$(this).replaceWith($(this).clone().attr("type","text"));
			$(this).attr("oldtype","password")
		}
		
		$(this).attr("oldtitle",$(this).attr("value"));
		
		$(this).focus(function() {
			if($(this).attr("value") == $(this).attr("oldtitle")) this.value = "";
			if($(this).attr("oldtype") == "password") {
				//alert('robim heslo');
				//$(this).replaceWith($(this).clone().attr("type","password"));
			}
			
		}).blur(function() {
			if($(this).attr("value") == "") $(this).attr("value",$(this).attr("oldtitle"));
			if($(this).attr("oldtype") == "password") {
				//$(this).replaceWith($(this).clone().attr("type","text"));
			}
		});
	});
}

function initLoginForm(){
	var loginOn = 0;
	$("div#loginform").css('height', '0px').css('overflow','hidden');
	$(".loglink").click(function(){
		if (loginOn==0) {
			loginOn = 1;
			$("div#loginform").animate({ 
				height: "48px"
			}, 500 );
		}
		else {
			loginOn = 0;
			$("div#loginform").animate({ 
				height: "0px"
			}, 500 );
		}
		this.blur();
		return false;
	;})
}









var ajaxModelRequest = "/app/ajax_model.php";
var ajaxModel;

function changeModel(node){
	
	if(node.selectedIndex == 1){
		$('#new_znacka').css("display","block");
		//alert('nova');
	}
	else {
		$.ajax( {
			type: 'GET',
			async: false,
			url: ajaxModelRequest,
			data: node.value + '/',
			success: function(data) {
				//alert(data);
				$('#model').html('<select name="model" class="ac_me" onchange="checkModel(this)">' + data + '</select>*');
			}
		});
	}
	//checkAutoForm();
}

function changeModelNo(node){
	$.ajax( {
		type: 'GET',
		async: false,
		url: ajaxModelRequest,
		data: node.value + '~false',
		success: function(data) {
			$('#model').html('<select name="model">' + data + '</select>');
		}
	});
	//checkAutoForm();
}

function checkModel(node){
	//alert(node.selectedIndex);
	if(node.selectedIndex == 1){
		$('#new_model').css("display","block");
	}
	else {
		checkAutoForm();
	}
}

function newZnacka(){
	
}
function newModel(){
	
}

function checkAutoForm(){
	//alert(document.getElementById('model').firstChild.selectedIndex);
	//alert(document.getElementById('znacka').firstChild.selectedIndex);
	//alert('azene');
	//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('znacka').firstChild.selectedIndex != 1) document.getElementById('new_znacka').style.display = "none";
}





/* forum */

var formOn = 0;
var replyOn = 0;
var editOn = 0;


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;
}

/* banners */

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: '/' });
}



/* 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";
}

/* filter */
function showFilter(){
	var iform = document.getElementById('ifform');
	var ititle = document.getElementById('iftitle').firstChild;
	if(iform.style.display == "block"){
		iform.style.display = "none";
		ititle.style.background = "url(/styles/asc.gif) 4px 12px no-repeat";
	}
	else {
		iform.style.display = "block";
		ititle.style.background = "url(/styles/desc.gif) 4px 12px no-repeat";
	}
}

/* popis */

function showPopis(id){
	var popisDiv = document.getElementById('popis' + id);
	var popisButton = document.getElementById('ib_popis' + id)
	if(popisDiv.style.display == "block"){
		popisDiv.style.display = "none";
		popisButton.style.background = "#737373 url(/styles/icons/pencil2.png) center center no-repeat";
	}
	else {
		popisDiv.style.display = "block";
		popisDiv.childNodes[0].focus();
		popisButton.style.background = "#333333 url(/styles/icons/pencil2.png) center center no-repeat";
	}
}

var checkflag = false;

function check(classn) {
	allNodes = document.getElementsByClassName(classn);
	if (!checkflag) {
		for(i = 0; i < allNodes.length; i++) {
			allNodes[i].checked = true;}
			checkflag = true;
		}
	else {
		for(i = 0; i < allNodes.length; i++) {
		allNodes[i].checked = false; }
		checkflag = false;
	}
}




function initSharer(){
	$("div.rshare a").click(
		function(){
			//alert(this.className);
			openSharer(this.className,this.href,this.title);
			return false;
		}
	);
}

function openSharer(id,u,t){

	var w = 626; var h = 600;
	switch (id) {
		case 'fb': 
			w = 626; h = 436;
			break;
		case 'vs': 
			w = 626; h = 436;
			break;
		}
	if (u) window.open(u, 'sharer_'+id, 'toolbar=0,status=0,width='+ w +',height='+h);
	return false;
}








