﻿$(document).ready(function() {
jQuery.extend({
    xs: {
        setup: function() {           
            /* set global ajax settings */
            $.ajaxSetup({ cache: false });
            $(document).ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError){
                alert(XMLHttpRequest.responseText);
            });

            /* set custom settings */
            $('.m_chat').cycle({ fx:'fade',timeout:6000,speed:1000 }); 
            $('').maxlength();
            if ($('#xs_mLogin').length > 0) { $.xs.login.setup(); } else { $.xs.logout.setup(); }
            $('#mSearch').keypress(function(e) { if (e.which == 13) { e.preventDefault(); $.xs.redirect('/home/members?page=1&name='+$('#mSearch').val()); } });
            $('#mSearch').click(function() { this.value = ''; $(this).attr('class','small'); } );
            $('#mSearch').blur(function() { if (this.value.length == 0) { this.value = 'zoeken'; }; $(this).attr('class','smallblurred'); } );
            
//            $('#mMenuMoreButton').click(function() {
//                var objMenuMorePos = $(this).position();
//                
//                if ($('#mMenuMore').is(':visible')) {
//                    $('#mMenuMore').hide();
//                }
//                else {
//                    $('#mMenuMore').css('position','absolute').css('z-index',9999999).css('left',objMenuMorePos.left).css('top',objMenuMorePos.top+17).show();
//                }
//            });
            
            if ($('.m_left').height()+20 > $('.m_right').height()) {
                $('.m_page').css('min-height',$('.m_left').height()-170);
                $('.m_page').css('height','auto');
                
                if ($.browser.msie && $.browser.version < 7) {
                    $('.m_page').css('height',$('.m_left').height()-170); 
                }
            }
            
            if ($.browser.msie && $.browser.version < 7) {
                pngFix();
            }
        },
        login: {
            setup: function() {
                $('#mLoginForm').validate({
                    submitHandler: function(form) {
                        if ($.xs.isDisabled($('#mLogin_Save'))) return false;
                        
                        $('#mLogin_Save').attr('disabled',true);
                        var strPostData = $("#mLoginForm").serialize();
                        
                        $.ajax({
                            type: 'POST',
                            url: '/xseno/_site/pages/session/login.aspx?id=1',
                            data: strPostData,
                            success: function(data) {
                                $('#mLogin_Save').attr('disabled',false);
                                
                                var intResponseId = parseFloat(data);
                                if (isNaN(intResponseId)) { alert('Het verwerken is niet gelukt, probeer het nogmaals (fout: '+data+').'); return false; }
                                else if (intResponseId < 0) {
                                    switch(intResponseId) {
                                        case -1: alert('Fout bij het verzenden van de gegevens.'); break;
                                        case -2: alert('Deze nickname of dit e-mail adres is niet bij ons geregistreerd.'); $('input[name="xs$UserName"]').focus(); break;
                                        case -3: alert('Je hebt een onjuist wachtwoord ingevuld.\n\nWeet je je wachtwoord niet meer? Klik op help om je wachtwoord op te vragen!'); $('input[name="xs$Password"]').focus(); break;
                                        case -4: alert('Er is een ban geactiveerd op deze internet aansluiting of op dit profiel.\nDe ban kan tijdelijk zijn. Neem contact op voor meer informatie.'); break;
                                        default: alert('Het verwerken is niet gelukt, probeer het nogmaals (fout: '+data+').'); break;
                                    }
                                }
                                
                                else {
                                   window.location.reload();
                                }
                            }
                        });
                    },

                    focusInvalid: true,
                    rules: {
                        xs$UserName: { required:true, maxlength:100 },
                        xs$Password: { required:true, maxlength:40 }
                    }
                });
    
                $('#mLogin_Save').click(function() { $('#mLoginForm').submit(); });
                if ($('input[name="xs$UserName"]').val().length == 0) { $('input[name="xs$UserName"]').focus(); } else { $('input[name="xs$Password"]').focus(); }
                $('input[name="xs$Password"]').keypress(function(e) { if (e.which == 13) { e.preventDefault(); $('#mLogin_Save').trigger('click'); } });
            }
        },
        logout: {
            setup: function() {
                $('#mLogout_Save').click(function() {
                    $.xs.logout.logout();
                });
            },
            logout: function() {
                if ($.xs.isDisabled($('#mLogout_Save'))) return false;
                $('#mLogout_Save').attr('disabled',true);
                
                $.ajax({
                    type: 'GET',
                    url: '/xseno/_site/pages/session/logout.aspx?logout=true',
                    success: function(data) {
                        $.xs.redirect('/logout/');
                    }
                });
            }
        },
        payments: {
            setup: function() {
                $('#PaymentTypeSms').click(function() {
                    if ($('#PaymentTypeIvrInstructionsPh').is(':visible')) {
                        $('#PaymentTypeIvrInstructionsPh').hide();
                        $('#PaymentTypeSmsInstructionsPh').show();
                    }
                });
                
                $('#PaymentTypeIvr').click(function() {
                    if ($('#PaymentTypeSmsInstructionsPh').is(':visible')) {
                        $('#PaymentTypeSmsInstructionsPh').hide();
                        $('#PaymentTypeIvrInstructionsPh').show();
                    }
                });
            }
        },
        redirect: function(strURL) {
            location.href = strURL;
        },
        validString: function(str) {
            return str.replace(/\<|\>|\;|\"|\'|\+|\-/g,'');
        },
        urlEncode: function(string) {
		    return encodeURIComponent(string);
    	},
    	suggest: function(strName, strUrl) {
    	    $('input[name="'+strName+'"]').autocomplete(strUrl);
            $('input[name="'+strName+'"]').result(function(event, data, formatted) {
                $('input[name="'+strName+'Id"]').val(data[1]);
            });
    	},
    	emoticonsLastFocus: null, emoticonsIncludeFormat:true, emoticonsClick:false, emoticonsClickFunction:null,
        emoticons: function(strInput) {
            if ($.xs.emoticonsLastFocus == strInput) return false;
            $.xs.emoticonsLastFocus = strInput; $('#xsEmo').remove();
            $(strInput).after('<div id="xsEmo">'+$('#xs_mTextFormat').html()+'</div>');
            
            // Text formating
            if (!$.xs.emoticonsIncludeFormat) { 
                $('#xs_mTextFormatEmoticons').css('margin-top','2px'); $('#mTextFormatFormat').hide(); 
            } 
            
            else { 
                $('#xs_mTextFormatEmoticons').css('margin-top','0px'); $('#mTextFormatFormat').show(); 
            }
            
            // Default emoticons click handler
            if (!$.xs.emoticonsClick) {
                $('#mTextFormatBold').unbind('click').click(function() { $(strInput).insertAtInput($(this).attr('title')); });
                $('#mTextFormatItalic').unbind('click').click(function() { $(strInput).insertAtInput($(this).attr('title')); });
                $('#mTextFormatUnderline').unbind('click').click(function() { $(strInput).insertAtInput($(this).attr('title')); });
                $('#mTextFormatSmall').unbind('click').click(function() { $(strInput).insertAtInput($(this).attr('title')); });
                $('img.emopreview').unbind('click').click(function() { $(strInput).insertAtInput($(this).attr('title')); });
            }
            
            // Custom emoticons click handler
            else {
                $('img.emopreview').unbind('click').click(function() { $.xs.emoticonsClickFunction($(this).attr('title'), $(this).attr('src')); });
            }
            
            // Show all emoticons
            $('#mTextFormatEmoticonsShowAll').unbind('click').click(function() { $('#xs_mTextFormatEmoticons').css('height','auto'); $(this).remove(); });
            $('#xsEmo').show();
        },
        error: {
            checker: function(intErrorId, strCustomMsg) {
                if (isNaN(intErrorId) || !(intErrorId >= 0)) {
                    var strMsg = '';
                    if (strCustomMsg == undefined) {
                        switch (intErrorId) {
                            case -1: strMsg = 'De gegevens die werden verstuurd zijn niet correct of onvoldoende.'; break;
                            case -2: strMsg = 'De gegevens die werden verstuurd zijn niet correct.'; break;
                            case -3: strMsg = 'De gegevens konden als gevolg van een technische fout niet verwerkt worden.'; break;
                            case -4: strMsg = 'De benodigde gegevens om door te gaan ontbreken.'; break;
                            default: strMsg = 'Er is een onbekende fout opgetreden.'; break;
                        }
                    }
                    
                    else {
                        strMsg = strCustomMsg;
                    }
                    
                    alert('Fout (code: '+intErrorId+'):\n'+strMsg);
                    //Boxy.alert('Er is een fout opgetreden (foutnummer: '+intErrorId+')<br />Foutdetails: <i>'+strMsg+'</i><br />&nbsp;<br />Mocht deze fout zich opnieuw voordoen, neem dan contact op met de helpdesk. Onze excuses voor het ongemak.', null);
                    return false;
                }
                return true;
            }
        },
        isDisabled: function(jObj) {
            if (jObj.attr('disabled') == 'true' || jObj.attr('disabled') == true) { return true } else { return false };
        },
        profile: { }, admin: { }, forum: { }, help: { }, premium: { }, search: { }, crew: { }
    }});
    
    $.xs.setup();
});