/*

	UkrainianBlogs.com
	JavaScript functions
	
	Authors: UkrainianBlogs.com team
	Copyright: 2009, UkrainianBlogs.com. All rights resevered

-----------------------------------------------------------------------*/

    var UAblogs = {}

    /*
     *  Reloads captcha
     */
	function ReloadCaptcha()
	{		var src = $('#captcha_image').attr('src');
		if(src.indexOf('?')>-1)
		{            src += Math.round(Math.random()*20);		}
		else
		{			src += '?'+Math.round(Math.random()*20);		}	    $('#captcha_image').attr('src', src);
	}

    /*
     *  Replaces '/./' with current location to build correct urls if javascipt will become disable
     */
    function ReplaceCurrDirWithLocation(text)
    {    	var murl = window.location.href;
    	murl = murl.replace('.html', '');
    	while(window.location.hash!='' && murl.indexOf(window.location.hash)>-1)
		{			murl = murl.replace(window.location.hash, '');        }
        reg = /(blogs|category|search)\/(.+)\/page\d+.*/;
        parts = murl.match(reg);
        if(parts!= null && typeof(parts[2]) != 'undefined')
        {
            murl = parts[1] +  '/' +  parts[2];
        }
        reg = /(city|region)\/(.+)\/(.+)\/page\d+.*/;
        parts = murl.match(reg);
        if(parts!= null && typeof(parts[2]) != 'undefined')
        {
            murl = parts[1] +  '/' +  parts[2] + '/' +  parts[3];
        }    	while(text.indexOf('/./')>-1)
		{
    		text = text.replace('/./', murl + '/');
		}
		return text;    }

    /*
     *  Writes email in document body
     */
    function WriteEmail(user, domain)
    {
    	user = unescape(user);
    	domain = unescape(domain);
        document.write('<a h' + 'ref' + '="ma' + 'il' + 'to:');
    	document.write(user + '@');
    	document.write(domain);
    	document.write('">');
    	document.write(user + '@');
    	document.write(domain);
        document.write('</a>');
    }

    $(document).ready(function()
    {    	//sets languages handlers
        $('.lang a').click(function() {        	    $.cookie('lang', $(this).className(), { expires: 604800, path: "/", });                window.location.reload();
        	    return false;        });
		//language switcher (a bit hardcoded)	
		$('.lang a.ru').mouseover(function() {
			if(!$(this).parent().hasClass('selected')) {
				$(this).stop().animate({opacity: '1'},300);
			}
		});
		
		$('.lang a.ru').mouseout(function() {
			if(!$(this).parent().hasClass('selected')) {
				$(this).stop().animate({opacity: '0.3'},300);
			}
		})		
		$('.lang a.ua').mouseover(function() {
			if(!$(this).parent().hasClass('selected')) {
				$(this).stop().animate({opacity: '1'},300);
			}
		});
		
		$('.lang a.ua').mouseout(function() {
			if(!$(this).parent().hasClass('selected')) {
				$(this).stop().animate({opacity: '0.3'},300);
			}
		})
		
		//rel="external"
		$('a[rel="external"]').click( function() {
			window.open( $(this).attr('href') );
			return false;
		});		

		
    });
