﻿jQuery.preloadImages = function() {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}
$.preloadImages(
            "/Content/Conanomet/images/infoboxmiddle.png",
            "/Content/Conanomet/images/infoboxtopplain.png",
            "/Content/Conanomet/images/infoboxbottomcalloutleft.png",
            "/Content/Conanomet/images/infoboxbottomcalloutright.png",
            "/Content/Conanomet/images/infoboxbottomplain.png",
            "/Content/Conanomet/images/infoboxtopcalloutleft.png",
            "/Content/Conanomet/images/infoboxtopcalloutright.png",
            "/Content/Conanomet/images/maps/Albania.png",
            "/Content/Conanomet/images/maps/Austria.png",
            "/Content/Conanomet/images/maps/Belarus.png",
            "/Content/Conanomet/images/maps/Belgium.png",
            "/Content/Conanomet/images/maps/BosniaAndHerzegovina.png",
            "/Content/Conanomet/images/maps/Bulgaria.png",
            "/Content/Conanomet/images/maps/Croatia.png",
            "/Content/Conanomet/images/maps/Cyprus.png",
            "/Content/Conanomet/images/maps/CzechRepublic.png",
            "/Content/Conanomet/images/maps/Denmark.png",
            "/Content/Conanomet/images/maps/Estonia.png",
            "/Content/Conanomet/images/maps/Finland.png",
            "/Content/Conanomet/images/maps/France.png",
            "/Content/Conanomet/images/maps/Germany.png",
            "/Content/Conanomet/images/maps/Greece.png",
            "/Content/Conanomet/images/maps/Hungary.png",
            "/Content/Conanomet/images/maps/Iceland.png",
            "/Content/Conanomet/images/maps/Ireland.png",
            "/Content/Conanomet/images/maps/Italy.png",
            "/Content/Conanomet/images/maps/Latvia.png",
            "/Content/Conanomet/images/maps/Lithuania.png",
            "/Content/Conanomet/images/maps/Luxembourg.png",
            "/Content/Conanomet/images/maps/Macedonia.png",
            "/Content/Conanomet/images/maps/Malta-2.png",
            "/Content/Conanomet/images/maps/Moldova.png",
            "/Content/Conanomet/images/maps/Montenegro.png",
            "/Content/Conanomet/images/maps/Netherlands.png",
            "/Content/Conanomet/images/maps/Norway.png",
            "/Content/Conanomet/images/maps/Poland.png",
            "/Content/Conanomet/images/maps/Portugal.png",
            "/Content/Conanomet/images/maps/Romania.png",
            "/Content/Conanomet/images/maps/Russia.png",
            "/Content/Conanomet/images/maps/Serbia.png",
            "/Content/Conanomet/images/maps/Slovakia.png",
            "/Content/Conanomet/images/maps/Slovenia.png",
            "/Content/Conanomet/images/maps/Spain.png",
            "/Content/Conanomet/images/maps/Sweden.png",
            "/Content/Conanomet/mages/maps/Switzerland.png",
            "/Content/Conanomet/images/maps/Turkey.png",
            "/Content/Conanomet/images/maps/UK.png",
            "/Content/Conanomet/images/maps/ukraine.png"
        );

var mapwidth = 627;
var mapheight = 578;

var mapbump = {
    "finland": { x: -10, y: 0 },
    "latvia": { x: 0, y: -5 },
    "norway": { x: -26, y: 0 },
    "russia": { x: 36, y: -90 },
    "spain": { x: -70, y: 0 },
    "sweden": { x: -23, y: 0 },
    "turkey": { x: 20, y: 10 },
    "": { x: 0, y: 0 }
};

function getboundingbox(coords) {
    coords = String(coords).split(",");

    var bounds = new Object();
    bounds.x1 = bounds.x2 = Number(coords[0]);
    bounds.y1 = bounds.y2 = Number(coords[1]);

    for (var i = 2; i < coords.length - 1; i += 2) {
        if (Number(coords[i]) < bounds.x1) { bounds.x1 = Number(coords[i]); } else if (Number(coords[i]) > bounds.x2) { bounds.x2 = Number(coords[i]); }
        if (Number(coords[i + 1]) < bounds.y1) { bounds.y1 = Number(coords[i + 1]); } else if (Number(coords[i + 1]) > bounds.y2) { bounds.y2 = Number(coords[i + 1]); }
    }

    return bounds;
}

var _currentRequest = null;
var _winInterval = null;

