
function Colorator(ccLink, mode, model, exterior, interior, wheels, top, contrastcolor, modelyr) {
    var self = this;
    this.coloratorModes = new EnumColoratorModes();
    fbLog('modelyr (PS5-Parameter): ' + modelyr);
    this.modelyear = (modelyr != '' ? modelyr : '2010');
    fbLog('modelyear: ' + this.modelyear);
    this.Pool = "";
    this.Language = "";
    this.m_currentLink = null;
    this.m_model = model;
    this.m_selectedExterior = exterior;
    this.m_selectedInterior = interior;
    this.m_selectedWheels = wheels;
    this.m_top = top;
    this.m_contrastColor = contrastcolor;
    this.m_ccLink = ccLink;
    this.isFront = true;

    

    /// internal function parseMode:
    // wird benutzt um den gelieferten Mode-Textparameter 
    // in eine Enumaration zu parsen.
    this.parseMode = function(modeString) {
        switch (modeString) {
            case 'interior':
                return this.coloratorModes.Interieur;
                break;
            case 'exterior':
                return this.coloratorModes.Exterieur;
                break;
            case 'wheels':
                return this.coloratorModes.Wheels;
                break;
            case 'contrastcolor':
                return this.coloratorModes.ContrastColor;
                break;
            default:
                return coloratorModes.Exterieur;
        }
    }


    this.Mode = (mode == undefined || mode == '')
                    ? this.coloratorModes.Exterieur
                    : this.parseMode(mode);
    this.init = function () {
        this.initHtml();
    }

    this.initHtml = function () {
        log('************************** INIT HTML ********************************* ,  model: ' + this.m_model);
        this.replacePaintedWheel('445', this.m_model);

    }



    this.toggleFrontBack = function() {
        if (self.isFront) {
            doCall('rear_on');
            self.isFront = false;
        }
        else {
            doCall('front_on');
            self.isFront = true;
        }
    }

    this.setupGUI = function() {

        // hide the flash GUI.
        // so that we can show the HTML ones.
        doCall('hidegui_on');

        // show top only when:
        // - a soft top is defined for the current model
        // - the current mode is Exterieur
        if (self.m_top != '' && self.Mode == self.coloratorModes.Exterieur) {
            $('.gui .buttons_low .softtop,.gui .buttons_low .spacer').show();
        }
        // show front/rear buttons only in exterieur mode.
        if (self.Mode == self.coloratorModes.Exterieur) {
            $('.gui .arrows').show();
        }

        //buttons und arrows
        $('.gui .arrows *,.gui .buttons_low *').hover(function() { $(this).addClass('guihover'); }, function() { $(this).removeClass('guihover'); });
        $('.gui .softtop').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });

        // Beide Pfeile tauschen die Ansicht aus.
        $('.gui .front, .gui .rear').click(self.toggleFrontBack);

        $('.gui .softtop .on').click(function() {
            doCall('softtop_on');
            $(this).hide();
            $('.gui .softtop .off').show();
        });

        $('.gui .softtop .off').click(function() {
            doCall('softtop_off');
            $(this).hide();
            $('.gui .softtop .on').show();
        });

        $('.gui .zoom')
        .click(function() {
            doCall('zoom_on');
        });

    }

    this.doColorConfigLink = function(link) {
        link = link + 'int=' + this.m_selectedInterior + '&ext=' + this.m_selectedExterior + '&whl=' + this.m_selectedWheels + '&contrast=' + this.m_contrastColor;
//        fbLog("doColorConfigLink()");
//        fbLog("------------------");
        document.location.href = link;
    };

    this.getFlashLanguage = function() {
        switch (this.Pool) {
            case "usa":
                return "bbk";
                break;
            case "germany":
                return "bbk";
                break;
            default:
                return "bbk";
        }
    }

    this.initFlash = function () {
        log("initFlash entered");
        this.setupGUI();

        this.replacePaintedWheelFlash(wheels, model);

        fbLog('model: ' + model);
        fbLog('modelyear: ' + this.modelyear);
        fbLog('language: ' + this.getFlashLanguage());
        fbLog('mode: ' + this.Mode);
        fbLog('exterior: ' + exterior);
        fbLog('interior: ' + interior);
        fbLog('wheels: ' + wheels);
        fbLog('contrastcolor: ' + contrastcolor);
        fbLog('top: ' + top);

        doCall('text_' + this.getFlashLanguage());
        if (model != '') doCall(model + '_load');
        if (this.modelyear != '') doCall('my_' + this.modelyear);
        if (exterior != '') doCall(exterior + '_on');
        if (interior != '') doCall(interior + '_on');
        if (wheels != '') {
            doCall(wheels + '_on');            
        }

        if (top != '') {
            doCall(top + '_on');

        }
        if (contrastcolor != '') doCall(contrastcolor + '_on');

        if (this.Mode != this.coloratorModes.Exterieur) {
            //window.setTimeout(function() {
            doCall('in_on');
            //}, 300);
        }

        doCall('background_on');

        this.doExceptions();
    }

    this.doExceptions = function() {
        switch (this.m_model) {
            case '987720':
                if (self.Mode == this.coloratorModes.Interieur) {
                    doCall('P03_on'); // choose default sport bucket seats for Boxster Spyder. TODO implement this feature as control parameter.
                }
                break;

            default:
                break;

        }
    }

    this.replacePaintedWheel = function (wheelId, modelId) {
        var fName = wheelId + '.jpg';
        var path = '/images/colorconfigurator/thumb-wheels/';
        switch (wheelId) {
            case '445':
                if (/^(9871..|9873..)$/.test(modelId)) {

                    log('  - **replacePaintedWheel** - ');
                    fName = '445_987.jpg';
                    var imgPath = path + fName;
                    log('imgPath: ' + imgPath);
                    var elm = $('ul.categoryselection #link445 img');
                    if (elm.data('wheelreplaced') != true) {
                        elm.attr('src', imgPath).data('wheelreplaced', true);
                    }
                }
                break;
            default:
                log(' - replacePaintedWheel : other models');
                break;
        }
    }

    this.replacePaintedWheelFlash = function (wheelId, modelId) {
        switch (wheelId) {
            case '445':
                if (/^(9871..|9873..)$/.test(modelId)) {
                    log('  - **replacePaintedWheel Flash** - ');
                    doCall('XDA_on'); // black overlay.
                }
                else {
                    doCall('XDA_off');
                }
                break;
            default:
                log(' - replacePaintedWheel Flash : other models');
                doCall('XDA_off');
                break;
        }
    }

    this.changeActivation = function(newElemId, oldElemId) {
        var newElem = document.getElementById('link' + newElemId);
        var oldElem = document.getElementById('link' + oldElemId);

        if (newElem != null && oldElem != null) {
            oldElem.className = "colorselect";
            newElem.className = "colorselectselected selected";
            
            
        }
    }

    this.isBoxster = this.m_model == '987310' || this.m_model == '987320' || this.m_model == '987720';
    this.is987 = /^987.+/.test(this.m_model);
    log("this.isBoxster? : " + this.isBoxster);
    log("this.is987? : " + this.is987);

    this.changeImage = function (model, _mode, id) {
        doCall(id + '_on');
        var mode = this.parseMode(_mode);
        switch (mode) {
            case this.coloratorModes.Interieur:
                this.changeActivation(id, this.m_selectedInterior);
                this.m_selectedInterior = id;
                break;
            case this.coloratorModes.Exterieur:
                this.changeActivation(id, this.m_selectedExterior);
                this.m_selectedExterior = id;
                break;
            case this.coloratorModes.Wheels:
                this.changeActivation(id, this.m_selectedWheels);
                this.m_selectedWheels = id;

                this.replacePaintedWheelFlash(id, this.m_model);

                break;
            case this.coloratorModes.ContrastColor:
                this.changeActivation(id, this.m_contrastColor);
                this.m_contrastColor = id;
        }



        //fbLog(id + ' - ' + $('#link' + id + ' img').attr('title') + ' (' + id + '.jpg)');

    }

    this.openCCFromColorator = function() {
        var sWidth = 980;
        var sHeight = 680;
        sWidth -= g_is_ie ? 15 : 20;
        sHeight -= 25;
        ccLink = this.m_ccLink;
        if(ccLink.indexOf('?') > -1)ccLink += "&"
        else ccLink += "?"

        // RT Parameter dazu gekommen.
        ccLink +=     'RT=' + new Date().getTime() + 
                '&ORDERTYPE=' + this.m_model + 
                '&vLevel=2' +
                //'&hookURL=null' +
                '&option1=' + this.m_selectedInterior + 
                '&option2=' + this.m_selectedExterior + 
                '&option3=' + this.m_selectedWheels + 
                '&option4=' + this.m_contrastColor; 
                
      if (getScreenXYUrlParam())
        ccLink += getScreenXYUrlParam(); // default.js (BU -- 23.06.08 
      
      log('ccLink: ' + ccLink);
      gotoUrlNewWinSizeByName(ccLink, sWidth, sHeight, "PVA");
      }

}


