//aktuality slider
var coll_timer;
var currentPosition = 0;
var numberOfSlides = 6;
var timer;

// CONTENT READED STATUS
var already_readed = false;


// showAlertTimer
var readedTimer = null;
// confirmButtonTimer
var confirmTimer = null;


function showReadedAlert(inState){
	if (inState) {
		$('.content_readed_alert > .read_state').text("prečítaný");	
	} else {
		$('.content_readed_alert > .read_state').text("neprečítaný");	
	}   	
	$('.content_readed_alert').slideDown();	
	readedTimer = setTimeout(hideReadedAlert, 2000);

}
function hideReadedAlert(){
	clearTimeout(readedTimer);
	$('.content_readed_alert').slideUp();	
}

function editPortfolio(){
	$('.edit_port').hide()
	$('.confirmButton').fadeOut();
	$('.remove.actions, .move.actions').fadeIn();
	$('input.sub_button,.cancel_port').removeClass('hidden');
}

function cancelEditPortfolio(){
	location.reload();
}



function confirmButton(thisButton,ac2,lid){
	if(!$(thisButton).hasClass('state_confirm')){
		resetConfirmButtons();
		$(thisButton).addClass("state_confirm");	
		$('.base',thisButton).slideUp(100);
		$('.confirm',thisButton).delay(100).slideDown(100);
		clearTimeout(confirmTimer);
		confirmTimer = setTimeout(resetConfirmButtons,4000);   		
	} else if ($(thisButton).hasClass('state_confirm')) {
		$.ajax({
		  url: g_page_url+'/index.php?id=mod_content_readed&action=module',
		  type: 'GET',
		  dataType: 'json',
		  data: {
			list_id: lid,
			action2: ac2
		  }, success: function(data, textStatus, xhr) {
			if(data == true){
				location.reload();
			}
		}
		});
		
	}  
	return false;
}

function resetConfirmButtons(){
	$('.confirmButton').removeClass('state_confirm');
	$('.base','.confirmButton').slideDown();
	$('.confirm','.confirmButton').slideUp();
}



function setReaded(inID){
	already_readed = true;
	$.ajax({
	  url: g_page_url+'/index.php?id=mod_content_readed&action=module',
	  type: 'GET',
	  dataType: 'json',
	  data: {
		content_id: inID,
		action2: 'read'
	  }, success: function(data, textStatus, xhr) {
		if(data == true){
			showReadedAlert(true);
			$('.cont_read').hide();
			$('.cont_unread').show();
		}
	}
	});
}

function setUnreaded(inID){
	already_readed = false;
	$.ajax({
	  url: g_page_url+'/index.php?id=mod_content_readed&action=module',
	  type: 'GET',
	  dataType: 'json',
	  data: {
		content_id: inID,
		action2: 'unread'
	  }, success: function(data, textStatus, xhr) {
		if(data == true){
			showReadedAlert(false);
			$('.cont_read').show();
			$('.cont_unread').hide();
		}
	}
	});
}

function setReadedAfterScroll(inID) {
	var msie6 = $.browser == 'msie' && $.browser.version < 7;

	if (!msie6) {
		$(window).scroll(function (event) {
			visible_end = $("#content_end").offset().top - $(window).height();
			if (!already_readed && ($(window).scrollTop() >= visible_end)) {
				setReaded(inID);
			}
		});
	}
}


function getReadSettings(inID) {
	$.ajax({
		url: g_page_url+'/index.php?id=mod_content_readed&action=module',
		type: 'GET',
		dataType: 'json',
		data: {
			action2: 'getReadSettings'
		}, 
		success: function(data, textStatus, xhr) {
			if(data){
				switch(data){
					case 1:
						// automaticky po precitani
						setReadedAfterScroll(inID);
						break;
					case 2:
					  	// automaticky po zobrazeni
						setReaded(inID);
						break;
					default:
	 					// manualne (nespravi nic)
				}
			} else {
				// nemalo by sa sa stat - defaultne posiela hodnotu 3
			}			
		},
		error: function(data, textStatus, xhr) {
			// napriklad neprihlaseny uzivatel
		}
	});
}



