﻿
var ac_source = []; // new Array();

var autoCompleteCountries = [];

jQuery(document).ready(function () {

    if (Cufon) { Cufon.replace('#topmenu li, h2.grid_9, h2.grid_12'); }

    jQuery.each(jQuery('ul.countries li a'), function (i, v) {
        autoCompleteCountries.push({ label: jQuery(v).text().trim(), url: jQuery(v).attr('href'), type: 'country' });
        ac_source.push({ label: jQuery(v).text().trim(), value: jQuery(v).attr('href') });
    });

    //	jQuery.each(cpos,
    //		function (i, v) {
    //			autoCompleteCountries = jQuery.merge(v.countries, autoCompleteCountries)
    //		}
    //	);

    autoCompleteCountries = autoCompleteCountries.sort(function (a, b) { var n1 = a.label.toUpperCase(); var n2 = b.label.toUpperCase(); return (n1 < n2) ? -1 : (n1 > n2) ? 1 : 0; })

    var auto = jQuery('#searchTextbox').autocomplete(
    {
        source: function (req, res) {
            this._content = [];
            var term = req.term.toLowerCase();
            var re = new RegExp(term, 'gi');
            res(
				jQuery.map(
					jQuery.grep(
						autoCompleteCountries,
						function (i, v) {
						    var isMatch = re.test(i.label);
						    return isMatch;
						    if (isMatch) return i;
						}),
						function (itm) { return { label: itm.label, url: itm.url, value: itm.name, image: itm.image, type: 'country' }; }
					), true);

            jQuery.getJSON(
                'http://217.13.235.244:8080/ui-querycompletion/complete.do?format=json&q=' + req.term + '&callback=?',
                {},
				function (data) {
				    res(jQuery.map(data.suggestions[0].suggestions, function (itm) { return { label: itm.label, value: itm.value} }), false);
				})
        },
        select: function (e, ui) {
            if (ui.item.type && ui.item.type == 'country') {
                window.location = ui.item.url;
                return;
            }
            window.location = '/Sok.aspx?q=' + ui.item.value;
        }
    });

    auto.data('autocomplete')._response = function (content, flag) {

        if (flag) {
            this.items = content;
        } else {
            this.items = jQuery.merge(this.items, content);
        }


        //        CountryFlagconsole.log(flag)

        if (!this.options.disabled && this.items && this.items.length) {
            //this._content = this._normalize( this.items );
            this._content = this.items;
            this._suggest(this.items);
            this._trigger('open');
        } else {
            this.close();
        }
        this.pending--;
        if (!this.pending) {
            this.element.removeClass('ui-autocomplete-loading');
        }
    };

    auto.data('autocomplete')._renderItem = function (ul, item) {
        var label = item.label;
        if (item.type && item.type == 'country') {
            label = '<strong>' + label + '</strong>';
        }
        return $("<li></li>")
			.data("item.autocomplete", item)
			.append($("<a></a>").html(label))
			.appendTo(ul);
    },

	jQuery('#organisations a').click(function (e) {
	    e.preventDefault();

	    jQuery('#country-list').hide();
	    jQuery('#organisations-list').toggle().scrollTop();
	});

    jQuery('#countries a').click(function (e) {
        e.preventDefault();

        jQuery('#organisations-list').hide();
        jQuery('#country-list').toggle().scrollTop();
        if (worldmap) { google.maps.event.trigger(worldmap, 'resize'); }

    });

    /* close */
    jQuery('<a>').attr('id', 'close').appendTo('body');
    jQuery('<a>').attr('href', 'javascript:void(0);');
    jQuery('#close').click(function () { jQuery('#country-list').slideToggle(); jQuery('#close').toggle(); });
    jQuery('#image a').click(function (evt) {

        var w = jQuery(document).width();
        jQuery('#country-list').css({
            top: '150px',
            left: ((w / 2) - (960 / 2)) + 'px'
        });

        jQuery('#close').css({ top: '130px', left: ((w / 2) + (960 / 2) - 20) + 'px' }).toggle();

        jQuery('#country-list').slideDown();

        evt.preventDefault();
    });
    jQuery('.loggedin-link').click(function (e) {
        e.preventDefault();
        var d = jQuery('#loggedin-dialog');
        var t = jQuery(this);
        t.addClass('active');

        d.css({ position: 'absolute', top: t.offset().top + t.outerHeight() });
        jQuery('#close-login-dialog').click(function () { jQuery('#login-dialog').hide(); jQuery('.login-link').removeClass('active'); });
        d.show();

    });
    jQuery('.login-link').click(function (e) {
        //        e.preventDefault();
        //        addOverlay(); showOverlay();
        //        jQuery('#overlay').css({ backgroundColor: '#000', opacity: 0.90 })
        //        jQuery('#login-dialog').center().show();
        //        jQuery('#usernameTextbox').focus();
        //        $('#overlay, #close-login-dialog').click(function () { jQuery('#overlay, #login-dialog').hide(); });

        e.preventDefault();
        var d = jQuery('#login-dialog');
        var t = jQuery(this);
        t.addClass('active');
        d.css({ position: 'absolute', top: t.offset().top + t.outerHeight() });
        jQuery('#close-login-dialog').click(function () { jQuery('#login-dialog').hide(); jQuery('.login-link').removeClass('active') });
        d.show();
        jQuery('#loginView1_usernameTextbox').focus();
        //jQuery('div[id$="usernameTextbox"]').css({background:'red'});
    });

});

