$(function(){

    // Correct divs' heights
    var forums_cont = $("#forums");
    var news_cont = $("#news");
    var minisite_link = $("#student_minisite_link");
    var left_cont = $("#bodyInner .left");
    var right_cont = $("#bodyInner .right");
    var max_boxes_height;
    
    // Check if minisite_link exists
    if (minisite_link) {
        max_boxes_height = Math.max(forums_cont.height(),news_cont.height() + minisite_link.height());
        forums_cont.height(max_boxes_height);
        news_cont.height(max_boxes_height - minisite_link.height());
        news_cont.children("div.content").height(max_boxes_height - minisite_link.height() - 40);
    } else {
        max_boxes_height = Math.max(forums_cont.height(),news_cont.height());
        forums_cont.height(max_boxes_height);
        news_cont.height(max_boxes_height);
        news_cont.children("div.content").height(max_boxes_height - 40);
    }
    
    if (left_cont.height() < right_cont.height()) {
        left_cont.css('min-height',right_cont.height());
    }
    
    
	$.extend( $.ui.menu.prototype, {
		next: function() {
			this.move("next", ".ui-menu-item:first");
		},

		previous: function() {
			this.move("prev", ".ui-menu-item:last");
		},

		move: function(direction, edge) {
			if (!this.active) {
				this.activate(this.element.children(edge));
				return;
			}
			var next = this.active[direction + "All"]('.ui-menu-item').eq( 0 );
			if (next.length) {
				this.activate(next);
			} else {
				this.activate(this.element.children(edge));
			}
		}
	});

	$.widget("custom.catcomplete", $.ui.autocomplete, {
		_renderMenu: function( ul, items ) {
			var self = this,
				currentCategory = "";
			$.each( items, function( index, item ) {
				if ( item.category != currentCategory ) {
					ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
					currentCategory = item.category;
				}
				self._renderItem( ul, item );
			});
		}
	});


    $('#search').catcomplete({
        source: baseUrl() + '/search/search?format=json',
        select: function(event,ui) {
            location.href= ui.item.link;
        }
    });
    

    // Approve actions
    $(".approve").click(function() {
        return confirm("האם הנך בטוח/ה שברצונך לבצע פעולה זו?");
    });

    /*$('<input type="button" />').insertBefore($(".admin_form").css({display:'block'})).fancybox({
        type:'inline',
        href:'#addattraction'
    });*/

    // Date picker
    $(".datepicker").datepicker({dateFormat:'dd/mm/yy'});

    // CKEditor
    //$('.editable.textarea').ckeditor( function() { /* callback code */ }, { skin : 'office2003' } );
    
    // Set discard buttons
    $("input.discard").click(function() {
        history.back();
        return false;
    });
    
    // Delete course button
    $("#addcourse input.delete").click(function() {
        location.href = baseUrl() + '/course/' + $("input#course_id").val() + '/delete';
        return false;
    });
    
    // Registration button
    $("#login input.redirect").click(function() {
        location.href = baseUrl() + '/registration';
        return false;
    });
});
