var domready = false;
var resetHeight = function() {};
var mapShouldDisplay = false;
var mapCompatible = true;
var driveTimeOn = false;

window.addEvent('domready', function() {
    domready = true;
    mapCompatible = GBrowserIsCompatible();
    
    // height stuff
    resetHeight = function() {
        //determine scroller height (main column)
        var scrollSize = window.getSize();
        var scrollHeight = scrollSize.y - 8 - 73 - 95 - 60;
        if(scrollHeight < 300) scrollHeight = 300;
        
        //determine map height (right column)
        var cdetSize = $('company_details').getSize();
        var rightHeight = scrollHeight - cdetSize.y - 40 + 62 + 8;
        
        if(rightHeight + cdetSize.y + 40 < scrollHeight) rightHeight = scrollHeight - cdetSize.y - 40;
        
        if(driveTimeOn) rightHeight -= 20;
        
        $('map_inner').setStyle('height', rightHeight+'px');
        
        if(mapShouldDisplay && mapCompatible) $('company_map').setStyle('display', 'block');
        
        var newsBox = $('news_box');
        var newsArticle = $('newsArticle');
        if(newsBox.getStyle('display') == 'block') {
            var newsSize = newsBox.getSize();
            if(newsArticle) newsArticle.setStyle('height', scrollHeight +'px');
            else {
                if(newsSize.y - 62 > scrollHeight) scrollHeight = newsSize.y - 62 - 8;
            }
        }

        $('scroller').setStyle('height', scrollHeight+'px');

    }
    
    window.addEvent('resize', resetHeight);
    
    resetHeight();
    
    // nav stuff
    $('navCompanies').addEvent('click', function() { switchPage('list_content', 'navCompanies', 'hl_list'); return false; });
    $('navTop').addEvent('click', function() { switchPage('top_content', 'navTop', 'hl_top');  return false; });
    $('navResources').addEvent('click', function() { switchPage('resources_content', 'navResources', 'hl_resources'); return false; });
    $('navContact').addEvent('click', function() { switchPage('contact_content', 'navContact', 'hl_contact'); return false; });
    
    $('sort_alpha').addEvent('click', function() { getList('a'); return false; });
    $('sort_sector').addEvent('click', function() { getList('s'); return false; });
    $('sort_hiring').addEvent('click', function() { getList('h'); return false; });

});

function showComp(theID) {
    $('news_box').setStyle('display', 'none');
    var myHTMLRequest = new Request.HTML({
        'update': $('company_details'),
        'onSuccess':function() {
            resetHeight();
        }
    }).get('company_details.php?c='+theID);
    var myJSONRequest = new Request.JSON({
        'onSuccess':function(jsOb, txt) {
            if(jsOb.success) {
                targetLoc.lat = jsOb.lat;
                targetLoc.lng = jsOb.lng;
                showDirections();
                //showCompMap(jsOb.lat,jsOb.lng);
                mapShouldDisplay = true;
                resetHeight();
            }
            else {
                mapShouldDisplay = false;
                hideCompMap();
            }
        }
    }).get('company_loc.php?c='+theID);
}
function showCompMap(lat, lng) {
    var mapSpot = $('company_map');
    mapShouldDisplay = true;
    resetHeight();
    
    map = new GMap2($('map_inner'));
    map.addControl(new GSmallMapControl());
    addMarker(lat, lng);
}
function hideCompMap() {
    $('company_map').setStyle('display', 'none');
}

function getList(theVar) {
    var myHTMLRequest = new Request.HTML({
        'update': $('list_content')
    }).get('list_sorter.php?s='+theVar);
}

function switchPage(content_id, nav_id, hl_id) {
    $('top_content').setStyle('display', 'none');
    $('list_content').setStyle('display', 'none');
    $('resources_content').setStyle('display', 'none');
    $('contact_content').setStyle('display', 'none');
    
    $('hl_list').setStyle('display', 'none');
    $('hl_top').setStyle('display', 'none');
    $('hl_resources').setStyle('display', 'none');
    $('hl_contact').setStyle('display', 'none');
    
    $('navCompanies').removeClass('active');
    $('navTop').removeClass('active');
    $('navResources').removeClass('active');
    $('navContact').removeClass('active');
    
    $(nav_id).addClass('active');
    $(content_id).setStyle('display', 'block');
    $(hl_id).setStyle('display', 'block');
    
}

