$(document).ready(function() {
    $(".pushContent").click(function(e){
      e.preventDefault();
      document.location.href = $(this).attr('href');
      return false;
    });
    
    ////////////
    // CAROUSEL
    ////////////
    $(".roll_content").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: 10
    });
    
    $(function(){
  	    object = jQuery(this);
  		  object.find(".roll_content li").each(function(){
  		    $(this).addClass('roll-img');
        });
      	$('.roll-img').mouseover(function(){$(this).stop().fadeTo(200,0.5);});
      	$('.roll-img').mouseout(function(){$(this).stop().fadeTo(200,1);});
  	});

    //////////
    // TICKER
    //////////
    $(function(){ 
        $("ul#ticker_text").liScroll({travelocity: 0.15}); 
    });

    $(function(){
        object = jQuery(this);
        $(".concours_question input[type=checkbox]").click(function(){
            object.find(".concours_question input[type=checkbox]").each(function(){
                if($(this).attr('checked') == true){
                    $(this).attr('checked',false);
                }
            });
            $(this).attr('checked',true);
        });
    });

    $('.nyroOups').nyroModal({
        width: 430,
        minHeight: 205,
        bgColor:'#333333',
        closeButton: '<a href="#" class="nyroModalClose" id="closeBut2" title="close">Close</a>'
    });
    $('.nyroPop').nyroModal({
        width: 800,
        height: 600,
        bgColor:'#333333',
        closeButton: '<a href="#" class="nyroModalClose" id="closeBut2" title="close">Close</a>'
    });
    $('.nyroPop2').nyroModal({
        width: 430,
        minHeight: 205,
        bgColor:'#333333',
        closeButton: '<a href="#" class="nyroModalClose" id="closeBut2" title="close">Close</a>'

    });
    $('.nyroModal').nyroModal({
        bgColor:'#333333'
    });


    //////////////////////////////////////
    // ON CACHE LES QUESTIONS ET LE BRAVO
    //////////////////////////////////////
    $(".etape2").hide();
    $(".etape2 .contestQuestion").not(".question1").hide();
    $(".etape3").hide();
});

/////////////////////////////////////////////////////
// ON PASSE AU FORMULAIRE SI LES INFOS SONT CORRECTS
/////////////////////////////////////////////////////
var results = [];
$(".stepContest").click(function(e){
    e.preventDefault();
    var questionId = parseInt(this.id.split("_")[1],10);
    
    if(this.id == "startContest"){
        if(verifForm()){ 
            $(".etape1").fadeOut("slow", function(){  
                $(".etape2").fadeIn("slow"); 
            });
        }
    } else {
        $(this).parents(".contestQuestion").find(".error-question").css("display","none");
        var state = false;
        $(this).parents(".contestQuestion").find(".response-check").each(function(){ 
            if($(this).attr('checked')){
                results.push($(this).val());
                state = true;
            } 
        });
        if(!state){
            $(this).parents(".contestQuestion").find(".error-question").css("display","block");
        } else if(this.id == "endContest"){
            $(".etape2").fadeOut("slow", function(){
                $(".contestQuestion").show();
                var datas = $("#formConcours").serialize();
                var count = results.length;
                for(var index = 0; index < count; index++){
                    datas += "&" + escape("questions[" + index + "]") + "=" + escape(results[index]);
                }
                $.ajax({
                    type: "POST",
                    url: "/contest/create",
                    data: datas});
                $(".etape3").fadeIn("slow"); 
            });


        } else {
            $(".question"+questionId).fadeOut("slow",function(){
                $(".question"+(questionId + 1)).fadeIn("slow"); });
        }
    }
    return false;
});


///////////////////////////////////
// ON CACHE LES MESSAGES D'ERREURS
///////////////////////////////////
function cache(){
    $('#error-invalid-email').css("display", "none");
    $('#error-legal').css("display", "none");
    $('#error-email').css("display", "none");
    $('#error-birthday').css("display", "none");
}


//////////////////////////////////////////
// ON VERIFIE QUE LES INFOS SONT REMPLIES
//////////////////////////////////////////
function verifForm(){
    ok = true;
    if ( $(".email").val() != "" ) { 
        if ( emailCheck( $(".email").val() ) != false ) { 
            cache();
            if ( $("#contest_user_birthdate_day").val() == "" ) { 
                ok = false; 
                cache();
                $('#error-birthday').css("display", "block");
            } else if ( $("#contest_user_birthdate_month").val() == "" ) { 
                ok = false; 
                cache();
                $('#error-birthday').css("display", "block");
            } else if ( $("#contest_user_birthdate_year").val() == "" ) { 
                ok = false; 
                cache();
                $('#error-birthday').css("display", "block");
            } else if ( $(".checklegal").attr('checked') == false ) { 
                ok = false; 
                cache();
                $('#error-legal').css("display", "block");
            }
        }else{
            ok = false;
            cache();
            $('#error-invalid-email').css("display", "block");
        }	
    } else {
        ok = false; 
        cache();
        $('#error-email').css("display", "block");
    }
    return ok;
}
///////////////////////////////////////
// ON VERIFIE LA CONFORMITE DE L'EMAIL
///////////////////////////////////////
function emailCheck(str) {
    var at   = "@";
    var dot  = ".";
    var lat  = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    if (str.indexOf(at)==-1){return false;}
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
    if (str.indexOf(at,(lat+1))!=-1){return false;}
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
    if (str.indexOf(dot,(lat+2))==-1){return false;}
    if (str.indexOf(" ")!=-1){return false;}
}
function nyro_iframe(href, width, height){
  $.fn.nyroModalManual({debug:false, width:width, height:height, type:'iframe', url:href});
}