function cliptoview(pt) {
    var infoboxwidth = 382;
    var infoboxheight = 226;

    //spot infobox bounds
/*
    var bb1 = $('#bb1');
    bb1.css('left', pt.x + 'px');
    bb1.css('top', pt.y + 'px');
    bb1.css('width', infoboxwidth+'px');
    bb1.css('height', infoboxheight+'px');
*/

    //how much of the map is visible

    var maptop = $('#clickmap').position().top;
    var mapleft = $('#clickmap').position().left;
    var mapwidth = $('#clickmap').width();
    var mapheight = $('#clickmap').height();
    var viewtop = $(window).scrollTop();
    var viewleft = $(window).scrollLeft();
    var viewwidth = $(window).width();
    var viewheight = $(window).height();
    
    var x1, y1, x2, y2;
    var removecallout = 0;
    
    x1 = viewleft - mapleft; if (x1 < 0) { x1 = 0; }
    y1 = viewtop - maptop; if (y1 < 0) { y1 = 0; }
    x2 = viewleft + viewwidth - mapleft; if (x2 > mapwidth) { x2 = mapwidth; }
    y2 = viewtop + viewheight - maptop; if (y2 > mapwidth) { y2 = mapwidth; }
    
    x1 -= 5; y1 -= 5;
    x2 += 5; y2 += 5;
/*
    var bb2 = $('#bb2');
    bb2.css('top', y1 + 10);
    bb2.css('left', x1+10);
    bb2.css('width', x2-x1-20);
    bb2.css('height', y2 - y1-20);
*/
    //clip point to intersection, nudge bottom, right, then top then left
    if (pt.x + infoboxwidth > x2) { pt.x = x2 - infoboxwidth; removecallout = 1; }
    if (pt.y + infoboxheight > y2) { pt.y = y2 - infoboxheight; removecallout = 1; }
    if (pt.x < x1) { pt.x = x1; removecallout = 1; }
    if (pt.y < y1) { pt.y = y1; removecallout = 1; }

    if (removecallout) {
        $('#countryinfobox').attr("className", "calloutbox");
    }
}
function mouseovercountry(classname, countryid, menuobj) {
    var boundingbox;

    if (classname) {
        boundingbox = getboundingbox($('#area-' + classname).attr("coords"));

        //make the country shape glow
        $('#rolloverimage').attr("className", "selectedcountry " + classname);
    }


    //show the info box
    var jqcib = $('#countryinfobox');
    jqcib.show();

    var bumpx = 0;
    var bumpy = 0;

    if (mapbump[classname] != undefined) {
        bumpx = mapbump[classname].x;
        bumpy = mapbump[classname].y;
    }

    var lpos = 0;
    var tpos = 0;
    
    //are we pointing at a country, or is it a menu item?
    if (classname) {
        //are we using a left callout or a right callout?
        if (boundingbox.x1 + boundingbox.x2 > mapwidth) {
            //right callout
            lpos = (boundingbox.x1 + boundingbox.x2) / 2 - 336 + bumpx;
            if (lpos < 0) { lpos = 0; }

            //should we use a top or bottom bounding box?
            if (boundingbox.y1 > mapheight - boundingbox.y2) {
                //top position, bottom callout
                jqcib.attr("className", "calloutbottomright");
                tpos = boundingbox.y1 - jqcib.height() + 4 + bumpy;
            } else {
                //bottom position, top callout
                jqcib.attr("className", "callouttopright");
                tpos = boundingbox.y2 - 4 + bumpy;
            }
        } else {
            //left callout
            var lpos = (boundingbox.x1 + boundingbox.x2) / 2 - 46 + bumpx;
            if (lpos<0) { lpos=0; }

            //should we use a top or bottom bounding box?
            if (boundingbox.y1 > mapheight - boundingbox.y2) {
                //top position, bottom callout
                jqcib.attr("className", "calloutbottomleft");
                tpos = boundingbox.y1 - jqcib.height() + 4 + bumpy;
            } else {
                //bottom position, top callout
                jqcib.attr("className", "callouttopleft");
                tpos = boundingbox.y2 - 4 + bumpy;
            }
        }
        var pt = { x: lpos, y: tpos };
        cliptoview(pt);
        
        jqcib.css("left", pt.x);
        jqcib.css("top", pt.y);
    } else {
        jqcib.css("left", 80);
        jqcib.css("top", 30);
        jqcib.attr("className", "calloutbox");
    }

    //debug country name
    $('.boxcontent h2', jqcib).text("Loading...");
    $('.boxcontent h2', jqcib).css("background", "transparent url('/Content/Conanomet/images/loading.gif') no-repeat");
    $('.boxcontent h3', jqcib).hide();
    $('.boxcontent .mapicon', jqcib).hide();
    $('#piechart').hide();
    $('#piechartkey').hide();
//    $('.boxcontent').css('background', "transparent url('/Content/Conanomet/images/infoboxmiddle.png')");
    $('.boxcontent').attr('className', 'boxcontent noshadow');

    //          $.get("/Conanomet/getCountryOverviewXml/"+countryid, {}, countryinfoloaded, 'xml');

    //if there is an ajax op in progress, abort it
    if (_currentRequest) { _currentRequest.abort(); }

    //only start a new ajax op if we're over the same click point for a fifth of a second
    if (_winInterval) { window.clearInterval(_winInterval); }
    _winInterval = window.setTimeout(function() {
        _winInterval = null;
        _currentRequest = $.ajax({
            cache: false,
            dataType: 'xml',
            error: countryinfofailed,
            success: countryinfoloaded,
            timeout: 5000,
            url: "/Conanomet/getCountryOverviewXml/" + countryid
        });
    }, 200);
}

