/**
 * @filename    ms-functions.js
 * framework    jquery 1.4.2
 *
 * @author      Marko Stutz
 * @date        28.10.2010
 * @version     2.0.1
 *
**/

/* ms plugin's */
(function($){
    
    $.fn.msFormInputMsg = function(options){
        options = $.extend({
            defaultText: null,
            clsDefault: 'input-hasdefault',
            clsFocus: 'input-hasfocus',
            onFocusIn: function(opts){
                if( opts.defaultText && $(this).val() == opts.defaultText ){
                    $(this).val('').removeClass(opts.clsDefault);
                }
                $(this).addClass(opts.clsFocus);
            },
            onFocusOut: function(opts){
                if( opts.defaultText && $.trim( $(this).val() ) == '' ){
                    $(this).val(opts.defaultText).addClass(opts.clsDefault);
                }
                $(this).removeClass(opts.clsFocus);
            },
            onSubmit: function(element, opts){
                if( $(element).val() == opts.defaultText ) $(element).val('');
            }
        }, options);
        
        return this.each(function(index, element){
            var opts = $.extend(options,{
                defaultText: options.defaultText || $(element).attr('title')
            });
            if( opts.defaultText && ( !$(element).val() || $.trim( $(element).val() ) == '' ) ){
                $(element).val(opts.defaultText).addClass(opts.clsDefault);
            }
            $(element).focusin(function(){
                if( $.isFunction(opts.onFocusIn) ){
                    opts.onFocusIn.call(this, opts);
                }
            }).focusout(function(){
                if( $.isFunction(opts.onFocusOut) ){
                    opts.onFocusOut.call(this, opts);
                }
            });
            
            if( $.isFunction(options.onSubmit) ){
                $(element).parents('form:first').submit(function(){
                    return opts.onSubmit.call(this, element, opts);
                });
            }
        });
    };

    /* script on the basis of jQuery-Plugin "pngFix"
     * Version: 1.2, 09.03.2009
     * by Andreas Eberhard, andreas.eberhard@gmail.com
     * http://jquery.andreaseberhard.de/
     *
     * modified: 30.03.2010 by Marko Stutz
     */
    $.fn.pngFix = function(options){
        
        options = $.extend({blankgif: 'leer.gif'}, options);
        
        if($.browser.msie && ( ($.browser.version == '5.5') || ($.browser.version == '6.0') )){
            $(this).each(function(i, element){
                if(element.tagName.toLowerCase() == 'img'){
                    $(element).attr('width', $(element).width() );
                    $(element).attr('height', $(element).height() );
                    var sHtml = '<span ';
                    sHtml += ($(element).attr('id')) ? 'id="' + $(element).attr('id') + '" ' : '';
                    sHtml += ($(element).attr('class')) ? 'class="' + $(element).attr('class') + '" ' : '';
                    sHtml += ($(element).attr('title')) ? 'title="' + $(element).attr('title') + '" ' : '';
                    sHtml += ($(element).attr('alt')) ? 'alt="' + $(element).attr('alt') + '" ' : '';
                    sHtml += 'style="position:relative;display:inline-block;background:transparent;';
                    sHtml += ($(element).attr('align')) ? 'float:' + $(element).attr('align') + ';' : '';
                    sHtml += ($(element).parent().attr('href')) ? 'cursor:hand;' : '';
                    sHtml += 'width:' + $(element).width() + 'px;' + 'height:' + $(element).height() + 'px;';
                    sHtml += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + $(element).attr('src') + '\', sizingMethod=\'scale\');"></span>';
                    $(element).hide().after(sHtml);
                }
                if(element.tagName.toLowerCase() == 'input'){
                    var bgIMG = $(element).attr('src');
                    element.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
                    $(element).attr('src', options.blankgif);
                }
            });
        }
    };
    
    // toolbar menu
    $.fn.msMenu = function(options){
        
        options = $.extend({
            timeout: 400,
            rootClass: 'js-toolbar-rootmenu',
            hoverClass: 'js-menu-hover'
        }, options);
        
        return this.each(function(index, menu){
            menu.actRoot = null;
            
            menu.id = 'id-ms-menu-'+index;
            menu.closeActRoot = function(root){
                if(menu.actRoot && menu.actRoot.id != root.id){
                    menu.actRoot.close();
                }
            };
            menu.roots = $(menu).find('li.'+options.rootClass);
            
            menu.roots.each(function(i, root){
                root.timer = null;
                root.id = 'id-ms-menu-'+index+'-'+i;
                root.close = function(){
                    $(root).removeClass(options.hoverClass);
                };
                
                $(root).find('a:eq(0)').hover(
                    function(){
                        if(root.timer){
                            clearTimeout(root.timer);
                            root.timer = null;
                        }
                        menu.closeActRoot(root);
                        $(root).addClass(options.hoverClass);
                        menu.actRoot = root;
                    },
                    function(){root.timer = setTimeout( root.close, options.timeout);}
                );
                $(root).find('ul').hover(
                    function(){
                        if(root.timer){
                            clearTimeout(root.timer);
                            root.timer = null;
                        }
                    },
                    function(){root.timer = setTimeout( root.close, options.timeout);}
                )
                if($.browser.msie && ( ($.browser.version == '5.5') || ($.browser.version == '6.0') )){
                    var w = -1;
                    var t = $(root).find('.js-toolbar-submenu a');
                    $(root).find('.js-toolbar-submenu a').each(function(){
                        var aw = $(this).outerWidth();
                        if( aw > w ){
                            w = aw;
                        }
                    });
                    $(root).find('ul').css('width', w+'px').addClass('nav-shadow-off');
                    $(root).find('.js-toolbar-submenu a').css('display', 'block');
                }
            });
            
        });
    };
    
    $.fn.msMaxHeight = function(options){
        options = $.extend({
            set: false,
            max: 0
        }, options);
        this.each(function(){
            options.max = Math.max( options.max, $(this).height() );
        });
        if(options.set && options.max > 0){
            this.each(function(){
                $(this).height(options.max);
            });
        }
        return options.max;
    };
})(jQuery);


