$j = jQuery.noConflict();

$j(document).ready(function(){
	var _boxs = $j('#sidebar div.box:has(span.show-box)');
	var _slideDuration = 300;
	
	if ($j.cookie('cookiesBoxs')) {
		var _txt = $j.cookie('cookiesBoxs');
		_txt = _txt.split('|');
		for (var i=0; i<_txt.length; i++) {
			if (_txt[i]) {
				var _title = _txt[i].substr(0,_txt[i].indexOf('='));
				_txt[i] = _txt[i].replace(_title+'=','');
				if (_txt[i] == 'open') {
					_boxs.find('h3:contains('+_title+')').parent().find('span.show-box a').addClass('selected');
				} else {
					_boxs.find('h3:contains('+_title+')').parent().find('span.show-box a').removeClass('selected');
				}
			}
		}
	}
	
	_boxs.each(function(i,box){
		var _opener = $j('span.show-box a',box),
			_slider = $j('div.slide',box);
			
		if (!_opener.hasClass('selected')) _slider.hide();
		_opener.click(function(){
			if ($j(this).hasClass('selected')) {
				_slider.slideUp(_slideDuration);
				$j(this).removeClass('selected');
			} else {
				_slider.slideDown(_slideDuration);
				$j(this).addClass('selected');
			}
			saveCookies();
			return false;
		});
		
	});
	
	function saveCookies() {
		var _cookiesBoxs = '';
		_boxs.each(function(){
			var _title = $j('h3',this).text();
			if ($j('span.show-box a',this).hasClass('selected')) {
				_cookiesBoxs += _title+'=open|'
			} else {
				_cookiesBoxs += _title+'=close|'
			}
		});
		$j.cookie('cookiesBoxs', _cookiesBoxs, {expires: 7});
	}
	
	// content slide comment ***************************************************
	var _contentBox = $j('#content div.block');
	_contentBox.each(function(){
		var _opener = $j('ul.comment-holder a',this),
			_slide = $j('> div.comment-box',this),
			_close = $j('a.close-comment',this);
		_opener.click(function(){
			_slide.slideToggle(_slideDuration);
			return false;
		});
		_close.click(function(){
			_slide.slideUp(_slideDuration);
			return false;
		});
	});
});

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

