/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                             *
 *                                                                             *
 *                            Morris Engineering                               *
 *                         written by Will Gaggioli                            *
 *                       (c) 2010 All Rights Reserved                          *
 *                                                                             *
 *                                                                             *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/****** GLOBALS *******/

var fchIntervalId = 0;
var contHeight = 0;
var slideOn  = true;

/**
* Make sure content doesn't fly out of bounds
*/
function fixContentHeight(){
	var mainHeight = $('#mainContent').height();
	if (mainHeight > contHeight){
		contHeight = mainHeight + 30;
        $('#content').css('height', contHeight);
	}
    return false;
}

/**
* scroll through slideshow images
*/
function nextImage(img){
    var currContainer = $(img).parent();
    var nextContainer = currContainer.next('.imageItem');
    if (nextContainer.length == 0){
	    nextContainer = currContainer.siblings('.imageItem').first();
    }
    _imageSwitch( currContainer, nextContainer );
}

/**
* scroll through slideshow images
*/
function prevImage(img){
    var currContainer = $(img).parent();
    var nextContainer = currContainer.prev('.imageItem');
    if (nextContainer.length == 0){
	    nextContainer = currContainer.siblings('.imageItem').last();
    }
    _imageSwitch( currContainer, nextContainer );
}

/**
* image switch
*/
function _imageSwitch( current, next ){
	current.addClass('hidden');
	next.removeClass('hidden').addClass('current');
	current.removeClass('current');
}

/**
* slide
*/
function slide(){
    if (slideOn){
	    activeImage = $('.current > .slideshow');
	    nextImage(activeImage);
	    setTimeout('slide()', 3000);
    }
}

/**
* init slideshow
*/
function initSlideshow(){
   if ($('.imageItem').length > 1){
	   setTimeout('slide()', 3000);
   }
}

/******** ON PAGE READY *********/
$(document).ready(function(){

    // page properties
    var imgCache = [];
    var loadImage = '/img/ajax-loader.gif'

    /**
     * Preload Image
     */
    function preloadImage( src ) {
        var image = document.createElement('img');
        image.src = src;
        imgCache.push( image );
    }

    /**
     * Pause for t milliseconds
     */
    function pause(t) {
    	var dt = new Date();
    	while ((new Date()) - dt <= t) { /* Do nothing */ }
    }

    /**
     * Add the dropdown subnavigation menu
     */
    function addDropdown( categories, dropdownImage ){
		var imgId  = dropdownImage.replace('.png','');
		var active = ''

		// Add content
		var dropdownXhtml = '<img id="' + imgId + '" class="dropdown" src="/img/' + dropdownImage + '" alt="catMenu" style="display:none" />' +
                            '<div id="dropdownContainer" style="display:none"><div id="dropdownItems">';
        for (i in categories){
			if (categories[i]['active'] == true || categories[i]['active'] == 'true'){
				active = ' active';
			}
            dropdownXhtml += '<a class="dropdownItem' + active + '" href="/' + categories[i]['controller'] + '/' + categories[i]['uri'] + '/">' + categories[i]['name'] + '</a>';
			active = '';
        }
        dropdownXhtml += '</div></div>';
        $('#sideNav').append( dropdownXhtml );

		// Now animate!
		$('#' + imgId).slideDown('slow', function(){
			$('#dropdownContainer').show();
		});
    }

    /**
     * remove the dropdown subnavigation menu
     */
    function removeDropdown(){
        $('.dropdown').remove();
        $('#dropdownContainer').remove();
    }

    /**
     * Add the numbered navbar for the project page
     */
    function addProjectTopNav( navbar ){
        var xhtml = '<div id="projectNavBar"> Jobs:';
        for (i in navbar){
            xhtml += '<a class="projectNavLink" href="/me/projects/index/projectId/' + navbar[i]['id'] + '"> ' + navbar[i]['num'] + ' </a>';
        }
        xhtml += '</div>';
        $('#mainContent').prepend( xhtml );
    }

    /**
     * remove the numbered navbar for the project page
     */
    function removeTopNav(){
        $('#projectNavBar').remove();
    }

    /**
     * click event for left navbar ajax requests
     */
    function navClick(){
        var navId = $(this).attr('id');
		$('.deleteOnNavClick').remove();
        if (navId ==="projects"){$('.deleteOnProjectClick').remove();removeTopNav();}
        $(this).after('<img id="loader" alt="loading..." src="' + loadImage +'" />');
        $.post('/me/' + navId + '/getcategories/', function(response){
            removeDropdown();
            addDropdown( response.categories, navId + 'drop.png' );
            $('#loader').remove();
            if ('topNav' in response){
                addProjectTopNav( response.topNav );
            }
        }, 'json');
        return false;
    }

    /**
     * placeholder for lighthouse effect, if desired
     */
    function explodeImage(){

        return false;
    }

    /**
     * initialize bindings and tasks
     */
    function init(){
		// variables
		contHeight = $('#content').height();

        // bindings
        $('.subcats').click( navClick );
        $('.slide-right').live( 'click', function(){
			slideOn = false;
            var img = $(this).parent().siblings('.current').children('.slideshow');
            nextImage(img);
            return false;
        });
        $('.slide-left').live( 'click', function(){
			slideOn = false;
            var img = $(this).parent().siblings('.current').children('.slideshow');
            prevImage(img);
            return false;
        });
        $('.slideshow').click( function(){
            nextImage($(this));
            return false;
        });

		fixContentHeight();
		fchIntervalId = setInterval('fixContentHeight()', 2000);

        preloadImage(loadImage);
        preloadImage('/img/projectsdrop.png');
        preloadImage('/img/profiledrop.png')
    }

    init();

});

/****** ON PAGE LOAD *******/
$(window).load( function() {
	initSlideshow();
	fixContentHeight();
	clearInterval(fchIntervalId);

});