/* ms base object */
var ms = {};
ms.ready = null;
ms.base = {
    url: '',
    actCat: null,
    actRootCat: null,
    actClickCat: null,
    actPath: null,
    plugins: {
        blPrettyPhoto: false,
        blMSIconGallery: false,
        blAccordion: false,
        blTree: false,
        blCycle: false,
        blRssFeed: false
    }
};

ms.images = {
    preImages: new Array(),
    aSystem: new Array(
        ms.base.url+'out/basic/img/loading.gif',
        ms.base.url+'out/basic/src/jquery-dynatree/css/ltWait.gif'
    ),
    preloader: function(a){
        for(var i = 0; i < a.length; i++){
            var img = new Image();
            img.src = a[i];
            ms.images.preImages.push( img );
        }
    }
};

ms.window = {
    ajax: null,
    box: null,
    contentId: null,
    getHtmlWait: function(style, text){
        style = style || 'padding:20px; text-align:center;';
        text = text || '&nbsp;&nbsp;&nbsp;Loading...';
        return '<div style="' + style + '"><img src="'+ms.base.url+'out/basic/img/loading.gif" height="32" width="32">' + text + '</div>';
    },
    initAjax: function(options){
        $('#id-ajax-dialog').dialog(options);
        return $('#id-ajax-dialog');
    },
    initBox: function(id, options){
        $('#'+id).dialog(options);
        return $('#'+id);
    },
    show: function(typ, options){       
        $options = $.extend({
            id: 'noindex',
            url: '',
            modal: true,
            autoOpen: false,
            height: 500,
            width: 750,
            content: null
        }, options);
        if( typ == 'ajax' ){
            if( ms.window.ajax === null ){
                ms.window.contentId = null;
                ms.window.ajax = ms.window.initAjax({
                    modal: $options.modal,
                    autoOpen: false,
                    height: $options.height,
                    width: $options.width
                });
            }
            
            if( ms.window.contentId == $options.id && $options.id != 'noindex' ){
                // nicht neu laden
                ms.window.ajax.dialog('open');
            }else{
                var $content = ms.window.ajax.find('.js-ajax-content');
                $content.html( ms.window.getHtmlWait() );
                ms.window.ajax.dialog('open');
                $content.load($options.url);
            }
        }else if( typ == 'default' ){
            if( !$options.id || $options.id == 'noindex' ){
                return;
            }
            if( ms.window.box === null ){
                ms.window.box = ms.window.initBox($options.id, {
                    modal: $options.modal,
                    autoOpen: $options.autoOpen,
                    height: $options.height,
                    width: $options.width
                });
            }
            if( $options.content ){
                ms.window.box.find('.js-ajax-content').html( $options.content );
            }
            ms.window.box.dialog('open');
        }
    },
    setContentId: function(id){
        ms.window.contentId = id;
    },
    setTitle: function(typ, title){
        if( typ == 'ajax' && ms.window.ajax !== null ){
            ms.window.ajax.dialog('option', 'title', title );
        }
    }
};

