﻿/*
    styleSettings 
    responsible for controlling / setting font and layout options
*/

/* object */
this.styleSettings = function(){

    // consts
    var FONT_0 = 0;
    var FONT_1 = 1;
    var LAYOUT_0 = 0;
    var LAYOUT_1 = 1;
    var LAYOUT_2 = 2;

    // locals
    var _wgid;
    
    
    /* public props */
    
    // layout (int)
    this.layout;
    
    // font (int)
    this.font;

    // client root url (string)
    this.clientRootUrl;

    // current client version (string)
    this.clientVersion;

    // onChange (function)
    // hook fn to execute after settings are loaded / changed
    // + passed a single parameter, settings (this)
    this.onChange;

    // panelStateController (panelStateController()) initialised in loadUIPreferences();
    this.panelStateController;
    
    /* public methods */
    
    // load up current UI preferences from the server
    // + wgid (long) web gateway id
    // + cb (function) to execute on complete
    this.loadUIPreferences = function(wgid, callback){
    
        _wgid = wgid;
        
        $mb.jsonLoader.loadUIPreferences(function(data){
        
            var pb = new panelBuilder(wgid);
                
            // init the side panels
            pb.initialiseSidePanels();

            // my account
            pb.buildMyAccount(data);
            
            // TODO - we're calling back before we've finished ops here and if we move this to the end
            // we don't get our header loaded properly - wtf? :|
            
            // panel states
            getPanelStateController(data.InnerData).init(callback);
            
            
            var settings = $mb.styleSettings;

            // font and layout
            settings.clientRootUrl = data.InnerData.ClientRootUrl;
            settings.clientVersion = data.InnerData.ClientVersion;
						
						// to avoid having to override css styles we just ignore the line below
            // settings.setFontAndLayout(data.InnerData.WebFont, data.InnerData.WebLayout, false);
            
            var CSS_FORMAT = settings.clientRootUrl + 'css/{0}?v=' + settings.clientVersion;
            
            // ie css
            if($.browser.msie){
                $mb.addCss(doStringFormat(CSS_FORMAT, ['ie.css']));
                if($.browser.version < 8)
                    $mb.addCss(doStringFormat(CSS_FORMAT, ['ie7.css']));
                if($.browser.version < 7)
                    $mb.addCss(doStringFormat(CSS_FORMAT, ['ie6.css']));
            };
            
            // safari css
            if($.browser.safari){
                $mb.addCss(doStringFormat(CSS_FORMAT, ['safari.css']));
            };
            
            // mozilla
            if($.browser.mozilla && $.browser.version < "1.9.1"){
                // FF3.0 or below... (3.5 = version 1.9.1)
                $mb.addCss(doStringFormat(CSS_FORMAT, ['ff3.0.css']));
            };
            
            
            // TODO - see case 1667 - still not working after clearing cookies :|
            /*
            var showUrl = doStringFormat(CSS_FORMAT, ['show.css']);
            $mb.addCss(showUrl, "lShow");
            */
           
            
        });
    };
    
    // set the current layout / font
    // + layout (int, or null)
    // + font (int, or null)
    // + withCallBack (bool) - send confirmation to server
    this.setFontAndLayout = function(font, layout, withCallBack){
    
        // ie6 overrides
        var isIE6 = $.browser.msie && $.browser.version < 7;
        font = isIE6 ? FONT_1 : font;
        layout = isIE6 ? LAYOUT_0 : layout;
        
        if(layout != null) this.layout = layout;
        if(font != null) this.font = font;
        
        if(true == withCallBack){
            var settings = this;
            var url = $mb.urlBuilder.buildJsonUrl('cmd=SetFontAndLayout&wl=' + this.layout + '&wf=' + this.font + '&wgid=' + _wgid);
            $.getJSON(url, function(data){
                if(true == canHandleJSON(data)){ settings._doOnChange(); };
            });
        }else{
            this._doOnChange();
        };
        
    };
    
    
    this._doOnChange = function(){
    
        var FORMAT = this.clientRootUrl + 'css/{0}' + '.css?v=' + this.clientVersion;
        var fontUrl = doStringFormat(FORMAT, ['font_' + this.font]);
        var layoutUrl = doStringFormat(FORMAT, ['layout_' + this.layout]);
        
        $mb.addCss(fontUrl, "lFont");
        $mb.addCss(layoutUrl, "lLayout");
        
        if($.browser.msie)
        {
            var layoutRoot = this.clientRootUrl + 'css/layout_' + this.layout;
            $mb.addCss(layoutRoot + '_ie.css?v=' + this.clientVersion);
            if($.browser.version < 8) 
                $mb.addCss(layoutRoot + '_ie7.css?v=' + this.clientVersion);
            if($.browser.version < 7){
                $mb.addCss(layoutRoot + '_ie6.css?v=' + this.clientVersion);
            };
        };
        
        
		if($.browser.msie && $.browser.version < 7)
        {
            $mb.addCss('/mangolab.mangobasket.web.ui/store/merchants/0000100001/includes/header/ie6.css');
        };

        if(typeof(this.onChange) == 'function'){
            this.onChange(this);
        };

    };
    

};