(function($){ 
     $.fn.extend({  
         toogleboxes: function(options) {       
            $.fn.toogleboxes.defaults = {
					head_selector: '>h6',
					teaser_class: 'box',
					active_class: 'minus',
					passive_class: 'plus',
					cookie_name: 'closed_teaser',
					cookie_expires: 10,	//dauer in Tage
					cookie_separator: '~',
					prefix_separator: '_',
					check_headlines: []
			};
			
			// build main options before element iteration
			var opts = $.extend({}, $.fn.toogleboxes.defaults, options);
			
			return this.each(function() {
				var $this = $(this);
				
				//parse cookie get headlines array ----------------
				var aValues = getCookieArray();
				var anz = aValues.length;
				var pref = $this.attr('id');
				var parts = [];
				for (var i = 0; i < anz; i++) {
					parts = aValues[i].toString().split(opts.prefix_separator.toString());
					if (parts[0] == pref) opts.check_headlines[opts.check_headlines.length] = parts[1];
				}
				
				//iterarte all teasers ----------------
				$.each($this.find("."+opts.teaser_class+opts.head_selector), function(){
					var $head_el = $(this);
					var head = $head_el.html();
					
					// close default teasers ----------------------
					var found = false;
					for (key in opts.check_headlines) {
						if (opts['check_headlines'][key] == head) found = true;
					}
					if (found) closeBox($head_el, false);
					
					//click event -------------------------------
					$head_el.css('cursor', 'pointer').click(function(e){
							$el = $(this);
							var status = $el.attr('class');
							if (status == "") status = opts.active_class;
							if (status == opts.active_class) {
								//close and cookie
								closeBox($el, true);
							} else {
								//open - delete cookie
								openBox($el, true);
							}
							return false;
					});
				});
				
				function openBox(el, update){
					//css
					el.nextAll().show();
					el.removeClass(opts.passive_class).addClass(opts.active_class);
					
					if (update) {
						//cookie
						var boxname = el.html();
						removeCookieValue(addBoxPrefix(boxname, el));
					}	
				}
				
				function closeBox(el, update) { //close and cookie (head-Element,  update cookie)
					//css
					el.nextAll().hide();
					el.removeClass(opts.active_class).addClass(opts.passive_class);
					
					if (update) {
						//cookie
						var boxname = el.html();
						addCookieValue(addBoxPrefix(boxname, el));
					}	
				}
				
				function addBoxPrefix(txt, el) {
					return el.parent().parent().attr('id') + opts.prefix_separator + txt;
				}
				
				function addCookieValue(txt) {
					var aValues = getCookieArray();
					var anz = aValues.length;
					
					var txt_all = "";
					if (anz > 0) {
						var found = false;
						for (var i = 0; i < anz; i++) {
							if (aValues[i] == txt) found = true;
						}
						if (!found) aValues[anz] = txt;
						txt_all = aValues.join(opts.cookie_separator);
					} else {
						txt_all = txt;
					}
					$.cookie(opts.cookie_name, txt_all, { expires: opts.cookie_expires });
				}
				
				function removeCookieValue(txt) {
					var aValues = getCookieArray();
					var anz = aValues.length;
					
					if (anz > 0) {
						txt_all = "";
						for (var i = 0; i < anz; i++) {
							if (aValues[i] != txt) {
								if (txt_all != "") txt_all += opts.cookie_separator;
								txt_all += aValues[i];
							}	
						}
						$.cookie(opts.cookie_name, txt_all, { expires: opts.cookie_expires });
					} 
					
				}
				
				function getCookieArray() {
					var txt_all = $.cookie(opts.cookie_name);
					if (txt_all != null) {
						return txt_all.toString().split(opts.cookie_separator.toString());
					} else {
						return [];
					}
				}
				
            });
        } 
    }); 
})(jQuery);


