$(document).ready(function() {

  $('#bogracs_jelentk_overlay').overlay({
    fixed: false,
    load: false,
    mask: {color: '#ebecff',
      		 loadSpeed: 200,
		       opacity: 0.9},
    left: 150,
    top: -200
  });
  $('#bogracs_jelentk_modform').overlay({
    fixed: false,
    load: false,
    mask: {color: '#ebecff',
      		 loadSpeed: 200,
		       opacity: 0.9},
    left: 150,
    top: -200
  });

  $('.close').live('click',function(){
    $(this).parent().overlay().close();
  });

    if($('.side_slide').length){
        $('.side_slide div').hide();
        $('.side_slide').hover(
            function(){ //mouse over
                $div = $(this);
                $div.children("div").show();
                $div.children("div").children("p").hide();
                $(this).animate({
                    width: '160',
                    height: '120'
                },300, function(){
                    $(this).children('img').effect("size", { to: {height: 120}}, 300);                
                    $div.children("div").children("p").show(300);
                });
            },
            function(){ //mouse out
                $div = $(this);
                $div.children("div").children("p").hide();
                $div.children('img').animate({height: 60}, function(){
                    $div.animate({width: '30',height: '60'},100,function(){
                        $div.children("div").hide();
                    });
                });
            }
        );
    }
    
    if($('#kep_lista').length && $('#admin').length){
        
        $('#kep_lista').sortable({
                handle: '.handle',
                start: function(){
                    $('#kep_msg').removeClass('ok').fadeOut(1);
                    $('#kep_msg').children('span').remove();
                },
                update: function(){
                    $('#kep_msg').fadeIn().children('a').show(1000);
                },
                opacity: 0.3
        });
        
        $('#lista_ment').click(function(){
            $('#kep_msg').fadeOut().children('a').hide();
            $hash = $('#kep_lista').sortable('serialize',{key: 'kepek[]'});
            $url = window.location.href.split('/');
            $data = 'method=ajax&galery='+$url[$url.length-1]+'&lista_ment=true&'+$hash;
            $.ajax({
                type: 'POST', url: '/admin/kepek', data: $data,
                success: function(response){
                    if(response == 'ok'){
                        $valami = $("<span>A változtatások mentése sikeres volt !</span>");
                        $('#kep_msg').addClass('ok').fadeIn(1000).append($valami).delay(2000).fadeOut(1000);
                    }
                }
            });
            return false;
        });

        $('.save').click(function(){
            $kep_id = $(this).attr('rel');          
            $leiras = $(this).parents('div').prev('input').val();
            $data = 'method=ajax&title_save=true&kep='+$kep_id+'&leiras='+$leiras;
            $.ajax({
                type: 'POST', url: '/admin/kepek', data: $data,
                success: function(response){
                    if(response == 'hiba')
                        jAlert('hiba történt, próbálja újra!','Hiba');
                }
            });
            return false;
        });

        $('.cover').click(function(){
            $this = $(this);
            $album_id = $this.parents('div').attr('rel');
            $kep_id = $this.attr('rel');
            $data = 'method=ajax&set_cover=true&album='+$album_id+'&kep='+$kep_id;
            $.ajax({
                type: 'POST', url: '/admin/kepek', data: $data,
                success: function(response){
                    $('.borito').removeClass('borito');
                    $this.parents('.kep_doboz').addClass('borito');
                }
            });
            return false;
        });
        $('.delete').click(function(){
            $album_id = $(this).parents('div').attr('rel').split('#')[0];
            $kep_id = $(this).attr('rel');

            jConfirm('Biztosan törölni szeretné a képet?','Megerősítés',function(ok){
                if(ok == true){
                    var $kep_div = $('#kep_'+$kep_id);
                    $data = 'method=ajax&delete_picture=true&album='+$album_id+'&kep='+$kep_id;
                    $.ajax({
                        type: 'POST', url: '/admin/kepek', data: $data,
                        success: function(response){
                            if(response != 'hiba')
                                $kep_div.remove();
                        }
                    });
                }
            });
            return false;
        });
        
        $('.megjegyzes').change(function(){
            $(this).siblings('.controls').children('.save').addClass('changed');
            $('#osszes_mentese').show();
        });
        
        $('#osszes_mentese').click(function(){
            $('.save.changed').click().removeClass('changed');
            $(this).hide();
            return false;
        });
        
    }


    if($('#galeria_lista').length && $('#admin').length){
        $('.delete').click(function(){
            $album_id = $(this).parents('div').parents('div').attr('rel');
            jConfirm('Biztosan törölni szeretné az albumot?','Megerősítés',function(ok){
                if(ok == true){
                    var $album_div = $("div[rel='"+$album_id+"']");
                    $data = 'method=ajax&delete_galery=true&album='+$album_id;
                    $.ajax({
                        type: 'POST', url: '/admin/kepek', data: $data,
                        success: function(response){
                            if(response != 'hiba')
                                $album_div.remove();
                        }

                    });
                }
            });
            return false;
        });
    }
    $("input[name='title']").keyup(function(){
        $data = 'title=' + $(this).val() + '&url_convert=true';
        $.ajax({
            type: 'POST', url: '/admin/kepek/uj', data: $data,
            success: function(response){
                $("input[name='url']").val(response);
            }
        });
    });

//    $('textarea.tinymce').tinymce({
//		// Location of TinyMCE script
//		script_url : '/js/tiny_mce/tiny_mce.js',

//        theme : "advanced",
//        mode : "none",
//        plugins : "bbcode",
//        theme_advanced_buttons1 : "styleselect,bold,italic,underline,undo,redo,link,unlink,image,forecolor,removeformat,cleanup,code",
//        theme_advanced_buttons2 : "",
//        theme_advanced_buttons3 : "",
//        theme_advanced_toolbar_location : "bottom",
//        theme_advanced_toolbar_align : "center",
//        theme_advanced_styles : "Code=codeStyle;Quote=quoteStyle",
//        content_css : "css/bbcode.css",
//        entity_encoding : "raw",
//        add_unload_trigger : false,
//        remove_linebreaks : false,
//        inline_styles : false,
//        convert_fonts_to_spans : false
//	});


  $('td.bogracshely').each(function(){
    var $id = $(this).attr('rel');

    $(this).tooltip({
      tip: '#bogracshely_detail_'+$id,
      position: 'bottom',
		  offset: [-180, -120],
		  delay: 100,
      effect: 'toggle'
    }).dynamic({right: {offset: [100,100]}});
  });


  $('.bogracshely .szabad, .bogracs_tooltip a').click(function(){
    var $hely = $(this).parent().attr('rel');
    $('#bogracs_jelentk_overlay #errors').html('');
    $('#bogracs_jelentk_overlay input.bevitel').val('');
    $('#bogracs_jelentk_overlay input[type="radio"],#bogracs_jelentk_overlay input[type="checkbox"]').attr('checked',false);
    $('#bogracs_jelentk_overlay #hely_sorszam').text($hely);
    $('#bogracs_jelentk_overlay input[name="hely"]').val($hely);
    $('#bogracs_jelentk_overlay').overlay().load();
    return false;
  });

  $('#bogracs_jelentk_submit').click(function(){
    var $data = $('#jelentkezes').serialize();
    $.ajax({
        type: 'POST', url: '/bogracs_fesztival/jelentkezes', data: $data,
        success: function(response){
            if(response == 'ok'){
                $valami = $('<span class="ok">Jelentkezését sikeresen elmentettük !</span>');
                $('#errors').addClass('ok').fadeIn(1000).append($valami).delay(4000).fadeOut(2000, function (){
                  $('#bogracs_jelentk_overlay').overlay().close();
                  window.location.href=window.location.href;
                });
            } else {
              $('#errors').html(response);
            }
        }
    });
    return false;
  });
  
  $('#show_nf').click(function(){
    $('div#nfelt').toggle('slow');
    return false;
  });
  
  $('.tablesorter').tablesorter();
  
  $('#cb_mind').click(function(){
    if($(this).attr('checked'))
      $('.fyear_cb').attr('checked','checked');
    else
      $('.fyear_cb').removeAttr('checked');
  });
  
  $('.fyear_cb').click(function(){
    var uchkd=0,chkd=0;
    $('.fyear_cb').each(function(){
      if($(this).attr('checked') != 'checked')
        uchkd++;
      else
        chkd++;
    });
    if(uchkd > 0)
      $('#cb_mind').removeAttr('checked');
    else if(chkd > 0)
      $('#cb_mind').attr('checked','checked');
  });

  $('.modifyApplication').click(function(){
    var $id = $(this).attr('rel');
    var $data = 'ajax=true';
    $.ajax({
      type: 'POST', url: $(this).attr('href'), data: $data,
      success: function(resp){
        $('#bogracs_jelentk_modform').html(resp);
        $('#bogracs_jelentk_modform').overlay().load();
      }
    });
    return false;
  });

  $('.deleteApplication').click(function(){
    var $row = $(this).parents('tr');
    var $id = $(this).attr('rel');
    var $url = $(this).attr('href')
    jConfirm('Biztosan törölni kívánja a kiválasztott jelentkezést?','Megerősítés',function(ok){
      if(ok == true){
        $.ajax({
          type: 'POST', url: $url, data: 'ajax=true',
          success: function(resp){
            if(resp == 'delete_ok'){
              jAlert('Sikeres törlés !','Siker!');
              $row.remove();
            } else if(resp == 'fail'){
              jAlert('A törlés során hiba lépett fel, kérjük próbálja újra !','Hiba');
            }
          }
        });
      }
    });
    return false;
  });

  $('#sendModApplication').live('click',function(){
    var $data = $('#bogracs_jelentk_mod').serialize();
    $.ajax({
        type: 'POST', url: '/admin/bogracs_jelentkezok', data: $data+'&ajax=true&action=sendModApplication',
        success: function(response){
          if(response == 'mod_ok'){
            jAlert('A módosítások mentése sikeresen megtörtént!','Siker');
            window.location.href=window.location.href;
          }
        }
    });
    return false;
  });


});

