$(document).ready(function(){
    
    /*
     * pokaz / ukryj szczegoly przepisu 
     */
    $("#showRecipe").click(function(){
    	$(".recipeDetails").slideToggle();
    	$("#showRecipe").toggleClass("hideRecipe");
        return false;
    });
     
    /*
     * pokaz / ukryj szczegoly opisu (prodykty)
     */
     $("#showDesc").click(function(){
     	$("#descDetails").slideToggle();
     	$("#showDesc").toggleClass("hideDesc");
         return false;
     });

    /*
     * dodaj komentarz
     */
	var lock = false;
    $("#commentBtn").click(function(){
        if(!lock){
            lock = true;
            $.ajax({
                type: "POST",
                url: "/comments/comments/add/srcmodule/page",
                data: $(".commentForm").serialize(),
                dataType: "json",
                error: function (o){
                    alert("comment error");
                },
                success: function(msg){
                    if(msg.noValid == null){
                    	$('#userName').removeClass('wrong');
                    	$('#userName').val('');
                    	$('#commentText').removeClass('wrong');
                    	$('#commentText').val('');
                    	
                    	$('.commentList').load('/page/page/commentsajax/', { obj_ID: msg.insertData.obj_ID });
                    	location.href="#comment-preview";
                    } 
                    else {
                    	// username
                    	if(msg.noValid.userName != null) {
                    		$('#userName').addClass('wrong');
                        }
                    	else {
                    		$('#userName').removeClass('wrong');
                    	}
                    	
                    	// commentText
                    	if(msg.noValid.commentText != null) {
                    		$('#commentText').addClass('wrong');
                        }
                    	else {
                    		$('#commentText').removeClass('wrong');
                    	}
                    }
                    lock = false;
                }
            });
        }
        return false;
    });
    
    // sortowanie galerii
    $('.sort').change(function(){
    	location.href = $('.sort').val();
    });
    
    // ocenianie
    $('.star-rating').live('click', function(){
    	//alert($('#vote').serialize());
    	
        $.ajax({
            type: "POST",
            url: "/page/page/voteit",
            data: $('#vote').serialize(),
            dataType: "json",
            error: function (o){},
            success: function(msg){
            	 $('#votesCont').html('<p>Twój głos został zapisany.</p>');
            }
        });
    });
      
    /*
     * publikuj przepis
     */
    var lock = false;
    $('#submit').click(function(){
        if(!lock){
            lock = true;
            $.ajax({
                type: "POST",
                url: "/page/page/save",
                data: $("#add").serialize()+'&acceptRules='+$("#rules").attr("checked"),
                dataType: "json",
                error: function (o){
                    alert("error");
                 },
                success: function(msg){
                	if(msg.noValid != null) {
                		// user name
                		if(msg.noValid.authorName != null) {
                			$('#authorName').addClass('wrong');
                		}
                		else {
	                		$('#authorName').removeClass('wrong');
	                	}
                		
                		// user email
                		if(msg.noValid.authorEmail != null) {
                			$('#authorEmail').addClass('wrong');
                		}
                		else {
	                		$('#authorEmail').removeClass('wrong');
	                	}
                		
                		// page title
                		if(msg.noValid.pageTitle != null) {
                			$('#pageTitle').addClass('wrong');
                		}
                		else {
	                		$('#pageTitle').removeClass('wrong');
	                	}
                		
                		// short text
                		if(msg.noValid.shortText != null) {
                			$('#shortText').addClass('wrong');
                		}
                		else {
	                		$('#shortText').removeClass('wrong');
	                	}

                		// long text
                		if(msg.noValid.longText != null) {
                			$('#longText').addClass('wrong');
                		}
                		else {
	                		$('#longText').removeClass('wrong');
	                	}
                		
                		// long text
                		if(msg.noValid.acceptRules != null) {
                			$('#statute').css("font-weight", "bold");
                		}
                		else {
	                		$('#statute').css("font-weight", "normal");
	                	}
                		
                		// photo path
                		if(msg.noValid.photoPath != null) {
                			$('#photoPreview').addClass('wrong');
                		}
                		else {
	                		$('#photoPreview').removeClass('wrong');
	                	}
                	}
                	else {
                		location.href = msg.redirect+"?add=true";
                	}
                    lock = false;
                }
            });
        }
        return false;
    });

});