var imgcount = 0;
var form_selected = '';
var oldtext;

$(document).ready(function(){
    var button= $('#upload_button1, #upload_button2, #upload_button3');

    var uploadObj = {
        action: "/ajx/imageUpload",
        name: 'myfile',
        responseType:'json',
        data: {'form':button.attr('id')},
        onSubmit : function(file, ext){
            var NS='';
            form_selected = button.attr("id");
            button.attr("class",'btnparcourirloader');
            this.disable();
        },
        cache: false,
        dataType: "json",
        onComplete: function(file, response){
            button.text('Selectionnez une photo');
            this.enable();
            if (response.code=='200') {
                button.attr("class",'btnparcourir');
                imgcount+=1;
                if (form_selected == 'upload_button1' || form_selected == 'upload_button2') {
                    $('<div id=img'+imgcount+' class="photo_options2"></div>').appendTo('#upload .files').html('<a href="#" class="photos" target="_blank">'+file+'</a><input type="hidden" name="images[]" value="'+file+'">');
                }
                if (form_selected == 'upload_button3') {
                    $('#imageinput').val(file);
                }
            }
            else {
                alert(response.message);
            }

        }
    };
    
   
    $("#addPhotoSubmit").click(function(){
        if ($('#adultcheckbox').attr('checked') == false) {
            alert('Vous devez avoir 18 ans et accepter le règlement relatif à l\'ajout de photos avant de soumettre.');
            return false;
        }
        else {
            $.submit();
            $('#imageinput').val('');
        }
    });


    
    function update_count(){
        if (imgcount==1) {
            $('#upload .count').text('(Transféré: '+imgcount+' image)');
        }
        else {
            $('#upload .count').text('(Transféré: '+imgcount+' images)');
        }
    }
    
    $("#buzzSubmit").click(function(){
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/buzz_fiche",
             async: false,
             //pass the data           
             data: $("form#buzzForm").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    disablepopupbuzz();
                 }
                 else {
                    disablepopupbuzz();
                    alert(data.message);
                 }
             }         
         });
         return false;
    });
    
    function checkKey(e){
        var maxleft=$(".letters strong").text();
        var qtyStr = $("textarea").val();
        var maxStr = $("textarea").attr('maxlength');
        var maxlen = parseInt(maxStr);
        var qtylen = qtyStr.length;
        var txt = (maxlen - qtylen) + "";

        $(".letters strong").text(maxlen - qtylen);
    }

    if ($.browser.mozilla) {
        $(document).keypress (checkKey);
    } else {
        $(document).keydown (checkKey);
    }
    
    $("#forgotSubmit").click(function(){
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/forgot_password",
             async: false,
             //pass the data           
             data: $("form#forgotForm").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    disablepopupmotpass();
                    alert(data.message);
                 }
                 else {
                    disablepopupmotpass();
                    alert(data.message);
                 }
             }         
         });
         return false;
    });
    
    $("#messageSubmit").click(function(){
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/sendMessage",
             async: false,
             //pass the data           
             data: $("form#messageForm").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    disablepopupmessage();
                    alert(data.message);
                 }
                 else {
                    disablepopupmessage();
                    alert(data.message);
                 }
             }         
         });
         return false;
    });
    
    $("#messageSubmitReply").click(function(){
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/sendMessage",
             async: false,
             //pass the data           
             data: $("form#messageFormReply").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    disablepopupmessage();
                    alert(data.message);
                 }
                 else {
                    disablepopupmessage();
                    alert(data.message);
                 }
             }         
         });
         return false;
    });
   
    $("#commentSubmit").click(function(){
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/addComment",
             async: false,
             //pass the data           
             data: $("form#commentForm").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    disablepopucomm();
                    alert(data.message);
                    location.reload();
                 }
                 else {
                    disablepopupcomm();
                    alert(data.message);
                    location.reload();
                 }
             }         
         });
         return false;
    });
    
    
    $("#aFlasher").click(function(){
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/flasher",
             async: false,
             //pass the data           
             data: $("form#actionForm").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    alert(data.message);
                 }
                 else {
                    disablepopupbuzz();
                    alert(data.message);
                 }
             }         
         });
         return false;
    });
   
   $("#aFlasher2").click(function(){
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/flasher",
             async: false,
             //pass the data           
             data: $("form#actionForm").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    alert(data.message);
                 }
                 else {
                    disablepopupbuzz();
                    alert(data.message);
                 }
             }         
         });
         return false;
   });
   
   $("#aFlasher3").click(function(){
        var account_id = $('#pageAccountId').val();
        if (account_id) {
            $('#messageInputId').val(account_id);
        }
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/flasher",
             async: false,
             //pass the data           
             data: $("form#lastVotedForm").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    alert(data.message);
                 }
                 else {
                    disablepopupbuzz();
                    alert(data.message);
                 }
             }         
         });
         return false;
   });
   
   $("#buttonabus_yes").click(function(){
          //start the ajax  
         $.ajax({  
             //POST method is used  
             type: "POST",
             //this is the php file that processes the data and send mail  
             url: "http://www.bogoss.com/ajx/signalAbuse",
             async: false,
             //pass the data           
             data: $("form#formAbuse").serialize(),
             //Do not cache the page  
             cache: false,
             dataType: "json",
             //success  
             success: function (data) {
                 if (data.code=='200') {
                    alert(data.message);
                    disablepopupabus();
                 }
                 else {
                    disablepopupbuzz();
                    alert(data.message);
                 }
             }         
         });
         return false;
    });
    
    new AjaxUpload($('button.btnparcourir'), uploadObj);
    
    //new NotationSystem();
   $('#Star1').mouseover(function(){
        $("div#bogoss_rating .bogoss_stars ul li a#Star1").css("background-position","0 0px");
    });
   $('#Star1').mouseout(function(){
        $("div#bogoss_rating .bogoss_stars ul li a#Star1").css("background-position","0 -80px");
    });
    for (var i=2; i<=10; i++) {
        $('#Star'+i).mouseover(function(){
            var last = parseInt($(this).html());
            for (var j=1; j<last; j++) {
               $("div#bogoss_rating .bogoss_stars ul li a#Star"+j).css("background-position","0 0px");
            }
        });
        $('#Star'+i).mouseout(function(){
            var last = parseInt($(this).html());
            for (var j=1; j<last; j++) {
               $("div#bogoss_rating .bogoss_stars ul li a#Star"+j).css("background-position","0 -80px");
            }
        });
    }
    
    $("#account_pays").change(function(){
        var val = $('option:selected',this).attr('value');
        if (val!='FR') {
            $("#account_state").attr('disabled', 'disabled');
            $("#account_postalcode").attr('disabled', 'disabled');
            $('option[value=0]').attr('selected',true);
            $("#account_postalcode").val('');
            $("li#idcity").html(getCityDefault());
            $("#account_city").attr('disabled', 'disabled');
        }
        else {
            //$("#account_state").attr('disabled', '');
            $("#account_postalcode").attr('disabled', '');
        }
    })
    
    $("#idcp").keyup(function(){
        getCities();
        getRegions();
    })
    
    $("#idcp2").keyup(function(){
        getCities();
        getRegions();
    })
    
    $("#idcp").change(function(){
        getCities();
        getRegions();
    })
    
    $("#idcp2").change(function(){
        getCities();
        getRegions();
    })
    
});  


