/* Author: 
    Tom Murphy (tom@wokeupdead.com)
*/

function getUnixTimestamp() {
    Math.round((new Date()).getTime() / 1000);
}


//--------------------- BEGIN header ---------------------
var selfRegisterDialog;

function init_header() {
    //show if they are logged in or not AND
    //provide a login/logout link
    if (user.user_id) {
        dojo.byId("user_status").innerHTML = "You are logged in as " + user.nickname + "(" + user.role + ")";
		//dojo.create("div", {innerHTML: "You are logged in as " + user.nickname + "(" + user.role + ")"}, "user_status", "first");
		dojo.create("a", {innerHTML: "Logout", href: user.loginout_url}, "loginout_container", "first");
		
		if (! user.isRegistered) {
		    var node = dojo.create("li", null, "loginout_container", "before");
		    dojo.create("a", {
		            innerHTML: "Register", onclick: function() {
		                if (selfRegisterDialog == undefined) {
        					dojo.require("mijit.selfRegisterDialog");

        					dojo.addOnLoad(function() {
        						selfRegisterDialog = new mijit.selfRegisterDialog({
        							title: "Register",
        							style: "width: 300px;"
        						});

        						selfRegisterDialog.show();

        					});
        				} else {
        					selfRegisterDialog.show();
        				}
		            }
		        }, node, "first");
            
		}
	} else {
		//dojo.create("div", {innerHTML: "You are not logged in."}, "user_status", "first");
		dojo.byId("user_status").innerHTML = "You are not logged in";
        dojo.create("a", {innerHTML: "Login", href: user.loginout_url}, "loginout_container", "first");
	}
}

//--------------------- END header ---------------------

//--------------------- BEGIN leftnav ---------------------
function init_leftnav() {
    if (user.role == "admin" || user.role == "privileged") {
        var outerli = dojo.create("li", null, "menu", "last");
        var a = dojo.create("a", {innerHTML: "Administration"}, outerli, "last");
        dojo.addClass(a, "sub");
        var ul = dojo.create("ul", null, outerli, "last");
        
        var li;
        if (user.role == "admin") {
    		li = dojo.create("li", null, ul, "last");
    		dojo.create("a", {innerHTML: "UserRoles", href: "/user_role_index"}, li, "last");
    		li = dojo.create("li", null, ul, "last");
    		dojo.create("a", {innerHTML: "Links", href: "/link_index"}, li, "last");
    		li = dojo.create("li", null, ul, "last");
    		dojo.create("a", {innerHTML: "Nav Links", href: "/navlink_index"}, li, "last");
    	}
    	
    	li = dojo.create("li", null, ul, "last");
    	dojo.create("a", {innerHTML: "Articles", href: "/article_index"}, li, "last");
    	li = dojo.create("li", null, ul, "last");
    	dojo.create("a", {innerHTML: "Staff", href: "/staff_index"}, li, "last");
    	li = dojo.create("li", null, ul, "last");
    	dojo.create("a", {innerHTML: "Teams", href: "/team_index"}, li, "last");
        
    }
}
//--------------------- END leftnav ---------------------

//--------------------- BEGIN Team_Index ---------------------
function init_Team_Index() {
    if (user.role == "admin" || user.role == "privileged") {
		dojo.require("dijit.form.Button");

		dojo.addOnLoad(function() {
			new dijit.form.Button({
				type: "button",
				label: "Add New Team",
				iconClass: "iconAdd",
				onClick: function() {
					window.location = "/team_edit";
				}
			}).placeAt("team_index_wrap");
		});
	}
}
//--------------------- END Team_Index ---------------------

//--------------------- BEGIN Team_Show ---------------------
//this needs to be converted to dijit
var coachDialog;
var newCoachDialog;
var playerDialog;
var newPlayerDialog;
var newsDialog;
var addGameDialog;

