function accorDion() {
    //ACCORDION BUTTON ACTION (ON CLICK DO THE FOLLOWING)
    $('div.entry_meta').click(function() {
        $('div.entry_meta').next().children('#respond').hide();
        $('div.entry_meta2').next().children('#respond').hide();
        //REMOVE THE ON CLASS FROM ALL BUTTONS
        $('div.entry_meta').removeClass('expand');
        //NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
        $('div.entry_meta').next().slideUp('normal');

        var pid = $(this).parent().attr('id');
        var pname = $(this).children('h2.entry-title').text();
        //Added by Sam - Aug 26, 2011
        document.title = pname;
        var newpname = pname.split(' ');
        location.hash = pid + '/' + newpname.join('-') + '/';
        
        if ($('div.entry_meta2').is(':hidden') == false) {
            $('div.entry_meta2').next().slideUp('normal');
        }

        //IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
        if($(this).next().is(':hidden') == true) {
            //ADD THE ON CLASS TO THE BUTTON
            $(this).addClass('expand');
            //OPEN THE SLIDE
            $(this).next().slideDown('normal');
            $('div.entry_meta').next().children('#respond').hide();
            $('div.entry_meta2').next().children('#respond').hide();
        }
    });

    //CLOSES ALL ON PAGE LOAD
    $('div.entry_meta').next().hide();
}

function accorDion2() {
    //ACCORDION BUTTON ACTION (ON CLICK DO THE FOLLOWING)
    $('div.entry_meta2').click(function() {
        $('div.entry_meta').next().children('#respond').hide();
        $('div.entry_meta2').next().children('#respond').hide();
        //REMOVE THE ON CLASS FROM ALL BUTTONS
        $('div.entry_meta2').removeClass('expand');
        //NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
        $('div.entry_meta2').next().slideUp('normal');

        var pid = $(this).parent().attr('id');
        var pname = $(this).children('h2.entry-title').text();
        //Added by Sam - Aug 26, 2011
        document.title = pname;
        var newpname = pname.split(' ');
        location.hash = pid + '/' + newpname.join('-') + '/showall/';
        
        if ($('div.entry_meta').is(':hidden') == false) {
            $('div.entry_meta').next().slideUp('normal');
        }
        
        //IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
        if($(this).next().is(':hidden') == true) {
            //ADD THE ON CLASS TO THE BUTTON
            $(this).addClass('expand');
            //OPEN THE SLIDE
            $(this).next().slideDown('normal');
            $('div.entry_meta').next().children('#respond').hide();
            $('div.entry_meta2').next().children('#respond').hide();
        }
    });

    //CLOSES ALL ON PAGE LOAD
    $('div.entry_meta2').next().hide();
}

function loadBlog() {
    $(window).scroll(function(){
        if ($(window).scrollTop() == $(document).height() - $(window).height()){
            if ($(document).height() < '3000') {
                jQuery('#next_blog').html('<div id="ajax-loader"><img src="'+ file2 +'/images/ajax-loader.gif" alt="loading..." /></div>');
                setTimeout(function() {
                    jQuery('#next_blog').load(file2 + '/blog_page2.php');               
                }, 1000);
            }
        }
    });
}

$(document).ready(function() {
    $('#respond').hide();

    $('a.react-link').live('click', function() {
        $('.entry-info').next().slideToggle('slow').css({'visibility' : 'visible'});
        $('#author').val('');
        $('#email').val('');
        $('#url').val('');
        $('#comment').val('');
        return false;
    })

    getHashUrl();
    pageNumber();
})