function addMarker(lat, lng) {
    var latlng = new GLatLng(lat, lng);
    var marker = new GMarker(latlng);
    map.addOverlay(marker);
    map.setCenter(latlng, 14);
}

function showLocForm() {
    if(!domready) return;
    $('locForm').setStyle('display', 'block');
}
function hideLocForm() {
    if(!domready) return;
    $('locForm').setStyle('display', 'none');
}

function submitLocForm() {
    hideLocForm();
    var theLoc = $('locInput').value;
    var theMethod = $('locMethod').options[$('locMethod').selectedIndex].value;
    
    myLoc.loc = theLoc;
    myLoc.method = theMethod;
    var myJSONRequest = new Request.JSON({
        'onSuccess':function(jsObj,txt) {
            if(jsObj.success) {
                if(jsObj.gotCoords) {
                    myLoc.lat = jsObj.lat;
                    myLoc.lng = jsObj.lng;
                }
                
                showDirections();
            }
            else {
                alert('Couldn\'t get your location.  Please try again.');
                showLocForm();
            }
        }
    }).get('set_loc.php?loc='+theLoc+'&method='+theMethod);
}


function showDirections() {
    var mapSpot = $('company_map');
    mapShouldDisplay = true;
    resetHeight();
    
    map = new GMap2($('map_inner'));
    map.addControl(new GSmallMapControl());


    var fromLoc = false;
    if(myLoc.lat) {
        fromLoc = myLoc.lat + ', ' + myLoc.lng;
    }
    else if(myLoc.loc) {
        fromLoc = myLoc.loc;
    }
    
    var toLoc = false;
    if(targetLoc.lat) {
        toLoc = targetLoc.lat + ', ' + targetLoc.lng;
    }
    
    if(fromLoc && toLoc) {
        directions = new GDirections(map, $('console'));
        //GEvent.addListener(directions, 'error', function() {console.log(directions.getStatus()); });
        GEvent.addListener(directions, 'load', function() {
            var durObj = directions.getDuration();
            showDriveTime(durObj.html);
        });
        directions.load("from: "+fromLoc+" to: "+toLoc, {
            travelMode:eval(myLoc.method)
        });
    }
    else if(targetLoc.lat) {
        addMarker(targetLoc.lat, targetLoc.lng);
        hideDriveTime();
    }
    else if(myLoc.lat) {
        addMarker(myLoc.lat, myLoc.lng);
        hideDriveTime();
    }
    else {
        // sadness.
    }
}
function showDriveTime(msg) {
    driveTimeOn = true;
    $('driveTime').setStyle('display', 'block');
    $('driveTime').set('html', 'COMMUTE: '+msg.toUpperCase()+' | <a href="http://maps.google.com/maps?f=d&source=s_d&saddr='+myLoc.loc+'&daddr='+targetLoc.lat+', '+targetLoc.lng
        + (myLoc.method=='G_TRAVEL_MODE_WALKING' ? '&dirflg=w':'') +'" target="_blank">LARGE MAP</a>');
    resetHeight();
}

function hideDriveTime() {
    driveTimeOn = false;
    $('driveTime').setStyle('display', 'none');
    resetHeight();
}

function showNewsArticle(id) {
    $('news_box').setStyle('display', 'block');
    
    var myHTMLRequest = new Request.HTML({
        'update': $('news_box'),
        'onSuccess':function() {
            resetHeight();
        }
    }).get('news_article.php?id='+id);
}

function showNewsList() {
    $('news_box').setStyle('display', 'block');
    
    var myHTMLRequest = new Request.HTML({
        'update': $('news_box'),
        'onSuccess':function() {
            resetHeight();
        }
    }).get('news_list.php');
}