function init_Team_Show(teamkey) {
    dojo.require("dijit.form.Button");

    dojo.addOnLoad(function(){
        //calendar buttons
        new dijit.form.Button({
            type: "button",
            label: "team calendar",
            showLabel: false,
            iconClass: "iconCalendar",
            onClick: function() {
                window.location = "/team_calendar/" + team.key;
            }
        }).placeAt("team_calendar_buttonHolder");
        
        new dijit.form.Button({
            type: "button",
            label: "ical",
            showLabel:false,
            iconClass: "iconICal",
            onClick: function() {
                window.location = "http://www.google.com/calendar/ical/" + team.calendarid + "/public/basic.ics";
            }
        }).placeAt("team_calendar_buttonHolder");
        
        new dijit.form.Button({
            type: "button",
            label: "atom",
            showLabel: false,
            iconClass: "iconFeed",
            onClick: function() {
                window.location = "http://www.google.com/calendar/feeds/" + team.calendarid + "/public/basic";
            }
        }).placeAt("team_calendar_buttonHolder");

        //admin buttons
       
        if (user.role == "admin"|| user.role == "privileged" || user.role == "team") {
            new dijit.form.Button({
                type: "button",
                label: "Edit",
                iconClass: "iconPencil",
                onClick: function() {
                    window.location = "/team_edit?key=" + teamkey;
                }
            }).placeAt("team_show_editbuttons_container");

            new dijit.form.Button({
                type: "button",
                label: "Delete",
                iconClass: "iconTrash",
                onClick: function() {
                    if (confirm("Do you really want to delete this entire team? (Archiving is usually the better solution)")) {
                        window.location = "/team_delete?key=" + teamkey;
                    }
                }
            }).placeAt("team_show_editbuttons_container");

            new dijit.form.Button({
                type: "button",
                label: "Archive",
                iconClass: "iconTrash",
                onClick: function() {
                    if (confirm("Do you really want to archive this entire team?")) {
                        alert("soon");
                    }
                }
            }).placeAt("team_show_editbuttons_container");

            new dijit.form.Button({
   		        type: "button",
   		        label: "Add Staff",
   		        iconClass:"iconUserAdd",
   		        onClick: function() {
   			        if (coachDialog == undefined) {
               	        dojo.require("mijit.addExistingCoachDialog");

                        dojo.addOnLoad(function() {
               		        coachDialog = new mijit.addExistingCoachDialog({
               		            title: "Coach Dialog",
                   		        style: "width: 300px;",
                   		        teamkey: teamkey
               		        });

               	            coachDialog.show();
               	        });
                    } else {
               	        coachDialog.show();
               	    }
   			    }
            },"addCoachButtonHolder");

            new dijit.form.Button({
       	        type: "button",
       	        label: "Add Player",
       		    iconClass:"iconUserAdd",
       		    onClick: function() {
       			    if (playerDialog == undefined) {
                        //createPlayerDialog(teamkey);
                        dojo.require("mijit.addExistingPlayerDialog");

                        dojo.ready(function(){
                            playerDialog = new mijit.addExistingPlayerDialog({
                   			    title: "Player Dialog",
                   			    style: "width: 300px;",
                   			    teamkey: teamkey
                   		    });

                   		    playerDialog.show();
                        });

                    } else {
                        playerDialog.show();
                    }
                }
       	    },"addPlayerButtonHolder");

            new dijit.form.Button({
       		    type: "button",
       		    label: "Add News",
       		    iconClass:"iconPaperAdd",
       		    onClick: function() {
       			    if (newsDialog == undefined) {
                        //createPlayerDialog(teamkey);
                       dojo.require("mijit.addExistingNewsDialog");

                       dojo.ready(function(){
                           newsDialog = new mijit.addExistingNewsDialog({
                               title: "Add News Dialog",
                   			   style: "width: 300px;"
                           });

                   		   newsDialog.show();
                       });

                   } else {
                       newsDialog.show();
                   }
       	       }
            },"addNewsButtonHolder");
            
            new dijit.form.Button({
                type: "button",
                label: "Add Game",
                iconClass: "iconAdd",
                onClick: function() {
                    if (addGameDialog == undefined) {
                        dojo.require("mijit.addGameDialog");
                        
                        dojo.ready(function(){
                            addGameDialog = new mijit.addGameDialog({
                                title: "Add Game Dialog",
                                style: "width: 600px;",
                                teamkey: teamkey
                            });
                            
                            addGameDialog.show();
                        });
                    } else {
                        addGameDialog.show();
                    }
                }
            }, "addGameButtonHolder");
            
            //now put a delete button next to each staff document
            dojo.query(".remove_staff_placeholder").forEach(function(node, index, arr){
                //alert(node.id);
               // alert(node.id.substring(25));
                
                new dijit.form.Button({
                    label: "delete",
                    showLabel: false,
                    iconClass: "iconTrash",
                    onClick: function() {
                        if (confirm("Are you sure you want to delete this staff from the team?")) {
                            window.location = "/teamstaff_delete?key=" + node.id.substring(25);
                        }
                    }
                }).placeAt(node);
            });
            
            //add an edit button next to each player document
            dojo.query(".edit_player_placeholder").forEach(function(node, index, arr){
                //24
                var id = node.id.substring(24);
                
                /*  
                new dijit.form.Button({
                    label: "edit",
                    showLabel: false,
                    iconClass: "iconPencil",
                    onClick: function() {
                        dojo.require("mijit.editTeamPlayerDialog");

                        dojo.ready(function(){
                            var editTeamPlayerDialog = new mijit.editTeamPlayerDialog({
                       		    title: "Edit TeamPlayer Dialog",
                       		    style: "width: 300px;",
                       		    teamplayerkey: id
                       	    });

                   		    editTeamPlayerDialog.show();
                        });
                    }
                }).placeAt(node);
                */  
              
                // and a delete button
                /*
                new dijit.form.Button({
                    label: "delete",
                    showLabel: false,
                    iconClass: "iconTrash",
                    onClick: function() {
                        if (confirm("Are you sure you want to delete this staff from the team?")) {
                            window.location = "/teamplayer_delete?key=" + id;
                        }
                    }
                }).placeAt(node);
                */
                
                
                var a = dojo.create("a", 
                    {
                        innerHTML: " x ",
                        style: "width:20px; color: red;",
                        //class: "iconTrash",
                        onclick: function(){
                            window.location = "/teamplayer_delete?key=" + id;
                        }
                    },node, "last"
                );
            });     
        }
      
    });
}
//--------------------- END Team_Show ---------------------