function getHashUrl(){
    var reg = /#/;
    var reg2 = /showall/;
    var url = $(location).attr('href');
    var urlsplit = url.split('/');
    var post_id = urlsplit[5].substring(1);
    
    if (reg.test(url) == true) {      
        if (reg2.test(url) == true) {
            //jQuery('#next_blog').load(file2 + '/blog_page2.php');
            //alert($('#next_blog').children('div#'+post_id).attr('class'));
            $.ajax({
                  url: file2 + '/blog_page2.php',
                  success: function (data)
                  {
                    jQuery('#next_blog').html(data);
                    //ADD THE ON CLASS TO THE BUTTON
                    $('#next_blog').children('div#'+post_id).children('div.entry_meta2').addClass('expand');
                    //OPEN THE SLIDE
                    $('#next_blog').children('div#'+post_id).children('div.entry_meta2').next().slideDown('normal');
                    //Added by Sam - Aug 26, 2011
                    //alert( $('#next_blog').children('div#'+post_id).children('div.entry_meta2').children('h2.entry-title').text() );
                    document.title = $('#next_blog').children('div#'+post_id).children('div.entry_meta2').children('h2.entry-title').text();
                    $('html,body').animate({scrollTop: $("#"+post_id).offset().top},'slow');
                  }
            });
        } else {
            //ADD THE ON CLASS TO THE BUTTON
            $('div#'+post_id).children('div.entry_meta').addClass('expand');
            //OPEN THE SLIDE
            $('div#'+post_id).children('div.entry_meta').next().slideDown('normal');
            //Added by Sam - Aug 26, 2011
            //alert( $('div#'+post_id).children('div.entry_meta').children('h2.entry-title').text() );
            document.title = $('div#'+post_id).children('div.entry_meta').children('h2.entry-title').text();
            $('html,body').animate({scrollTop: $("#"+post_id).offset().top},'slow');
        }
        //alert(urlsplit[7]);
        //alert(post_id);
        //alert($('div.entry_meta').parent().attr('id'));
        
        //$('div.entry_meta').next().children('#respond').hide();
        //$('div.entry_meta2').next().children('#respond').hide();
    }
}

function insertComment(id) {
//    $('#submit-'+id).live('click', function() {
      //console.log('#submit-'+id);
        var author = $('.author-'+id).val();
        var email = $('.email-'+id).val();
        var url = $('.url-'+id).val();
        var comment = $('.comment-'+id).val();
        var validate = 'OK';
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
//        console.log(comment);
//        console.log(file2 + '/show-comments.php');
//        jQuery('.show-comments-'+id).load(file2 + '/show-comments.php?id='+id+'&author='+author+'&email='+email+'&url='+url+'&comment='+comment);
        if (author == '' || author == null) {
            $('.error-author-'+id).css({'visibility' : 'visible'});
            validate = 'NO';
        } else {
            $('.error-author-'+id).css({'visibility' : 'hidden'});
        }

        if (email == '' || email == null || reg.test(email) == false) {
            $('.error-email-'+id).css({'visibility' : 'visible'});
            validate = 'NO';
        } else {
            $('.error-email-'+id).css({'visibility' : 'hidden'});
        }

        if (comment == '' || comment == null) {
            $('.error-comment-'+id).css({'visibility' : 'visible'});
            validate = 'NO';
        } else {
            $('.error-comment-'+id).css({'visibility' : 'hidden'});
        }

        if (validate == 'OK') {
            $.ajax({
                  url: file2 + '/show-comments.php',
                  data: 'id='+id+'&author='+author+'&email='+email+'&url='+url+'&comment='+comment+'&insert=insert',
                  success: function (data)
                  {
                    jQuery('.show-comments-'+id).html(data);
                    $('.author-'+id).val('');
                    $('.email-'+id).val('');
                    $('.url-'+id).val('');
                    $('.comment-'+id).val('');
                  }
            });
        }
//    })
}

function pageNumber() {
    $('#page_number').live('click', function(){
        var title = $(this).attr('title');
        var titleArray = title.split('-');
        var pageNum = titleArray[0];
        var postID = titleArray[1];

        $.ajax({
            url: file2 + '/show-comments.php',
            data: 'page='+pageNum+'&postID='+postID+'&next=next',
            success: function (data){
                jQuery('.show-comments-'+postID).html(data);
            }
        })
    })
}