function EnumColoratorModes() {
    this.Exterieur = 0;
    this.Interieur = 1;
    this.Wheels = 2;
    this.ContrastColor = 3;
}



//RTT ab hier
function RunFlashOld() {
    var prefix = "/all/visualizer-flash/";
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" WIDTH="680" HEIGHT="329" id="flash">');
    document.write('    <param NAME="base" VALUE="' + prefix + '" />');
    document.write('    <param NAME="movie" VALUE="' + prefix + 'trigger.swf" />');
    document.write('    <param NAME="quality" VALUE="high" />');
    document.write('    <param name="menu" value="false" /> ');
    document.write('    <param NAME="bgcolor" VALUE="#FFFFFF" />');
    document.write('    <embed base="' + prefix + '" src="' + prefix + 'trigger.swf" quality="high" menu="false" bgcolor="#FFFFFF"  WIDTH="680" HEIGHT="329" NAME="flash" swLiveConnect="true" TYPE="application/x-shockwave-flash" PLUGINSPAGE="//www.macromedia.com/go/getflashplayer"></embed>');
    document.write('</object>');
}

function RunFlash() {
    var prefix = "/all/visualizer-flash/";
    var params = {  base: prefix                  
                  , quality: 'high'
                  , menu: 'false'
                  , bgcolor: '#FFFFFF'
                  , wmode: 'transparent'
                  , name: 'flash'
                  , id: 'flash'
                  };
    var flashvars = {};
    var attributes = {};

    swfobject.embedSWF(prefix + 'plugin/trigger.swf', 'flash', 680, 329, "8", "", flashvars, params, attributes);

}

