﻿//*************************************************************************************
// File     : mind_function.js
// Requires : jquery.js (version 1.3.1+), braingnat.js (version 0.1.5+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : March 05, 2009
// Modified : June 22, 2009
//*************************************************************************************

BrainGnat.html5Shiv();
var currentSlide = 0;

$(document).ready(function() {
    portfolioSlider();
    setPrimaryHeight();
});

// Activates the portfolio slider.
function portfolioSlider() {
    var tallest = 0;
    for (i = 0; i < $('.porfolio-list li').length; i++) {
        if ($('.porfolio-list li:eq(' + i + ')').height() > tallest) {
            tallest = $('.porfolio-list li:eq(' + i + ')').height();
        }
    }
    tallest = (tallest * 1) + 20;
    $('.portfolio').css('height', tallest + 'px');
    $('.porfolio-list').css('width', (655 * $('.porfolio-list li').length) + 'px');

    //On page load, slide to a specific slide if it's identified in the URL.
    var temp = window.location.href;
    var urlBits = temp.split("#");
    if (urlBits.length > 1) {
        var index = $('.porfolio-list h4 a').index($('a#' + urlBits[1]));
        currentSlide = index;
        $('.porfolio-list').css('margin-left', -(currentSlide * 655) + 'px');
    }
    checkSliderNav();
    
    // Bind the next button to scroll to the right.
    $('.portButton.next').bind('click', function() {
    if (currentSlide < ($('.porfolio-list li').length - 1)) {
            currentSlide = currentSlide + 1;
            $('.porfolio-list').animate({ marginLeft: -(currentSlide * 655) }, 1000);
        }
        checkSliderNav();
        return false;
    });
    
    // Bind the previous button to scroll to the left.
    $('.portButton.previous').bind('click', function() {
        if (currentSlide > 0) {
            currentSlide = currentSlide - 1;
            $('.porfolio-list').animate({ marginLeft: -(currentSlide * 655) }, 1000);
        }
        checkSliderNav();
        return false;
    });
    
    // Bind the side navigation buttons to scroll to the selected item.
    $('#portfolio a').bind('click', function() {
        var index = $('#portfolio a').index(this);
        currentSlide = index;
        $('.porfolio-list').animate({ marginLeft: -(currentSlide * 655) }, 1000);
        checkSliderNav();
        return false;
    });
}

// Makes the portfolio's previous and next buttons disappear if the user is at the start or end.
function checkSliderNav() {
    $('.portButton').css('visibility', 'visible');
    if (currentSlide == 0) {
        $('.portButton.previous').css('visibility', 'hidden');
    } else if (currentSlide == ($('.portfolio > ul li').length - 1)) {
        $('.portButton.next').css('visibility', 'hidden');
    }
}

function setPrimaryHeight() {
    if ($('#home').length < 1) {
        var height = $('#content').height();
        height = height - ($('#content > h2').height() + $('#content > h3').height());
        if (($('#content .primary').height() + 10) < height) {
            $('#content .primary').css('height', (height - 10) + 'px');
        }
    }
}

function mapUs() {
    $('.address a.map').bind('click', function() {
        $('#content').append('<div id="gMap"></div>');
        return false;
    });
}