var DOM = YAHOO.util.Dom;

var alert_reasons 	  = new Array;
	alert_reasons[0]  = 'Select reason';
	alert_reasons[1]  = 'bad content';
	alert_reasons[2]  = 'copyrighted material';
	alert_reasons[3]  = 'vulgar';
	alert_reasons[4]  = 'abusive';
	alert_reasons[5]  = 'other';

function AlertDialog () {
    var dialog, postLink, reasonsSel, OtherTxt;
    var postBtn;
    var wait, error, errorMsg;
    var dialogW = 420;
    var dialogH = 275;
    
    return {
        init : function(){
			var opt;
			reasonsSel = document.createElement("SELECT");
			otherTxt = DOM.get('otherTxt');
            for(i=0; i<alert_reasons.length; i++){
				opt = new Option(alert_reasons[i], i, true, false);
            	reasonsSel.options[i] = opt;
            }
			reasonsSel.name = "reasonsSel";
			reasonsSel.style.width = '200px';
			reasonsSel.onchange = function (ev, el) {
				if(!el) el = this;
				if(el.options[el.selectedIndex].value == 5)
					otherTxt.disabled = false;
				else{
					otherTxt.value = '';
					otherTxt.disabled = true;
				}
			}
			
			var reasonTd = DOM.get("reasonsSelTD");
			reasonTd.appendChild(reasonsSel);
			
            postLink = getEl('community-alert-link');
            
            if(!postLink) return;
            
            wait = getEl('alert-wait');
            error = getEl('alert-error');
            errorMsg = getEl('alert-error-msg');
            this.createDialog();
			
             postLink.addHandler('click', true, function(){
	        	var position = DOM.getXY(postLink.id);
	        	dialog.resizeTo(dialogW, dialogH);
				dialog.moveTo(position[0]-dialog.width+37, position[1]+19);
                dialog.show(postLink);
             });
        },
        
        submitComment : function(){
            wait.radioClass('active-msg');
            YAHOO.util.Connect.setForm(DOM.get('alert-form'));
			var commentSuccess = function(o){
			
            	var result = eval('new Object(' + o.responseText + ')');
                wait.removeClass('active-msg');
                if(result.errors.length == 0){
                    dialog.hide();
                }else{
                    error.radioClass('active-msg');
                    var err = '';
                    for(i in result.errors){
                    	err += '<div class="Error">' + result.errors[i] + '</div>\n';
                    }
                    dialog.resizeTo(dialogW, dialogH+30);
                    errorMsg.update(err);
                }
            };

            var commentFailure = function(o){
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
            YAHOO.util.Connect.asyncRequest('POST', 'query_wrapper.php',
                    {success: commentSuccess, failure: commentFailure});
        },
        
        createDialog : function(){
            dialog = new YAHOO.ext.BasicDialog("alert-dlg", {
					draggable: false,
                    modal:false,
                    width: dialogW,
                    height: dialogH,
                    shadow:true,
                    minWidth:dialogW,
                    minHeight:dialogH,
                    resizable: false
            });

            dialog.addKeyListener(27, dialog.hide, dialog);
            //dialog.addKeyListener(13, this.submitComment, this);
            
            postBtn = getEl('alert-post-dialog-btn');
            postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('alert-cancel-dialog-btn');
            cancelBtn.on('click', dialog.hide, dialog);

            dialog.on('hide', function(){
                wait.removeClass('active-msg');
                error.removeClass('active-msg');
            });
            
            dialog.on('show', function(){
				reasonsSel.selectedIndex = 0;
				otherTxt.value = '';
				otherTxt.disabled = true;
				reasonsSel.focus();
            });
        }
    };
};

var AD = new AlertDialog();
YAHOO.ext.EventManager.onDocumentReady(AD.init, AD, true);
