$(document).ready(function() {
    $("a.fancy").fancybox({
        //'padding'               : 10,           //Space between FancyBox wrapper and content
        //'scrolling'             : 'auto',       //Set the overflow CSS property to create or hide scrollbars. Can be set to 'auto', 'yes', or 'no'
        //'autoScale'             : true,         //If true, FancyBox is scaled to fit in viewport
        //'autoDimensions'        : true,         //For inline and ajax views, resizes the view to the element recieves. Make sure it has dimensions otherwise this will give unexpected results
        //'centerOnScroll'        : false,        //When true, FancyBox is centered while scrolling page
        //'hideOnOverlayClick'    : true,         //Toggle if clicking the overlay should close FancyBox
        //'hideOnContentClick'    : false,        //Toggle if clicking the content should close FancyBox
        //'overlayShow'           : true,         //Toggle overlay
        //'overlayOpacity'        : 0.2,          //Opacity of the overlay (from 0 to 1; default - 0.3)
        //'overlayColor'          : '#000',       //Color of the overlay
        //'titleShow'             : true,         //Toggle title
        //'titlePosition'         : 'inside',     //The position of title. Can be set to 'outside', 'inside' or 'over'
        //'transitionIn'          : 'none',       //The transition type. Can be set to 'elastic', 'fade' or 'none'
        //'transitionOut'         : 'none',       //The transition type. Can be set to 'elastic', 'fade' or 'none'
        //'speedIn'               : 300,          //Speed of the fade and elastic transitions, in milliseconds
        //'speedOut'              : 300,          //Speed of the fade and elastic transitions, in milliseconds
        //'changeSpeed'           : 300,          //Speed of resizing when changing gallery items, in milliseconds
        //'changeFade'            : 'fast',       //Speed of the content fading while changing gallery items
        //'showCloseButton'       : true,         //Toggle close button
        //'showNavArrows'         : true,         //Toggle navigation arrows
        //'enableEscapeButton'    : true          //Toggle if pressing Esc button closes FancyBox
    });

	$("a.fancy_orige").fancybox({
		'autoScale'			: false,
		'overlayColor'		: '#fff',
		'overlayOpacity'	: '0.5',
		'titlePosition'     : 'over',
		'showCloseButton'   : false,
        'padding'           : 0
    });

    // tom
    setVisibility();

    // ha csak fórum felhasználó, nem jelenítjük meg a telefonszám, cím mezőket
    $('#fp_felhasznalok_regisztracio_lvl, #fp_felhasznalok_regisztracio_lvl1').click(function(){
        setVisibility();
    });

    $("#addfield").click(function() {
        strFieldTitle = 'field';
        strNewField = '<tr><td><select name="cat_%" id="cat_%" class="select" style="width: 100px;"><option value="1">-- --</option></select><input type="hidden" name="newMotors[]" value="m_%"></td><td><select name="brand_%" id="brand_%" class="select" style="width: 100px;"><option value="1">-- --</option></select></select></td><td><input type="text" name="type_%" id="type_%" style="width: 100px;" /></td><td><a href="#" onClick="removeItemFromList(this); return false;" style="color: #424237; text-decoration: none;">Töröl <img src="/gfx/ico_del.gif" alt="Töröl" title="Töröl" border="0" width="20" height="20" style="vertical-align: middle; margin-left: 3px;" /></a></td></tr>';
        addField(strFieldTitle, strNewField);
    });

    if (!$("input[name=newMotors\\[\\]]").length && !relMotorsCount){
        $("#addfield").click();
    }

    $('#keyword_time_from').mask("9:99:999");
    $('#keyword_time_to').mask("9:99:999");

    $('.eranking-time').mask("9:99:999");

        // szavazás elküldése
    $("#btn_vote").click(function(){
        url   = '/hu/ajax/setVote/';
        data  = $('#vote').serialize();

        $.post(
            url,
            data,
            function(data){
                $('#vote_wrapper').html(data);
            }
        );

        return false;
    });

    $("#btn_get_poll_results").click(function(){
        url   = '/hu/ajax/getPollResults/';

        $.get(
            url,
            function(data){
                $('#vote_wrapper').html(data);
            }
        );

        return false;
    });
    

});

var motors_cats_uri   = '/hu/ajax/getMotorsCats/';
var motors_brands_uri = '/hu/ajax/getMotorsBrands/';
var relMotorsCount    = 0;
// -------------------
//  mezők láthatósága
// -------------------


function setVisibility(){
    checkshow('fp_felhasznalok_regisztracio_lvl', 'competitor-data');
}

function checkshow(ctrlr, ctrld) {
    flag = $('#reg-form input[type="radio"]:checked').val();

    if (typeof(flag) === 'undefined'){
        $('#'+ctrld).hide();
    }

    if( flag != 0) {
        $('#'+ctrld).hide();
    } else {
        $('#'+ctrld).show();
    }
}

// ---------------------------------------------
//  új sor a motorok felvitelénél a táblázatban
// ---------------------------------------------

function addField(strFieldTitle, strNewField) {
    // utolsó sor utáni sor sorszáma miatt
    ctRowNum = $('#div_motors tr').length;
    strNewField = strNewField.replace(/%/g, ctRowNum+1);

    $("#div_motors tr:last").after(strNewField);

    setParams(ctRowNum+1, motors_cats_uri, 'cat');
    setParams(ctRowNum+1, motors_brands_uri, 'brand');
}

/**
 * Töröl egy kiválasztott elemet az autocomplet-es táblázatból
 */
function removeItemFromList(element) {
    // a táblázat neve
    tableID = $(element).parent().parent().parent().parent().attr('id');

    // a rejtett mező értéke
    hiddenFieldName = $(element).parent().parent().children(':first-child').children(':first-child').attr('name').replace('[]', '');


    // sor törlése
    $(element).parent().parent().remove();

    /* táblázatot elreti, ha nincs tétel */
    /*if (!$("input[name=" + hiddenFieldName + "\\[\\]]").length){
        $('#' + tableID).hide();
    }*/
}

/**
 * Új sor selectFieldjeinek adatai (kategória, márka)
 */
function setParams(id, uri, fieldType){
    jQuery.getJSON(
                  uri,
                  {},
                  function(j){
                      var options = '<option value="0">-- --</option>';
                      
                      if (j.length){
                          for (var i = 0; i < j.length; i++) {
                              options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                          }

                          jQuery('select#' + fieldType + '_' + id).html(options);
                      }
    });
}
