$(document).ready(function() {
		
		// -- Start jquery twitter plugin ----------------------------------------------------------
		$('#tweets').jTweetsAnywhere({
		    username: 'casper60',
		    count: 3
		});
				
    // -- Rollover effects on contact send button ----------------------------------------------
    $("#contact_btn").mouseover(function() {
        $(this).attr("src", "img/send_btn_h.png");
    }).mouseout(function() {
        $(this).attr("src", "img/send_btn.png");
    });

    // -- Starts the ShadowBox() ---------------------------------------------------------------
    Shadowbox.init();

    // -- Rollover effects in portfolio show all -----------------------------------------------
    $(".show_all_btn").mouseover(function() {
        $(this).removeClass().addClass('show_all_btn_h');
    }).mouseout(function() {
        $(this).removeClass().addClass('show_all_btn');
    });

    // -- Rollover effects in portfolio --------------------------------------------------------
    $(".view").mouseover(function() {
        $(this).removeClass().addClass('view_h');
    }).mouseout(function() {
        $(this).removeClass().addClass('view');
    });

    // -- Start the validationEngine -----------------------------------------------------------
    $("#contact_form").validationEngine();

    // -- Clear search results -----------------------------------------------------------------
    $("#clear").click(function() {
        $.ajax({
            type: "POST",
            url: "inc/php/req.inc.php",
            data: "clear=true",
            beforeSend: function(html) {
                $("#search_query").hide();
            },
            success: function(html) {
                $("#search_results").empty();
                $(".word").html('');
                $(".count_wrap").html('');
                $("#clear").hide();
            }
        });
        $("#search_form").val('');
    });

    // -- Session AJAX search function ---------------------------------------------------------
    $(function() {

        var session = $('#sess_search').attr("value");

        if (session) {
            var searchString = $("#sess_search").val();
            var data = 'search=' + searchString;

            if (searchString) {
                $.ajax({
                    type: "POST",
                    url: "inc/php/req.inc.php",
                    data: data,
                    beforeSend: function(html) {
                        $(".word").html('<span class="dark">Search for</span> <span class="result">' + searchString + '</span>');
                        $("#search_results").html('<img src="img/shadowbox/loading.gif" alt="" class="loader_gif" />');
                    },
                    success: function(html) {
                        $("#search_query").show();
                        $("#search_results").html('');
                        $("#search_results").append(html);

                        var count = $('#search_count').val();

                        if (count <= 1) {
                            var result = 'result';
                            var link = '';
                        } else {
                            var result = 'results';
                            if (count > 4) {
                                var link = '&nbsp;&nbsp;&nbsp;<a href="?p=portfolio&amp;search-list">see all</a>';
                            } else {
                                var link = '';
                            }
                        }

                        if (!count) {
                            var count = 0;
                            var result = 'results';
                        }

                        $(".count_wrap").empty();

                        if (count > 4) {
                            $(".count_wrap").html('<span class="dark">displaying 4 of ' + count + ' ' + result + '</span> ' + link);
                        } else {
                            $(".count_wrap").html('<span class="dark">displaying ' + count + ' ' + result + '</span> ' + link);
                        }

                        $("#clear").show();
                    }
                });

            }
            return false;
        }

    });

    // -- Live AJAX search function ------------------------------------------------------------
    $(function() {

        $("#search_form").keyup(function() {

            $("#search_query").show();

            var searchString = $("#search_form").val();
            var data = 'search=' + searchString;

            if (searchString) {
                $.ajax({
                    type: "POST",
                    url: "inc/php/req.inc.php",
                    data: data,
                    beforeSend: function(html) {
                        $(".word").html('<span class="dark">Search for</span> <span class="result">' + searchString + '</span>');
                        $("#search_results").html('<img src="img/shadowbox/loading.gif" alt="" class="loader_gif" />');
                    },
                    success: function(html) {
                        $("#search_query").show();
                        $("#search_results").html('');
                        $("#search_results").append(html);

                        var count = $('#search_count').val();

                        if (count <= 1) {
                            var result = 'result';
                            var link = '';
                        } else {
                            var result = 'results';
                            if (count > 4) {
                                var link = '&nbsp;&nbsp;&nbsp;<a href="?p=portfolio&amp;search-list" class="all">see all</a>';
                            } else {
                                var link = '';
                            }
                        }

                        if (!count) {
                            var count = 0;
                            var result = 'results';
                        }

                        $(".count_wrap").empty();

                        if (count > 4) {
                            $(".count_wrap").html('<span class="dark">displaying 4 of ' + count + ' ' + result + '</span> ' + link);
                        } else {
                            $(".count_wrap").html('<span class="dark">displaying ' + count + ' ' + result + '</span> ' + link);
                        }
                    }
                });
                $("#clear").show();
            }
            return false;
        });
    });

    // -- Scroll to top animation --------------------------------------------------------------
    $("#toTop").scrollToTop();

    // -- Featured projects cycle --------------------------------------------------------------
    $('#feat_img').cycle({
        fx: 'fade',
        speed: 800,
        timeout: 6000,
        next: '#feat_overlay'
    });

    // -- Rollover effects on main menu --------------------------------------------------------
    $("#home").mouseover(function() {
        $(this).attr("src", "img/nav/home_btn_h.png");
    }).mouseout(function() {
        $(this).attr("src", "img/nav/home_btn.png");
    });

    $("#portfolio").mouseover(function() {
        $(this).attr("src", "img/nav/portfolio_btn_h.png");
    }).mouseout(function() {
        $(this).attr("src", "img/nav/portfolio_btn.png");
    });

    $("#blog").mouseover(function() {
        $("#blog").attr("src", "img/nav/blog_btn_h.png");
    }).mouseout(function() {
        $("#blog").attr("src", "img/nav/blog_btn.png");
    });

    $("#search").mouseover(function() {
        $("#search").attr("src", "img/nav/search_btn_h.png");
    }).mouseout(function() {
        if (!search_clicked) {
            $("#search").attr("src", "img/nav/search_btn.png");
        } else {
            $("#search").attr("src", "img/nav/search_btn_h.png");
        }
    });

    $("#search").click(function() {
        if (!search_clicked) {
            $("#search_wrapper").stop().animate({
                height: '142px'
            }, {
                queue: false,
                duration: 1200,
                easing: 'easeOutBounce'
            })
            search_clicked = true;
        } else {
            $("#search_wrapper").stop().animate({
                height: '1px'
            }, {
                queue: false,
                duration: 1200,
                easing: 'easeOutBounce'
            })
            search_clicked = false;
            $("#search_form").val('');
        }
    });


    $("#search_act").mouseover(function() {
        $("#search_act").attr("src", "img/nav/search_btn_h.png");
    }).mouseout(function() {
        $("#search_act").attr("src", "img/nav/search_btn_a.png");
    });

    $("#search_act").click(function() {
        if (!search_clicked) {
            $("#search_wrapper").stop().animate({
                height: '142px'
            }, {
                queue: false,
                duration: 1200,
                easing: 'easeOutBounce'
            })
            search_clicked = true;
        } else {
            $("#search_wrapper").stop().animate({
                height: '1px'
            }, {
                queue: false,
                duration: 1200,
                easing: 'easeOutBounce'
            })
            search_clicked = false;
            $("#search_form").val('');
        }
    });
    
    if( $(".search_cat").length > 0 ) {
    		$("#search").attr("src", "img/nav/search_btn_a.png");
    }

    $("#contact").mouseover(function() {
        $("#contact").attr("src", "img/nav/contact_btn_h.png");
    }).mouseout(function() {
        $("#contact").attr("src", "img/nav/contact_btn.png");
    });

});