jQuery(document).ready(function($) {
	// poznamky pod ciarou

	var noteFloatWindowTimer = null;
	function noteFloatWindowTimerStart () {
		noteFloatWindowTimer = setTimeout(noteFloatWindowHide, 2000);
	}

	function noteFloatWindowTimerCancel () {
		clearTimeout(noteFloatWindowTimer);
	}
	
	function noteFloatWindowHide () {
		$("#noteFloatWindow").fadeOut(function () {
			$("#noteFloatWindow").remove();
		});
	}

	$(".InsertNoteMarker").bind('mouseover', function() {
	    $("#noteFloatWindow").remove();
		$(this).after('<div id="noteFloatWindow"></div>');		
		$("#noteFloatWindow").hide().bind('mouseover', noteFloatWindowTimerCancel);
		$("#noteFloatWindow").hide().bind('mouseout', noteFloatWindowTimerStart);
		
		hrefID = $("sup a", this).attr("href");
		li = $(hrefID);
		liID = li.attr('id');
		noteFloatWindowTimerCancel();
		$("#noteFloatWindow").html(li.html());
 		$("#"+liID+"_LinkBacks", "#noteFloatWindow").remove();
		$("#noteFloatWindow").fadeIn();
	});

	$(".InsertNoteMarker").bind('mouseout', function() {
		noteFloatWindowTimerStart();
	});
	
			
	// login inputs init
	$('input.login_input[name=passwd-clear]').show();
	$('input.login_input[name=passwd]').hide();

	// neprehliadnite
	$('#nepr_menu ul li.folder > a', '#menu_nepr').appendTo('#nepr_header', '#menu_nepr');
	$('#nepr_menu ul li.folder:first', '#menu_nepr').addClass('open');
	$('#nepr_header a:first', '#menu_nepr').addClass('open');
	$('#nepr_header a', '#menu_nepr').click(function(){
		var myIndex = $('#nepr_header a', '#menu_nepr').index(this);
		$('#nepr_menu li.folder.open', '#menu_nepr').removeClass('open');
		$('#nepr_menu li.folder:eq('+myIndex+')', '#menu_nepr').addClass('open');
		$('#nepr_header a.open', '#menu_nepr').removeClass('open');
		$(this).addClass('open');
		return false;
	})

	// najcitanejsie
	$('#najc_menu ul li.folder:first', '#menu_najc').addClass('open');
	$('#najc_header a:first', '#menu_najc').addClass('open');
	$('#najc_header a', '#menu_najc').click(function(){
		var myIndex = $('#najc_header a', '#menu_najc').index(this);
		$('#najc_menu li.folder.open', '#menu_najc').removeClass('open');
		$('#najc_menu li.folder:eq('+myIndex+')', '#menu_najc').addClass('open');
		$('#najc_header a.open', '#menu_najc').removeClass('open');
		$(this).addClass('open');
		return false;
	})
	
	
	$('#quicklinks_menu ul li.folder > a').appendTo('#quicklinks_header');
	$('#quicklinks_header a').click(function(){
		var myIndex = $('#quicklinks_header a').index(this);
		$('#quicklinks_menu li.folder.open').removeClass('open');
		$('#quicklinks_menu li.folder:eq('+myIndex+')').addClass('open');
		$('#quicklinks_header a.open').removeClass('open');
		$(this).addClass('open');
		$('.box.quicklinks .box_footer').show();
	});
	$('.box.quicklinks .box_footer').click(function() {
		$('.quicklinks .open').removeClass('open');
		$('.box.quicklinks .box_footer').hide();
		return false;		
		});


	
	if($(':header','#content').length >= 1){
		$('.grid_content_left .obsah .box_content').append(generateIndex('#content'));		
	} else {
		$('.grid_content_left .box.obsah').hide();
	}

	$('.collapsible','#content').collapsible();
	$('#pravnicke_menu > ul > li:last').addClass('lilast');
	$('#institut_menu > ul > li:last').addClass('lilast');
	// $('#quicklinks_menu > ul > li:first').addClass('open');

	// aktuality slider
 	var slides = $('.slide');
	$('#slidesContainer').css('overflow', 'hidden');
	slides.wrapAll('<div id="slideInner"></div>').css({'float' : 'left','width' : '12.5%'});
	$('#slideInner').css('width', '800%');
	setTimer();


	// save/load quicklinks status	
	function saveQuicklinksMenuStateToCookie () {
		indexOfOpenMenu = $('#quicklinks_menu ul li.folder ul').index($('#quicklinks_menu ul li.folder.open ul'));

		// Cookie info
		var cookieName = 'gCMS_menu_quicklinks_state';
		var cookie = $.cookie(cookieName);
		var options = {expires: 99, path: '/'};

		var data = {};
		// get data from global variables
		data['menuindex'] = indexOfOpenMenu;

		$.cookie(cookieName, $.toJSON(data), options);
	}
	
	function loadQuicklinksMenuStateFromCookie () {
		var cookieName = 'gCMS_menu_quicklinks_state';
		var cookie = $.cookie(cookieName);

		var cookieExists = (typeof(cookie) != "undefined" && cookie != null);
		if (cookieExists) {
			data = $.evalJSON(cookie);

			if (typeof data['menuindex'] != "undefined") {
				$('#quicklinks_menu ul li.folder ul').hide().parents('li').removeClass('open');;
				$('#quicklinks_menu ul li.folder ul').eq(data['menuindex']).show().parents('li').addClass('open');
				if (data['menuindex'] >= 0) {
					$('.box.quicklinks .box_footer').show();
				}
			}
		}
	}

//	loadQuicklinksMenuStateFromCookie();

	// $('#quicklinks_menu ul li.folder a:first').click(function() {		
	// 	$('#quicklinks_menu ul li.folder.open ul').hide().parent().removeClass('open');
	// 	$(':parent',this).parents('li').addClass('open');
	//     $(this).next('ul').show();
	// 	$('.box.quicklinks .box_footer').show();
	// 	
	// 	// save state of menu to cookie
	// 	saveQuicklinksMenuStateToCookie();
	// 	return false;		
	// });

	$('#left_menu ul li.folder.blind a:first').click(function(){
		$('#left_menu ul li.folder.open ul').slideUp('fast');
		$('#left_menu ul li.folder.open').removeClass('open');
		$(':parent',this).parents('li').addClass('open');
	    $(this).next('ul').slideDown('fast');
		return false;
	})
	
	$('#reklama_rotate_top').innerfade({ speed: 'slow', timeout: 5000, type: 'sequence', containerheight: '85px', children:'li'});
	
});			

