$("form#admin_login").ready(function(){
    $("input.empty_field").click(function(){
        empty_field($(this));
    });
});

            
// DatePicker
$(function(){
    $("#set_my_date").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
        onSelect:function(dateText){
            window.location.href = 'index.php?set_date='+dateText;
        }
    });
});

$(function(){
    $("#date_art_1").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd'
    });
});

$(function(){
    $("#date_art").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd'
    });
});
    
// End DatePicker
   
$(function() {
    $('#banner_img').click(function(){
        $('#banner_setings').submit();
    });
}); 

$(function() {
    $('#banner_flash').click(function(){
        $('#banner_setings').submit();
    });
});             

// Filter articles => category
$(function(){
    $('#filter_category').change(function(el){
        var val = $('#filter_category :selected').val();
        var has_filterDate = $.url.param('date');
        if(has_filterDate){
            window.location.href = "index.php?section=articles&date=" + has_filterDate + "&categ=" + val;    
        } else {
            window.location.href = "index.php?section=articles&categ=" + val;   
        }
    });
    
    $('#reset_ultima_ora').change(function(el){
        var val = $('#reset_ultima_ora').attr('checked')?1:0;
         
        if(val==1){
            window.location.href = "index.php?reset_u=yes";    
        }

    });   
    
    $('#reset_header').change(function(el){
        var val = $('#reset_header').attr('checked')?1:0;
         
        if(val==1){
            window.location.href = "index.php?reset_h=yes";    
        }

    });       
    
    $('#filter_ultima_ora').change(function(el){
        var val = $('#filter_ultima_ora').attr('checked')?1:0;
         
        if(val==1){
            window.location.href = "index.php?section=articles&ultima_ora=yes";    
        }
        if(val==0){
            window.location.href = "index.php?section=articles";   
        }
    });        
    
    $("input[name='search_id']").click(function(){
    	id = $("input[name='filter_id']").val();
    	window.location.href = "index.php?section=articles&search_id=" + id;   
    });
    
    $("input[name='select_all']").click(function(){
    	if($(this).attr('checked') == true ){
    		$('.check_ids').attr('checked','checked');   	
    	}else{
    		$('.check_ids').attr('checked','');   	
    	}

    });
    
//    $('input#del_all').click(function(){
//    	console.log($('.check_ids').val());
//    });
});

$(function(){
    $('table.list_items tbody tr').hover(
        function (){
            $(this).addClass('hover_row');
        },
        function (){
            $(this).removeClass('hover_row');
        } 
    );    
});

$(function(){
    $("#filter_date").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function(dateText){
            var has_filterCateg = $.url.param('categ');
            if(has_filterCateg){
                window.location.href = "index.php?section=articles&categ=" + has_filterCateg + "&date=" + dateText;
            } else {
                window.location.href = "index.php?section=articles&date=" + dateText;
            }
        }
    });
    
    
    $("#filter_banner_date").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function(dateText){
            var has_filterCateg = $.url.param('area');
            if(has_filterCateg){
                window.location.href = "index.php?section=banners&area=" + has_filterCateg + "&date_in=" + dateText;
            } else {
                window.location.href = "index.php?section=banners&date_in=" + dateText;
            }
        }
    });
});

