$(document).ready(function(){
	$(".wyszukiwarka h3").click(function() {
		$("#wyszukiwarka_form").toggle('blind');
	});
	$("#w_wiecej_opcji").click(function() {
		$("#w_zaawansowane").toggle('blind');
		return false;
	});
	$("#w_wiecej_opcji1").click(function() {
		$("#w_zaawansowane1").toggle('blind');
		return false;
	});
	if(!((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))) {
	$("#w_zaawansowane").hide();
	}
    
    $("#awaiting_queries").mouseenter(function(){
        $( '#queued_queries' ).css('display', 'block');
    });
    $("#awaiting_queries").mouseleave(function(){
        $( '#queued_queries' ).css('display', 'none');
    });

    $('div.buttons').click( function() {
        me = $(this);
        if( me.hasClass('company') ) {
            typ = 'f';
            id = me.children().html();
            title = me.children().next().html();
        } else if( me.hasClass('course') ) {
            typ = 'k';
            id = me.children().html();
            title = me.children().next().html();
        } else if( me.hasClass('training') ) {
            typ = 's';
            id = me.children().html();
            title = me.children().next().html();
        }
        if( me.hasClass('observe') ) {
            $.ajax({
                url: '/ajax/insert_to_observed/' + typ + '/' + id,
                dataType: 'json',
                success: function(data) {
                    if( data ) {
                        me.removeClass('observe').addClass('resign');
                    }
                }
            });
        } else if( me.hasClass('resign') ) {
            $.ajax({
                url: '/ajax/remove_from_observed/' + typ + '/' + id,
                dataType: 'json',
                success: function(data) {
                    if( data === true ) {
                        me.removeClass('resign').addClass('observe');
                        if( me.parent().hasClass('element') ) {
                            me.parent().remove();
                        }
                    } else {
                        alert(data);
                    }
                },
                error: function (request, status, error) {
                }
            });
        } else if( me.hasClass('later') ) {
            $.ajax({
                url: '/ajax/insert_to_awaiting/' + typ + '/' + id,
                dataType: 'json',
                success: function(data) {
                    $( '#queued_queries > p' ).remove();
                    $( '#queued_queries > h3' ).remove();
                    count = 0;
                    $.each( data, function(index, value) { 
                        $( '#queued_queries' ).append( '<h3>' + index + '</h3>' );
                        $.each( this, function(index, value) {
                            $( '#queued_queries' ).append( '<p>' + value + '</p>' );
                            count++;
                        });
                    });
                    $('#awaiting_queries > a > span').html( count );
                    me.remove();
                }
            });
        }
    });
	
	$('a.has-more').qtip({
		content: {
			text: function(api) {
				// Retrieve content from custom attribute of the $('.selector') elements.
				var id = $(this).attr('id');
				var idSub = "#sub-" + id + " .sub-main-menu-div";
				var idTop = "#sub-" + id + " .sub-main-menu-top";
				
				$(idTop).empty().append('<p><b>Ładuję</b> <img src="/media/img/szkielet/loader_menu.gif" /></p>');
				
				var submenu = $(idSub).clone();
				
				$.post("/ajaxmenu/submenu/", { param: id },
					function(data){
						submenu = submenu.children(".sub-main-menu-top").empty().append(data.content);
					}, "json");
				  
				return submenu;
			}
		},
		position: {
			my: 'left top',  
			at: 'right center', 
			target: $(this).attr('id'),
			viewport: $(window),
			adjust: {
				method: 'shift shift'
			}
	    },
		style: {
			classes: 'ui-tooltip-szkolenia ui-tooltip-shadow'
		},
		hide: {
			delay: 100,
			fixed: true,
			effect: function(offset) {
				$(this).fadeOut();
			}
	    },
		show: {
			delay: 700,
			effect: function(offset) {
				$(this).fadeIn();
			}
		}
	});
});
function countred( url ) {
    window.open( url );
    return false;
}
