﻿(function() 
{
    var CID = window.CID = 
    {
        load: function( moduleName, options )
        {
            if( typeof moduleName.init != 'function' ) return;
            
            var options = options == undefined ? {} : options;
            $(document).ready( function()
            {
                moduleName.init( options );
            } );
        },
        init: function()
        {
            jQuery.fn.extend( this.elementMethods );
            CID.load( CID.Shared );            
        },
        // Methods each jQuery object should be extended with. Extending is performed in CID.init.
        elementMethods: 
        {
            addLoader: function( preserveContent, loadingMessage )
            {
                var preserveContent = preserveContent == null ? false : preserveContent;
                var loadingMessage = loadingMessage == null ? '' : '<span class="ajaxLoadingMessage">' + loadingMessage + '</span>';
                // No more than one ajax loader should be shown.
                if( $(this).find( '.ajaxLoader' ).length != 0 )
                {
                    return $(this);
                }
                if( !preserveContent )
                {
                    $(this).empty();
                }
                return $(this).append( '<span class="ajaxLoader"><img src="/images/ajaxLoader.gif" alt="Loading" title="Loading" class="ajaxLoadingImage">' + loadingMessage + '</span>' );
            },
            removeLoader: function()
            {
                return $(this).find( '.ajaxLoader' ).remove();
            },
            cidSlider: function( options )
            {
                var defaults = 
                {
                    change: function(e, ui){}                    
                };                
                var options = $.extend( defaults, options );
                
                return $(this).prepend( '<img class="cidSliderHandle" src="/images/cidSliderHandle.png">' ).slider( 
                    {   handle: '.cidSliderHandle',
	                    min: 0,
	                    max: 100,
	                    stepping: 10,
	                    animate: true,
	                    startValue: 50,
	                    change: function(e, ui)
	                    {
                            $(this).find( '.cidSliderValue' ).val( ui.value );
                            options.change(e, ui);
                        },
	                    start: function(e, ui)
	                    {
                        },
                        stop: function(e, ui)
                        {
                        },
                        slide: function(e, ui)
                        {
                        }
	                }
	            );
            },
            popup: function()
            {
                return $(this).click( function() { window.open( $(this).attr( 'href' )); return false; } );
            }
        }
    };
})();
    
$(document).ready(function()
{
    CID.init();
});


CID.Shared = function()
{
    return {
        init: function()
        {
            $( '.popupLink' ).popup();
            
            // Execute PNG fix for Internet Explorer.
            if( $.browser.msie && $.browser.version < 7 )
            {
                $.ifixpng( '/images/pixel.gif' );
                $( '.cidSliderHandle img' ).ifixpng( );
            }
        },
        getSeperatedCompetenceString: function( selector )
        {
            var competences = '';            
            $(selector).each( function()
                {
                    var competenceId = $(this).attr( 'id' ).replace( 'competence', '' );
                    var val = $(this).val();
                    competences += competenceId + '|' + val + ';';
                }
            );
            return competences;
        }
    };
}();


CID.Dashboard = function()
{
    return {
        init: function()
        {
            $( '.toDo h3' ).click( function()
                {
                    $(this).next().toggle();
                }
            );
        }
    };
}();


CID.Matching = function()
{
    return {
        init: function()
        {
            $('.columnOptions').multiSelect( 
                { 
                    selectAll: false,
                    useStaticText: 'Meer/minder kolommen (max. 5)'
                }, 
                function()
                {
                    $( '.column' + $(this).val() ).toggle();
                    var values = new Array();
                    $(this).parent().parent().find( ':checked' ).each( function()
                        {
                            values.push( $(this).val() );
                        }
                    );
                   $.cookie( 'MatchingGridColumns', values.join( ',' ), { path: '/', expires: 200 } );
                }            
            );
            
            this.readCookie();           
        },
        readCookie: function()
        {
            var cookieValue = $.cookie( 'MatchingGridColumns' );
            if( cookieValue == null ) return;
            
            var i = $( '.configColumn' ).size() - 1;
            var values = cookieValue.split( ',' );
            while( i >= 0 )
            {
                var column = $( '.column' + i );
                var checkbox = $( '.multiSelectOptions :nth-child(' + (i + 1).toString() + ') input:eq(0)' );                
                if( jQuery.inArray( i.toString(), values ) != -1 )
                {
                    column.show();
                    checkbox.attr( 'checked', 'checked' ).attr('disabled', '').parent().removeClass('disabled');
                }
                else
                {
                    column.hide();
                    checkbox.attr( 'checked', '' ).attr('disabled', 'disabled').parent().addClass('disabled');
                }
                i--;
            }          
        }
    };
}();


