$(function () {
    
    // Highlight CTA Icon when CTA is Hovered on
    $('div.HomeCTARow a').bind('mouseover', function () {
        // Get the Icon
        var iconObj = $(this).children('img.HomeCTAIcon');
        
        // Get the current Src for this CTA Image
        var imageSrc = iconObj.attr('src').replace('.png', '_hover.png');
        
        // Now, apply this to our image
        iconObj.attr('src', imageSrc);
    });

    // Return the CTA Icon to it's original state when mouseout occurs
    $('div.HomeCTARow a').bind('mouseout', function () {
        // Get the Icon
        var iconObj = $(this).children('img.HomeCTAIcon');

        // Get the current Src for this CTA Image
        var imageSrc = iconObj.attr('src').replace('_hover.png', '.png');

        // Now, apply this to our image
        iconObj.attr('src', imageSrc);
    });

    // sets up tabs. Tab code in utils.js
    $('#casestudy').tabs();
    var bodyClasses = $('html').hasClass('msie');
    $cs = $('.study');
    $csl = $cs.length - 1;
    $cs.eq(0).addClass('activeStudy');

    $tm = $('.testimonial');
    $tml = $tm.length - 1;
    $tm.eq(0).addClass('activeTestimonial');

    var testimonialTabClicks = 0;
    var reflectHeight = 0.375;
    fadeSpeed = 500
    $('a#nextStudy').click(function () {
        studyForward();
        killTimers();
        return false;
    });

    $('a#prevStudy').click(function () {
        var indexStudy = $('.activeStudy').prevAll('.study').length;
        if (indexStudy > 0) {
            $('.activeStudy').prev('div').fadeIn(fadeSpeed).addClass('activeStudy').next('div').css('display', 'none').removeClass('activeStudy');
        } else if (indexStudy == 0) {
            $cs.removeClass('activeStudy').css('display', 'none');
            $cs.eq($csl).fadeIn(fadeSpeed).addClass('activeStudy');
        }
        if (bodyClasses == true) {
            $('div.activeStudy .flip').reflect({ height: reflectHeight });
        }
        killTimers();
        return false;
    });


    function studyForward() {
        var indexStudy = $('.activeStudy').prevAll('.study').length;
        if (indexStudy < $csl) {
            $('.activeStudy').next('div').fadeIn(fadeSpeed).addClass('activeStudy').prev('div').css('display', 'none').removeClass('activeStudy');
        } else if (indexStudy == $csl) {
            $cs.removeClass('activeStudy').css('display', 'none');
            $cs.eq(0).fadeIn(fadeSpeed).addClass('activeStudy');
        }
        if (bodyClasses == true) {
            $('div.activeStudy .flip').reflect({ height: reflectHeight });
        }
    }


    $('a#nextTestimonials').click(function () {
        testimonialForward();
        killTimers();
        return false;
    });

    $('a#prevTestimonials').click(function () {
        var indexTestimonial = $('.activeTestimonial').prevAll('.testimonial').length;
        if (indexTestimonial > 0) {
            $('.activeTestimonial').prev('div').fadeIn(fadeSpeed).addClass('activeTestimonial').next('div').css('display', 'none').removeClass('activeTestimonial');
        } else if (indexTestimonial == 0) {
            $tm.removeClass('activeTestimonial').css('display', 'none');
            $tm.eq($tml).fadeIn(fadeSpeed).addClass('activeTestimonial');
        }
        if (bodyClasses == true) {
            $('div.activeTestimonial .flip').reflect({ height: reflectHeight });
        }
        killTimers();
        return false;
    });

    function testimonialForward() {
        var indexTestimonial = $('.activeTestimonial').prevAll('.testimonial').length;
        if (indexTestimonial < $tml) {
            $('.activeTestimonial').next('div').fadeIn(fadeSpeed).addClass('activeTestimonial').prev('div').css('display', 'none').removeClass('activeTestimonial');
        } else if (indexTestimonial == $tml) {
            $tm.removeClass('activeTestimonial').css('display', 'none');
            $tm.eq(0).fadeIn(fadeSpeed).addClass('activeTestimonial');
        }
        if (bodyClasses == true) {
            $('div.activeTestimonial .flip').reflect({ height: reflectHeight });
        }
    }


    if (bodyClasses == false) {
        $('.flip').reflect({ height: reflectHeight });
    } else {
        $('div.activeStudy .flip').reflect({ height: reflectHeight });
    }
    $('#testimonialsTab a').click(function () {
        $('div.activeTestimonial .flip').reflect({ height: reflectHeight });
        if (testimonialTabClicks == 0) {
            timedTestimonials();
            testimonialTabClicks += 1;
        }
        $.clearTimer(studyTimer);
    });

    $('#casestudiesTab a').click(function () { killTimers(); });

    var testimonialTimer = {};
    var studyTimer = {};

    function timedStudy() {
        studyTimer = $.timer(6000, function () {
            studyForward();
            $.clearTimer(studyTimer);
            timedStudy();
        });
    }
    timedStudy();

    function timedTestimonials() {
        testimonialTimer = $.timer(6000, function () {
            testimonialForward();
            $.clearTimer(testimonialTimer);
            timedTestimonials();
        });
    }

    function killTimers() {
        $.clearTimer(studyTimer);
        $.clearTimer(testimonialTimer);
    }



    //News

    var newsTimer = {};
    $news = $('#news li');
    $news.eq(0).addClass('active');
    $newsl = $news.length - 1;
    function timedNews() {
        newsTimer = $.timer(6000, function () {
            var indexNews = $('#news li.active').prevAll().length;
            if (indexNews == $newsl) {
                $news.removeClass('active').css('display', 'none');
                $news.eq(0).fadeIn('slow').addClass('active');
            } else {
                $news.eq(indexNews).removeClass('active').css('display', 'none').next('li').fadeIn('slow').addClass('active');
            }
            $.clearTimer(newsTimer);
            timedNews();

        });

    }

    timedNews();

    $('#bottomPortlets > div').click(function () {
        var url = $(this).children('a').attr('href');
        location.href = url;
        return false;
    });




    var $tools = $('#tools');
    var $sl = $('#showLooking');
    var $sc = $('#showClient');
    var $looking = $('#lookingForCMS');
    var $client = $('#client');


    //Tools
    uta = $('#userType a', $tools);
    uta.click(function () {
        uta.removeClass('current');
        $(this).addClass('current');
        if ($(this).hasClass('looking')) {
            $(this).parent().siblings('ul').removeClass('current');
            $looking.addClass('current');
            $sc.removeClass('current');
            $sl.addClass('current');
        } else if ($(this).hasClass('client')) {
            $(this).parent().siblings('ul').removeClass('current');
            $client.addClass('current');
            $sl.removeClass('current');
            $sc.addClass('current');
        }

        return false;
    });

    $('ul.buttons li a', $tools).click(function () {
        var which = $(this).parent().prevAll().length;
        var whichId = $(this).parent().parent().attr('id');
        $(this).addClass('current').parent().siblings('li').children('a').removeClass('current');
        if (whichId == 'lookingForCMS') {
            $sl.children('div').removeClass('current').parent().children('div:eq(' + which + ')').addClass('current');
        } else if (whichId == 'client') {
            $sc.children('div').removeClass('current').parent().children('div:eq(' + which + ')').addClass('current');
        }
        return false;
    });

    $('div.col2 ul li a', $tools).click(function () {
        var whichCol3 = $(this).parent().prevAll().length;
        $(this).addClass('current').parent().siblings().children('a').removeClass('current');
        $(this).closest('div.col2').next('div.col3').children('div:eq(' + whichCol3 + ')').addClass('current').siblings().removeClass('current');
        return false;
    });



});  



