$(function() {
	$('#gallery a').lightBox();
	$('#real_gallery a').lightBox();
});
$(document).ready(function(){
	$(".menu_button").hover(
		function(){$(this).css("background","url(images/menu_hover.png)");},
		function(){$(this).css("background","url(images/menu.png)");}
	);

    $(".menu_button_projects").hover(
		function(){$(this).css("background","url(images/menu_hover.png)");},
		function(){$(this).css("background","url(images/menu.png)");}
	);

	$(".menu").hover(
		function(){
			$(this).stop().animate({left: "40px"}, {queue: false, duration: 200} );
		},
		function(){
			$(this).animate({left: "25px"}, {queue: false, duration: 200} );
		}
	);

	// Pager states
	$('.pager_prev').hover(function(){
		$(this).css('background-position', '0px -29px');
	}, function(){
		$(this).css('background-position', '0px 0px');
	}).mousedown(function(){
		$(this).css('background-position', '0px -58px');
	}).mouseup(function(){
		$(this).css('background-position', '0px 0px');
	});
    $('.pager_next').hover(function(){
		$(this).css('background-position', '0px -29px');
	}, function(){
		$(this).css('background-position', '0px 0px');
	}).mousedown(function(){
		$(this).css('background-position', '0px -58px');
	}).mouseup(function(){
		$(this).css('background-position', '0px 0px');
	});

	// Show search options
	// BG
		$("#searchbox").focus(function () {
	         $("#soptions").fadeIn();
	    })
	// EN
		$("#searchbox_en").focus(function () {
	         $("#soptions").fadeIn();
	    })

	// Search image
	$('#search_img').click(function(){
		$('#search_form').submit();
	});
	// Slider options
	$(".forw").click(function(){
		if($(this).parents(".slider").data("move") != true){
			$(this).parents(".slider").data("move", true);

			var sTop = $(this).parents(".slider").find(".slide").scrollLeft() + 89;
			$(this).parents(".slider").find(".slide").animate({scrollLeft: sTop},  function(){$(this).parents(".slider").data("move", false);});
			$(this).parents(".slider").find(".back").attr("src", "images/pr_prev.png");
			if(sTop >= $(this).parents(".slider").find(".slide").attr("scrollWidth") - $(this).parents(".slider").find(".slide").width()) this.src = "images/pr_next_light.png";
			else this.src = "images/pr_next.png";

		}
	});
	$(".back").click(function(){
		if($(this).parents(".slider").data("move") != true){
			$(this).parents(".slider").data("move", true);

			var sTop = $(this).parents(".slider").find(".slide").scrollLeft() - 89;
			$(this).parents(".slider").find(".slide").animate({scrollLeft: sTop}, function(){$(this).parents(".slider").data("move", false);});
			$(this).parents(".slider").find(".forw").attr("src", "images/pr_next.png");
			if(sTop > 0) this.src = "images/pr_prev.png";
			else this.src = "images/pr_prev_light.png";

		}
	});

	// News toggle
	$(".shownews").click(function(){
		$(this).parents(".news").find(".news_t").toggle("slow");
		$(this).find("p").toggle();
	});

	// Subscribe to newsletter
    $(".bulletin").submit(function(event){
    	event.preventDefault();
    	$.post("bulletin_add", {mail: encodeURIComponent($("#bmail").val())}, function(data){
			$("#bulletin_message").text(data).fadeOut(7000);
    	}, 'html');
    });

    // Check length
    function correctLen(str, len){
		if (str.length < len)
			return false;
		else if(str.length >= len )
			return true;
		else
			return false;
	}

	// Validate e-mail
	function validMail(str){
		if( str.indexOf(".") > 0 && str.indexOf("@") > 0 && str.lastIndexOf(".") < (str.length - 2) )
			return true;
		else
			return false;
	}

    // Check username on input
	$('#register_name').keyup(function(){
		if( !correctLen($(this).val(), 3) ){
			$('#register_name_alert').html('<img src="images/exclamation-small.png" alt="" /> Минимум 3 символа');
		}
		else{
			$.post('check_username', {user: encodeURIComponent($(this).val())}, function(data){
				$('#register_name_alert').html(data);
			}, 'html');
		}
	});
		// EN
	    $('#register_name_en').keyup(function(){
			if( !correctLen($(this).val(), 3) ){
				$('#register_name_alert_en').html('<img src="images/exclamation-small.png" alt="" /> At least 3 characters');
			}
			else{
				$.post('check_username', {user: encodeURIComponent($(this).val())}, function(data){
					$('#register_name_alert_en').html(data);
				}, 'html');
			}
		});

	// Check password1 on input
	$('#register_pass1').blur(function(){
		if( $(this).val() == '' ){
			$('#register_pass1_alert').html('<img src="images/exclamation-small.png" alt="" /> Паролата е задължителна');
			$('#register_now').preventDefault();
		}
    	else if( correctLen($(this).val(), 5) )
			$('#register_pass1_alert').html('<img src="images/tick-small.png" alt="" /> Паролата е достатъчно дълга');
		else if( !correctLen($(this).val(), 5) ){
			$('#register_pass1_alert').html('<img src="images/cross-small.png" alt="" /> Паролата е малка');
			$('#register_now').preventDefault();
		}
	});
	    // EN
		$('#register_pass1_en').blur(function(){
			if( $(this).val() == '' ){
				$('#register_pass1_alert_en').html('<img src="images/exclamation-small.png" alt="" /> You must enter a password');
				$('#register_now_en').preventDefault();
			}
	    	else if( correctLen($(this).val(), 5) )
				$('#register_pass1_alert_en').html('<img src="images/tick-small.png" alt="" /> Password is long enough');
			else if( !correctLen($(this).val(), 5) ){
				$('#register_pass1_alert_en').html('<img src="images/cross-small.png" alt="" /> Password is too short');
				$('#register_now_en').preventDefault();
			}
		});

    // Check password2 on input
	$('#register_pass2').blur(function(){
		if( $(this).val() == '' ){
			$('#register_pass2_alert').html('<img src="images/exclamation-small.png" alt="" /> Паролата е задължителна');
			$('#register_now').preventDefault();
		}
    	else if( $(this).val() == $('#register_pass1').val() )
			$('#register_pass2_alert').html('<img src="images/tick-small.png" alt="" /> Паролите съвпадат');
		else if( $(this).val() != $('#register_pass1').val() ){
			$('#register_pass2_alert').html('<img src="images/cross-small.png" alt="" /> Паролите не съвпадат');
			$('#register_now').preventDefault();
		}
	});
	    // EN
		$('#register_pass2_en').blur(function(){
			if( $(this).val() == '' ){
				$('#register_pass2_alert_en').html('<img src="images/exclamation-small.png" alt="" /> You must enter a password');
				$('#register_now_en').preventDefault();
			}
	    	else if( $(this).val() == $('#register_pass1_en').val() )
				$('#register_pass2_alert_en').html('<img src="images/tick-small.png" alt="" /> Passwords are identical');
			else if( $(this).val() != $('#register_pass1_en').val() ){
				$('#register_pass2_alert_en').html('<img src="images/cross-small.png" alt="" /> Passwords must be identical');
				$('#register_now_en').preventDefault();
			}
		});

	// Validate e-mail_1 input
	$('#register_mail_1').keyup(function(){
			$.post('check_mail', {mail: encodeURIComponent($(this).val())}, function(data){
				$('#register_mail_1_alert').html(data);
			}, 'html');
	}).blur(function(){
        if( $(this).val() == '' ){
			$('#register_mail_1_alert').html('<img src="images/exclamation-small.png" alt="" /> E-mail-ът е задължителен');
            $('#register_now').preventDefault();
		}
		else if( !validMail( $(this).val() ) ){
			$('#register_mail_1_alert').html('<img src="images/cross-small.png" alt="" /> E-mail адресът не е валиден');
			$('#register_now').preventDefault();
		}
	});
	    // EN
		$('#register_mail_1_en').keyup(function(){
				$.post('check_mail', {mail: encodeURIComponent($(this).val())}, function(data){
					$('#register_mail_1_alert_en').html(data);
				}, 'html');
		}).blur(function(){
	        if( $(this).val() == '' ){
				$('#register_mail_1_alert_en').html('<img src="images/exclamation-small.png" alt="" /> You must enter your e-mail');
	            $('#register_now_en').preventDefault();
			}
			else if( !validMail( $(this).val() ) ){
				$('#register_mail_1_alert_en').html('<img src="images/cross-small.png" alt="" /> This e-mail is invalid');
				$('#register_now_en').preventDefault();
			}
		});

	// Check e-mail_2
    $('#register_mail_2').blur(function(){
        if( $(this).val() == '' ){
			$('#register_mail_2_alert').html('<img src="images/exclamation-small.png" alt="" /> E-mail-ът е задължителен');
			$('#register_now').preventDefault();
		}
    	else if( $(this).val() == $('#register_mail_1').val() )
			$('#register_mail_2_alert').html('<img src="images/tick-small.png" alt="" /> E-mail адресите съвпадат');
		else if( $(this).val() != $('#register_mail_1').val() ){
			$('#register_mail_2_alert').html('<img src="images/cross-small.png" alt="" /> E-mail адресите не съвпадат');
			$('#register_now').preventDefault();
		}
    });
	    // EN
	    $('#register_mail_2_en').blur(function(){
	        if( $(this).val() == '' ){
				$('#register_mail_2_alert_en').html('<img src="images/exclamation-small.png" alt="" /> You must enter your e-mail');
				$('#register_now_en').preventDefault();
			}
	    	else if( $(this).val() == $('#register_mail_1_en').val() )
				$('#register_mail_2_alert_en').html('<img src="images/tick-small.png" alt="" /> E-mails are identical');
			else if( $(this).val() != $('#register_mail_1_en').val() ){
				$('#register_mail_2_alert_en').html('<img src="images/cross-small.png" alt="" /> E-mails must be identical');
				$('#register_now_en').preventDefault();
			}
	    });

	// Check agree to terms
	$('#register_now').click(function(event){
		if( $('#register_bot').val() != 300 ){
	        event.preventDefault();
			$('#register_bot_alert').html('<img src="images/cross-small.png" alt="" /> Само хора могат да се регистрират :)');
		}
		else{
        	$('#register_bot_alert').html('');
		}
	});
	    // EN
		$('#register_now_en').click(function(event){
			if( $('#register_bot_en').val() != 300 ){
		        event.preventDefault();
				$('#register_bot_alert_en').html('<img src="images/cross-small.png" alt="" /> Only humans can register :)');
			}
			else{
	        	$('#register_bot_alert_en').html('');
			}
		});

	// Antispam check
	$('#register_now').click(function(event){
		if( !$('#register_terms').is(':checked') ){
        	event.preventDefault();
            $('#register_terms_alert').html('<img src="images/cross-small.png" alt="" /> Трябва да се съгласите с условията');
		}
        else{
        	$('#register_terms_alert').html('');
		}
	});
    // EN
	$('#register_now_en').click(function(event){
		if( !$('#register_terms_en').is(':checked') ){
        	event.preventDefault();
            $('#register_terms_alert_en').html('<img src="images/cross-small.png" alt="" /> You must agree to the terms');
		}
        else{
        	$('#register_terms_alert_en').html('');
		}
	});

	// Check oldpass in usersettings on input
	$('#settings_old_pass').blur(function(){
		$.post('check_pass.php', {pass: $(this).val()}, function(data){
			$('#settings_old_pass_alert').html(data);
		}, 'html')
	});

	// Check pass1 in usersettings on input
	$('#settings_pass1').blur(function(){
        if( correctLen($(this).val(), 5) )
			$('#settings_pass1_alert').html('<img src="images/tick-small.png" alt="" /> Паролата е достатъчно дълга');
		else if( !correctLen($(this).val(), 5) )
			$('#settings_pass1_alert').html('<img src="images/cross-small.png" alt="" /> Паролата е малка');
	});
	// Check pass2 in usersettings on input
    $('#settings_pass2').blur(function(){
    	if( $(this).val() == $('#settings_pass1').val() )
			$('#settings_pass2_alert').html('<img src="images/tick-small.png" alt="" /> Паролите съвпадат');
		else if( $(this).val() != $('#settings_pass1').val() )
			$('#settings_pass2_alert').html('<img src="images/cross-small.png" alt="" /> Паролите не съвпадат');
	});
        //EN
		$('#settings_pass1_en').blur(function(){
	        if( correctLen($(this).val(), 5) )
				$('#settings_pass1_alert').html('<img src="images/tick-small.png" alt="" /> Password is long enough');
			else if( !correctLen($(this).val(), 5) )
				$('#settings_pass1_alert').html('<img src="images/cross-small.png" alt="" /> Password is too short');
		});
		// Check pass2 in usersettings on input
	    $('#settings_pass2_en').blur(function(){
	    	if( $(this).val() == $('#settings_pass1_en').val() )
				$('#settings_pass2_alert').html('<img src="images/tick-small.png" alt="" /> Passwords are identical');
			else if( $(this).val() != $('#settings_pass1').val() )
				$('#settings_pass2_alert').html('<img src="images/cross-small.png" alt="" /> Passwords must be identical');
		});

    // Check e-mail to recover password
    $('#recover_mail').blur(function(){
        if( $(this).val() == '' ){
			$('#recover_mail_alert').html('<img src="images/exclamation-small.png" alt="" /> E-mail-ът е задължителен');
		}
		else if( !validMail( $(this).val() ) ){
			$('#recover_mail_alert').html('<img src="images/cross-small.png" alt="" /> E-mail адресът не е валиден');
		}
        else if ( validMail( $(this).val() ) ){
			$('#recover_mail_alert').html('');
		}
	});
	    // EN
	    $('#recover_mail_en').blur(function(){
	        if( $(this).val() == '' ){
				$('#recover_mail_alert_en').html('<img src="images/exclamation-small.png" alt="" /> You must enter your e-mail');
			}
			else if( !validMail( $(this).val() ) ){
				$('#recover_mail_alert_en').html('<img src="images/cross-small.png" alt="" /> This e-mail is invalid');
			}
	        else if ( validMail( $(this).val() ) ){
				$('#recover_mail_alert_en').html('');
			}
		});

	// Show tooltip on project-slider hover
	// Projects slider tooltips
    $('#latest_projects a').each(function(){
    	$(this).qtip({
	    	content: {
	    		text: '<img class="throbber" src="images/loading5.gif" alt="Loading..." />',
	    		url: 'project_tooltip.php',
				data: {url: $(this).attr('href')},
				method: 'get'
	    	},
			position: {
				corner: {
					target: 'rightMiddle',
					tooltip: 'leftBottom'
				}
			},
			style: {
                border: {
			    	width: 1,
			    	radius: 4
			    },
				name: 'light'
			},
			show: {
				delay: 50,
               	effect: {
               		length: 500
               	}
			},
               hide: {
				delay: 50,
               	effect: {
               		length: 500
               	}
			}
		});
    });
    // Show tooltips on network-link hover
		$('.strezov_network').each(function(){
			$(this).qtip({
				content: $(this).attr('alt'),
                position: {
					corner: {
						target: 'leftTop',
						tooltip: 'rightBottom'
					}
				},
				style: {
	                border: {
				    	width: 1,
				    	radius: 4
				    },
					name: 'light',
					tip: 'rightBottom'
				},
				show: {
					delay: 50,
                	effect: {
                		length: 500
                	}
				},
                hide: {
					delay: 50,
                	effect: {
                		length: 500
                	}
				}
			});
		});
	//jqPrint plugin settings
	$('.single_blog_print').click(function(event){ // Print single blog post
       $('.printable').jqprint({
			operaSupport: true
		});
		event.preventDefault();
	});

	$('.single_article_print').click(function(event){ // Print sigle article
       $('.printable').jqprint({
			operaSupport: true
		});
		event.preventDefault();
	});
	// Send AJAX request when clicking Print from the posts list
	$('.all_blog_print').click(function(event){
			$.get('request_blog.php', {id: $(this).attr('href')}, function(data){
	        	$('.hidden_print').append(data).fadeOut(1).jqprint({
	        		operaSupport: true
	        	});
	        });
			event.preventDefault();
	});
    // Send AJAX request when clicking Print from the articles list
	$('.all_articles_print').click(function(event){
			$.get('request_article.php', {id: $(this).attr('href')}, function(data){
	        	$('.hidden_print').append(data).fadeOut(1).jqprint({
	        		operaSupport: true
	        	});
	        });
			event.preventDefault();
	});

	// Change language flags on hover
	$('#bgflag').imghover({suffix: '_hover'});
	$('#enflag').imghover({suffix: '_hover'});

	// Change network links on hover
	$('.strezov_network').imghover({suffix: '_hover'});

	// Change play icon on hover
	$('.play_icon').imghover({suffix: '_hover'});

	// Change login submit button on hover
	$('#login_submit').mouseover(function(){
		$(this).css('background', 'url(images/login_hover.gif) no-repeat');
	}).mouseout(function(){
		$(this).css('background', 'url(images/login.gif) no-repeat');
	});
	// Change the newsletter submit button on hover
	$('#newsletter_submit').mouseover(function(){
		$(this).css('background', 'url(images/icon_e-mail_hover.png) no-repeat');
	}).mouseout(function(){
		$(this).css('background', 'url(images/icon_e-mail.png) no-repeat');
	});

	// Toggle username input field value on focus and on blur
	// BG
		$('#login_username').focus(function(){
			if($(this).val() == 'име'){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val() == ''){
				$(this).val('име');
			}
		});
    // EN
		$('#login_username_en').focus(function(){
			if($(this).val() == 'user'){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val() == ''){
				$(this).val('user');
			}
		})

    // Toggle password input field value on focus and on blur
	$('#login_password').focus(function(){
		if($(this).val() == '******'){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('******');
		}
	});

    // Toggle search input field value on focus and on blur
	// BG
		$('#searchbox').focus(function(){
			if($(this).val() == 'ключови думи...'){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val() == ''){
				$(this).val('ключови думи...');
			}
		});
	// EN
		$('#searchbox_en').focus(function(){
			if($(this).val() == 'keywords...'){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val() == ''){
				$(this).val('keywords...');
			}
		});

    // Toggle newsletter input field value on focus and on blur
	$('#bmail').focus(function(){
		if($(this).val() == 'e-mail...'){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('e-mail...');
		}
	});


	// Show RSS feed modal dialog
	var rssicon = '<img src="images/rss.png" width="16" height="16" alt="" />';
	// BG
		$('a#rss_feed').click(function(event){
			event.preventDefault();
		});
		$('a#rss_feed').qtip({
	           content: {
	         title: {
	            text: 'Изберете канал',
	            button: '<img src="images/cross-small.png" alt="X" />'
	         },
	         text: '<span class="rss_modal">' + rssicon + '&nbsp;<a href="http://www.google.com/ig/add?feedurl=http://www.strezov.net/rss/channel-news-bg" target="_blank">Новини</a><br />' +
	                rssicon + '&nbsp;<a href="http://www.google.com/ig/add?feedurl=http://www.strezov.net/rss/channel-projects-bg" target="_blank">Проекти</a><br />' +
	               	rssicon + '&nbsp;<a href="http://www.google.com/ig/add?feedurl=http://www.strezov.net/rss/channel-articles-bg" target="_blank">Статии</a><br />' +
				   	rssicon + '&nbsp;<a href="http://www.google.com/ig/add?feedurl=http://www.strezov.net/rss/channel-blog-bg" target="_blank">Блог</a></span>'
		      },
		      position: {
		         target: $(document.body), // Position it via the document body...
		         corner: 'center' // ...at the center of the viewport
		      },
		      show: {
		         when: 'click', // Show it on click
		         solo: true // And hide all other tooltips
		      },
		      hide: false,
		      style: {
		         width: 160,
		         padding: '14px',
		         border: {
		            width: 3,
		            radius: 6,
		            color: '#666666'
		         },
		         name: 'light'
		      },
		      api: {
		         beforeShow: function()
		         {
		            // Fade in the modal "blanket" using the defined show speed
		            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
		         },
		         beforeHide: function()
		         {
		            // Fade out the modal "blanket" using the defined hide speed
		            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
		         }
		      }
		});
	// EN
		$('a#rss_feed_en').click(function(event){
			event.preventDefault();
		});
		$('a#rss_feed_en').qtip({
	           content: {
	         title: {
	            text: 'Choose channel',
	            button: '<img src="images/cross-small.png" alt="X" />'
	         },
	         text: '<span class="rss_modal">' + rssicon + '&nbsp;<a href="http://www.google.com/ig/add?feedurl=http://www.strezov.net/rss/channel-news-en" target="_blank">News</a><br />' +
	                rssicon + '&nbsp;<a href="http://www.google.com/ig/add?feedurl=http://www.strezov.net/rss/channel-projects-en" target="_blank">Projects</a><br />' +
				   	rssicon + '&nbsp;<a href="http://www.google.com/ig/add?feedurl=http://www.strezov.net/rss/channel-blog-en" target="_blank">Blog</a></span>'
		      },
		      position: {
		         target: $(document.body), // Position it via the document body...
		         corner: 'center' // ...at the center of the viewport
		      },
		      show: {
		         when: 'click', // Show it on click
		         solo: true // And hide all other tooltips
		      },
		      hide: false,
		      style: {
		         width: 160,
		         padding: '14px',
		         border: {
		            width: 3,
		            radius: 6,
		            color: '#666666'
		         },
		         name: 'light'
		      },
		      api: {
		         beforeShow: function()
		         {
		            // Fade in the modal "blanket" using the defined show speed
		            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
		         },
		         beforeHide: function()
		         {
		            // Fade out the modal "blanket" using the defined hide speed
		            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
		         }
		      }
		});
     // Create the modal backdrop on document load so all modal tooltips can use it
   $('<div id="qtip-blanket">')
      .css({
         position: 'absolute',
         top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
         left: 0,
         height: $(document).height(), // Span the full document height...
         width: '100%', // ...and full width

         opacity: 0.7, // Make it slightly transparent
         backgroundColor: 'black',
         zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
      })
      .appendTo(document.body) // Append to the document body
      .hide(); // Hide it initially


   // Change validator icon on hover
   $('#valid').hover(
		function(){
			$(this).attr('src', 'images/xhtml_hover.gif');
		},
		function(){
        	$(this).attr('src', 'images/xhtml.gif');
		}
	);

	// Toggle help topics
	$('.help_answer').hide();
	$('#help div.help_title').css('cursor', 'pointer').click(function(){
		$(this).css({"background" : "url('images/arrow-315-medium.png') center left no-repeat", "color" : "#000"}).next('div.help_answer').slideToggle(300).siblings('.help_answer').slideUp('slow');
		$(this).siblings('.help_title').css({"background" : "url('images/arrow-000-medium.png') center left no-repeat", "color" : "#666"});
	});

});

