YAHOO.namespace("Daiwai.community");
var Dom = YAHOO.util.Dom;

YAHOO.Daiwai.community.User =
{
    tackle: function(user_id, user_name) {
        this.handleTackle = function()
        {
            var url = '/user/details-' + user_id + '-tackle.html';
            var postData = 'src=xhr';
            var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);
        };

        var handleCancel = function()
        {
            dialog.hide();
            dialog.destroy();
        };

        this.hideDialog = function()
        {
            dialog.hide();
            dialog.destroy();
        };

        var callback = {
            success: function(o) {
                dialog.setBody("Du hast " + user_name + " gegrätscht!");
                //dialog.blurButtons();
                dialog.configButtons('', [[{
                        text: " OK ",
                        handler: handleCancel}]]
                        );
                setTimeout(function(){dialog.hide()}, 1200);
            },
            failure: function(o) {
            //alert('failed');
            }
        };
        var div = document.createElement('div');
        document.body.appendChild(div);
        
        var dialog = new YAHOO.widget.SimpleDialog(div,
        {
           width: "300px",
           fixedcenter: false,
           x: 100,
           y: 400,
           visible: true,
           draggable: true,
           text: "Möchtest du " + user_name + " grätschen?",
           icon: YAHOO.widget.SimpleDialog.ICON_HELP,
           constraintoviewport: true,
           effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.40},
           buttons: [ {
             text: " Ja ",
             handler: this.handleTackle,
             isDefault:true
           },
           {
              text:" Abbrechen ",
              handler: handleCancel}
              ]
         });
        dialog.setHeader("Grätschen");
        dialog.render(document.body);
    },

    removeTackle: function(tackle_id, user_id) {
        var callback = {
            success: function(o) {
                var str = '';
                for(key in o)
                {
                    str += key + ' => ' + '' + "\n";
                }
                var el = document.getElementById('tackle_' + tackle_id);
                el.parentNode.removeChild(el);
            },
            failure: function(o) {
            //alert('failed');
            }
        };

        var url = '/user/details-' + user_id + '-remove_tackle-' + tackle_id + '.html';
        var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);
    },
	
	confirm_friend: function(user_id, request_id)
	{
        var frame = new windowFrame("confirm_friend", 400, 250);

        frame.initAlert();
        frame.submitButton.value = "schließen";
        frame.submitButton.onclick = function()
        {
            frame.close();
        }
        var callback = {
            success: function(o) {
				document.getElementById(frame.getName()).innerHTML = o.responseText;
				frame.setPosition();
            },
            failure: function(o) {
            	alert('failed');
            }
        };
		var url = '/user/details-'+user_id+'-confirmfriend-'+request_id+'.html';
        var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);	
	
	},
	
	deny_friend: function(user_id, request_id)
	{
        var frame = new windowFrame("deny_friend", 400, 250);

        frame.initAlert();
        frame.submitButton.value = "schließen";
        frame.submitButton.onclick = function()
        {
            frame.close();
        }
		
        var callback = {
            success: function(o) {
                
                try {
                    var response = YAHOO.lang.JSON.parse(o.responseText);
                    dialog.setHeader(response.title);
                    dialog.setBody(response.msg);
                    dialog.configButtons('', [[{
                            text: " OK ",
                            handler: handleCancel}]]
                            );
                    setTimeout(function(){dialog.hide()}, 10000);
                }
                catch (x) {
                    alert("Fehler beim Senden der Anfrage. Bitte noch einmal versuchen.");
                    return;
                }
            },
            failure: function(o) {
            //alert('failed');
            }
        };

        var callback = {
            success: function(o) {
				document.getElementById(frame.getName()).innerHTML = o.responseText;
				frame.setPosition();
            },
            failure: function(o) {
            	alert('failed');
            }
        };
		var url = '/user/details-'+user_id+'-denyfriend-'+request_id+'.html';
        var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);	
	
	},

    add_friend: function(user_id, user_name) {
        this.handleSend = function()
        {
            var url = '/user/details-' + user_id + '-addfriend.html';
            var postData = 'src=xhr';
            var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);
        };

        var handleCancel = function()
        {
            dialog.hide();
            dialog.destroy();
        };

        this.hideDialog = function()
        {
            dialog.hide();
            dialog.destroy();
        };

        var callback = {
            success: function(o) {
                
                try {
                    var response = YAHOO.lang.JSON.parse(o.responseText);
                    dialog.setHeader(response.title);
                    dialog.setBody(response.msg);
                    dialog.configButtons('', [[{
                            text: " OK ",
                            handler: handleCancel}]]
                            );
                    setTimeout(function(){dialog.hide()}, 10000);
                }
                catch (x) {
                    alert("Fehler beim Senden der Anfrage. Bitte noch einmal versuchen.");
                    return;
                }
            },
            failure: function(o) {
            //alert('failed');
            }
        };
        var div = document.createElement('div');
        document.body.appendChild(div);
        
        var dialog = new YAHOO.widget.SimpleDialog(div,
        {
           width: "300px",
           fixedcenter: false,
           x: 100,
           y: 400,
           visible: true,
           draggable: true,
           text: user_name + " als Freund hinzufügen?",
           icon: YAHOO.widget.SimpleDialog.ICON_HELP,
           constraintoviewport: true,
           effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.40},
           buttons: [ {
             text: " Anfrage senden ",
             handler: this.handleSend,
             isDefault:true },
                      {
                      text:" Abbrechen ",
                      handler: handleCancel }
                      ]
         });
        dialog.setHeader("Freundanfrage");
        dialog.render(document.body);        
    },

    del_friend: function(user_id, user_name) 
	{
        this.handleSend = function()
        {
            var url = '/user/details-' + user_id + '-delfriend.html';
            var postData = 'src=xhr';
            var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);
        };

        var handleCancel = function()
        {
            dialog.hide();
            dialog.destroy();
        };

        this.hideDialog = function()
        {
            dialog.hide();
            dialog.destroy();
        };

        var callback = {
            success: function(o) {
                
                try {
                    var response = YAHOO.lang.JSON.parse(o.responseText);
                    dialog.setHeader(response.title);
                    dialog.setBody(response.msg);
                    dialog.configButtons('', [[{
                            text: " OK ",
                            handler: handleCancel}]]
                            );
                    setTimeout(function(){dialog.hide()}, 10000);
                }
                catch (x) {
                    alert("Fehler beim Senden des Löschvorgangs. Bitte noch einmal versuchen.");
                    return;
                }
            },
            failure: function(o) {
            //alert('failed');
            }
        };
        var div = document.createElement('div');
        document.body.appendChild(div);
        
        var dialog = new YAHOO.widget.SimpleDialog(div,
        {
           width: "300px",
           fixedcenter: false,
           x: 100,
           y: 400,
           visible: true,
           draggable: true,
           text: user_name + " als Freund löschen?",
           icon: YAHOO.widget.SimpleDialog.ICON_HELP,
           constraintoviewport: true,
           effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.40},
           buttons: [ {
             text: " Freundschaft beenden ",
             handler: this.handleSend,
             isDefault:true },
                      {
                      text:" Abbrechen ",
                      handler: handleCancel }
                      ]
         });
        dialog.setHeader("Freundschaft beenden");
        dialog.render(document.body);  


    },

    updateStatus: function(user_id) {

        var status = Dom.get('user_status');	
		status.innerHTML = '<p style="padding:20px;text-align:center;"><img src="/include/designs/rsv2/img/ajax-loader.gif"></p>';
		var callback = {
            success: function(o) {
                var status_field = Dom.get('user_status_field'),
                    status_updated = Dom.get('user_status_updated');

                status_field.value = '';
                status.innerHTML = o.responseText;
                status_updated.innerHTML = 'vor 1 Sekunde';
            },
            failure: function(o) {
                alert('Status konnte nicht aktualisiert werden. Bitte noch einmal vesuchen.');
            }
        }

        var url = '/user/details-' + user_id + '-update_status.html';
        var status_field = Dom.get('user_status_field');
        var postData = 'src=xhr&status=' + encodeURIComponent(status_field.value);
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);

        return false;
    },

    saveBoardPost: function(user_id) {
        var url = '/user/details-' + user_id + '-add_board_post.html',
            field = Dom.get('board_post_field'),
            submit = Dom.get('board_post_submit'),
            postData = 'src=xhr&post=' + encodeURIComponent(field.value),
            form = Dom.get('board_post_form');
        if(field.value == '')
        {
            return false;
        }
        var callback = {
            success: function(o) {
                var board_posts = Dom.get('board_posts'),
                    field = Dom.get('board_post_field'),
                    submit = Dom.get('board_post_submit');
                board_posts.innerHTML = o.responseText + board_posts.innerHTML;
                field.value = '';
                field.disabled = false;
                submit.disabled = false;
            },
            failure: function(o) {
            	alert('failed');
            }
        };

        submit.disabled = true;
        field.disabled = true;
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);
        return false;
    },

    removeBoardPost: function(user_id, post_id) {
        var callback = {
            success: function(o) {
                try {  
                    var response = YAHOO.lang.JSON.parse(o.responseText);
                    if(response.status == 'ok') {
                        var el = Dom.get('user_board_post_' + response.post_id);
                        el.parentNode.removeChild(el);                        
                    }
                    else {
                        alert("Beitrag konnte nicht entfernt werden ");
                    }
                }  
                catch (x) {
                    alert("Beitrag konnte nicht entfernt werden");
                    return;
                }
            },
            failure: function(o) {
            }
        };
        
        var url = '/user/details-' + user_id + '-remove_board_post-' + post_id + '.html';
        var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);
    },
	
	search_club: function(query)
	{
		YAHOO.Daiwai.community.User.searchClub(query);
		return false;
	},

	searchClub: function(query, frame)
	{
        if(typeof(frame) === 'undefined')
        {
        	var frame = new windowFrame("searchClub", 520, 250);
        	frame.initAlert();
        }
        frame.submitButton.value = "Verein suchen";
        frame.submitButton.className = "button_red";
        frame.submitButton.onclick = function() {
            //alert(document.getElementById('club_query').value);
            YAHOO.Daiwai.community.User.searchClub(document.getElementById("club_query").value,frame);
        };

        var callback = {
            success: function(o) {
				document.getElementById(frame.getName()).innerHTML = o.responseText;
				frame.setPosition();
            },
            failure: function(o) {
            	alert('failed');
            }
        };
		var url = '/user/searchclub.html';
        var postData = typeof(query) != 'undefined' ? ('src=xhr&club_query=' + encodeURIComponent(query)) : 'src=xhr';        
        //alert('pd ' + postData);
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);


    },
	
	getFriendsOnline: function(div_id)
	{
        document.getElementById(div_id).innerHTML = '<p style="padding:20px;text-align:center;"><img src="/include/designs/rsv2/img/ajax-loader.gif"></p>';

		var callback = {
            success: function(o) {
				document.getElementById(div_id).innerHTML = o.responseText;
            },
            failure: function(o) {
            	document.getElementById(div_id).innerHTML = 'failed';
            }
        };
		var url = '/community/friends-online.html';
        var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);	
	},

	getFriendsCommon: function(user_id, div_id)
	{
        document.getElementById(div_id).innerHTML = '<p style="padding:20px;text-align:center;"><img src="/include/designs/rsv2/img/ajax-loader.gif"></p>';
		
		var callback = {
            success: function(o) {
				document.getElementById(div_id).innerHTML = o.responseText;
            },
            failure: function(o) {
            	document.getElementById(div_id).innerHTML = 'failed';
            }
        };
		var url = '/community/friends-common-'+ user_id +'.html';
        var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);	
	},
	
	getMatchReports: function(user_id, div_id)
	{
		var callback = {
            success: function(o) {
				document.getElementById(div_id).innerHTML = o.responseText;
            },
            failure: function(o) {
            	alert('failed');
            }
        };
		var url = '/user/details-' + user_id + '-spielberichte.html';
        var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);	
	},

	showFriendsStatus: function(frame)
	{
        if(typeof(frame) === 'undefined')
        {
        	var frame = new windowFrame("friendsStatus", 400, 250);
        	frame.initAlert();
        }
        frame.submitButton.value = "close";
        frame.submitButton.onclick = function()
        {
            frame.close();
        }

        var callback = {
            success: function(o) {
				document.getElementById(frame.getName()).innerHTML = o.responseText;
				frame.setPosition();
            },
            failure: function(o) {
            	alert('failed');
            }
        };
		var url = '/community/friends-status.html';
        //var postData = typeof(query) != 'undefined' ? 'src=xhr&query=' +query : 'src=xhr';
		var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);

    },

	showNotConfirmedRequests: function()
	{
        var frame = new windowFrame("notyetconfirm", 400, 250);
        frame.initAlert();
        frame.submitButton.value = "close";
        frame.submitButton.onclick = function()
        {
            frame.close();
        }

        var callback = {
            success: function(o) {
				document.getElementById(frame.getName()).innerHTML = o.responseText;
				frame.setPosition();
            },
            failure: function(o) {
            	alert('failed');
            }
        };
		var url = '/community/friends-notconfirmed.html';
        //var postData = typeof(query) != 'undefined' ? 'src=xhr&query=' +query : 'src=xhr';
		var postData = 'src=xhr';
        var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);

    }	
	
};