CID.Register = function()
{
    return {
        options: {},
        init: function( options )
        {
            this.options = options;
            
            $( this.options.toggleTermsSelector ).click( this.toggleTerms );
        },
        validateTerms: function( source, args )
        {
			var checkBox = document.getElementById( this.options.termsCheckboxId );
			args.IsValid = checkBox == null || checkBox.checked;
        },
        toggleTerms: function()
        {
            $( CID.Register.options.overlaySelector ).toggle();
            return false;
        }        
    };
}();


CID.Resume = function()
{
    return {
        init: function()
        {
            $( '.dtEndToggle input' ).click( this.toggleEndDate );
        },
        toggleEndDate: function()
        {
            $(this).parent().prev().toggle( $(this).is( ':checked' ));
        }
    };
}();


CID.Setup = function()
{
    return {
        init: function()
        {
            $( '#fieldActive input' ).click( this.toggleEndDate );
            $( '#fieldActive input' ).triggerHandler( 'click' );
        },
        toggleEndDate: function()
        {
			var checked = $(this).is( ':checked' );
            if( checked )
            {
                $( '.until' ).hide().next().hide();
            }
            else
            {
                $( '.until' ).show().next().show();
            }
        }
    };
}();


CID.GeneralSkills = function()
{
    return {
        init: function()
        {
            $( '#competenceForm ol' ).sortable( {
                start: function( event, ui )
                {
                    ui.item.addClass( 'ui-sortable-highlight' );
                },
                stop: function( event, ui )
                {
                    ui.item.find( 'span' ).animate( { backgroundColor: '#c8dbf6', color: '#4d4d4d' }, 1000 );
                    ui.item.removeClass( 'ui-sortable-highlight' );
                    $(this).find( 'li input.order' ).each( function( i )
                        {
                            $(this).parent().addClass( 'item' + (i + 1).toString() ).css( 'background-position', '-' + (30 * i).toString() + 'px' );
                            $(this).val( (i + 1).toString() );
                        }
                    );
                }
            });
            $( '.edit' ).click( function()
                {
                    $( '#competenceView' ).hide();
                    $( '#competenceForm' ).show().removeClass( 'completed' );
                }
            );
        }
    };
}();


/* All below is obsolete: remove || rewrite */
function validateEndDate(activeCheckBoxId, endDateTextBoxId)
{
    var cbActive = document.getElementById(activeCheckBoxId);
    var tbEndDate = document.getElementById(endDateTextBoxId);
    
    if (!cbActive.checked && tbEndDate.value.length <= 0)
    {
        return false;
    }
    else
    {
        return true;
    }    
}

function validatePostCode(postCodeNumeric, postCodeAlphaNumeric)
{
    var tbPostCodeNumeric = document.getElementById(postCodeNumeric);
    var tbpostCodeAlphaNumeric = document.getElementById(postCodeAlphaNumeric);
    
    
    if ( tbPostCodeNumeric.value != '' && tbpostCodeAlphaNumeric.value != '')
    {
        return true;
    }
    else
    {
        return false;
    }
}

//TextAread Javascript Max chars
function maxlength(element, maxvalue)
 {
 var q = eval("document.getElementById('" + element + "').value.length");
 var r = q - maxvalue;
// var msg = localizationText.replace("{0}",maxvalue);
 
 if (q > maxvalue)
    { 
    //eval("document.form1."+errorLabel+".value") = msg;
    return false;
    }
    else
    {
    return true;
    }
 }