function generateIndex(inScope){
	var obsah = "<ul>";
		$(':header',inScope).each(function(index) {
		
			var actH = $(this).clone();
			$('.badge',actH).remove();
			var label=actH.text();       
			actH.remove();
		
			if($("a[name]",this).length == 0){
				$(this).attr("id","kap"+index);
				obsah = obsah+("<li class='obs"+this.tagName+"'><a href='#kap"+index+"' onclick='tryCollapsed(\"#kap"+index+"\")'>"+label+"</a></li>");
			} else {
				index = $("a[name]",this).attr('name');				
				obsah = obsah+("<li class='obs"+this.tagName+"'><a href='#"+index+"'>"+label+"</a></li>");				
			}
		});
	obsah = obsah+"</ul>";
	return obsah;
}
function tryCollapsed(inID){
    if($(inID).is(":visible")){
			$(inID).parents('.collapsible').children('.coll_head.collapsible_close').click();
		} else { 
			$(inID).parents('.collapsible').children('.coll_head.collapsible_close').click();
		}
}

function openAllColl(){
	$('.coll_head.collapsible_close').click();
}

function closeAllColl(){	
	$('.coll_head.collapsible_open').click();
}



(function($) {
	//  osetrenie poslednej polozky menu aby sa preklapala do vnutornej strany
   $.fn.collapsible = function(settings) {
    this.each(function() {
   		var c_head = $('.coll_head:first',this);
		var c_body = $('.coll_content:first',this);			   		
		$(this).hover(function() {
			c_head.before("<div class='collaps_all_wrapp'><a class='coll_all_plus' href='javascript:openAllColl()'>+ Rozbaľ všetky</a>  <a class='coll_all_minus' href='javascript:closeAllColl()'>- Zbaľ všetky</a></div>");
		}, function() {
			$('.collaps_all_wrapp').remove();
		});
		if(!c_head.hasClass('collapsible_open')){
			if( $(window.location.hash,c_body).size()>0){
				c_head.click(function() {
					$(this).toggleClass("collapsible_open").toggleClass("collapsible_close");			
					c_body.toggle();
					return false;
				}).addClass('collapsible_open');				
			} else {
		   		c_body.hide();						
				c_head.click(function() {
					$(this).toggleClass("collapsible_open").toggleClass("collapsible_close");			
					c_body.toggle();
					return false;
				}).addClass('collapsible_close');				
			}
		} else {
			c_head.click(function() {
				$(this).toggleClass("collapsible_open").toggleClass("collapsible_close");			
				c_body.toggle();
				return false;
			})
		}
     });
     return this;
   };
})(jQuery);

function showCalendarEventDetail (id) {
	if ($('.event_detail', 'li.event_wrapp[eventID='+id+']').css('display') == 'none') {
		// $('.event_text').slideDown();
		// $('.event_text', 'li.event_wrapp[eventID='+id+']').slideUp();
		$('.event_detail').slideUp('fast');
		$('.event_detail', 'li.event_wrapp[eventID='+id+']').slideDown('fast');		
	} else {
		// $('.event_text').slideDown();
		$('.event_detail', 'li.event_wrapp[eventID='+id+']').slideUp('fast');		
	} 	   
}

