$(document).ready(function(){


//  Dropdown menu

	$("ul.subnav").parent().addClass("parent"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul.topnav li.parent").hover(function() { //When trigger is clicked...
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click


		//Following events are applied to the trigger (Hover events for the trigger)
	}, function(){
			$(this).find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up	
	});

// End Dropdown menu


//Inline edit
/*        function slideout() {
            setTimeout(function () {
                $("#response").slideUp("slow", function () {});
            },
            2000);
        }

        $(".inlineEdit").bind("click", updateText);

        var OrigText, NewText;

        $(".save").live("click", function () {

            $("#loading").fadeIn('slow');

            NewText = $(this).siblings("form").children(".edit").val();
            var id = $(this).parent().attr("id");
            var data = 'id=' + id + '&text=' + NewText;

            $.post("update.php", data, function (response) {
                $("#response").html(response);
                $("#response").slideDown('slow');
                slideout();
                $("#loading").fadeOut('slow');

            });

            $(this).parent().html(NewText).removeClass("selected").bind("click", updateText);

        });

        $(".revert").live("click", function () {
            $(this).parent().html(OrigText).removeClass("selected").bind("click", updateText);
        });

        function updateText() {
            var height = $(this).height();
            var width = $(this).width();
            $(this).removeClass("inlineEdit");
            OrigText = $(this).html();
            $(this).addClass("selected").html('<form ><textarea class="edit">' + OrigText + '</textarea> </form><a href="#" class="save"><img src="images/save.png" border="0" width="48" height="15"/></a> <a href="#" class="revert"><img src="images/cancel.png" border="0" width="58" height="15"/></a>').unbind('click', updateText);
            $(".selected .edit").height(height).width(width);
        }*/
// End inline edit

});

// Code for creating the tables for schema

var Week = function(label,shortLabel,stopDate){
        this.label = label;
        this.shortLabel = label;
        if(typeof stopDate != "undefined")
            var stopDate = new Date(parseInt(stopDate.split("-")[0],10),parseInt(stopDate.split("-")[1],10)-1,parseInt(stopDate.split("-")[2],10));
        else
            var stopDate = null;
        var days = [];
        var table;
        this.addDay = function(day){
            days.push(day);
        };
        this.creatTable = function(targetId){
            if( stopDate == null || stopDate >= new Date() ){
                var times = [];
                table = $("<table class=\"schema\"></table>");
                var thead = $("<thead></thead>");
                var tr = $("<tr></tr>").append("<th>Tid</th>");

                jQuery.each(days, function(index, day) {
                    tr.append("<th>"+day.label+"</th>");
                    jQuery.each(day.data, function(index, pass) {
                        if( $.inArray(pass.start,times) < 0 )
                            times.push(pass.start)
                    });
                });
                times.sort();
                thead.append("<tr><th colspan=\""+(days.length+1)+"\">"+this.label+"</th></tr>");
                table.append(thead);
                thead.append(tr);
                var tbody = $("<tbody></tbody>");

                var obj = this;
                jQuery.each(times, function(index, time) {
                    tbody.append(obj.createTr(time,index));
                });
                table.append(tbody);
                $(targetId).append(table);
            } else{
                $(targetId).hide();    
            }
        };
        this.createTr = function(time,index){
            var tr = $("<tr></tr>");
            tr.append("<td class=\"time\">"+time+"</td>");
            jQuery.each(days, function(i, day) {
                var cl = "";
                if(day.closed)
                    cl+="closed ";
                if(typeof day.data[index] != "undefined" && day.data[index].start === time){
                    tr.append("<td class=\""+cl+day.data[index].type.toLocaleLowerCase().replace("/","")+"\">"+day.data[index].type+(day.data[index].length > 0 ? "<br/>" + day.data[index].length + " min":"")+"</td>");
                }else{
                    tr.append("<td class=\""+cl+"\">&nbsp;</td>");
                    day.data.splice(index,0,"")
                }
            });
            return tr;
        }

    };


    var Day = function(label,closed){
        this.closed = (typeof closed != "undefined" && closed);
        this.label = label;
        this.data = [];
        this.addPass = function(start,length,type){
            this.data.push({start:start,length:length,type:type});
        }
    };