(function() {
    var Event = YAHOO.util.Event;
    var Dom = YAHOO.util.Dom;

    SportTypePromo = function(cfg) {
        
        // Calling parent constructor
        var fnSuperClass =  SportTypePromo.superclass.constructor;
        fnSuperClass.call(this, cfg);
        
        this.questionId = 0;
        this.resultScore = {
            'well-being'  : 0,
            'urban-fit'   : 0,
            'action-pure' : 0
        };
        
        // Start button
        Event.on(this.id + "-start-btn", "click", this.setQuestionPage, this, true);
        
        // Show win button
        Event.on(this.id + "-start-show-win-btn", "click", this.setWinPage, this, true);
        
        // Show start button
        Event.on(this.id + "-win-show-start-btn", "click", this.setStartPage, this, true);
        
        
        // Show/Hide win Siemens button
        Event.on(this.id + "-win-show-siemens-btn", "click", this.showWinSiemens, this, true);
        Event.on(this.id + "-win-hide-siemens-btn", "click", this.hideWinSiemens, this, true);

        
        
        // Question
        Event.on(this.id + "-question-body", "click", this.onAnswerClick, this, true);
        Event.on(this.id + "-next-btn", "click", this.onNextQuestion, this, true);
        Event.on(this.id + "-show-result-btn", "click", this.onNextQuestion, this, true);
        
        // Participation form button
        Event.on(this.id + "-form-btn", "click", this.showForm, this, true);
        
        
        // Submit form button
        Event.on(this.id + "-submit-btn", "click", this.onSubmit, this, true);
        
        
    };

    YAHOO.extend(SportTypePromo, Sweepstake, {
        onAnswerClick : function(e) {
            e = Event.getEvent(e);
            Event.stopEvent(e);
            var target = Event.getTarget(e);
            
            var listItems = target.parentNode.parentNode.childNodes;
            for(var i = 0; i < listItems.length; i++)
            {
                if(listItems[i] == target.parentNode)
                {
                    Dom.addClass(listItems[i], this.id + '-checked');
                }
                else
                {
                    Dom.removeClass(listItems[i], this.id + '-checked');        			
                }
            }
            
            
        },
        onNextQuestion : function() {
            this.stopEvent();
            
            var formCheck = new FormCheck(this.formCfg);
            var formVars = formCheck.getFormVars(this.id + "-userform");
            
            var checkedItems = Dom.getElementsByClassName('swp2010_sporttyp-checked');
            
            if(checkedItems.length == 0)
            {
                alert(this.formCfg.errorTexts.answer.empty);
            }
            else
            {
                var answerIndex = checkedItems[0].firstChild.id.match(/[0-9]+$/)[0];
                var question = SportTypePromoConfig.questions[this.questionId];
                var answerType = question.answers[answerIndex].type;
                this.resultScore[answerType]++;
                
                this.questionId++;
                if(this.questionId < SportTypePromoConfig.questions.length)
                {
                    this.showQuestion(this.questionId);    			
                }
                else
                {
                    
                    this.showResult();
                }
            }
        },
        onSubmit : function() {
            this.stopEvent();
            
            var that = this;
            
            if(this.isSending)
            {
                return;
            }
            
            this.isSending = true;
            
            Perwoll.sendForm(this.formCfg, this.id + "-userform", "/?ajax=1&ext=sweepstake&sweepstake=" + this.id + "&pid=" + this.pid, function(result) {
                
                that.isSending = false;
                
                if(result.success)
                {
                    that.setPage(that.id + "-finish");
                }
            });
        },
        showWinSiemens : function () {
            var element = document.getElementById(this.id + "-win-siemens");
            element.style.display = 'block';
        },
        hideWinSiemens : function () {
            var element = document.getElementById(this.id + "-win-siemens");
            element.style.display = 'none';
        },
        setStartPage : function () {
            this.setPage(this.id + "-start");
        },
        setWinPage : function () {
            this.setPage(this.id + "-win");  
        },
        setQuestionPage : function() {
            this.setPage(this.id + "-question");
            
            this.showQuestion(this.questionId);
        },
        showQuestion : function(questionId) {
            var question = SportTypePromoConfig.questions[questionId];
            
            Dom.get(this.id + '-question-title').innerHTML = question.name;
            
            var bodyContent = ['<ul id="' + this.id + '-answers-ul' + '">'];
            for(var i = 0; i < question.answers.length; i++)
            {
                bodyContent.push('<li>');
                bodyContent.push('<a href="#" id="' + this.id + '-answer' + i + '">');
                bodyContent.push(question.answers[i].name + '</a></li>');    			
            }
            bodyContent.push('</ul>');
            Dom.get(this.id + '-question-body').innerHTML = bodyContent.join("");
            
            if(questionId == SportTypePromoConfig.questions.length - 1)
            {
                Dom.get(this.id + '-show-result-btn').style.visibility = "visible";
                Dom.get(this.id + '-next-btn').style.visibility = "hidden";
            }
            
        },
        showForm : function() {
            
            Event.on(this.id + '-privacy-link', 'click', function(e) {
                Event.stopPropagation(e);
            }, this);
            
            Event.on(this.id + "-terms-link", "click", this.showLayer, '-terms-layer', this);
            
            var privacyLinks = Dom.getElementsByClassName(this.id + "-privacy-ext-link");
            for(var i = 0; i < privacyLinks.length; i++)
            {
                Event.on(privacyLinks[i], "click", this.showLayer, '-privacy-layer', this);
            }
            
            this.setPage(this.id + '-form');
            
        },
        showResult : function() {
            this.setPage(this.id + "-result");
            
            var max = {
                type  : "",
                count : 0
            };
            
            for(var key in this.resultScore)
            {
                if(this.resultScore[key] > max.count)
                {
                    max.count = this.resultScore[key];
                    max.type = key;
                }
            }
            
            var element = document.getElementById(this.id + '-lifetimes-' + max.type);
            
            element.style.display = 'block';

            
            
            var resultType = max.type;
            
            // Setting images
            Dom.get(this.id + '-result-hd-img').innerHTML = '<img src="' + this.relPath + 'images/result/' + resultType + '-hd.png" class="ie-png" width="369" height="42" />';
            Dom.get(this.id + '-result-img').innerHTML = '<img src="' + this.relPath + 'images/result/' + resultType + '.png" class="ie-png" width="349" height="349" />';
            Dom.get(this.id + '-form-result-img').innerHTML = '<img src="' + this.relPath + 'images/result/' + resultType + '-result.png" class="ie-png" width="349" height="349" />';
            Dom.get(this.id + '-result-text').innerHTML = SportTypePromoConfig.results[resultType].text;
            Dom.get(this.id + '-result-tip').innerHTML  = SportTypePromoConfig.results[resultType].tip;
            Dom.get(this.id + '-size').value = resultType;
        }
    });

})();


// Creating instance of sweepstake
var promo = new SportTypePromo(sweepstakeCfg);
