(function($) {
    //Load Main Flash on HomePage
    $.fn.swfHome = function(lang, page) {
        var lang = lang || "en";
        return this.each(function(){
            var $choice = $("#choice", this);
            var params = { wmode: 'transparent' };
            var attributes = {};
            var flashvars = { man: $('.man', $choice).attr('href') , woman: $('.woman', $choice).attr('href'), doctor: $('.dr', $choice).attr('href') };
            swfobject.embedSWF(siteroot + "local/flash/"+lang+"/EDHome.swf", "swf_home", "704", "622", "8.0.0", false, flashvars, params, attributes);
            $("input", "form.quiz").one("click.cssForms", function(){
                eval(quizVote('/vote', page,'0',lang));
            });
        });
    };



$(function() {
    //Rollovers for Input
    $("input:image")
        .bind('mouseover focus', function(){this.src = this.src.replace(/(?:_o)?\.([a-z]*)$/i,"_o.$1");})
        .bind('mouseout blur',   function(){this.src = this.src.replace(/_o\./i,".");});
    
    //Custome radio button
    $("form.quiz, form.shim").each(function () {
        var $btnVote = $(".b_vote", this);
        $btnVote.css({ cursor:"default", opacity:.3 }).attr("disabled", "disabled");

        $(this).addClass('cssForms');
        // If a radio button is already selected at the page loading / reloading
        $(":radio:checked", this).parent().addClass("selected");
        
        $(":radio", this).parent()
            .addClass('cssRadio')  // allow custom css that degrade gracefully
            .bind('click.cssForms', function (){    // "merge" the input with his parent
                $(this).siblings(".cssRadio").removeClass('selected');
                $(this).addClass("selected");
                $("input", this).attr('checked', true);
                $btnVote.css({ cursor:"pointer", opacity:.99 }).removeAttr("disabled"); //Active button
            })
            .hover(    // Crossbrowswer solution for hovering.
                function() { $(this).addClass("over"); },
                function() { $(this).removeClass("over");}
            )
            .children("input")     // Allow the keyboard navigation
                .bind('focus.cssForms', function () {$(this).parent().addClass('focus');})
                .bind('blur.cssForms', function () {$(this).parent().removeClass('focus');});
    });

    //Add Flash animation on each Question
    var flashvars = {}; //text: $('#intro_content p').html()
    var params = { wmode: 'transparent' };
    var attributes = {};
    var $intro_content = $('#intro_content');
    if ($intro_content.size()) {
        $intro_content.prev().hide().animate({opacity: 1}, 1500).fadeIn(1000);
        swfobject.embedSWF($intro_content.attr('title'), "intro_content", "801", "326", "8.0.0", false, flashvars, params, attributes);
    };

    if(typeof sIFR == "function"){
        sIFR.replaceElement(named({
            sSelector:".sifr",
            sFlashSrc: siteroot + "local/flash/univers.swf",
            sColor: "#4c3828",
            sWmode: "transparent"
        }));
    }

    $('#frm_quiz, #frm_shim').submit(function () { return false; });
    
    //Show Print button
    $("#utilities").show();
    
     //Toggle content
    var $toggle = $("#toggle");
    $toggleContainers =[]; 
    $("a", $toggle).each(function(){ $toggleContainers.push($(this.hash).get(0)); }).bind("click", function(){ $($toggleContainers).filter(this.hash).toggle(); return false; });
    $($toggleContainers).hide();
    
    // IE6 bug fix for hovering over image links
    if($.browser.msie && $.browser.version == "6.0") {
        $('#nav a').bind('mouseout', function () {$('span', this).hide().show();});
    }
});

function quizVote(theUrl, questionBasenameUrl, questionId, culture) {
    var $quiz = $('#frm_quiz')
    var $answer = $("input:checked",$quiz);
    if ($answer.size() > 0) {
        var theVal = $answer.val();
        var theResult = 'wrong';
        if (theVal == $('#good_answer',$quiz).val()) { theResult = 'good'; }
        //uncomment google analytics event and test it!!:_trackEvent(category, action, optional_label, optional_value)
        //alert(questionId + "-" + questionBasenameUrl +" <> vote-" + questionId +" <> " + theResult + "-" + theVal +" <> null");
        pageTracker._trackEvent(
            questionId + '-' + questionBasenameUrl, //category
           'vote-' + questionId, //action 
           theResult + '-' + theVal
        );
        $('fieldset', $quiz).addClass("hide");
        $('#quiz_response', $quiz).removeClass('hide');
    }else{
        alert("Please answer the question.");
    }
}

window.quizVote = quizVote;

})(jQuery);