//--------------------- BEGIN Article_Show ---------------------
function init_article_show() {
    if (user.role == "admin"|| user.role == "privileged" || user.role == "team") {
        dojo.require("dijit.form.Button");
        
        dojo.addOnLoad(function() {
    		new dijit.form.Button({
    			label: "Edit",
    			showLabel: true,
    			iconClass: "iconPencil",
    			onClick: function() {
    				window.location = "/article_edit?key=" + article.key;
    			}
    		}).placeAt("article_header", "last");

    		new dijit.form.Button({
    			label: "Delete",
    			showLabel: true,
    			iconClass: "iconTrash",
    			onClick: function() {
    				if (confirm("Are you sure you want to delete this document forever? (in most cases you should archive instead)")) {
    					window.location = "/article_delete?key=" + article.key;
    				}

    			}
    		}).placeAt("article_header", "last");

    		new dijit.form.Button({
    			label: "Archive",
    			showLabel: true,
    			iconClass: "iconTrash",
    			onClick: function() {
    				if (confirm("Are you sure you want to archive this document?")) {
    					alert("soon")
    					//window.location = "/article_archive?key=" + article.key;
    				}

    			}
    		}).placeAt("article_header", "last");
    	});
        
    }
}
//--------------------- END Article_Show ---------------------
