(function(jQuery) {
    jQuery.fn.vladabox = function(o) {
        return this.each(function() {
            new $vb(this, o);
        });
    };

    // Default configuration properties.
    var defaults = {
        displayNum: 10,
		minDisplay: 1,
        maxDisplay: 20,
		showEdit: 1,
		showPlusMinus: 1,
		initCallback: null
    };

	jQuery.vladabox = function(e, o) {
        this.options    = jQuery.extend({}, defaults, o || {});
		this.container	 = null;
		this.head	 	 = null;
        this.editButton  = null;
        this.plusButton  = null;
        this.minMutton   = null;
		this.checkAll 	 = null;
        this.editBody	 = null;
        this.boxContent	 = null;
		this.boxName	 = null;
		this.checkboxes	 = null;
		this.checkwoall	 = null;
		this.labels	 = null;

        this.container = jQuery(e);
        
		this.headBox = jQuery('.heading', this.container);
		this.head = jQuery('.buttons', this.headBox);
		
		this.editButton = jQuery('.edit', this.head);
		this.plusButton = jQuery('.more', this.head);
		this.minMutton = jQuery('.less', this.head);
		this.editBody = jQuery('.editBody', this.container);
		this.boxContent = jQuery('.short', this.container);
		this.boxName = this.container.attr("id");
		this.checkAll = jQuery(".checkAll input", this.editBody);
		this.labels = jQuery('label', this.editBody);
		this.checkboxes = jQuery("input[name='categories']", this.editBody);
		this.checkwoall = jQuery("input[name=categories][value!=all]", this.editBody);
		this.funcEdit   = function() { self.edit(); };
        this.funcPlus   = function() { self.plus(); };
        this.funcMinus = function() { self.minus(); };

		if (this.options.initCallback != null)
            this.options.initCallback(this, 'init');
		
        this.setup();
    };
	
    var $vb = jQuery.vladabox;

    $vb.fn = $vb.prototype = {
        vladabox: '1.0.1'
    };

    $vb.fn.extend = $vb.extend = jQuery.extend;

    $vb.fn.extend({
	
	    setup: function() {

			if (!this.options.showEdit) 
				this.editButton.css('display', 'none');
			
			if (!this.options.showPlusMinus) {
				this.plusButton.css('display', 'none');
				this.minMutton.css('display', 'none');
			}
				
			if (!jQuery.cookie(this.boxName + 'showNum')) 
				 jQuery.cookie(this.boxName + 'showNum', this.options.displayNum, { expires: 100});
			
			this.checkboxset('normal');
			this.showcatset('normal');
			this.editBody.css("opacity", 0);
        },
		
		reset: function() {
			jQuery.cookie(this.boxName + 'showNum', this.options.displayNum, { expires: 100});
			this.checkboxset('reset');
			this.showcatset('reset');
		},
		
		edit: function() {
			this.checkboxes.css('display', 'none');
			if(this.editBody.css("opacity") == 0){
				this.editBody.animate({
					height: 'show', opacity: 1
				}, 500, "swing");
			}else{
				this.editBody.animate({
					height: 'hide', opacity: 0
				}, 500, "swing");
			}
			
			
			return false;
        },
		
		plus: function() {
			var self = this;
			var found = false;
			var hiddenCat = false;
			var next = true;
			var showNum = jQuery.cookie(this.boxName + 'showNum');
			var display = '';
			var childrenNum = this.boxContent.children().size();
			
			this.boxContent.children().each(function() {
				hiddenCat = false;
				var cat = jQuery(this).attr("rel");
				if (jQuery.cookie(self.boxName + '_catView_' + cat) === "none") 
					hiddenCat = true;
				
				display = jQuery(this).css("display");
				if (display === "none" && found === false && hiddenCat === false && showNum < self.options.maxDisplay) {
					jQuery(this).css("display", "block");
					showNum++;
					jQuery.cookie(self.boxName + 'showNum', showNum, { expires: 100});
					found = true;
				}
				if (jQuery(this).css("display") === "block" && showNum < childrenNum) {
					jQuery(this).removeClass("noBorder");
				}
				if (found === true && next === true) {
					jQuery(this).addClass("noBorder");
					next = false;
				}
			});
        },
		
		minus: function() {
			var self = this;
			var found = false;
			var hiddenCat = false;
			var next = true;
			var showNum = jQuery.cookie(this.boxName + 'showNum');
			var display = '';

			this.boxContent.children().reverse().each(function() {
				
				hiddenCat = false;
				var cat = jQuery(this).attr("rel");
				if (jQuery.cookie(self.boxName + '_catView_' + cat) === "none") 
					hiddenCat = true;
				
				display = jQuery(this).css("display");
				
				if (found === true && next === true) {
					jQuery(this).addClass("noBorder");
					next = false;
				}
				
				if (display === "block" && found === false && hiddenCat === false && showNum > self.options.minDisplay) {
					jQuery(this).css("display", "none");
					showNum--;
					jQuery.cookie(self.boxName + 'showNum', showNum, { expires: 100});
					found = true;
				}
			});
        },
		
		checkunchech: function() {
			var self = this;
			var state = true;
			if(this.checkAll.is(":checked")) {
				this.checkwoall.each(function() { 
					if(jQuery.browser.msie){state=false;}
					var checkVal = jQuery(this).val();
					jQuery("input[name=categories][value="+checkVal+"]", self.editBody).attr('checked', state);
				});
			} else {
				this.checkwoall.each(function() {
					if(!jQuery.browser.msie){state=false;}
					var checkVal = jQuery(this).val();
					jQuery("input[name=categories][value="+checkVal+"]", self.editBody).attr('checked', state);
				});
			}
		},
		
		checkboxset: function(setup) {
			var self = this;
			
			if (jQuery.cookie(this.boxName + '_catView_all') === 'none' && setup === 'normal') {
				this.checkwoall.attr('checked', false);
			} else {
				this.checkwoall.attr('checked', true);
			}
			
			this.checkwoall.each(function() { 
				var checkVal = jQuery(this).val();
				if (jQuery.cookie(self.boxName + '_catView_' + checkVal) === 'none' && setup === 'normal') {
					jQuery("input[name=categories][value="+checkVal+"]", self.editBody).attr('checked', false);
				}
				else {
					jQuery("input[name=categories][value="+checkVal+"]", self.editBody).attr('checked', true);
				}
			});
		},
		
		showcatset: function(setup) {
			var showNum = jQuery.cookie(this.boxName + 'showNum');
			var pos = 1;
			var counter = 1;
			var self = this;
			var boxChildren = this.boxContent.children();
			boxChildren.each(function() {
				var catClass = jQuery(this).attr("rel");
				var showCat = jQuery.cookie(self.boxName + '_catView_' + catClass);
				
				if (pos <= showNum && showCat !== "none" && setup === 'normal') {
					jQuery(this).show();
					pos++;
				}
				else if (pos <= showNum && setup === 'reset'){
					jQuery(this).show();
					pos++;
				}
				else {
					jQuery(this).hide();
				}
				
			});
			
			boxChildren.each(function() {
				if (jQuery(this).css("display") === "block") 
					counter++;
				if (counter === pos) {
					jQuery(this).addClass("noBorder");
				}
				else {
					jQuery(this).removeClass("noBorder");
				}
			});
		},
		
		processCheckboxes: function(checkbox) {
			var checkVal = checkbox.val();
			var self = this;
			var state;
			if (checkVal === "all") {
				if (checkbox.attr('checked') === true) {state = 'block';} else {state = 'none';}
				jQuery.cookie(this.boxName + '_catView_all', state, { expires: 100});
				this.checkwoall.each(function() {  
					jQuery.cookie(self.boxName + '_catView_' + jQuery(this).val(), state, { expires: 100});
					self.showcatset('normal');
				});
			} else {
				if (checkbox.attr('checked') === true) {
					jQuery.cookie(this.boxName + '_catView_' + checkVal, 'block', { expires: 100});
					this.showcatset('normal');
				}
				else {
					jQuery.cookie(this.boxName + '_catView_' + checkVal, 'none', { expires: 100});
					this.showcatset('normal');
				}
			}
		}
	});
	$vb.extend({
        defaults: function(d) {
            return jQuery.extend(defaults, d || {});
        }
	});
})(jQuery);