﻿var glb_defaultControlTexts = [];
var glb_defaultControlTextsFocus = [];
var GLOBAL_WINDOW_IS_IN_FOCUS = true;

/* Required for infovis, but useful for all javascript. IE has no indexOf function!!*/
if (!Array.indexOf) {
    Array.prototype.indexOf = function (obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

$(function () {
    //Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(); //add update panel post request ping
    //prm.add_initializeRequest(EVT_UpdatePanelInitialiseRequest); //add update panel pre request ping

    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(GLB_BeginRequest);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(GLB_EndRequest);

    //GLB_UpdatePanelRefreshed(undefined, undefined);

    $(window).focus(function () { GLOBAL_WINDOW_IS_IN_FOCUS = true; })
    $(window).blur(function () { GLOBAL_WINDOW_IS_IN_FOCUS = false; })
    
});

function Tabify() {
    // This functions means that any tab div with an attribute 'data-hiddenIndexControl' whose value 
    // is a valid .NET hidden control has its state maintained across ajax postbacks
    $('.tabs')
    .tabs({
        select: function (event, ui) {
            $('#' + $(this).attr('data-hiddenIndexControl')).val(ui.index);
        }
    })
    .each(function () {
        if ($(this).attr('data-hiddenIndexControl')) {
            var hdn = $('#' + $(this).attr('data-hiddenIndexControl'));
            if ($(hdn).length == 1) {
                if ($(hdn).val() != "" && parseInt($(hdn).val()) > -1) {
                    // Set the tab
                    $(this).tabs("select", parseInt($(hdn).val()));
                } else {
                    // Set hdn
                    $(hdn).val($(this).tabs('option', 'selected'));
                }
            }
        }
    });
}

function doBusy() {
    $.blockUI({
        css: {
            border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff' 
        }
    });
}
function doUnBusy() {
    try {
        $.unblockUI();
    } catch (e) {

    }    
}

function doFullscreen() {
    $('.divHeader').toggle('slow');
    isFullscreen = !(isFullscreen);
    try {
        var t = setTimeout("InitAll();", 500)
    } catch (e) { }
}

function GLB_UpdatePanelRefreshed(sender, args) {
    doUnBusy();
    //$('a').click(function () { doBusy(); });
    try {
        //$('.blockquote').quovolver();

       // $(".divTabs").tabs({ cookie: { expires: 30} });
        $(".divTabs").tabs();
        //$('.divBox').corner();
        $('.datepicker').datepicker({ dateFormat: 'dd/mm/yy' });
        try { $('#js-news').ticker(); } catch (e) { }
        try { initTooltips(); } catch (e) { }
        try { init(); } catch (e) { }
        try { $('a.lightbox').prettyPhoto(); /*alert('asd'); */} catch (e) { }
        try {
            InitMultiFileUploader();
        } catch (e) { }


    }
    catch (err) {
        //Handle errors here
    }
}
function GLB_BeginRequest(sender, args) {
    if (sender._postBackSettings !== undefined && sender._postBackSettings !== null) {
        try {
            var componentsOfName = sender._postBackSettings.panelsToUpdate[0].split("$");
            var panelID = "Content_" + componentsOfName[componentsOfName.length - 1];

            $('#' + panelID + ' input[type=submit]').attr('disabled', true);
            //$('#' + panelID + ' a').css("visibility", "hidden");

            if (sender._activeElement.id != "") {
                if ($('#' + sender._activeElement.id).hasClass("aButton")) {
                    $('#' + sender._activeElement.id).attr({ "disabled": "disabled", "href": "" }).css("background", "url(/images/animations/button-loader.gif) center no-repeat #999").removeAttr("onclick").html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                }
                $('#' + sender._activeElement.id).attr({ "disabled": "disabled", "onclick": "return false;", "href": "" });
            }
        }
        catch (err) {
        }
        //$('input[type=submit]').attr('disabled', true).css("background","url(/images/layout/container-header-background.jpg) repeat-x scroll left top transparent;");
        //$('a').hide();
    }
}
function GLB_EndRequest(sender, args) {
    try {
        if (sender._postBackSettings !== undefined && sender._postBackSettings !== null) {
            var componentsOfName = sender._postBackSettings.panelsToUpdate[0].split("$");
            var panelID = "Content_" + componentsOfName[componentsOfName.length - 1];

            $('#' + panelID + ' input[type=submit]').removeAttr('disabled');
            //$('input[type=submit]').attr('disabled', false);

            if (sender._activeElement.id != "") {
                $('#' + sender._activeElement.id).removeAttr("disabled").removeAttr("style");
            }
        }
    } catch (err) {
    }
    GLB_UpdatePanelRefreshed(sender, args);
    Tabify();
}
//*******************************************************************//
function GLB_Watermark(inputId) {
    if (GLB_GetIndexByKey(glb_defaultControlTexts, inputId) === undefined) {
        glb_defaultControlTexts.push(new GLB_KeyValuePair(inputId, $('#' + inputId).val()));
        glb_defaultControlTextsFocus.push(new GLB_KeyValuePair(inputId, false));
    }
    $('#' + inputId).click(function () {
        if ($(this).val() == GLB_GetValueByKey(glb_defaultControlTexts, $(this).attr('id'))) {
            $(this).val('').removeClass('watermark');
        } else {
            if (GLB_GetValueByKey(glb_defaultControlTextsFocus, $(this).attr('id')) === false) {
                $(this).select();
                glb_defaultControlTextsFocus[GLB_GetIndexByKey(glb_defaultControlTextsFocus, $(this).attr('id'))].Value = true;
            }
        }
    }).blur(function () {
        glb_defaultControlTextsFocus[GLB_GetIndexByKey(glb_defaultControlTextsFocus, $(this).attr('id'))].Value = false;
        if ($(this).val() == "") {
            $(this).val(GLB_GetValueByKey(glb_defaultControlTexts, $(this).attr('id'))).addClass('watermark');
        }
    }).addClass('watermark');
}
function GLB_GetDefaultControlText(inputId) {
    return GLB_GetValueByKey(glb_defaultControlTexts, inputId);
}
function GLB_ValidateEmail(elementValue) {
    return /^([a-zA-Z0-9._-]{3,50})+@([a-zA-Z0-9.-]{3,50})+\.([a-zA-Z.]{2,5})$/.test(elementValue);
}
function GLB_KeyValuePair(key, value) {
    this.Key = key;
    this.Value = value;
}
function GLB_GetValueByKey(arrayOfKvp, keyToFind) {
    for (var i in arrayOfKvp) {
        if (arrayOfKvp[i].Key == keyToFind) {
            return arrayOfKvp[i].Value;
        }
    }
    return undefined;
}
function GLB_GetIndexByKey(arrayOfKvp, keyToFind) {
    for (var i in arrayOfKvp) {
        if (arrayOfKvp[i].Key == keyToFind) {
            return i;
        }
    }
    return undefined;
}
function GLB_ToBool(val) {
    switch (val.toString().toLowerCase()) {
        case '1': return true;
        case 'true': return true;
        case '0': return false;
        case 'false': return false;
        default: return null;
    }
}
function GLB_OnEnter(textBoxId, buttonToClick) {
    $('#' + textBoxId).keyup(function (e) {
        if (e.keyCode == 13) { $('#' + buttonToClick).click(); }
    });
}

jQuery.fn.outerHTML = function (s) {
    return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
}



// Autocomplete Code
function OnItemSelected(event) {
    var selInd = $find("AutoCompleteEx")._selectIndex;
    if (selInd != -1)
        $find("AutoCompleteEx").get_element().value = $find("AutoCompleteEx").get_completionList().childNodes[selInd]._value;
}
function OnClientPopulated(sender, eventArgs) {
    //Find the autocompleteextender list
    var autoList = $find("AutoCompleteEx").get_completionList();
    for (i = 0; i < autoList.childNodes.length; i++) {
        // Consider value as image path
        var imgeUrl = autoList.childNodes[i]._value;
        //First node will have the text
        var text = autoList.childNodes[i].firstChild.nodeValue;

        autoList.childNodes[i]._value = text;
        //Height and Width of the mage can be customized here...
        autoList.childNodes[i].innerHTML = "<img align=absmiddle src=" + imgeUrl + " />&nbsp;" + text;
    }
}


//Upload Code
function OnComplete() {
    //alert("uploads complete");
    window.location.reload();
}




$(function () {
    try {
        $('#slides').slides({
            preload: true,
            preloadImage: 'img/loading.gif',
            play: 5000,
            pause: 2500,
            hoverPause: true,
            animationStart: function (current) {
                $('.caption').animate({
                    top: 0
                }, 100);
                if (window.console && console.log) {
                    // example return of current slide number
                    console.log('animationStart on slide: ', current);
                };
            },
            animationComplete: function (current) {
                $('.caption').animate({
                    bottom: 0
                }, 200);
                if (window.console && console.log) {
                    // example return of current slide number
                    console.log('animationComplete on slide: ', current);
                };
            },
            slidesLoaded: function () {
                $('.caption').animate({
                    top: 0
                }, 200);
            }
        });
    } catch (e) {
    }
});
