$(document).ready(function() {


    $("#event-members > ul").tabs({ fx: { opacity: 'toggle' } });
    $("#event-details > ul").tabs();
    $("#event-comments > ul").tabs();
    $(".sk-tip").cluetip({splitTitle:'|',showTitle:false});

    $("#form-add-comment").submit(sk_addComment);
    $("#comment_text").focus(function() {
    	var text = $.trim($(this).val());
    	if (text=='Enter your comment...') {
	        $(this).val('');
	        $(this).css('color', '#555555');
    	}
    });

    $("#comment_text").blur(function() {
        var text = $.trim($(this).val());
        if(text=="") {
            $(this).val('Enter your comment...');
            $(this).css('color', 'gray');
        }
    });


});


function sk_addComment() {

    var comment = $.trim($("#comment_text").val());
    if (comment=="" || comment=="Enter your comment...") {
        return false;
    }

    var callback = function(response) {
    	
    	if(response.result==false) {
    	   $("#msg")
    	       .html(response.msg)
    	       .attr('class', 'error-short')
    	       .fadeIn()
    	       .animate({opacity: 1.0}, 2000)
    	       .fadeOut('slow');
    	} else {
    	   $("#form-add-comment").resetForm();
           $("#msg")
               .html(response.msg)
               .attr('class', 'ok-short')
               .fadeIn()
               .animate({opacity: 1.0}, 2000)
               .fadeOut('slow');
    	   
    	   $("#image_random_value").attr('src', response.image_random_value);
    	   $("#div-comment-list").html(response.html);
    	}
    	$("#submit").attr('disabled', '');
    };

    $("#submit").attr('disabled', 'diabled');
    sk_ajaxSubmit('form-add-comment', callback, 'addComment');
    return false;
}

function sk_deleteComment(commentId, sectionNo) {
	if(confirm("Do you really want to delete this comment?")==false) {
		return false;
	}
	
	var data = {
		'comment_id': commentId,
		'section_no': sectionNo
	}
	
	var callback = function(response) {
		if (response.result==false) {
			$("#msg")
    	       .html(response.msg)
    	       .attr('class', 'error-short')
    	       .fadeIn()
    	       .animate({opacity: 1.0}, 2000)
    	       .fadeOut('slow');
		} else {
			$("#div-comment-list").html(response.html);
		}
	};
	
	sk_ajaxCall(data, callback, 'deleteComment')
}

function sk_showCommentList(sectionNo) {
	var data = { 'section_no': sectionNo };
	var callback = function(response) {
		if (response.result) {
			$("#div-comment-list").html(response.html);	
		}
	};
	
	sk_ajaxCall(data, callback, 'showCommentList');
}

function sk_showMap() {
    if (!sk_locationLng || !sk_locationLat) {
        return;
    }

    var mapDiv = document.getElementById("sk-event-map");
    if (mapDiv.loaded) {
        return;
    }
    
    $('#sk-event-map').css( {width:'450px',height: '300px'});
    $('#sk-event-map').html('Loading...');
    setTimeout("sk_loadMap()", 100);
    
}

function sk_loadMap() {
    var mapDiv = document.getElementById("sk-event-map");
    var map = new GMap2(mapDiv);
    var point = new GLatLng(sk_locationLat, sk_locationLng);
    map.setCenter(point, 16);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

    var marker = new GMarker(point);
    map.addOverlay(marker);
    // TODO: add listener
   
    // add flag to avoid reloading 
    mapDiv.loaded = true;
}

function sk_acceptInvitation() {
    
    var data = {};
    var callback = function(response) {
    	if (response.result) {
    		$("#div-invitation").html(response.msg).attr('class', 'ok-short');
    	} else {
    		$("#div-invitation").html(response.msg).attr('class', 'error-short');
    	}
    };
    
    sk_ajaxCall(data, callback, 'acceptInvitation');
    return false;

}

function sk_rejectInvitation() {
    if (confirm("Are you sure to turn down this invitation?")==false) {
    	return false;
    }
    
    var data = {};
    var callback = function(response) {
    	if (response.result) {
    		$("#div-invitation").html(response.msg);
    	} else {
            $("#div-invitation").html(response.msg).attr('class', 'error-short');
    	}
    };
    
    sk_ajaxCall(data, callback, 'rejectInvitation');
    return false;	
}

function sk_requestJoin() {
    var data = {};
    var callback = function(response) {
        if (response.result) {
            $("#div-request").html(response.msg);
        } else {
            $("#div-request").html(response.msg).attr('class', 'error-short');
        }
    };
    
    sk_ajaxCall(data, callback, 'requestJoin');
    return false;   
}

function sk_removeMember(uid) {
    if (confirm('Do you really want to remove/reject this member from this event?')==false) {
    	return false;
    }
    
    var data = {'uid': uid, 'action': 'remove'};
    var callback = function(response) {
    	if (response.result) {
    		sk_refreshMembers(response);
    	} else {
    		if (response.msg) {
    			alert(response.msg);
    		}
    	}
    };
    
    sk_ajaxCall(data, callback, 'manageMember');
    return false;   
}

function sk_acceptMember(uid) {
    if (confirm('You are going to accept this member for the event. Continue?')==false) {
        return false;
    }
    
    var data = {'uid': uid, 'action': 'accept'};
    var callback = function(response) {
        if (response.result) {
            sk_refreshMembers(response);
        } else {
            if (response.msg) {
                alert(response.msg);
            }
        }
    };
    
    sk_ajaxCall(data, callback, 'manageMember');   
    return false;   
}

function sk_refreshMembers(response) {
    if (!response.memberList) {
    	return;
    }
    
    if (response.memberList.member_joined) {
    	$('#d-event-joined').html(response.memberList.member_joined);
    }
    if (response.memberList.member_invited) {
        $('#d-event-invited').html(response.memberList.member_invited);
    }
    if (response.memberList.member_requesting) {
        $('#d-event-requesting').html(response.memberList.member_requesting);
    }
}