﻿$.fn.overlabel = function () {
    this.each(function () {
        var label = $(this);
        var id = this.htmlFor || label.attr('for') || "NO-ID";
        var element = $("#" + id.replace(/\:/, '\\:'));
        element.parent().addClass("overlabel-wrapper").end()
	    .focus(function () { label.css("text-indent", "-5000px"); })
	    .blur(function () { this.value || label.css("text-indent", "0px"); })
	    .length && label.addClass("overlabel-apply");

        if (element.val() != "") {
            element.trigger("focus");
        }
    });
}

function LaunchCKEditor(strCKEditorTextArea) {
    if (!CKEDITOR.instances[strCKEditorTextArea]) {
        CKEDITOR.replace(strCKEditorTextArea, { customConfig: '../ckeditor/emoo_config.js' });
    }
}

function LaunchCKEditorAll() {
    CKEDITOR.replaceAll();
}

function LaunchCKEditorBare(strCKEditorTextArea) {
    if (!CKEDITOR.instances[strCKEditorTextArea]) {
        CKEDITOR.replace(strCKEditorTextArea, { customConfig: '../ckeditor/bare_config.js' });
    }
}

function LaunchCKEditorAdmin(strCKEditorTextArea) {
    if (!CKEDITOR.instances[strCKEditorTextArea]) {
        CKEDITOR.replace(strCKEditorTextArea, { customConfig: '../ckeditor/admin_config.js' });
    }
}

function DisposeCKEditor(strCKEditorTextArea) {
    if (CKEDITOR) {
        if (CKEDITOR.instances[strCKEditorTextArea]) {
            var edEditor = CKEDITOR.instances[strCKEditorTextArea];
            var strData = edEditor.getData().replace(/<\/?[^>]+>/gi, '');
            strData = strData.replace(/^[ \t\r\n]+|[ \t\r\n]+$/, '');
            edEditor.destroy();

            var $tbEditor = $('#' + strCKEditorTextArea);
            $tbEditor.attr('value', strData);
        }
    }
}

function UpdateCKEDitor(isp) {
    if (isp == '0') {
        DisposeCKEditor($('textarea[id$=tbEditor]').attr('id'))
    }
    else {
        LaunchCKEditor($('textarea[id$=tbEditor]').attr('id'))
    }
}

function UpdateCKEDitorAdmin(isp) {
    if (isp == '0') {
        DisposeCKEditor($('textarea[id$=tbEditor]').attr('id'))
    }
    else {
        LaunchCKEditorAdmin($('textarea[id$=tbEditor]').attr('id'))
    }
}

function UpdateDirectoryPremiumStuff() {
    var isp = $("input[id$=hfISP]").val();

    if (isp == '0') {
        $('div[id$=pPremiumStuff]').hide();
    }
    else {
        $('div[id$=pPremiumStuff]').show();
    }
}

function SimpleTimeEntry(event, object) {
    if ($(object).caret().start == 2 && event.which != 8 && event.which != 0) {
        $(object).val(object.value.substring(0, 2) + ':' + object.value.substring(2, object.value.length));
    }

    if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
        event.preventDefault();
    }
}

function BindToBox(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        window.location.href = $('a[id$=ButtonLogin]').attr('href');
        return false;
    }
};

function PatchRadCalendar() {
    $('.pickerWrapper_Default2006').width('160px');
    $('.pickerWrapper_Default2006 table').width('140px');
    $('.pickerWrapper_Default2006 table').css('display', 'inline-block');
}

$(document).ready(function () {
    //Login page bindings
    $('input[id$=TextBoxUserEmail]').bind('keypress', BindToBox);
    $('input[id$=TextBoxPasword]').bind('keypress', BindToBox);

    //Print button in forms
    $('.buttonPrint').click(function () {
        window.print();
        return false;
    });

    //Time entry
    $('input[id$=tbEventStartTime]').keypress(function (event) { SimpleTimeEntry(event, this) });
    $('input[id$=tbEventEndTime]').keypress(function (event) { SimpleTimeEntry(event, this) });

    //Overlabel tips
    $(".overlabel").overlabel();

    // Initialize Advanced Galleriffic Gallery
    if ($('#gallery-adv')[0] != undefined) {
        var galleryAdv = $('#gallery-adv').galleriffic('#thumbs-adv', {
            delay: 2000,
            numThumbs: 6,
            preloadAhead: 3,
            enableTopPager: true,
            enableBottomPager: false,
            imageContainerSel: '#slideshow-adv',
            controlsContainerSel: '#controls-adv',
            renderSSControls: false,
            renderNavControls: false,
            nextPageLinkText: '&rsaquo;',
            prevPageLinkText: '&lsaquo;',
            enableHistory: false,
            autoStart: false
        });
    }

    UpdateDirectoryPremiumStuff();

    PatchRadCalendar();

    if (typeof (Sys) != 'undefined') {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(PatchRadCalendar);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(UpdateDirectoryPremiumStuff)
    }

    //Urchin tracker
    _userv = 0;
    urchinTracker();

    $('input[id$=TextBoxUserEmail]').focus();
    $('input[id$=tbEMail]').focus();
});