function RunZoomFlash() {
    log("RunZoomFlash..");
    var prefix = "/all/visualizer-flash/";
    var params = {  base: prefix                  
                  , quality: 'high'                  
                  , wmode: 'transparent'                  
                  };
    var flashvars = {};
    var attributes = {};
    
	swfobject.embedSWF(prefix + "plugin/zoom.swf", "zoomflashdiv", "924", "404", "8","", flashvars, params, attributes);

}






function doCall(value) {
    if (typeof (value) == 'undefined' || value == '') return;
    fbLog("doCall : " + value);

    // Exception for GT3RS Contrast colors. 
    // 510 is actually the default, but is not implemented.
    // so: 511_on => white, 511_off => red)
    if (value == '510_on') {
        value = '511_off';
    }
    
    var isPanamera = colorator.m_model.indexOf('970') == 0;
    var is991 = colorator.m_model.indexOf('991') == 0;
    

    var rubyRedCondition = !(isPanamera || is991);
    //var rubyRedCondition = true;
    // Exception ruby red (A6) must be mapped to L2
    if (value == 'A6_on' && rubyRedCondition) {
        value = 'L2_on';
        log('ruby red (A6) mapped to L2');
    }
   
       
    $("#flash")[0].doCall(value);
    
}



function controller_start() {
    newWindow = window.open('/all/visualizer-flash/flash.html', 'thewin', 'height=270,width=207,toolbar=no');
    newWindow.focus();
    alert("controller_start");
}

