/*

	UkrainianBlogs.com
	Blog page functions
	
	Authors: UkrainianBlogs.com team
	Copyright: 2009, UkrainianBlogs.com. All rights resevered

-----------------------------------------------------------------------*/


/*
 *    Sets limit for textarea
 */
UAblogs.LimitChars = function(textid, limit, infodiv)
{
    var text = textid.val();
    var textlength = text.length;
    if(textlength > limit)
    {
        infodiv.html(txt_blog_comment_limit+limit+txt__symbols_escl);
        textid.val(text.substr(0,limit));
        return false;
    } else {
        infodiv.html(txt_left_ + (limit - textlength) + txt__symbols);
        return true;
    }
}

/*
 *    Sets comment pagination
 */
function SetPagination()
{
    $('.pagination li a').each(function(index) {
        $(this).click(function()
            {            	//loads new comments with AJAX
                 if(blog_id)
                 {
                     var url = '/blogs/ajax_comment_rotation.html';
                  	 var href = $(this).attr('href');
                  	 if(href!=undefined && href!='')
                  	 {
                     	 if(href.indexOf(window.location)>-1)
                     	 {
                     	     href = href.replace(window.location, '');
                         }

            	         reg = /page(\d+)/;
    	                 parts = href.match(reg);
    	                 href = '?page=' + escape(parts[1]);

                         url += href;

                         $.post(url, { blog_id : escape(blog_id)}, function(xml)
                  		 {
    						//replacec /./ with current url
    						$('#pagenator').html(ReplaceCurrDirWithLocation($("pagination", xml).text()));
    						$('#comments').html(ReplaceCurrDirWithLocation($("comment", xml).text()));
    					    SetPagination();
                    	 });
                     }
                 }
                 return false;
            });
    });
}

/*
 *    Sets captcha reload handler
 */
$("#changecaptcha").click(function(){
		ReloadCaptcha();
		return false;
	});

/*
 *    Sets validators and pagenators
 */
$(document).ready(function() {

    $(".field input, .field textarea").focusFields("#000", "1", "#fff", "#000");

    $('textarea.max250').each(function(index) {
        $(this).after('<div class="maxLimit"></div>');
        $(this).keyup(function(){
            UAblogs.LimitChars($(this), '250', $(this).siblings('.maxLimit'));
        });
        $(this).keyup()

    });

     $("#addOpinionForm").validate({
            rules : {
                    _input_author : {required : true, minlength: 2},
                    _input_email : {required : true, email: true},
                    _input_url : {required : true, url : true},
                    _input_text : "required",
                    _input_captcha : "required"
            },
            messages : {
                    _input_author : {
                            required : error_addcomm_author,
                            minlength : error_addcomm_min_auth
                    },
                    _input_email : {
                            required : error_addcomm_email,
                            email : error_addcomm_email2
                    },
                    _input_url : {
                            required : error_addcomm_url,
                            url : error_addcomm_url2
                    },
                    _input_text : { required : error_addcomm_text },
                    _input_captcha : {required : error_addblog_captcha }
            }
    });

    SetPagination();

    if($('#onair'))
    {
       	$('#onair').html(txt_loading);
        $.post('/blogs/onair.html', {id:blog_id}, function(xml)
        {
        	$('#onair').html(xml);
			$('#onair ul.entries').cycle({
			  fx:     'scrollHorz',
			  prev:   '#prevBlogEntry',
			  next:   '#nextBlogEntry',
			  height: 250,
			  timeout: 0,
			  easing: 'easeInOutExpo'
			});
        });
    }
});
