var CoMaVars = {
	comment_adder_url : '/ap_photosendcomment',
	comment_remove_url : '/ap_photodelcomment',
	comment_photo_service_url : '/a_photocomments',
	comment_album_service_url : '/a_albumcomments',
	busy : 'comment_adder_busy_indicator',
	working : 'comment_adder_working',
	button : 'comment_submit_button'
}

var commentDisplayer;
var commentManager;

var CommentDisplayer = Class.create();
CommentDisplayer.prototype = {
    initialize : function(){},
    refreshComments : function(page_number){
	    var ref = $('comment_container');
	    var arr = Element.cumulativeOffset(ref); //Position.cumulativeOffset(ref);
	    var left = arr[0];var top = arr[1];
	    var width = ref.getDimensions().width;var height = ref.getDimensions().height;
	    var _this = this;
	    /**********************/
	    $(CoMaVars.busy).setStyle({'left':left+'px','top':top+'px','width':width+'px','height':height+'px','visibility' : 'visible'});
	    var tmpStr = new StringBuffer();
	    var comment_service_url= CoMaVars.comment_photo_service_url;
	    var albumOrImageId = globDH.get('common', 'resim_no');
	    if(albumOrImageId == null){
	    	albumOrImageId = globDH.get('common', 'album_no');
	    	comment_service_url= CoMaVars.comment_album_service_url;
	    }
	   
	    tmpStr.append(comment_service_url).append('/').append(albumOrImageId).append('/').append(page_number);
	    var inv_code = globDH.get('common', 'inv_code');
	    if(inv_code != null){
			tmpStr.append('/').add(inv_code);
	    }
	    var comment_provider_full_url = tmpStr.toString();
	    new Ajax.Request(comment_provider_full_url, { 
			    method: 'get',
			    onSuccess: function(transport) {
				    $('comment_and_link_container').update(transport.responseText.strip());  
				    $('comment_adder_busy_indicator').setStyle({'visibility' : 'hidden'});
			    }, 
			    onFailure : function(){
				    showError(Lang.commenting.error_unkown); 
				    $('comment_adder_busy_indicator').setStyle({'visibility' : 'hidden'});
			    }	    
	    });
    },
    addCommentToList : function(){
	    var user_name = globDH.get('common', 'user_name');
	    var last_html = '';
	    var safe_comment_text = getClearText($('comment_text').value);
	    var tmpStr = new StringBuffer();
	    tmpStr.append('<div><div class="commentbox">').append(safe_comment_text).append('</div>');
	    tmpStr.append('<div class="commentfooter">').add(Lang.commenting.sender).add(':').append(user_name).append('</div></div>');

	    new Insertion.Top('comments', tmpStr.toString());
	    $('comment_text').value = '';
    },
    removeCommentFormList : function(comment_id){
	    $('comment_' + comment_id).setStyle({'display' : 'none'});
    }
}

function getClearText(text){
	var ret = text.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\\/, '&#47;');
	return ret;
}

var CommentManager = Class.create();
CommentManager.prototype = { 
	initialize : function(){
		this.last_yorum_id = '';
	},
	addComment : function(){
		if($('comment_text').value.length == 0){
			showError(Lang.commenting.error_no_comment);
			return;
		}
		var _this = this;
		var comment = $('comment_text').value;comment = encodeURIComponent(comment);
		var tmpStr = new StringBuffer();var resim_no = globDH.get('common', 'resim_no');
		tmpStr.append(CoMaVars.comment_adder_url).append('/').append(resim_no);
		var comment_adder_full_url =  tmpStr.toString();
		this.showSending();
		new Ajax.Request(comment_adder_full_url, {
				method: 'post',
				onSuccess: function(transport) {
					_this.onAddCommentSuccess(transport);
				},
				onFailure : function(){
					_this.onAddCommentFailure();
				},
				parameters : 'yorum_ekle=' + comment
		});
	},
	onAddCommentSuccess : function(transport){
		if(transport.responseText.strip().match('1')){
			commentDisplayer.addCommentToList();
			$('comment_submit_button').disabled = true;
			$('comment_submit_button').value = Lang.commenting.comment_sended;
		}else if(transport.responseText.strip().match('2')){
			showError(Lang.commenting.signup);
			$('comment_submit_button').disabled = true;
		}else{
			showError(Lang.commenting.error_unkown);
		}
		this.hideSending();
	},
	onAddCommentFailure : function(){showError(Lang.commenting.error_unkown);this.hideSending();},
	removeComment : function(comment_id){
		if(!confirm(Lang.commenting.confirm_delete))
			return;
		
		this.last_yorum_id = comment_id;
		var comment_remover_full_url = CoMaVars.comment_remove_url+'/'+comment_id;

		new Ajax.Request(comment_remover_full_url, {
				method: 'get',
				onSuccess: function(transport) {
					commentManager.onRemoveSuccess(transport);
				},
				onFailure : function(transport){
					showError(Lang.commenting.error_unkown);
				}
		});
	},
	showSending : function(){$(CoMaVars.working).show();$(CoMaVars.button).disabled = true;},
	hideSending : function(){$(CoMaVars.working).hide();$(CoMaVars.button).disabled = true;},
	onRemoveSuccess : function(transport){
		if(transport.responseText.strip().match('1')){
			commentDisplayer.removeCommentFormList(this.last_yorum_id);
		}else{
			showError(Lang.commenting.error_unkown);
		}
	},
	onAJAXFailure : function(transport){}
}
function showError(msg){$('comment_error').setStyle({'display' : 'block'}).update(msg);}
function showCommentAdderForm(){$('comment_adder_form_area_container').setStyle({'display':'block'});}
function hideCommentAdderForm(clear){
	if(clear){$('comment_text').value = '';}
	$('comment_adder_form_area_container').setStyle({'display':'none'}); 
	$('comment_error').setStyle({'display' : 'none'}); 
}
function showComments(page_number){commentDisplayer.refreshComments(page_number);}
function addComment(){commentManager.addComment();}
function removeComment(comment_id){commentManager.removeComment(comment_id);}
commentDisplayer = new CommentDisplayer();
commentManager = new CommentManager();
