$(document).ready(function() {

    if ($('#quicksearch_sector').length > 0) {

        $.getJSON('combobox.php?_list=Sector', function(data) {
            var html = '';
            var len = data.length;
            for (var i = 0; i < len; i++) {
                html += '<option title="' + data[i][1] + '" value="' + data[i][0] + '">' + data[i][1] + '</option>';
            }
            $('#quicksearch_sector').empty();
            $('#quicksearch_sector').append(html);
            $('#quicksearch_sector').click(function() {
                $.cookies.set('#quicksearch_sector', $('#quicksearch_sector').val());
            });
            if ($.cookies.get('#quicksearch_sector')) {
                $('#quicksearch_sector').val($.cookies.get('#quicksearch_sector'));
            }

        });
    }

    if ($('#quicksearch_region').length > 0) {

        $.getJSON('combobox.php?_list=Region', function(data) {
            var html = '';
            var len = data.length;
            for (var i = 0; i < len; i++) {
                html += '<option title="' + data[i][1] + '" value="' + data[i][0] + '">' + data[i][1] + '</option>';
            }
            $('#quicksearch_region').empty();
            $('#quicksearch_region').append(html);
            $('#quicksearch_region').click(function() {
                $.cookies.set('#quicksearch_region', $('#quicksearch_region').val());
            });
            if ($.cookies.get('#quicksearch_region')) {
                $('#quicksearch_region').val($.cookies.get('#quicksearch_region'));
            }

        });
    }

    var fakeFileUpload = $('<div class="fakefile"><input type="text" value="" /><input type="button" value="Choose" /></div>');

    $('div.fileinput input[type="file"]').each(function() {
        $(this).addClass('hidden');
        var clone = fakeFileUpload.clone();
        $(this).parent().append(clone);
        this.relatedElement = $(clone).children('input[type="text"]')[0];
        $(this).change(function() {
            this.relatedElement.value = this.value;
        });
        $(this).mouseout(function() {
            this.relatedElement.value = this.value;
        });
    });

    //$('form .fakefile input[type="text"]').attr('value', 'Upload CV...');
    $('form .fakefile input[type="text"]').attr('value', 'Upload Resume...');
    //$('.sidebarRight .fakefile input[type="text"]').attr('value', 'Upload CV...');

    $('.feature').each(function() {
        var img = $(this).children('img').outerHeight();
        var box = $(this).outerHeight();

        if (img == box) { $(this).children('img').css('padding-bottom', 0); }
    });

    $('#banners').cycle(
	{
	    height: '222px',
	    pager: '#bannerNav',
	    activePagerClass: 'selected'
	});

    $('#tabs').tabs();
    $('#tabs ul').show();

    $('#portal div').height(($('#portal').innerHeight() - ($('#portal div:first').outerHeight() - $('#portal div:first').height())));

    // line tab background up with dots
    var spacing = 3;
    $('.tabs-nav li').each(function() {
        var extra = spacing - $(this).width() % spacing;
        $(this).width($(this).width() + extra);
    });

    // line side nav background up with dots
    $('#sectionNav a').each(function() {
        var extra = spacing - $(this).height() % spacing;
        $(this).height($(this).height() + extra);
    });

    // level columns
    var colHeight = false;
    $('#main.sectionHome .content, #main.sectionHome .sidebarRight').each(function() {
        if ($(this).outerHeight(true) > colHeight) {
            colHeight = $(this).outerHeight(true);
        }
    });

    $('#main.sectionHome .content, #main.sectionHome .sidebarRight').each(function() {
        if ($(this).outerHeight(true) < colHeight) {
            $(this).css('border-bottom', colHeight - $(this).outerHeight(true) + 'px solid #fff');
        }
    });

    // style first content para
    $('.content > p:first').addClass('highlight');
    $('#contentHeader p a:last').addClass('last');

    $('.job .details li').addClass('otherCol');
    $('.job .details li:nth-child(3n+1)').removeClass('otherCol').addClass('firstCol');

    // kick off scroller
    $('#imageRotator').cycle(
	{
	    fx: 'scrollHorz',
	    speed: 'fast',
	    timeout: 0,
	    next: '#imageNav .next',
	    prev: '#imageNav .prev',
	    before: onBefore,
	    after: onAfter
	});

    function onBefore() { $('#imageNav .title').attr('innerHTML', '&nbsp;'); }
    function onAfter() { $('#imageNav .title').attr('innerHTML', $(this).attr('title')); }
});

// wait for images to load
$(window).load(function() {
    // level bottoms...
    var count = 0;
    var dims = new Array();
    dims[count] = new Array();
    var side = 'lft';

    $('.sideBySide.level div div:last-child').each(function() {
        dims[count][side] = new Array();
        dims[count][side]['div'] = this;
        dims[count][side]['offset'] = $(this).offset();
        dims[count][side]['height'] = $(this).outerHeight();
        dims[count][side]['bottom'] = dims[count][side]['offset'].top + dims[count][side]['height'];

        if (side == 'lft') {
            side = 'rgt';
        }
        else {
            side = 'lft';
            count++;
            dims[count] = new Array();
        }
    });

    $(dims).each(function() {
        if (!this['lft'] || !this['rgt']) { return; }

        if (this['lft']['bottom'] > this['rgt']['bottom']) {
            $(this['rgt']['div']).height($(this['rgt']['div']).height() + (this['lft']['bottom'] - this['rgt']['bottom']));
        }
        else {
            $(this['lft']['div']).height($(this['lft']['div']).height() + (this['rgt']['bottom'] - this['lft']['bottom']));
        }
    });
});
