jQuery.fn.COTY_formReturn = function(textData){
    //alert("Form has returned : "+textData);
    CG_triggerLightbox("/car-of-the-year/site/peoples-choice-2009/COTY_thanks_lightbox/?keepThis=true&amp;TB_iframe=true&amp;height=420&amp;width=416");
    $("#CG_coty_voting_flash").hide();
    $("#CG_coty_voting").addClass("CG_coty_votesubmitted");
};

jQuery.fn.COTY_formReturnEmail = function(textData){
    //alert("Email form has returned : "+textData);
    CG_triggerLightbox("/car-of-the-year/site/peoples-choice-2009/COTY_emailthanks_lightbox/?keepThis=true&amp;TB_iframe=true&amp;height=420&amp;width=416");
};

$(document).ready(function(){
    $("#COTY_voting_form").submit(function(){
        //Use ajax to send the information

        postData = {
            family:$("#COTY_family").val(),
            first:$("#COTY_first").val(),
            commercial:$("#COTY_commercial").val(),
            prestige:$("#COTY_prestige").val(),
            suv:$("#COTY_suv").val(),
            performance:$("#COTY_performance").val(),
            name:$("#COTY_name").val(),
            email:$("#COTY_email").val(),
            state:$("#COTY_state").val(),
            gender:$("input[name='COTY_gender']:checked").val(),
            age:$("#COTY_age").val(),
            overall_best:$("#COTY_overall_best").val(),
            overall_best_why:$("#COTY_overall_best_why").text(),
            form_source:$("#COTY_form_source").val(),
            antispam:$("#antispam").val()
        }

        //alert("About to post form");
        $.post("/car-of-the-year/site/peoples-choice-2009/COTY_php/", postData, jQuery.fn.COTY_formReturn, "text");

        //Don't actually submit the form
        return false;
    });

    $("#COTY_email_submit").click(function(){
        $("#COTY_emailfriend").submit();
    });


    $("#COTY_emailfriend").submit(function(){
        //Use ajax to send the information

        postData = {
            your_name:$("#your_name").val(),
            email_name_1:$("#email_name_1").val(),
            email_name_2:$("#email_name_2").val(),
            email_name_3:$("#email_name_3").val(),
            email_name_4:$("#email_name_4").val(),
            email_1:$("#email_1").val(),
            email_2:$("#email_2").val(),
            email_3:$("#email_3").val(),
            email_4:$("#email_4").val()
        }

         $.post("/car-of-the-year/site/peoples-choice-2009/COTY_email_php/", postData, jQuery.fn.COTY_formReturnEmail, "text");

        //Don't actually submit the form
        return false;
    });


    $(".CG_form").CG_validator({
        rules: {
            email_1: {
                required: true,
                email: true
            },
            email_2: {
                email: true
            },
            email_3: {
                email: true
            },
            email_4: {
                email: true
            }
        },
        messages: {
            email_1: "Email not valid.",
            email_2: "Email not valid.",
            email_3: "Email not valid.",
            email_4: "Email not valid."
        }
    });

});

function COTY_readmore(carID){
    try {
        $("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
        $("#TB_overlay").click(tb_remove);

        if(tb_detectMacXFF()){
            $("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
        }else{
            $("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
        }

        TB_WIDTH = 558;
        TB_HEIGHT = 450;

        ajaxContentW = TB_WIDTH - 30;
        ajaxContentH = TB_HEIGHT - 45;

        $("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'></div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>Close <em>X</em></a></div></div><iframe SCROLLING='AUTO' frameborder='0' hspace='0' src=\"/car-of-the-year/site/peoples-choice-2009/COTY_readmore_lightbox/#" + carID + "\" id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' ></iframe>");

        $("#TB_window").css({display:"block"});
        $("#TB_closeWindowButton").click(tb_remove);

        tb_position();
    }catch(exception){}
}


function COTY_dataFromFlash(COTY_family,COTY_first,COTY_commercial,COTY_prestige,COTY_suv,COTY_performance,COTY_name,COTY_email,COTY_state,COTY_gender,COTY_age,COTY_overall_best,COTY_overall_best_why){
    //Example params ('Subaru Liberty 2.5','Hyundai Getz','Mercedes-Benz Vito','Audi A6 TDI','Toyota Prado','BMW Z4 sDrive35i','kb','kb@.','NSW','female','31-40','BMW X5','Just because OK');
    $("#COTY_family").val(COTY_family);
    $("#COTY_first").val(COTY_first);
    $("#COTY_commercial").val(COTY_commercial);
    $("#COTY_prestige").val(COTY_prestige);
    $("#COTY_suv").val(COTY_suv);
    $("#COTY_performance").val(COTY_performance);
    $("#COTY_name").val(COTY_name);
    $("#COTY_email").val(COTY_email);
    $("#COTY_state").val(COTY_state);
    $("#COTY_gender_"+COTY_gender).click();
    $("#COTY_age").val(COTY_age);
    $("#COTY_overall_best").val(COTY_overall_best);
    $("#COTY_overall_best_why").text(COTY_overall_best_why);
    $("#COTY_form_source").val("flash");
    $("#COTY_voting_form").submit();
}