/**
 * @author Kamil Szafranek
 */
function c(data){
    console.log(data);
}

function a(data){
    alert("" + data);
}

$(document).ready(function(){

	
    $(".table_preview").click(function(){
		
        if ($(this).hasClass('selected')) {
            $(this).parent().parent().next('tr').fadeOut();
            $(this).removeClass('selected');
        }
        else {
            $(this).parent().parent().next('tr').fadeIn();
            $(this).addClass('selected');
        }
        return false;
    });



	$(".delete_localization_image").livequery(function(){
		$(this).click(function(){
	        var th = $(this);
	        var title = 'Potwierdź usunięcie';
	        var text = 'Czy na pewno chcesz usunąć zdjęcie?';
	        var ob = $("<div/>").addClass("dialog").attr("style", "display: block;").attr("title", title).append($("<p/>").text(text));
	        ob.dialog({
	            bgiframe: true,
	            resizable: false,
	            height: 140,
	            modal: true,
	            overlay: {
	                backgroundColor: '#000',
	                opacity: 0.5
	            },
	            buttons: {
	                'Usuń': function(){
						$.post(th.attr('link'),{},function(){
							th.parent().fadeOut();							
						})
	                    $(this).dialog('close');
	                },
	                'Anuluj': function(){
	                    $(this).dialog('close');
	                }
	            }
	        });
	        return false;
	    });

	});

    
	
    /**
     * usuniecie obrazka w galerii
     */
    $(".delete_multi_gallery").livequery(function(){
		var th = $(this);
		$(this).confirmDialog({
			'title': 'Pytanie', 
			'text' : 'Czy na pewno chcesz usunąć zdjęcie?',
			buttons: {
				'Tak': function(){
			        $.post('/toys/client/deletefile',{'obj': th.attr('imgv')}, function(){
						 th.parent().parent().fadeOut('slow');
			                th.removeClass("dnd");
			                $("#thumbnails").sortable("refresh");
			                var arr = $("#thumbnails").sortable("toArray");
			                var valy = arr.toString();
							valy = valy.split(',');
							valy.splice(valy.indexOf(th.attr('imgv')));
			                $("input#multi_gallery_order").val(valy);
					});
			        $(this).dialog('close');
			    },
			    'Nie': function(){
			        $(this).dialog('close'); 
			    }
			}
		});
	})
    
    
    
    $('.jspaginator span:not(.selected)').livequery(function(){
        $(this).click(function(){
            var pn = $(this).attr('pid');
            $(".toys_main_view").fadeOut('slow', function(){
                $(".toys_main_view").html(getPage(pn));
                $(".toys_main_view").fadeIn('slow');
            })
        })
        
    });
    
    $(".i_want_this_dialog").confirmDialog({
		'title': 'Pytanie', 
		'text' : 'Czy chcesz rozpocząć negocjacje?',
		buttons: {
			'Tak': function(){
		        location.href = $(".container .i_want_this_dialog").attr('href');
		        $(this).dialog('close');
		    },
		    'Nie': function(){
		        $(this).dialog('close'); 
		    }
		}
	});
	$(".confirm_dialog").confirmDialog({
		'title': 'Pytanie', 
		'text' : 'Czy na pewno chcesz usunąć przedmiot?',
		buttons: {
			'Tak': function(){
		        location.href = $(this).attr('href');
		        $(this).dialog('close');
		    },
		    'Nie': function(){
		        $(this).dialog('close'); 
		    }
		}
	});
	
	//zmiana koloru po najechaniu -> tableka panel clienta
	$(".page .forcontainer .container .content table.clienttoys tr:only-child").hover(function(){
	 	$(this).children('td').css({
			'background-color': '#ECF9FF',
			'cursor'    : 'pointer'
		})
	 },function(){
	 	$(this).children('td').css({
			'background-color': 'transparent'
		})
	 })
	 
	 
	$("form.updateForm").ajaxForm(function(data){
	});
    $("form.updateForm select").change(function(){
        $(this).parent('form.updateForm').submit();
    })
    $("form.updateForm input[type='checkbox']").click(function(){
        $(this).parent('form.updateForm').submit();
    })
	
	
	
	$(".preview_button").click(function(){
		var item = $(this).parent().parent().next().children('td')
		var th = $(this).parent().parent()
		if (item.hasClass('visible')){
			item.fadeOut().removeClass('visible')
			$(this).html('Szczegóły')
		} else {
			if (th.hasClass('was_readed')){
				$.post('/comments/client/update', {'id' : th.attr('cid')},function(){
					th.removeClass('was_readed');
					$.each(th.children(), function(i, item){
						$(item).css('font-weight', 'normal')
					})
				})
				
			}
			item.fadeIn().addClass('visible')
			$(this).html('Ukryj')
		}
		
		return false;
	})
	
	$(".preview_button").parent().parent('tr.tr_preview').click(function(event){
		
		if (!$(event.target).hasClass('action_button')){
			$(this).find('.preview_button').click();
		}
	})
	$(".preview_button").parent().parent('tr.tr_preview.was_readed').click(function(event){
		
		var th = $(this);
		
		if (!$(event.target).hasClass('action_button')){
			$.post('/comments/client/update', {'id' : th.attr('cid')},function(){
				th.removeClass('was_readed');
				$.each(th.children(), function(i, item){
					$(item).css('font-weight', 'normal')
				})
			})
		}
	})
	$('tr.tr_click').click(function(){
	
		location.href = $(this).find('.a_click').attr('href');
	})
	
	
	$(".disabled").click(function(){
		return false;
	})
	$("#search_type-2").click(function(){
		$(".non_user").hide();
	})
	
	$("#search_type-1").click(function(){
		$(".non_user").show();
	})
	if ($("input[name='search_type']:checked").val() == 2){
		$(".non_user").hide();
	}
	
})

function multi_gallery_writeImage(obj){
    obj = eval('(' + obj + ')');
    var img = '<img class="photo" src="/' + obj.tpath + '"   onload="$(this).fadeIn(\'slow\');" style="display: none;"/>';
    var div = '<div id="' + obj.id + '" class="item dnd"  >' + img + '<br /><div class="ac"><input type="radio" name="main_photo" value="' + obj.id + '"/><span> Ustaw jako główne</span><br /><img class="delete_multi_gallery ro_button" href="" src="/public/images/gfx/usun_button.gif" imgv="' + obj.id + '" /><br /></div></div>';
    
    
    $("#thumbnails").prepend(div);
    $("#thumbnails").sortable('refresh');
    var arr = $("#thumbnails").sortable('toArray');
    var valy = arr.toString();
    $(function(){
        $("input#multi_gallery_order").val(valy);
    })
}