function countryinfofailed() {
    $('#countryinfobox .boxcontent h2').text("Error: " + _currentRequest.responseText);
    _currentRequest = null;
}

function countryinfoloaded(xdata) {
    _currentRequest = null;
    var piechart = new PieChart3D("piechart", 120, 68, 12);

    var jqcib = $('#countryinfobox');
    var jqcountryxml = $(xdata);
    var jqcountryinfo = $('country-info', jqcountryxml);

    //load data for text
    $('.boxcontent h2', jqcib).text(jqcountryinfo.attr("name"));
    //set up the flag
    var flag = $('flag', jqcountryxml).attr('src');
    if (flag != '') {
        $('.boxcontent h2', jqcib).css("background", "transparent url(" + flag + ") no-repeat");
    }
    $('.boxcontent h3', jqcib).show();

    var jqdataitems = $('results > item', jqcountryinfo);
    $('#piechartkey li').each(function() { $(this).remove() });

    if (jqdataitems.size() > 0) {
        for (var i = 0; i < jqdataitems.size(); i++) {
            var jqitem = jqdataitems.eq(i);
            var colourstr = jqitem.attr('colour');
            var colourvec = colourstr.split(',');

            $('#piechartkey').append('<li>' + escape(jqitem.attr('searchterm')) + '</li>');
            piechart.add(Number(jqitem.text()));

            piechart.setitemcolour(i, Number(colourvec[0]), Number(colourvec[1]), Number(colourvec[2]));
        }

        $('#piechart').show();
        $('#piechartkey').show();
//        $('.boxcontent').css('background', "transparent url('/Content/Conanomet/images/infobox_bg.png')");
        $('.boxcontent').attr('className', 'boxcontent');
        piechart.draw();
    } else {
        $("#piechart").text("No organisations or capabilities registered");
        $('#piechart').show();
    }
    //Grab the key colours
    var items = $('#piechartkey li');
    for (var i = 0; i < items.size(); i++) {
        var item = items.eq(i);
        var c = piechart.getitemcolour(i);

        item.css("border-color", "RGB(" + c.r + "," + c.g + "," + c.b + ")");
    }

    //set up the map
    var map = $('infoboxmap', jqcountryxml).attr('src');

    if (map != '') {
        var jqmap = $('.boxcontent .mapicon', jqcib);
        jqmap.show();
        jqmap.attr('alt', jqcountryinfo.attr("name"));
        jqmap.attr('src', map);
    }
}
function mouseoutcountry() {
    $('#rolloverimage').attr("className", "selectedcountry");
    $('#countryinfobox').hide();
}
function mouseoverworld() {
    $('#worldicon').attr('src', '/Content/Conanomet/images/globe_colour.png');
}
function mouseoutworld() {
    $('#worldicon').attr('src', '/Content/Conanomet/images/globe_bw.png');
}
function showworldmenu() {
    $('#worldmenu').show();
    $('#clickmapclickable').hide();
}
function hideworldmenu() {
    $('#worldmenu').hide();
    $('#clickmapclickable').show();
}
function setcountry(countryid) {
    $('#SearchCriteriaForm option[value=' + countryid + ']').attr('selected', true);
    $('#SearchCriteriaForm').get(0).submit();
}