function addOverlay() {
    if (!jQuery('#overlay').length) {
        jQuery('<div>').attr('id', 'overlay').appendTo('body');
    }
}

function showOverlay() {
    jQuery('#overlay').fillWindow().show();
}

jQuery.fn.fillWindow = function () {
    this.css('position', 'absolute');
    this.css('top', 0);
    this.css('left', 0);
    this.css('height', (jQuery(window).height()));
    this.css('width', (jQuery(window).width()));
    return this;
}

jQuery.fn.center = function () {
    this.css("position", "absolute");
    this.css("top", (jQuery(window).height() - this.height()) / 2 + jQuery(window).scrollTop() + "px");
    this.css("left", (jQuery(window).width() - this.width()) / 2 + jQuery(window).scrollLeft() + "px");
    this.css('z-index', 10000);
    return this;
}

if (typeof String.prototype.trim !== 'function') {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g, '');
    }
}


(function () { $.fn.pulse = function () { var o = $(this); o.animate({ opacity: 0 }, 300, function () { o.animate({ opacity: 1 }, 300); }); } })(jQuery);

/**
* require is used for on demand loading of JavaScript
*
* require r1 // 2008.02.05 // jQuery 1.2.2
*
* // basic usage (just like .accordion)
* $.require("comp1.js");
*

* @param  jsFiles string array or string holding the js file names to load
* @param  params object holding parameter like browserType, callback, cache
* @return The jQuery object
* @author Manish Shanker
*/

(function () {
    $.require = function (jsFiles, params) {

        var params = params || {};
        var bType = params.browserType === false ? false : true;

        if (!bType) {
            return $;
        }

        var cBack = params.callBack || function () { };
        var eCache = params.cache === false ? false : true;

        if (!$.require.loadedLib) $.require.loadedLib = {};

        if (!$.scriptPath) {
            var path = $('script').attr('src');
            $.scriptPath = path.replace(/\w+\.js$/, '');
        }
        if (typeof jsFiles === "string") {
            jsFiles = new Array(jsFiles);
        }
        for (var n = 0; n < jsFiles.length; n++) {
            if (!$.require.loadedLib[jsFiles[n]]) {
                $.ajax({
                    type: "GET",
                    url: $.scriptPath + jsFiles[n],
                    success: cBack,
                    dataType: "script",
                    cache: eCache,
                    async: false
                });
                $.require.loadedLib[jsFiles[n]] = true;
            }
        }
        //console.dir($.require.loadedLib);

        return $;
    };
})(jQuery);

(function () { $.fn.rounded = function (o) { $(this).css({ 'border-radius': o.radius, '-moz-border-radius:': o.radius, '-webkit-border-radius': '4px' }); } })(jQuery);

(function ($) { $(".ui-autocomplete-input").live("autocompleteopen", function () {
    var autocomplete = $(this).data("autocomplete"),
    menu = autocomplete.menu;
    
    if (!autocomplete.options.selectFirst) { return; }

    menu.activate($.Event({ type: "mouseenter" }), menu.element.children().first());
});
} (jQuery));


