﻿//global variables
var _xmlLocalize = null;
var _root = location.protocol + '//' + location.host;

$(function () {

    var _txtGlobalSearch$ = $($('.txtGlobalSearch')[0]);
    // =text //
    var textBoxes$ = $('.txt');
    var _lbParticipantSearch$ = $('#global_search_btn .btn_search');
    var _golbalLabel$ = $('#global_search_box .right label');
    var _blSearching = false;

    var _culture = $('#culture').val();
    var _textInput$ = $('.txt');
    var _language$ = $('#language');
    var _localize$ = $('.localize');

    //_localize$.hide();
    //bind hover intent for slide up and slide down
    _language$.find('a.selected').click(function () { _localize$.slideDown('fast'); }, function () { _localize$.slideUp('fast'); });

    //=flags
    $('#lang_o').hide();
    $('#lang_c').hoverIntent(function () { $('#lang_o').slideDown('fast'); }, function () { $('#lang_o').slideUp('fast'); });

    // =checkbox / =radio //
    $('input').not('.omit').checkBox();

    for (i = 0; i <= (_textInput$.size() - 1); i++) {
        if ($(_textInput$[i]).val() !== '') {
            var label$ = $('label[for="' + $(_textInput$[i]).attr('id') + '"].overlabel');
            if (label$.size() > 0) { label$.addClass('focus'); }
        }
    }


    // =text field //
    $('.txt').focus(function () {
        $(this).removeClass('txt').addClass('txtfocus');
        $('label[for="' + $(this).attr('id') + '"].overlabel').addClass('focus');
    });

    $('.txt').blur(function () {
        var label$ = $('label[for="' + $(this).attr('id') + '"].overlabel');

        $(this).removeClass('txtfocus txton').addClass('txt');
        if (label$.size() > 0) {
            if ($(this).val() != '') { if (!label$.hasClass('focus')) { label$.addClass('focus'); } }
            else { label$.removeClass('focus'); }
        }
    });

    $('.txt').keyup(function () {
        var label$ = $('label[for="' + $(this).attr('id') + '"].overlabel');

        $(this).removeClass('txtfocus txton').addClass('txt');
        if (label$.size() > 0) {
            if ($(this).val() != '') { if (!label$.hasClass('focus')) { label$.addClass('focus'); } }
            else { label$.removeClass('focus'); }
        }
    });

    $('.txt').hover(
		function () {
		    var label$ = $('label[for="' + $(this).attr('id') + '"].overlabel');
		    if (!$(this).hasClass('txtfocus')) { $(this).removeClass('txtfocus').addClass('txton') };
		    if (label$.size() > 0) { label$.addClass('focus'); }

		},
		function () {
		    var label$ = $('label[for="' + $(this).attr('id') + '"].overlabel');
		    if (!$(this).hasClass('txtfocus')) { $(this).removeClass('txtfocus txton').addClass('txt') };
		    if (label$.size() > 0) {
		        if ($(this).val() != '') { label$.addClass('focus'); }
		        else { label$.removeClass('focus'); }
		    }

		    //if ($(this).val() != '') { if (!label$.hasClass('focus')) { label$.addClass('focus'); } }
		});

    var cache = {};

    _lbParticipantSearch$.bind('click', function (e) {
        e.preventDefault();

        var query = trim(_txtGlobalSearch$.val());

        if (IsNumeric(query)) { window.location = _root + '/' + query; }
        else { window.location = _root + '/search/' + query; }


        return false;
    });

    _txtGlobalSearch$.bind('keyup', function (e) {
        var label$ = $('label[for="' + $(this).attr('id') + '"].overlabel');
        if ($(this).val() != '') { label$.addClass('focus'); }
        else { label$.removeClass('focus'); }

        if (e.keyCode == '13') {
            e.preventDefault();

            if (IsNumeric($(this).val())) {
                window.location = _root + '/' + +trim($(this).val());
            }

            _txtGlobalSearch$.autocomplete("close");
            window.location = _root + '/search/' + $(this).val();

            return false;
        }
    });

    //autocomplete for event search
    _txtGlobalSearch$.autocomplete({
        source: function (request, response) {
            if (!_blSearching) {

                $.ajax({
                    url: _root + '/handler/event.ashx?q=' + _txtGlobalSearch$.val() + '&limit=25'
	                    , dataType: "json"
                        , type: 'POST'
	                    , delay: 1000
	                    , beforeSend: function () {
	                        _lbParticipantSearch$.css('background', 'url("../img/loading_1_small.gif") no-repeat scroll center top transparent');
	                    }
	                    , complete: function () {
	                        _lbParticipantSearch$.css('background', 'url("../img/btn_search.png") no-repeat scroll center top transparent');
	                        _blSearching = false;
	                    }
	                    , success: function (data) {

	                        response($.map(data, function (item) {
	                            var m = item.StartDate.match(/^\/Date\((\d+)([-+]\d\d)(\d\d)\)\/$/);
	                            var dateStart = new Date(1 * m[1] + 3600000 * m[2] + 60000 * m[3]);
	                            var month = dateStart.getMonth() + 1;
	                            var date = dateStart.getMonth();

	                            return {
	                                label: item.Name + ', ' + AddLeadingZero(month) + '/' + AddLeadingZero(date) + '/' + dateStart.getFullYear() + ' - ' + item.Location
							, value: item.ID
	                            }
	                        }))
	                    }
                });
            }
        }
        , minLength: 3
	            , select: function (event, ui) {
	                //go to the event home page
	                if ((_culture == null) || (_culture == 'en-US')) { window.location = _root + '/' + ui.item.value; }
	                else { window.location = _root + '/' + _culture + '/' + ui.item.value; }

	            }
	            , open: function () {
	                //$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
	                $('.ui-autocomplete').css({ "z-index": 95 });
	            },
        close: function () {
            //$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });


    $('#languages').hover(function () { $('#languages .localize').removeClass('hidden').fadeIn('fast'); }, function () { $('#languages .localize').addClass('hidden').fadeIn('fast'); });

    //=tooltip
    //Select all anchor tag with rel set to tooltip
    $('a[rel=tooltip], .tooltip').mouseover(function (e) {

        //Grab the title attribute's value and assign it to a variable
        var tip = $(this).attr('title');

        //Remove the title attribute's to avoid the native tooltip from the browser
        $(this).attr('title', '');

        //Append the tooltip template and its value
        $(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');

        //Set the X and Y axis of the tooltip
        $('#tooltip').css('top', e.pageY + 10);
        $('#tooltip').css('left', e.pageX + 20);

        //Show the tooltip with faceIn effect
        $('#tooltip').fadeIn('500');
        $('#tooltip').fadeTo('10', 0.8);

    }).mousemove(function (e) {

        //Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
        $('#tooltip').css('top', e.pageY + 10);
        $('#tooltip').css('left', e.pageX + 20);

    }).mouseout(function () {

        //Put back the title attribute's value
        $(this).attr('title', $('.tipBody').html());

        //Remove the appended tooltip template
        //$(this).children('div#tooltip').remove();
        $('#tooltip').remove();
    });
});