function getCities()
{
    var search = $("#account_postalcode").val();
    if (5 == search.length) {
        getCityData();
    } else {
        $("li#idcity").html(getCityDefault());
    }
}


function getRegions()
{
    var search = $("#account_postalcode").val();
    if (5 == search.length) {
        getRegionData();
    } else {
        $("li#idreg").html(getRegionDefault());
    }
}
function getCityDefault()
{
    return '<li id="idcity"><label>Ville :</label><input name="account[city]" id="account_city" type="text" class="input-item" value=""></li>';
}

function getCityData()
{
    var cpvalue=$("#account_postalcode").val();
    var ctvalue=$("#account_pays").val();
    oldtext=$("#account_city").val();
    if (ctvalue!='FR') {
        $("li#idcity").html('<input name="account[city]" type="text" class="input-item" value="">');
    }
    else {
        $.ajax({
             type: "POST",
             url: "http://www.bogoss.com/ajx/getcitiesbypc",
             async: false,
             data: {'pc':cpvalue,'ct':ctvalue},
             contentType: "application/x-www-form-urlencoded;charset=UTF-8",
             cache: false,
             dataType: "html",
             success: function(data) {
                 $("li#idcity").html(data);
             }         
        });
    }
}

function getRegionData()
{
    var cpvalue=$("#account_postalcode").val();
    var ctvalue=$("#account_pays").val();
    oldtext=$("#account_city").val();
    if (ctvalue!='FR') {
        $("li#idreg").html('<input name="account[state]" id="" type="text" class="input-item" value="">');
    }
    else {
        $.ajax({
             type: "POST",
             url: "http://www.bogoss.com/ajx/getregionbypc",
             async: false,
             data: {'pc':cpvalue,'ct':ctvalue},
             contentType: "application/x-www-form-urlencoded;charset=UTF-8",
             cache: false,
             dataType: "html",
             success: function(data) {
                if ('0'!=data) {
                    $("li#idreg").html(data);
                } else {
                    $("li#idreg").html(getRegionDefault());
                }
             }         
        });
    }
}

function getRegionDefault()
{
    return '<li id="idreg"><label>Region :</label><select class="select-list" id="account_state" name="account[state]"><option value="0" selected="selected">Peu importe</option></select></li>';
}

function photoOptionSubmit()
{
    //start the ajax  
    $.ajax({
        //POST method is used  
        type: "POST",
        //this is the php file that processes the data and send mail  
        url: "http://www.bogoss.com/ajx/photo_options",
        async: false,
        //pass the data           
        data: $("form#formAlbum").serialize(),
        //Do not cache the page  
        cache: false,
        dataType: "json",
        //success  
        success: function (data) {
            if (data.code=='200') {
                disablepopupmotpass();
                alert(data.message);
            }
            else {
                disablepopupmotpass();
                alert(data.message);
            }
        }         
    });
    return false;
}
   

