$(function(){

	prettyPrint();

    //slide down comment
    $('.comments_title').click(function(){

        var thisIcon = $(this).children('img');
        var thisIconSrc = $(this).children('img').attr('src');
        var thisComm = $(this).next('.comments');
        var thisPostId = $(this).children('.posts_id').html();

        if(thisIconSrc == 'images/icon_plus.png'){
            thisIcon.attr('src', 'images/icon_hourglass.gif');
            $.post('query.php', {
                action: 'getCommentsByPostIdLimit',
                page: 0,
                postId: thisPostId
                }, function(data){
                    thisComm.children('.replaced').html(data);
                    thisIcon.attr('src', 'images/icon_minus.png').attr('alt', 'Hide Comments').attr('title', 'Hide Comments');
                    $('.comments').not(thisComm).slideUp(300, function(){
                        $('.icon_plus').not(thisIcon).attr('src', 'images/icon_plus.png').attr('alt', 'Show Comments').attr('title', 'Show Comments');
                    });
                    thisComm.slideDown(300);
            });
            $.post('query.php', {
                action: 'getNumOfAllCommentsByPostId',
                postId: thisPostId
                }, function(data){
                    thisIcon.prev('.comments_count').html(data);
            });
        }else{
            thisComm.slideUp(300, function(){
                thisIcon.attr('src', 'images/icon_plus.png').attr('alt', 'Show Comments').attr('title', 'Show Comments');
            });
        }
    });

    //Toggle category posts
    $('.categories_title').click(function(){
        var thisIcon = $(this).children('img');
        var thisIconSrc = $(this).children('img').attr('src');

        if(thisIconSrc == 'images/icon_plus.png'){
            $(this).next('.categories_post').slideDown(300, function(){
                thisIcon.attr('src', 'images/icon_minus.png');
            });
        }else{
            $(this).next('.categories_post').slideUp(300, function(){
                thisIcon.attr('src', 'images/icon_plus.png');
            });
        }
    });

    //Effect of form on focus or on blur
    $('.add_comments_name').focus(function(){
        if($(this).val() == 'Name'){
            $(this).val('');
        }
    }).blur(function(){
        if($(this).val() == ''){
            $(this).val('Name');
        }
    });
    $('.add_comments_email').focus(function(){
        if($(this).val() == 'Email'){
            $(this).val('');
        }
    }).blur(function(){
        if($(this).val() == ''){
            $(this).val('Email');
        }
    });
    $('.add_comments_textarea').focus(function(){
        if($(this).val() == 'Comment'){
            $(this).val('');
        }
    }).blur(function(){
        if($(this).val() == ''){
            $(this).val('Comment');
        }
    });

    //Add comment

    function lockCommentForm(inForm){
        $(inForm).children('.add_comments_submit').attr('disabled', 'disabled').attr('style', 'background-color: #eee;background-image: url(images/button_hourglass.gif);background-repeat: no-repeat;background-position: 23px 4px;').val('').blur();
        $(inForm).children('input').not('.add_comments_submit').attr('readonly', 'readonly').attr('style', 'color: #999;background-color: #eee;');
        $(inForm).children('textarea').attr('readonly', 'readonly').attr('style', 'color: #999;background-color: #eee;');
    }
    function unlockCommentForm(inForm){
        $(inForm).children('.add_comments_submit').removeAttr('disabled').attr('style', '').val('Submit').blur();
        $(inForm).children('input').removeAttr('readonly').attr('style', '');
        $(inForm).children('textarea').removeAttr('readonly').attr('style', '');
    }
    function resetCommentForm(inForm){
        $(inForm).children('.add_comments_submit').removeAttr('disabled').attr('style', '').val('Submit').blur();
        $(inForm).children('.add_comments_name').removeAttr('readonly').attr('style', '').val('Name');
        $(inForm).children('.add_comments_email').removeAttr('readonly').attr('style', '').val('Email');
        $(inForm).children('.add_comments_textarea').removeAttr('readonly').attr('style', '').val('Comment');
        $(inForm).children('.add_comments_char_count').children('span').html('2');
    }
    function autoFill(inName, inEmail){
        $('.add_comments_submit').removeAttr('disabled').attr('style', '').val('Submit').blur();
        $('.add_comments_name').removeAttr('readonly').attr('style', '').val(inName);
        $('.add_comments_email').removeAttr('readonly').attr('style', '').val(inEmail);
        $('.add_comments_textarea').removeAttr('readonly').attr('style', '').val('Comment');
        $('.add_comments_char_count').children('span').html('7');
    }

    $('.add_comments').submit(function(){
        var inName = $.trim($(this).children('.add_comments_name').val());
        var inEmail = $.trim($(this).children('.add_comments_email').val());
        var inText = $.trim($(this).children('.add_comments_textarea').val());
        var errorInfo = $(this).children('.add_comments_error_info');
        var thisPostId = $(this).parents('.comments').prev('.comments_title').children('.posts_id').html();
		var replyVisible = $(this).parents('.comments').prev('.comments_title').children('.reply_visible').html();
        var thisComm = $(this).parents('.comments');
        var thisForm = $(this);
        var postUrl = $(this).parent().parent().find('.post_title').attr('href');

        //alert(postUrl);
        
        var respondId = $(this).children('.respond_title').attr('target');
        var respondUser = $(this).children('.respond_title').children('span').attr('target');

        //alert(respondId + ' | ' + responduser);

        errorInfo.html('');

        if(inName == 'Name' || !inName){
            errorInfo.html('Please enter name.').fadeOutReset(5000);
            thisForm.children('.add_comments_name').shake(200, 10, 0.95);
            thisForm.children('.add_comments_name').focus();

        }else if(inName.length < 3){
            
			errorInfo.html('Too short Name.').fadeOutReset(5000);
            thisForm.children('.add_comments_name').shake(200, 10, 0.95);
            thisForm.children('.add_comments_name').focus();

		}else if(inEmail == 'Email' || !inEmail){
			
            errorInfo.html('Please enter email.').fadeOutReset(5000);
            thisForm.children('.add_comments_email').shake(200, 10, 0.95);
            thisForm.children('.add_comments_email').focus();

        }else if(inEmail.length < 5){

            errorInfo.html('Invalid email.').fadeOutReset(5000);
            thisForm.children('.add_comments_email').shake(200, 10, 0.95);
            thisForm.children('.add_comments_email').focus();

        }else if(inText == 'Comment' || !inText){

            errorInfo.html('Please enter comment.').fadeOutReset(5000);
            thisForm.children('.add_comments_textarea').shake(200, 10, 0.95);
            thisForm.children('.add_comments_textarea').focus();

        }else if(inText.length > 400){

            errorInfo.html('Overlong comment.').fadeOutReset(5000);
            thisForm.children('.add_comments_textarea').shake(200, 10, 0.95);
            thisForm.children('.add_comments_textarea').focus();

        }else{

            if(respondUser){
                inText = 'Reply ' + respondUser + ' : ' + inText;
            }

            lockCommentForm(this);
            $.post('query.php',{
                    action: 'addComment',
                    postId: thisPostId,
                    name: inName,
                    email: inEmail,
                    text: inText,
					replyVisible: replyVisible
                }, function(data){
					
                    if(data == 'invalidUsername'){

                        errorInfo.html('Overlong name.').fadeOutReset(5000);
                        unlockCommentForm(thisForm);
                        thisForm.children('.add_comments_name').shake(500, 10, 0.95);
                        thisForm.children('.add_comments_name').focus();

                    }else if(data == 'invalidEmail'){
                        
						errorInfo.html('Invalid email.').fadeOutReset(5000);
                        unlockCommentForm(thisForm);
                        thisForm.children('.add_comments_email').shake(200, 10, 0.95);
                        thisForm.children('.add_comments_email').focus();

                    }else if(data == 'invalidCommentText'){

                        errorInfo.html('Overlong comment.').fadeOutReset(5000);
                        unlockCommentForm(thisForm);
                        thisForm.children('.add_comments_textarea').shake(200, 10, 0.95);
                        thisForm.children('.add_comments_textarea').focus();

                    }else if(data == 'success' || data == 'replied'){
                        $.post("query.php", {
                            action: 'getCommentsByPostIdLimit',
                            page: 0,
                            postId: thisPostId
                            }, function(data){
                                thisComm.children('.replaced').html(data);
                                autoFill(inName, inEmail);
                                
                            });
                        $.post('query.php', {
                            action: 'getNumOfAllCommentsByPostId',
                            postId: thisPostId
                            }, function(data){
                                thisComm.prev('.comments_title').children('.comments_count').html(data);
                            });

                        if(respondId){
                            $.post('query.php', {
                                action: 'sendMailOfRespond',
                                from: inName,
                                toId: respondId,
                                comment: inText,
                                url: postUrl
                                }, function(data){
                                    //$('#btm').html(data);
                                    //alert(data);
                                });
                        }
						if(data == 'replied'){
							window.location.reload();
                        }
                    }else{
                        //alert('Apology to you for the program error.');
						errorInfo.html('Unknown Error.').fadeOutReset(5000);
                        unlockCommentForm(thisForm);
                        //thisComm.children('.replaced').html(data);
                    }
            });
        }
    });

    //Comment chars count
    $('.add_comments_textarea').keyup(function(){
        var len = $(this).val().length;
        $(this).siblings('.add_comments_char_count').children('span').html(len);
    }).focus(function(){
        var len = $(this).val().length;
        $(this).siblings('.add_comments_char_count').children('span').html(len);
    }).blur(function(){
        var len = $(this).val().length;
        $(this).siblings('.add_comments_char_count').children('span').html(len);
    });

    //To top animated
    $('#more_top').click(function(){
        $('html, body').animate({
            scrollTop: 0
         }, 500);
    });

    //Btn Pagination style
    var preHref = $('.more .previous a').attr('href');
    $('.more .previous').replaceWith('<a href="'+ preHref+ '" id="more_prev" hidefocus = "true">Next&nbsp;<img src="images/icon_right.png" /></a>');
    var nextHref = $('.more .next a').attr('href');
    $('.more .next').replaceWith('<a href="'+ nextHref+ '" id="more_next" hidefocus = "true">Previous&nbsp;<img src="images/icon_left.png" /></a>');

    //Blur button onfocus dashed frame
    $('#search_submit').focus(function(){
        $(this).blur();
    });
    $('.add_comments_submit').focus(function(){
        $(this).blur();
    });
	$('.post_password_submit').focus(function(){
        $(this).blur();
    });
	$('.add_comments').focus(function(){
        $(this).blur();
    });

    $('.gallery a, ._lightbox').lightBox();
    
	/*
    $('img').each(function(i){
        if(!this.complete){
            var img = $(this);
            img.animate({
                opacity: 0
            }, 0, function(){
                img.load(function(){
                    img.animate({
                        opacity: 1
                    }, 500);
                });
            });
        }
    });
	*/

    //Search input event

    $('#search_input').focus(function(){
        //alert('s');
        $(this).animate({
            width: '140px'
        }, 200);
    });
    $('#search_input').blur(function(){
        //alert('s');
        $(this).animate({
            width: '100px'
        }, 200);
    });

});