function openPlayer() {
	popup = window.open("../player.php", 'player', "status=0, menubar=0, toolbar=0, width=200, height=300");
}

function closePlayer() {
	if (false == popup.closed)
		popup.close();
	else
		alert("Player already closed! ");
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+"="+escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
}

function loadPlaylist(c_name,value,expiredays){
	setCookie(c_name,value,expiredays);
	openPlayer();
}

function getCookie(c_name){
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "no cookie";
}



// BB code text area code

// Insert a text-formatting tag
	function insertTag(id, tag) {
	    if (document.selection) { // Opera and IE
			var area = document.getElementById(id);
	    	var str = document.selection.createRange().text;
			area.focus();
			var sel = document.selection.createRange();
			sel.text = "[" + tag + "]" + str + "[/" + tag + "]";
		}
		else { // Firefox and Opera
	        var area = document.getElementById(id);
	        var a = area.selectionStart, b = area.selectionEnd;
	        area.value =
	            area.value.substring( 0, a ) + '[' + tag + ']' +
	            area.value.substring( a, b ) + '[/' + tag + ']' +
	            area.value.substring( b );

		}
		return;
	}

//BG

	// Insert a link
	function insertUrl(id) {
		if (document.selection) { // Opera and IE
			var area = document.getElementById(id);
			var str = document.selection.createRange().text;
			if (str != null) {
				area.focus();
			    var link = prompt("Въведи URL", "http://");

				if (link != "" && link != "http://" && link != null && str == "") {
					var name = prompt("Въведи име за линка");
					if (name != null && name != "") {
						var sel = document.selection.createRange();
						sel.text = "[url=" + link + "]" + name + "[/url]";
					}
					else
						return;
	            }

				if (link != null && link != "" && link != "http://" && str != "") {
					var sel = document.selection.createRange();
			        sel.text = "[url=" + link + "]" + str + "[/url]";
				}
			}
		}
		else { // Firefox and Opera
			var area = document.getElementById(id);
			var a = area.selectionStart, b = area.selectionEnd;
			if (b > a) { // If text is selected, ask only for the URL
	        	address = prompt("Въведи URL", "http://");
				if (address != null && address != "" && address != "http://") {
					area.value =
						area.value.substring(0, a) + "[url=" +
						address + "]" +
						area.value.substring(a, b) + "[/url]" +
						area.value.substring(b);
				}
			}
			else { // If nothing is selected, ask for both URL and title
		        var address = prompt("Въведи URL", "http://");
				if (address != null && address != "" && address != "http://") {
					name = prompt("Въведи име за линка", "");
					if (name != "" && name != null) {
						area.value =
							area.value.substring(0, a) + "[url=" +
							address + "]" +
							name + "[/url]" +
							area.value.substring(b);
					}
				}
			}
		}

		return;
	}

