(function($) {

    $.fn.myAccordion = function(options) {
		
		var settings = {
			toggler : '.toggler',
			slider : '.slider',
			effect : 'slide'
		};
		
        if(options) {
            $.extend(settings, options);
        }
		
        var opened = false;
		return this.each(function() {
			var self = $(this);
			$(self.children(settings.slider)).each(function() {
				$(this).hide();
			});
			$(self.children(settings.toggler)).each(function() {
				//check if self elm has children, if it has add eventListeners and give it a pointer cursor
				if (self.children(settings.slider).length > 0) {
					$(this).click(openJobs);
					$(this).css('cursor','pointer');
					$(this).addClass('accordion');
				}
			});
		});
		
		function openJobs(e) {
			if ($(e.target).attr('opened') == 'true') {
				return closeJobs(e);
			}
			$($(e.target).parent().children(settings.slider)).each(function(){
				$(this).slideDown('slow', function() {
					setOpened($(e.target), 'true');
				});
			});
		}

		function closeJobs(e) {
			if ($(e.target).attr('opened') == 'false') {
				return openJobs(e);
			}
			$($(e.target).parent().children(settings.slider)).each(function() {
				$(this).slideUp('slow', function() {
					setOpened($(e.target), 'false');
				});
			});
		}
		
		function setOpened(clicked, opened) {
			$(clicked).parent().children(settings.toggler).each(function() {
				if (opened == 'true') {
					$(this).addClass('accordion-active');
				} else {
					$(this).removeClass('accordion-active');
				}
				$(this).attr('opened', opened);
				$(this).unbind('click');
				$(this).bind('click', (opened == 'true') ? openJobs : closeJobs);
			});
		}
		
		
    }
    
})(jQuery);