$(function(){
    $("input[name='set_editorial']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=articles",
            data: "action=editorial&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
    
    $("input[name='set_header']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=articles",
            data: "action=header&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
    
    $("input[name='set_status']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=articles",
            data: "action=status&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
    
    $("input[name='set_status_scorfinal']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=cm",
            data: "action=status&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });    
    
    $("input[name='set_zvon_status']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=zvonuri",
            data: "action=status&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
    
    $("input[name='set_last_hour']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=articles",
            data: "action=last_hour&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
    
	 $("input[name='set_top_news']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=articles",
            data: "action=top_news&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
	
    $("select[name='set_position']").change(function(){
        $('#loading').show();
        var art_id = $(this).attr('id');
        var position_art = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=articles",
            data: "action=set_position&position_art="+ position_art + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
    
    $("select[name='set_position2']").change(function(){
        $('#loading').show();
        var art_id = $(this).attr('id');
        var position_art = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=concursuri",
            data: "action=set_position&position_art="+ position_art + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });    
    
    $("input[name='set_last_hour']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=articles",
            data: "action=last_hour&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
    
	$("input[name='set_promo']").click(function(){
        $('#loading').show();
        var set = $(this).is(':checked') ? "checked" : "unchecked";
        var art_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"index.php?section=zvonuri",
            data: "action=promo&set="+ set + "&art_id=" + art_id,
            success:function(transport){
                if(transport == "ok"){
                    $('#loading').hide();       
                }
            }
        });
    });
});

// POLLS
$(function(){
    $('#add_question').click(function(){
        var container = $('#questions');
        var questions = container.find('p');
        var question_index = eval(questions.length + 1);
        var new_p = '<p><label for="id_' +  question_index + '">Question ' + question_index + ':</label><input type="text" name="question_' + question_index + '" id="id_' + question_index + '" value=""/><a href="#" class="icons delete del_question" id="del_question_' + question_index + '" onclick="del_question(\'id_' + question_index + '\'); return false;">del</a></p>';
        container.append(new_p)
        return false;
    });
});

function del_question(q_id){
    var parent = $('#' + q_id).parent();
    parent.remove();
    var input_values = new Array();
    $('div#questions input[type="text"]').each(function(){
        var question = $(this).val();
        input_values.push(question);
    });
    
    $('div#questions').empty();
    var question_index = 0;
    for(var key in input_values){
        question_index++;
        var new_p = '<p><label for="id_' +  question_index + '">Question ' + question_index + ':</label><input type="text" name="question_' + question_index + '" id="id_' + question_index + '" value="' + input_values[key] + '"/><a href="#" class="icons delete del_question" id="del_question_' + question_index + '" onclick="del_question(\'id_' + question_index + '\'); return false;">del</a></p>';
        $('#questions').append(new_p)
    }
}

$(function(){
    $('input.status_poll').click(function(){
        var poll_id = $(this).val();
        var checked = false;
        if($(this).is(":checked")){
            var checked = true;    
        }
        window.location = "index.php?section=polls&action=view&enabled=" + checked + "&poll_id=" + poll_id;
    });
    
});

$(function(){
    $('input.status_poll_cm').click(function(){
        var poll_id = $(this).val();
        var checked = false;
        if($(this).is(":checked")){
            var checked = true;    
        }
        window.location = "index.php?section=polls_cm&action=view&enabled=" + checked + "&poll_id=" + poll_id;
    });
    
});
// End polls

// Video
$(function(){
    $('#new_video').click(function(){
        $('#add_new_video_link').show();
        return false;
    });
    
    $('input.enabled_video').click(function(){
        var video_id = $(this).val();
        var checked = false;
        if($(this).is(":checked")){
            var checked = true;    
        }
        window.location = "index.php?section=video&action=change_status&enabled=" + checked + "&video_id=" + video_id;
        
    });    
});
//End video

$(document).ready(function(){
    $('#btn_update_exprestv').click(function(){
        $('#loading').show();
        var exprestv_id = $('#exprestv_id').val();
        var art_id = $('#art_id').val();
    
        $.ajax({
             type:"POST",
             url:"index.php?section=articles&action=edit",
             data:"&id=" + art_id + "&update=" + exprestv_id,
             success:function(transport){
                 if(transport == "success"){
                    $('#loading').hide();                 
                 }
             }
        });
        return false;
    });
});

// Banners
$(document).ready(function(){
   // Set area 
   $('select.banner_areas').change(function(){
       $('#loading').show();
       var banner_id = "";
       var zone = "";
       var parent = $(this).parents('form');
       zone = parent.find("select[name='banner_areas'] :selected").val();
       banner_id  = parent.find("input[name='banner_id']").val();
       $.ajax({
           type:"post",
           url:"index.php?section=banners&action=set_area",
           data:"banner_id=" + banner_id + "&zone=" + zone,
           success:function(transport){
               if(transport == "success"){
                   $('#loading').hide();
               }
           }
       });
   });
    
   //Set date out
   $(".set_banner_date_out").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function(dateText){
            $('#loading').show();
            var parent = $(this).parents('form');
            banner_id  = parent.find("input[name='banner_id']").val();
            
            $.ajax({
                type:"post",
                url:"index.php?section=banners&action=set_date_out",
                data:"date_out=" + dateText + "&banner_id=" + banner_id,
                success:function(transport){
                    if(transport == 'success'){
                        $('#loading').hide();
                    }
                }
            }); 
        }
    });
    
   //Set date in
   $(".set_banner_date_in").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function(dateText){
            $('#loading').show();
            var parent = $(this).parents('form');
            banner_id  = parent.find("input[name='banner_id']").val();
            
            $.ajax({
                type:"post",
                url:"index.php?section=banners&action=set_date_in",
                data:"date_in=" + dateText + "&banner_id=" + banner_id,
                success:function(transport){
                    if(transport == 'success'){
                        $('#loading').hide();
                    }
                }
            }); 
        }
    });
    
    // Update link 
    $('form.up_link a.save_link').click(function(){
        $('#loading').show();
        var parent = $(this).parents('form.up_link');
        var banner_id = parent.find("input[name='banner_id']").val();
        var link = parent.find('input[name="set_link"]').val();
        $.ajax({
            type:"post",
            url:"index.php?section=banners&action=save_link",
            data:"banner_id=" + banner_id + "&link=" + link,
            success:function(transport){
                if(transport == "success"){
                    $('#loading').hide();
                }
            }
        });
        return false;
    });
    
    // Delete banner
    $('a.page_delete').click(function(e){
        var stergere = confirm("Stergere articol");
		
		if(stergere==true){
			$('#loading').show();
			var details_banner = $(this).attr("rel");
			var details_banner_tmp = details_banner.split("|");
			var id = details_banner_tmp[0];
			var type = details_banner_tmp[1];
			var name = details_banner_tmp[2];
			$.ajax({
				type:"post",
				url:"index.php?section=banners&action=delete",
				data:"id=" + id + "&type=" + type + "&name=" + name,
				success:function(transport){
					if(transport == "success"){
						$('#row_' + id).remove();
						$('#loading').hide();
					}
				}
			});
		}else{
			e.preventDefault();
		}
		
    });
    
  
    $('#filter_areas').change(function(el){
        var val = $('#filter_areas :selected').val();
        var has_filterDate = $.url.param('date_in');
        if(has_filterDate){
            window.location.href = "index.php?section=banners&date_in=" + has_filterDate + "&area=" + val;    
        } else {
            window.location.href = "index.php?section=banners&area=" + val;   
        }
    });
    
});
// End Banners