function controller_stop() {
    newWindow.close();
}

function sprache() {
    doCall('pfad_xml/');
    //colorator.initFlash();
}

function requirements() {
}

function model() {
    fbLog('model(): Modell geladen');
    colorator.initFlash();
}

function loaded_ex() {
    //fbLog('loaded_ex(): Exterior geladen');
}

function loaded_in() {
    log('loaded_in(): Interior geladen');
}

function flash_DoFSCommand(command, args) {
    fbLog("flash_doFSCommand - args: " + args);
    var flashObj = document.getElementById("flash");
    if (args == "sprache") {
        sprache();
    }
    if (args == "model") {
        model();
    }
    if (args == "Loaded_Ex") {
        loaded_ex();
    }
    if (args == "Loaded_In") {
        loaded_in();
    }
}




function visualizerSetCursor(obj, selected, isMouseOver) {
    if (selected == 'selected' || obj.className == 'colorselectselected selected') return;
    if (isMouseOver) {
        obj.className = 'colorselectselected';
        obj.style.cursor = 'pointer';
    }
    else {
        obj.className = 'colorselect';
        obj.style.cursor = 'default';
    }
}


function zoom_view() {
    log('zoom_view geklickt!');
	
	var footer = document.getElementById('footer');
	var parent = footer.parentNode;
	
	var middletop = document.clientHeight;
	var middleleft = document.clientWidth;
  
	var zoomdiv = document.createElement('div');
    zoomdiv.setAttribute('id', 'zoomdiv');
	//zoomdiv.setAttribute('style', 'position:absolute;border:1px solid #808080;z-index:1250;top:108px;left:70px;width:924px;height:404px;background-color:white;');	
	zoomdiv.style.cssText = "position:absolute;border:1px solid #808080;z-index:3000;top:108px;left:70px;width:924px;height:404px;background-color:white;";
	parent.appendChild(zoomdiv);
	
	var zoomflashdiv = document.createElement('div');
    zoomflashdiv.setAttribute('id', 'zoomflashdiv');
	//zoomflashdiv.setAttribute('style', 'position:relative;z-index:1260;width:924px;height:404px;');
	zoomflashdiv.style.cssText = "position:relative;z-index:1260;width:924px;height:404px;";
	zoomdiv.appendChild(zoomflashdiv);
	
	var backdiv = document.createElement('div');
    backdiv.setAttribute('id', 'backdiv');
	//backdiv.setAttribute('style', 'position:absolute;background-color:white;z-index:1249;top:0px;left:0px;width:100%;height:100%;opacity:0.75;filter: Alpha(opacity=75, finishopacity=75, style=2);');	
	backdiv.style.cssText = "position:absolute;background-color:white;z-index:2999;top:0px;left:0px;width:1064px;height:100%;opacity:0.75;filter: Alpha(opacity=75, finishopacity=75, style=2);";
	parent.appendChild(backdiv);
	
	var close = document.createElement('div');
	close.setAttribute('id', 'close');
	//close.setAttribute('style', 'position:absolute;top:6px;left:902px;z-index:1300');
	close.style.cssText = "position:absolute;top:6px;left:902px;z-index:1300";
	zoomdiv.appendChild(close);
	
	var closebutton = document.createElement('img');
	//closebutton.setAttribute('style', 'cursor:pointer');
	closebutton.style.cssText = "cursor:pointer";
	//closebutton.setAttribute('onclick', 'javascript:closeZoomdiv();');
	closebutton.onclick = closeZoomdiv;
	closebutton.setAttribute('src', '/images/close-button.gif');
	close.appendChild(closebutton);	
	
	RunZoomFlash();
	 
}

function closeZoomdiv()
{
	var footer = document.getElementById('footer');
	var parent = footer.parentNode;
	var zoomdiv = document.getElementById('zoomdiv');
	var backdiv = document.getElementById('backdiv');
	
	parent.removeChild(zoomdiv);
	parent.removeChild(backdiv);	
}

function fbLog(msg) {
    try {
        console.log(msg);
    }
    catch (e) {        
    } 
}



