$(document).ready(function(){
    // misc functions
    
    function jsScrib(theURL) {
        theURL = theURL.replace(/ /g, "_");
        return theURL;
    }
    
    // hovers for the links	
    
    $('#right .link').addClass('cursorChange').hover(function() {
        $(this).addClass('linkHover');
    }, function() {
        $(this).removeClass('linkHover');
        
    }).click(function() {
        window.location = $(this).children().children('a').attr('href')
    });
    
    function mainLinks() {
        $('.searchBox').addClass('cursorChange').hover(function(){
            $(this).addClass('searchHover');
        }, function() {
            $(this).removeClass('searchHover');

        }).click(function() {
            window.location = $(this).children().children('a').attr('href');
        });
    }
    
    mainLinks();
    


// home functions   

    // move the continue reading anchor inline with the para above it
    
    if($('#homeCont').length > 0) {
        addToMe = $('#homeCont').prev();
        if($(addToMe)[0].tagName == "P") {
            $(addToMe).append('\n').append($('#homeCont'));
        }
    }
    
    if($('#filterToggle').length > 0) {
        $('#topicsRight').hide();
        $('#filterToggle').toggle(function() {
            $('#topicsRight').slideDown('fast');
        }, function() {
            $('#topicsRight').slideUp('fast');
            
        });
    }
    $('.ajaxTopic').click(function() {
        $('#content').html('');
         data = {}
         data.filter_key = $(this).attr('id');
         
         $.post("filter/", data, function(responseData){
             // do a simple alert for now, alert "Article was saved"
             $('h1').html(responseData.page_title);
             $('h2').html(responseData.page_subtitle);
             $('#postInfo li:first').html(responseData.article_counter);
             the_html = "";

            $.each(responseData.articles_html, function(i){
                the_html += "<div class=\"searchBox\"><h3><a href=\"/posts/"+jsScrib(responseData.articles_html[i].fields.title)+"\">"+responseData.articles_html[i].fields.title+"</a></h3><p>"+responseData.articles_html[i].fields.subtitle+"</p></div> <!-- searchBox -->"
            });
             $('#content').html(the_html);
             mainLinks();
             
         }, "json");
        return false; 
     });
    
// comment functions
    
    if($('#addComment').length > 0) {
        // add remove help vals from inputs
        if(!$('#dynamicCommentsOff').length > 0) {
            initialVal = new Array();
            $('#addComment input, #addComment textarea').not('#comSubmit').each(function(i) {
                initialVal[i] = $(this).val();
                $(this).addClass('dynInput').focus(function() {
                    if($(this).val() == initialVal[i]) {
                        $(this).removeClass('dynInput').val('');
                    } 
                }).blur(function() {
                    if($(this).val() == "") {
                        $(this).addClass('dynInput').val(initialVal[i])
                    }
                });
            });
            $('#addComment').before('<a href="#" class="smallLeftLink" id="writeComment">Write a Comment</a>').hide();
            
        } else {
            $('#addComment').before('<a href="#" class="smallLeftLink" id="writeComment">Write a Comment</a>');
        }
        
        $('#comSubmit').click(function() {
            if($('#id_website').val() == "Website (optional)") {
                $('#id_website').val("");
            }
        });
        
        //add the write comment button and hide form depending on if errors are showing after postback
        if($('.errorlist').length > 0 ) {
            
        } else {
        }
        
        // show and hide comment block
        
        function toggleComment() {
            $('#writeComment').toggle(function() {
                 $('#addComment').slideDown('fast')
             }, function() {    
                 $('#addComment').slideUp('fast')

             });
        }
        
        toggleComment();
 
    }
    
// fancy box

    if($('.fancyWrap').length > 0 ) {
        $('.fancyWrap a').fancybox({
			'zoomSpeedIn': 300,
			'zoomSpeedOut': 300,
			'overlayShow': true,
			'overlayOpacity': 0
		});
    }

});