var grayOutInputs = function() {
    $("input.grayOut").each(function(){
        if (this.value != this.title && $(this).hasClass("grayOut")) {
            $(this).removeClass("grayOut");
        }
        
        $(this).focus(function () {
            if (this.value == this.title) {
                this.value = "";
                $(this).removeClass("grayOut");                
            }
        });
        $(this).blur(function () {
            if (this.value == "") {            
                $(this).addClass("grayOut");
                this.value = this.title;
            }
        });        
     });
};

var showError = function(input) {
    if (input) {
        $("#errorField").html(input);
        $("#errorField").fadeIn();
        $("#errorField").queue(function () {
            setTimeout(function(){
                $("#errorField").fadeOut();            
            },5000);
            $(this).dequeue();
        });
        $("#errorField").dequeue();
    }
};

var checkErrors = function() {
    if (error) {
        showError(error);
    }        
};

var selectClientCounrtyInDropdown = function () {
    if (typeof google != 'undefined') {
        if ($("#countryList") && google && google.loader && google.loader.ClientLocation != null) {
            var gCountryCode = google.loader.ClientLocation.address.country_code;
            if ($("#countryList").val() == 0) {
                $("#countryList option").each(function(){
                    if (this.value == gCountryCode) {
                        this.selected = true;
                    } else {
                        this.selected = false;
                    }
                });            
            }
        }        
    }
};

var newUserTips = function () {
    $("img.avatarMiddle", "#userProfile").qtip({
        content: 'Click here to change your photo',
        style: {
            corner: 'topLeft',
            color: '#685d2b',           
            tip: 'topLeft',
            background: '#fcf6da',
            border: {
               width: 3,
               radius: 8,
               color: '#f0d144'
            }
        }
    });
    
    if ($("#blog").length > 0) {
        if ($("#blog").attr("checked") && location.hash.indexOf("blogBlock") > -1) {
            $("#surfBlogLink").qtip({
                content: 'You can access your new surfblog here',
                position: {
                    corner: {
                       target: 'topMiddle',
                       tooltip: 'bottomMiddle'
                    }
                },
                style: {
                    color: '#685d2b',
                    background: '#fcf6da',
                    tip: 'bottomMiddle',
                    border: {
                       width: 3,
                       radius: 8,
                       color: '#f0d144'
                    }
                },
                show: { ready: true }
            });            
        }
    }
};

var addCommentsHandler = function () {
	if ($('div.comments')) {
	    
		// add toggle handler to "kommenteeri" links
		$('a.commentsLink').click(function(e) {
	        e.preventDefault();
	        var idAsArray = this.id.split("-");
			var postType = idAsArray[1];
			var postId = idAsArray[2];
			var commentsBlock = "#comments-"+postType+"-"+postId;
			$(commentsBlock).slideToggle(200);
	    });
	    
	    // add post comment handler
	    $('input.submitComment').click(function() {
			this.disabled = true;
	        var idAsArray = this.id.split("-");
			var postType = idAsArray[1];
			var postId = idAsArray[2];
            if (postId) {
                var commentContent = $('#commentContent-'+postType+'-'+postId).val();
                if (commentContent.length > 0) {
                    $.post("/apps/commentshandler.php", { postId: postId, commentType: postType, commentContent: commentContent },
                        function(data){
                	        if (data['error']) {
                	            showError(data['error']);
                			} else {
                			    $("#postCommentFields-"+postType+"-"+postId).after($("<div class='comment newComment'>"));
                			    var commentHtml = "<h3><strong><a href=\"/members/#profile|"+data['userid']+"\">"+data['name']+"</a></strong><small class='gray'>"+data['commentDate']+"</small></h3><p>"+data['commentContent']+"</p>";
                                $("div.newComment", "#comments-"+postType+"-"+postId).html(commentHtml);
                                $("div.newComment").removeClass("newComment");
                                $('#commentContent-'+postType+'-'+postId).val("");
                			}
                        }, "json");
                } else {
					showError("Comment missing!");
				}
            }
			this.disabled = false;
	    });
		
	}
};

$(document).ready(function(){
    grayOutInputs();
    checkErrors();
    addCommentsHandler();
    selectClientCounrtyInDropdown(); // triggers on registration page
    newUserTips();
});