jQuery.noConflict();

var MEVS = {
    set: function ( property) {
        for (key in property) {
            MEVS[key] = property[key];
        }
        
        MEVS.init();
    },

    get: function (key) {
        if(!MEVS[key]) {
            return false;
        }
        return MEVS[key];
    },

    setPageLink: function (lang_link) {
        jQuery("#switchlang").attr({ 
            href: lang_link
        });
    },

    setKey: function(key, value) {
        MEVS[key] = value;
    },
    
    getLang: function() {
        return MEVS.get('lang');
    },
    
    getText: function(text) {
        return MEVS.get('lang_'+ text +'');
    },
    
    highlightSubnavi: function (subnavi) {
        jQuery('#subnavi_'+ subnavi + '').addClass('active');
    },
    
    highlightPageNavi: function (item) {
        jQuery('#i_'+ item + '').addClass('active');
        jQuery('#a_'+ item + '').addClass('active');
    },
    
    updateBreadcrumb: function() {
        if( MEVS.get('treepath') != false ) {
            if( MEVS.get('treepath').length > 0 ) {
                jQuery.each(MEVS.get('treepath'),function(i,item) {
                    if( item.href != '' && item.text != '' ) {
                        updateBreadcrumb(item.index, item.href, item.text, MEVS.get('base_href'));
                    }
                });
            }
        }
    },
    
    checkField: function (j,field_name) {
        if( eval('j.'+ field_name +'') == 1 ) {
            jQuery('#'+ field_name + '').addClass('fielderror');
            jQuery('label[for="'+ field_name +'"]').find('span').addClass('texterror');
        } else {
            jQuery('#'+ field_name + '').removeClass('fielderror');
            jQuery('label[for="'+ field_name +'"]').find('span').removeClass('texterror');
        }
    },
    
    checkFields: function(j,fields) {
        for(i = 0; i < fields.length; i++) {
            MEVS.checkField(j, fields[i]);
        }
    },
    
    setErrorField: function(field_name) {
        jQuery('#'+ field_name + '').addClass('fielderror');
        jQuery('label[for="'+ field_name +'"]').find('span').addClass('texterror');
    },
    
    checkLogin: function() {
        if( MEVS.get('login') == false ) {
            jQuery("#authenticate").html(MEVS.getText('session_timeout'));
            jQuery('#logoutDialog').jqmShow();
        }
    },
    
    activateBasket: function() {
        jQuery('#basketDialog').jqm({
            onShow: function(h) {
                h.w.slideDown('slow');
                var basketJobs
            },
            onHide: function(h) {
                h.w.slideUp('slow',function() { if(h.o) h.o.remove(); });
                window.clearInterval(basketJobs);
            } 
        });
    },
    
    init: function() {
        
        // init ajaxSetup
        jQuery.ajaxSetup({
            timeout: 7000,
            type: 'POST',
            dataType: 'json'
        });

        // load lang text
        jQuery.each(lang,function(i,item) {
            MEVS['lang_'+ item.title +''] = item.text;
        });
        
        // init product_area dropdown
        if( MEVS.get('init_product_area') == false || MEVS.get('init_product_area') == '') {
            jQuery('#product_area').attr('selectedIndex',0);
        }

        jQuery('#product_id').attr('selectedIndex',0);

        // init product search form
        jQuery('#product_area_search').attr('selectedIndex',0);
        jQuery('#product_id_search').attr('selectedIndex',0);
        
        // highlight navi
        if( MEVS.get('navi') != false ) {
            MEVS.highlightSubnavi( MEVS.get('navi') );
        }
    
        // highlight subnavi
        if( MEVS.get('subnavi') != false ) {
            MEVS.highlightPageNavi( MEVS.get('subnavi'));
        }
        
        // get lang link
        if( MEVS.get('page') != false ) {
            MEVS.setPageLink( MEVS.get('page') );
        }

        // init alert dialog
        jQuery('#alertDialog').jqm({overlay: 0, modal: true, trigger: false});
                    
        // check authenticate
        if( MEVS.get('login') != false ) {

            // enable logout          
            jQuery('#logoutDialog').jqm({
                trigger:'#logoutUser',
                onShow: function(h) {
                    h.w.slideDown();
                },
                onHide: function(h) {
                    h.w.slideUp("fast",function() { if(h.o) h.o.remove(); });
                } 
            });
            
            jQuery('#handleLogout').click(function() {
                logoutUser();
            });
            
            jQuery('#cancelLogout').click(function() {
                jQuery('#logoutDialog').jqmHide();
            });
            
        } else {
            // enable login
            jQuery('#loginDialog').jqm({
                onShow: function(h) {
                    h.w.slideDown('fast', function() {
                        window.setTimeout("redirectPage('')", 2000);
                    });
                    
                },
                onHide: function(h) {
                    h.w.slideUp("fast",function() { if(h.o) h.o.remove(); });
                    if( MEVS.get('error_code') == 95 ) {
                        jQuery('#username').focus();
                    } else if( MEVS.get('error_code') == 94 ) {
                        jQuery('#password').focus();
                    } else if( MEVS.get('error_code') == 1 ) {
                        if( jQuery('#username').val() == '' ) {
                            jQuery('#username').focus();
                        } else {
                            jQuery('#password').focus();
                        }
                    }
                } 
            });
      
            jQuery('#loginUser').click(function() {
                loginUser();
            }); 
            
            // check keycode to submit the form
            jQuery("#username").keydown(
                function(e){
                    var keyCode = e.charCode || e.keyCode || 0;
                    if( keyCode == 13 ) {
                        loginUser();
                    }
                }
            );
            
            jQuery("#password").keydown(
                function(e){
                    var keyCode = e.charCode || e.keyCode || 0;
                    if( keyCode == 13 ) {
                        loginUser();
                    }
                }
            );

        }

    }
};