function toggleSection(inID,title){
	if ( $(inID).css('display') == 'none') {
		$(title).addClass('open');
		$(inID).slideDown('fast');
		$('.badge',title).fadeOut();
		// $('.badge',inID).fadeIn();
	} else {
		$(title).removeClass('open');
		$(inID).slideUp('fast');		
		$('.badge',title).fadeIn();
		// $('.badge',inID).fadeOut(); 
	}	
}

function doSlide(pos,deac){
	if(deac){ clearTimer()};
	$('.akt_nav li.active').removeClass('active');
	$('.akt_nav li').eq(pos).addClass('active');
	currentPosition = pos;
	pos = (pos == 0) ? 0 : 100*(-pos)+'%';
	$('#slideInner').animate({'marginLeft' : pos});
}
function nextSlide(){
	clearTimer();
	currentPosition = (currentPosition == 6) ? currentPosition=0 : currentPosition+1;
	doSlide(currentPosition);
}
function prevSlide(){
	clearTimer();
	currentPosition = (currentPosition == 0) ? currentPosition=4 : currentPosition-1;
	doSlide(currentPosition);
}
function clearTimer(){
	$('.nav_pause').hide();	
	$('.nav_play').show();
	clearInterval(timer);
    timer = '';    
}

function setTimer(){	
	$('.nav_play').hide();
	$('.nav_pause').show();
    timer = setInterval(function(){
		currentPosition = (currentPosition == 6) ? currentPosition=0 : currentPosition+1 ;
		doSlide(currentPosition);
		}, 8000);	
}

function pleaseRegister(link){
	$('.pleaseRegister',link).slideDown('fast').delay(2000).slideUp('fast');
}

// TABULKY V PROFILE EDITORA
function addFromTemplate(target){
	$(".tpl",target).clone().removeClass("tpl").addClass("values").appendTo(target).show("fast").find("input,select").each(function(){
		$(this).attr("name",$(this).attr('name')+'[]');
	}).first().focus();
	$('.labels', target).show();
}

function removeLine2(target){
	if (confirm("Prajete si naozaj odstrániť?")) {
		$(target).parents('tr').remove();
	}
	return false;	
}

function moveUp(target){
	$(target).parents('tr').insertBefore( $(target).parents('tr').prev('tr') );
}
function moveDown(target){
	$(target).parents('tr').insertAfter( $(target).parents('tr').next('tr') );
}

function removeLine(target){
	if (confirm("Potvrďte odstránenie.")) {
		table = $(target).parents('table.tbl_manag');
		$(target).parent().parent().remove();
		if ($('.values:visible', table).length <= 0)
			$('.labels', table).hide();
	}
	return false;
}

// BLOG INTRO - ULOZENIE DO COOCKIE
function saveBlogIntroStateToCookie () {
	if ($('#blog_intro').is(':visible')) {
		status = false;
	} else {
		status = true;
	}
	// Cookie info
	var cookieName = 'gCMS_blogintro_state';
	var cookie = $.cookie(cookieName);
	var options = {expires: 99, path: '/'};

	// get data from global variables
	$.cookie(cookieName, status, options);
}

function savePortfolioStateToCookie () {
	if ($('#blog_intro').is(':visible')) {
		status = false;
	} else {
		status = true;
	}
	// Cookie info
	var cookieName = 'gCMS_portfolio_state';
	var cookie = $.cookie(cookieName);
	var options = {expires: 99, path: '/'};

	// get data from global variables
	$.cookie(cookieName, status, options);
}

function toggleBlogIntro(){
	saveBlogIntroStateToCookie();
	if ($('#blog_intro').is(':visible')) {
		$('.blog_intro_button .tool_button.show').show();
		$('.blog_intro_button .tool_button.hide').hide();
	} else {
		$('.blog_intro_button .tool_button.show').hide();
		$('.blog_intro_button .tool_button.hide').show();
	};
	$('#blog_intro').slideToggle('fast');
}

function togglePortfolio(){
	savePortfolioStateToCookie();
	if ($('#blog_intro').is(':visible')) {
		$('.blog_intro_button .tool_button.show').show();
		$('.blog_intro_button .tool_button.hide').hide();
	} else {
		$('.blog_intro_button .tool_button.show').hide();
		$('.blog_intro_button .tool_button.hide').show();
	};
	$('#blog_intro').slideToggle('fast');
}