ms.isset = function(v){
    eval('var r = window.' + v + ' === undefined ? false : true;');
    return r;
};

ms.empty = function(v){
    if( !v ) return true;
    if( typeof v == 'object' && $.isEmptyObject(v) ) return true;
    if( $.isArray(v) && v.length==0) return true;
    return false;
}

ms.animate = {
    showhide: function(id, value){
        var element = $('#'+id);
        if(element){
            if(value){
                element.show('fast');
            }else{
                element.hide('fast');
            }
        }
    },
    display: function(id, type){
        var element = $('#'+id);
        if(element){
            element.css('display', type);
        }
    }
};

ms.forms = {
    validEmail: function(m){
        var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return(regex.test(m));
    },
    validPwd: function(pwd){
        var regex = /^\w+$/;
        return(regex.test(pwd) && pwd.length >= 5  && pwd.length <= 20);
    },
    setCheckboxes: function(value, selector){
        $(selector).each(function(){
            this.checked = value;
        });
    },
    initPaymentForms: function(){
        aRadBut = $('#id-order-payment-payment :radio:checked');
        if(aRadBut.length == 1){
            return;
        }else if(aRadBut.length < 1){
            $('#id-order-payment-payment :radio:first').attr('checked','checked');
        }else if(aRadBut.length > 1){
            $('#id-order-payment-payment :radio:checked').removeAttr('checked');
            $('#id-order-payment-payment :radio:first').attr('checked','checked');
        }
    },
    selectPayment: function(rb, selector){
        $(selector).each(function(){
            if( this.id == rb ){
                this.checked = true;
            }else{
                this.checked = false;
            }
        });
    },
    submitActiveForm: function(selector){
        aRadBut = $(selector);
        if(aRadBut.length > 0){
            aRadBut[0].form.submit();
        }
    },
    changeUserAddress: function(select, options){
        var value = select.value;
        if(options){
            if( $.isArray(options.aset) ){
                // set values
                $.each(options.aset, function(index, set){
                    $('#'+set.id).val(set.value);
                });
            }
            if( !(value === '-1') ){
                // set cl and fnc, after reload form
                $('#'+options.form+' input[name="cl"]').val(options.cl);
                $('#'+options.form+' input[name="fnc"]').val(options.fnc);
                $('#'+options.form).submit();
            }
            if( !(value !== '-1') ){
                // clear all fields with oxaddress
                $('#'+options.form+' input[name*="oxaddress__"]').val('');
                $('#'+options.form+' select[name*="oxaddress__"]').each(function(){
                    this.item(0).selected = true;
                });
            }
        }
    }
};

