$(document).ready(function () {
    content_resize();
    $(window).resize(content_resize);
    $(document).resize(content_resize);
    $('#slideshow').cycle({
        fx: 'scrollLeft',
        speed: 1000,
        timeout: 8000,
        easing: 'easeOutQuad'
    });
    $("#sitemenu li").hover(function () {
        $(this).addClass('hover')
    }, function () {
        $(this).removeClass('hover')
    });
    if (document.getElementById('news-page')) {
        var newsItems;
        var rowCnt;
        var colCnt = 4;
        var gridH = $(".newscol").height();
        $(".newscol").hide();
        newsItems = new Array();
        $(".newscol").each(function () {
            newsItems.push($(this))
        });
        rowCnt = Math.ceil((newsItems.length) / colCnt);
        $('#pg-content').css({
            'height': ((gridH * rowCnt) + (rowCnt * 30)) + 'px'
        });
        doAnimation(newsItems.shift())
    }
    function doAnimation(item) {
        item.fadeIn("fast", function () {
            if (newsItems.length > 0) doAnimation(newsItems.shift())
        })
    }
    if (document.getElementById('content-grid')) {
        var fitems;
        var rCnt;
        var cCnt = 4;
        var cH;
        var gH = $(".grid").height() + 40;
        var gM = 16;
        var nH = 65;
        $(".grid").hide();
        fitems = new Array();
        $(".grid").each(function () {
            fitems.push($(this))
        });
        rCnt = Math.ceil((fitems.length) / cCnt);
        cH = ((gH * rCnt) + (rCnt * gM)) + nH;
        $('#content-grid').css({
            'height': cH + 'px'
        });
        gridAnimation(fitems.shift())
    }
    function gridAnimation(item) {
        item.fadeIn("fast", function () {
            if (fitems.length > 0) gridAnimation(fitems.shift())
        })
    }
    if (document.getElementById('project-page')) {
        $(".grid").hover(function () {
            $(this).css({
                'cursor': 'pointer'
            });
            $(this).find('img').fadeTo(150, 0.5);
            $(this).find('h2').stop().animate({
                'color': 'rgb(61,55,45)'
            }, 300)
        }, function () {
            $(this).find('img').fadeTo(150, 1.0);
            $(this).find('h2').stop().animate({
                'color': 'rgb(233,66,53)'
            }, 300)
        });
        $(".grid").click(function () {
            var link = $(this).find('a').attr('href');
            window.location.href = link
        })
    }
    if (document.getElementById('pg-content')) {
        $(".newscol").hover(function () {
            $(this).css({
                'cursor': 'pointer'
            });
            $(this).find('img').fadeTo(150, 0.5);
            $(this).find('h2, h2 a').stop().animate({
                'color': 'rgb(61,55,45)'
            }, 300)
        }, function () {
            $(this).find('img').fadeTo(150, 1.0);
            $(this).find('h2, h2 a').stop().animate({
                'color': 'rgb(233,66,53)'
            }, 300)
        })
    }
    if (document.getElementById('blog-sidebar')) {
        $(".togglebox ul").hide();
        $(".togglebox h4").hover(function () {
            $(this).css({
                'cursor': 'pointer'
            })
        }, function () {});
        $(".togglebox h4").click(function () {
            $(".togglebox ul").toggle()
        })
    }
    if (document.getElementById('comments')) {
        $("#commentform").validate()
    }
    function content_resize() {
        var w = $(window);
        var H = w.height();
        var W = w.width();
        var hdrHeight = $('#header').height();
        var pgHeight = $('#content').height();
        var ftrHeight = $('#footer').height();
        var fPos;
        if (document.getElementById('content-grid')) {
            pgHeight = $('#content-grid').height() - 40
        }
        fPos = (H - ((hdrHeight + 40) + pgHeight) - ftrHeight);
        if (document.getElementById('project-page')) {
            if (fPos > 0 && fPos < H) {
                $('#footer').css({
                    'margin-top': fPos
                })
            }
        }
        if (document.getElementById('project-gallery')) {
            fPos = fPos - 380
        }
        if (fPos > 0) {
            if (typeof document.body.style.maxHeight !== "undefined") {
                $('#footer').css({
                    'margin-top': fPos
                })
            }
        }
    }
});
