
var parseURL = function(URL) {
    if ( URL == undefined || URL == '') {
        return '';
    }
    URL = URL.split('/');
    newURL = parseInt(URL[URL.length-1]);
    if ( isNaN(newURL) ) {
        newURL = parseInt(URL[URL.length-2]);
        if ( isNaN(newURL ) ) return '';
        else return newURL;
    } 
    return newURL;
}

var getNumbers = function(selectString) {
    var $links = $(selectString);
    var idNumbers = [];
    for ( i = 0; i < $links.length; i++ ) {
        str = new String($links[i]);
        idNumbers[i] = parseURL(str);
    }
    var ids = [];
    if ( idNumbers[0] == '' && idNumbers[1] == '' ) {
        return;
    } else if ( idNumbers[0] == '' ) {
        ids['prevId'] = 0;
        ids['nextId'] = idNumbers[1];
        ids['currentId'] = ids['nextId'] -1;
    } else if ( idNumbers[1] == '') {
        ids['nextId'] = 0;
        ids['prevId'] = idNumbers[0];
        ids['currentId'] = ids['prevId'] + 1;
    } else {
        ids['prevId'] = idNumbers[0];
        ids['currentId'] = ids['prevId'] +1;
        ids['nextId'] = idNumbers[1];
    }
    return ids;
}

var init = function(id, activeId) {
    var featuredId = 'featured';
    var slideId = 'slide';

    $('#'+id).find('#' + featuredId).wrap('<div id="'+ slideId +'"></div>');

    var navSelector = '#' + featuredId + ' .news-nav a';
    var idNumbers = getNumbers(navSelector);
    var currentId = idNumbers['currentId'];

    var newId = featuredId + '-' + currentId;
    $('#' + featuredId).attr('id', newId);
    $('#' + slideId + ' > div').hide();
    $('#featured-' + currentId).show();
    update(id, featuredId, slideId, currentId);
}

var update = function(id, featuredId, slideId, currId) {
    var navSelector = '#' + featuredId + '-' + currId + ' .news-nav a';
    var idNumbers = getNumbers(navSelector);

    $(navSelector).not('.bound').not('.disabled').bind('click', function (event) {
        var wrapperHeight = 390;
        if ( $(this).is('.nav-newer') && idNumbers != undefined ) {
            event.preventDefault();
            if ( $('#' + featuredId + '-' + idNumbers['prevId'] ).length <= 0 ) {
                $.get('/n/' + idNumbers['prevId'], function(data) {
                    $('#' + slideId).prepend(data);
                    $('#' + featuredId).attr('id', featuredId + '-' + idNumbers['prevId']).hide();
                    $('#' + featuredId + '-' + idNumbers['prevId'])
                        .css({'position': 'absolute', 'z-index': '10', 'height': wrapperHeight})
                        .fadeIn('fast', function() {
                            $('#' + featuredId + '-' + idNumbers['currentId']).hide();
                            $(this).css({'position': 'static', 'z-index': '0' });
                            update(id, 'featured', 'slide', idNumbers['prevId']);
                        });
                });
            } else {
                $('#' + featuredId + '-' + idNumbers['prevId'])
                    .css({'position': 'absolute', 'z-index': '10', 'height': wrapperHeight})
                    .fadeIn('fast', function() {
                        $('#' + featuredId + '-' + idNumbers['currentId']).hide();
                        $(this).css({'position': 'static', 'z-index': '0' });
                    });
            }
        } else if ( idNumbers != undefined ) {
            event.preventDefault();
            if ( $('#' + featuredId + '-' + idNumbers['nextId'] ).length <= 0 ) {
                $.get('/n/' + idNumbers['nextId'], function(data) {
                    $('#' + slideId).append(data);
                    $('#' + featuredId).attr('id', featuredId + '-' + idNumbers['nextId']).hide();
                    $('#' + featuredId + '-' + idNumbers['nextId'])
                        .css({'position': 'absolute', 'z-index': '10', 'height': wrapperHeight})
                        .fadeIn('fast', function() {
                            $('#' + featuredId + '-' + idNumbers['currentId']).hide();
                            $(this).css({'position': 'static', 'z-index': '0' });
                            update(id, 'featured', 'slide', idNumbers['nextId']);
                        });
                });
            } else {
                $('#' + featuredId + '-' + idNumbers['nextId'])
                    .css({'position': 'absolute', 'z-index': '10', 'height': wrapperHeight})
                    .fadeIn('fast', function() {
                        $('#' + featuredId + '-' + idNumbers['currentId']).hide();
                        $(this).css({'position': 'static', 'z-index': '0' });
                    });
            }
        }
        $(this).addClass('bound');
    });
}

$(document).ready(function(){

    var height = $('#menu ul').height();
    $('#menu').css('height', height + 'px');

    var maxWidth = 300;
    var maxImageWidth = 0;
//    $('#side-image-wrapper img').each(function() {
//        var imageWidth = $(this).width();
//        if ( imageWidth > maxImageWidth ) {
//            maxImageWidth = imageWidth;
//        }
//        if ( imageWidth > maxWidth ) {
//            $(this).css('width', maxWidth);
//        }
//    });
//
//    if ( maxImageWidth > maxWidth ) {
//        var width = maxWidth;
//    } else {
//        var width = maxImageWidth;
//    }
//    if ( width ) {
//        $('#text').css('marginLeft', width + 20 + 'px');
//        $('#locations').css('marginLeft', width + 20 + 'px');
//        $('#listing').css('marginLeft', width + 'px');
//    }

    init('featured-wrapper');

});


