function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function blocklist_init(id) {
	var block = getElem(id);
	var ul = getChild(block,'ul','story_list');
	block.roundbox = getChild(block,'div','roundbox');
	block.minus = getChild(block,'a','btn_minus');
	block.plus = getChild(block,'a','btn_plus');
	block.colexp = getChild(block,'a','btn_col');
	block.content = getChild(block,'div','content cfix');
	block.showing = readCookie(id);
	block.hide = readCookie(id + '-hide');
	if (block.showing == null) { block.showing = 3; }
	if (ul != null) {
		var lis = ul.getElementsByTagName('li');
		if (lis.length < block.showing) { block.showing = lis.length; }
		block.itemNum = lis.length;
		for (i=lis.length-1;i>=0;i--) {
			lis[i].id = id + '-' + (i + 1);
			if (i>=block.showing) {
				lis[i].style.display = 'none';
			}
		}
	}
	if (block.showing == 0 || block.hide) { block.content.style.display = 'none'; }
	if (block.content.style.display == 'none') { block.hide = true } else { block.hide = false; }
	blocklist_btn_check(block);
}

function blocklist_getul_height(id) {
	var box = getElem(id);
	var ul = getChild(box,'ul','story_list');
	return getHeight(ul);
}

var blockMin = 2;

function blocklist_minus(id) {
	var block = getElem(id);
	if (block.showing <= blockMin || block.hide == true) { return; }
	if (block.hide == true) { blocklist_colexp(id); }
	getElem(id + '-' + (block.showing--)).style.display = 'none';
	if (block.showing == 0) { block.content.style.display = 'none'; }
	blocklist_btn_check(block);
	createCookie(id,block.showing,1000);
}

function blocklist_plus(id) {
	var block = getElem(id);
	if (block.showing >= block.itemNum || block.hide == true) { return; }
	if (block.hide == true) { blocklist_colexp(id); }
	getElem(id + '-' + (++block.showing)).style.display = '';
	if (block.showing == 1) { block.content.style.display = ''; }
	blocklist_btn_check(block);
	createCookie(id,block.showing,1000);
}

function blocklist_colexp(id) {
	var block = getElem(id);
	if (block.hide) {
		block.content.style.display = '';
		block.hide = false;
	} else {
		block.content.style.display = 'none';
		block.hide = true;
	}
	blocklist_btn_check(block);
	createCookie(id,block.showing,1000);
}

function blocklist_minus_check(block) {
	if (block.showing > blockMin) { enable = true; } else { enable = false; }
	if (!enable || block.hide == true) { block.minus.className = 'btn_minus_disabled'; }
	else { block.minus.className = 'btn_minus'; }
}

function blocklist_plus_check(block) {
	if (block.showing < block.itemNum) { enable = true; } else { enable = false; }
	if (!enable || block.hide == true) { block.plus.className = 'btn_plus_disabled'; }
	else { block.plus.className = 'btn_plus'; }
}

function blocklist_colexp_check(block) {
	if (block.content.style.display == 'none') {
		block.hide = true;
		block.colexp.className = 'btn_exp';
		block.roundbox.className = 'roundbox roundbox_hide';
		createCookie(block.id + '-hide', true,1000);
	} else {
		block.hide = false;
		block.colexp.className = 'btn_col';
		block.roundbox.className = 'roundbox';
		eraseCookie(block.id + '-hide');
	}
}

function blocklist_btn_check(block) {
	blocklist_colexp_check(block);
	if (block.minus != null) {
		blocklist_minus_check(block);
		blocklist_plus_check(block);
	}
}

var joblist_showitem = 5;
function joblist_init(id) {
	var block = $("#" + id);
	var ul = $("#" + id).find("div.item-list > ul")[0];
	block.attr("rotation",0);
	if (ul != null) {
		var lis = ul.getElementsByTagName('li');
		block.attr("maxrotation", Math.floor((lis.length - 1) / joblist_showitem));
		block.attr("itemNum", lis.length);
		for (i=0;i<lis.length;i++) {
			lis[i].id = id + '-' + (i + 1);
		}
	}
	joblist_rotate(id);
	pager = '';
	for (i=0;i<=block.attr("maxrotation");i++) {
		pager += '<a href="#" onClick="joblist_rotate_to(\''+id+'\','+i+');return false;" id="'+id+'-page-'+i+'">'+(i+1)+'</a>';
	}
	$("#" + id + " .headerpluspager").append('<div class="pager">'+pager+'</div>');
	$("#" + id+'-page-'+0).addClass("selected");
	
}

function joblist_rotate(id) {
	var block = $("#" + id);
	if (block.attr("maxrotation") == 0) { return; }
	$("#" + id + ' .item-list li').css("display","none");
	for (i=0;i<joblist_showitem;i++) {
		current_li_num = (block.attr("rotation") * joblist_showitem) + i + 1;
		if (current_li_num > block.attr("itemNum")) { break; }
		$("#" + id + '-' + current_li_num).css("display","block");
	}
	$("#" + id + " .headerpluspager a").removeClass("selected");
	$("#" + id+'-page-'+block.attr("rotation")).addClass("selected");

	if (block.attr("rotation") == block.attr("maxrotation")) { block.attr("rotation",0); }
	else { block.attr("rotation", parseInt(block.attr("rotation")) + 1); }

	if (block.attr("rotation_stop") == undefined) {
		joblist_rotate_timeout = setTimeout("joblist_rotate('" + id + "');",6000);
	}
}

function joblist_rotate_to(id,num) {
	var block = $("#" + id);
	block.attr("rotation",num);
	clearTimeout(joblist_rotate_timeout);
	block.attr("rotation_stop",true);
	joblist_rotate(id);
}

