
function fixNewsCloudHeight(){
	var news_cloud = $('#news_cloud');

	if(news_cloud){
		var holder = news_cloud.parent('.side-box');

		if(holder){
			while(holder[0].scrollHeight > holder.height()){
				$('ul li:last', holder).remove();
			}
		}
	}
}

$(function() {
	fixNewsCloudHeight();

	$("#news_tab1, #news_tab2, #news_tab3, #news_tab4, #news_tab5").each(function(i, elm){
		$(elm).click(function() {
			$(elm).addClass('act').parents('ul').find('a[class=act][id!=' + elm.id + ']').removeClass('act');
		});
	});

	//comments
	$('#toggle_comments, #toggle_list_comments, #but_toggle_comments').click(function(){
		/*
		$('#comments').toggle(500, function(){
			if($('#comments').is(":visible")) {
				$('#toggle_comments').attr('title', 'Затвори коментарите').text('Затвори коментарите');
			}
			else {
				$('#toggle_comments').attr('title', 'Коментирай').text('Коментирай');
			}
		});
		*/
		$('.comment.hidden_comment').toggle(500, function(){
			if($('.comment.hidden_comment').is(":visible")) {
				$('#toggle_list_comments').attr('title', 'Затвори коментарите');
				$('#toggle_comments').attr('title', 'Затвори коментарите').text('Затвори коментарите');
				$('#but_toggle_comments').attr('title', 'Затвори коментарите').text('Затвори коментарите');
			}
			else {
				$('#toggle_list_comments').attr('title', 'Разгледай коментарите');
				$('#toggle_comments').attr('title', 'Разгледай коментарите').text('Разгледай коментарите');
				$('#but_toggle_comments').attr('title', 'Разгледай коментарите').text('Виж всички коментари');
			}
		});
	});

	$('#toggle_sent2friend').click(function(){
		$('#sent2friend').toggle(500);
	});

	$('.edit')
		.focus(function(){
			$(this).val() == this.title ? $(this).val('') : null;//$(this).val(this.title);
		})
		.blur(function(){
			$(this).val() == '' ? $(this).val(this.title) : null;
		});


	function validate_form(form)
	{
		var is_form_ok = true;
		$('#' + $(form).attr('id') + ' .edit').each(function()
		{
			if($(this).val() == this.title && is_form_ok) {
				alert('Моля попълнете полето!')
				$(this).focus();
				is_form_ok = false;
				return;
			}
		});

		return is_form_ok;
	}


	$('#post_comment, #post_news, #post_sent2friend').each(function(ind, elm){
		$(elm).submit(function(){return validate_form(elm);});
	})


	$('input.search, input.gallery_q, #detailed_search')
		.focus(function(){
			this.value == this.title ? $(this).val('') : null;
		})
		.blur(function(){
			this.value == '' ? $(this).val(this.title) : null
		});

	$('div.hidden_component').click(function(){
		$(this).addClass("hidden_component_click");
	});

	$('.search')
		.mouseenter(function(){
			$('.search').addClass("ie_search");
			$('.submit_search').addClass("ie_search_bgr");
		})
		.mouseleave(function(){
			$('.search').removeClass("ie_search");
			$('.submit_search').removeClass("ie_search_bgr");
		});
		
	$('.gallery_q')
		.mouseenter(function(){
			$('.gallery_q').addClass("ie_gallery_search");
			$('.submit_gallery_search').addClass("ie_gallery_search_bgr");
		})
		.mouseleave(function(){
			$('.gallery_q').removeClass("ie_gallery_search");
			$('.submit_gallery_search').removeClass("ie_gallery_search_bgr");
		});

	$("#fileinput").change(function(){
		$("#file").attr("value", $("#fileinput").attr("value"));
	});

	function change_mouseover(elm){
		var active = $('#active_');
		if(active){
			active.removeAttr('id').removeClass("act");
		}
		$(elm).attr("id", "active_").addClass('act');
	}

	function move_news(check){
		check=check-1;
		$(".dis-new ul").stop(true).animate({left: -check*285}, 200);
	}

	$(".new-nav li a").each(function(i, elm){
		$(elm).mouseover(function() {
			change_mouseover(elm);
			var str = $(elm).attr("class");
			move_news(parseInt(str.split(" ", 1)[0].slice(-1)));
		});
	});

	var i = 1;

	$(".last-next").click(function()
	{
		if (i < $('li[class^="last_news_"]').length)
		{
			$(".last-prev").fadeTo("fast", 1)
							.css("cursor", "pointer");

			$(".last-nav span").text(++i);
			//console.log(".last_news_"+i+" .new-nav li:first a");
			change_mouseover(".last_news_"+i+" .new-nav li:first a");

			$(".dis-new ul").animate({left: 0}, 200);

			$("#last-news").animate({"left" : "-=294"}, 200, function(){
				if(i >= $('li[class^="last_news_"]').length){
					$(".last-next").fadeTo("fast", 0.5).css("cursor", "default");
				}
			});
		}
	});

	$(".last-prev").click(function()
	{
		if (i > 1)
		{
			$(".last-next").fadeTo("fast",1)
							.css("cursor", "pointer");

			$(".last-nav span").text(--i);
			change_mouseover(".last_news_"+i+" .new-nav li:first a");

			$(".dis-new ul").animate({left: 0}, 200);

			$("#last-news").animate({"left" : "+=294"}, 200, function(){
				if(i <= 1){
					$(".last-prev").fadeTo("fast", 0.5).css("cursor", "default");
				}
			});
		}
	});

	$(".p, .n").hide();

	$(".zoom a[class^='lv']").each(function(i, elm){
		$(elm).click(function(){
			var stupka = $(elm).attr("class").slice(-1);
			$('.novina p').css("font-size", 11 + stupka * 4 );
			return false;
		});
	});


	if($('#pics img').length > 1)
	{
		$("#pics")
			.mouseenter(function(){
				$(".p, .n").fadeIn();
			}).mouseleave(function(){
				$(".p, .n").fadeOut();
			});
	}


	$(".carousel").jCarouselLite({
		btnNext: ".n",
		btnPrev: ".p",
		visible: 1,
		scroll: 1,
		speed: 800,
		mouseWheel: true,
		afterEnd: function(vli) {
			var img = $('img', vli);
			$(".title-img").text(img.attr('title'));
			$("p.link").text(img.attr('alt'));
		}
	});


	$("#tabcontent").jCarouselLite({
		btnGo		: ["#news_tab1", "#news_tab2", "#news_tab3", "#news_tab4", "#news_tab5"],
		visible		: 1,
		speed		: 200,
		circular	: false,
		start		: 0
	});

});


function bookmarksite(title,url){
	if(document.all) {// ie
		window.external.AddFavorite(url, title);
	}
	else if (window.sidebar) {// firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print)
	{ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
}