//EN

	// Insert a link
	function insertUrl_en(id) {
		if (document.selection) { // Opera and IE
			var area = document.getElementById(id);
			var str = document.selection.createRange().text;
			if (str != null) {
				area.focus();
			    var link = prompt("Enter URL", "http://");

				if (link != "" && link != "http://" && link != null && str == "") {
					var name = prompt("Enter displayed name for the link");
					if (name != null && name != "") {
						var sel = document.selection.createRange();
						sel.text = "[url=" + link + "]" + name + "[/url]";
					}
					else
						return;
	            }

				if (link != null && link != "" && link != "http://" && str != "") {
					var sel = document.selection.createRange();
			        sel.text = "[url=" + link + "]" + str + "[/url]";
				}
			}
		}
		else { // Firefox and Opera
			var area = document.getElementById(id);
			var a = area.selectionStart, b = area.selectionEnd;
			if (b > a) { // If text is selected, ask only for the URL
	        	address = prompt("Enter URL", "http://");
				if (address != null && address != "" && address != "http://") {
					area.value =
						area.value.substring(0, a) + "[url=" +
						address + "]" +
						area.value.substring(a, b) + "[/url]" +
						area.value.substring(b);
				}
			}
			else { // If nothing is selected, ask for both URL and title
		        var address = prompt("Enter URL", "http://");
				if (address != null && address != "" && address != "http://") {
					name = prompt("Enter displayed name for the link", "");
					if (name != "" && name != null) {
						area.value =
							area.value.substring(0, a) + "[url=" +
							address + "]" +
							name + "[/url]" +
							area.value.substring(b);
					}
				}
			}
		}

		return;
	}