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 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 eraseCookie(name) {
	createCookie(name,"",-1);
	}
function dbg_out(string){
	$('#DBG').append('<div>'+string+ '</div>');
	}
function in_array(obj, val){
	if ( !obj ) return false;
	var len = obj.length;
	for ( var x = 0 ; x < len ; x++ ) {
		if ( obj[x] == val ) return true;
		}
	return false;
	}
function array_remove(info, zitem){
	var newArray = [];
	var lc = info.length;
	for( var i=0; i<lc; i++ ){
		if ( info[i] != zitem ){
			newArray.push(info[i]);
			}
		}
	return newArray;
	}
function toggle_subtype(type_id, status){
	var obj = $('#'+type_id);
	if ( status ){
		obj.find("img").attr("src", "images/icon_cat_plus.png");
		obj.addClass('collapsed_type');
	} else {
		obj.find("img").attr("src", "images/icon_cat_minus.png");
		obj.removeClass('collapsed_type');
		}
	}
function activate(){
	$(".detail_type").unbind("click");
	$(".detail_type").click(function(){
		var type_id = $(this).parent()[0].id;
		toggle_subtype(type_id, open_type_list[type_id]);
		open_type_list[type_id] = !open_type_list[type_id];
		var string = '';
		for( type_id in open_type_list ){
			if ( !open_type_list[type_id] ){
				string += type_id + ',';
				}
			}
		createCookie('scene_search_open_subtypes',string);
		});
	$("div.detail_more").unbind("click");
	$("div.detail_more").click(function(){ $(this).parent().addClass("show_advanced"); });
	}
function print_attribute_bar(){
	$(".attribute_param_display").addClass('ClearAttribute');
	$(".attribute_param_display > .info").html("");

	if ( scene_data["star"][0] && scene_data["star"][0].length > 0 ){
		$("#stars_param").removeClass('ClearAttribute');
		var string = '';
		for(key in scene_data["star"]){
			star_id = scene_data["star"][key];
			string += "<span class=\"js_link\" onclick=\"remove_star('"+star_id+"')\">"+star_name_list[star_id]+"</span>";
			}
		$("#stars_param > .info").html(string);
		}

	var gender = false;
	for(var i=0; i<current_performer_count; i++){
		if ( performer_list[i] ){
			$("#performer_"+i+"_param").removeClass('ClearAttribute');
			$("#performer_"+i+"_param > .info").html( get_performer_attribute_string('performer_'+i, performer_list[i]) );
			$("#performer_"+i+"_param > strong > span").html("("+performer_list[i]+")");
			}
		}
	if ( scene_data["attribute"] && scene_data["attribute"][0] && scene_data["attribute"][0].length>0 ){
		string_list = get_attribute_string_list();
		var string = '';
		for( key in string_list ){
			if ( key != 'trans' ){
				// set attribute_key to key to display scene attributes seperated by type, or 0 to have them all together
				var attribute_key = 0;
				for( type_id in string_list[key] ){
					type = string_list['trans'][key][type_id];
					$("#attribute_"+attribute_key+"_param").removeClass('ClearAttribute');
					string += '<strong>'+type+':</strong> ' + string_list[key][type_id];
					}
				$("#attribute_"+attribute_key+"_param > .info").html(string);
				}
			}
		}
	$(".attribute_param_display").show();
	$(".ClearAttribute").hide();
	$(".attribute_param_display").removeClass('ClearAttribute');
	}
function get_star_info_string(){
	var string = '';
	var lc = 0;
	if ( scene_data['star'][0] ){
		lc = scene_data['star'][0].length;
		}
	for(var i=0; i<lc; i++){
		string += "&scene_star["+i+"]="+scene_data['star'][0][i];
		}
	return string;
	}
function get_performer_info_string(){
	var string = '';
	var performer_offset = 0;
	for(var i=0; i<current_performer_count; i++){
		id = 'performer_'+i;
		if ( performer_list[i] && performer_list[i].length!=undefined ){
			string += process_performer_info(performer_offset, i, id, performer_list[i]);
			performer_offset++;
			}
		}
	return string;
	}
function process_performer_info(performer_offset, NDX, id, gender){
	var string = '&scene_performer['+performer_offset+'][gender]='+performer_list[NDX];
	if ( scene_data[id] && scene_data[id][gender] && scene_data[id][gender].length ){
		for(var i=0; i<scene_data[id][gender].length; i++){
			string += '&scene_performer['+performer_offset+']['+i+']='+scene_data[id][gender][i];
			}
		}
	return string;
	}
function get_scene_info_string(){
	var string = '';
	if ( scene_data['attribute'][0] && scene_data['attribute'][0].join(',') ){
		for(var i=0; i<scene_data['attribute'][0].length; i++){
			string += '&scene_attribute['+i+']='+scene_data['attribute'][0][i];
			}
		}
	return string;
	}
function add_performer(performer, gender){
	set_current_performer(performer, gender);

	current_performer_count++;

	update_performer_display();
	}
function remove_performer(performer){
	for( var i=performer+1; i<current_performer_count; i++ ){
		if ( performer_list[i].length ){
			performer_list[i-1] = performer_list[i];
			scene_data['performer_'+i-1] = scene_data['performer_'+i];
			}
		}
	performer_list[i] = '';
	scene_data['performer_'+i] = new Array;

	current_performer_count--;

	if ( current_performer_count > 0 ){
		set_current_performer(0, performer_list[0]);
	} else {
		current_gender = false;
		}

	update_performer_display();
	}
function set_current_performer(performer, gender){
	// set_current_gender?
	current_performer = performer;
	current_item = 'performer_'+performer;
	current_gender = gender;

	performer_list[current_performer] = gender;
	}
function update_display(type){
	update_list = new Array;
	update_list['P'] = 1;
	if ( typeof type == 'string' && type=='ALL' ){
		update_list['A'] = 1;
		update_list['S'] = 1;
		}

	if ( update_list['P'] ){
		$("#performer_attribute_display").html( get_attribute_list(current_performer, current_gender) );
		}
	if ( update_list['A'] ){
		$("#scene_actions_list").html( get_attribute_list('attribute', 'A') );
		}
	if ( update_list['S'] ){
		$("#scene_setting_list").html( get_attribute_list('attribute', 'S') );
		}
	activate();
	for( id in open_type_list ){
		if ( !open_type_list[id] ){
			toggle_subtype(id, true);
			}
		}
	}
function update_performer_display(type){
	if ( current_performer_count<4 ){
		$("#performer_add_list").show();
	} else {
		$("#performer_add_list").hide();
		}

	var gender_count = new Array;
	gender_count["F"] = 0;
	gender_count["M"] = 0;
	gender_count["O"] = 0;
	gender_count["G"] = 0;

	var count_text = new Array;
	count_text.push("First", "Second", "Third", "Forth");

	var button_text = '';
	var buffer = '';
	if ( current_performer_count > 0 ){
		for(var i=0; i<current_performer_count; i++){
			if ( i==current_performer ){
				selected = ' detail_selected';
			} else {
				selected = '';
				}
			button_text = count_text[gender_count[performer_list[i]]] + ' ' + gender_xlate[performer_list[i]];
			gender_count[performer_list[i]]++;
			buffer += "<div class=\"js_link detail"+selected+"\" "
				+"onclick='set_current_performer("+i+", \""+performer_list[i]+"\");update_performer_display();'>"+button_text+"</div>";
			}
		}
	if ( buffer == '' ){
		buffer = '<div class="detail detail_selected"><em>None Selected</em></div>';
		}
	$("#performer_display").html(buffer);

	update_display(type);

	print_attribute_bar();
	}
function get_attribute_list(item, att_class){
	if ( !att_class || !attribute_id_list[att_class].length  ){ return ''; }
	var local_attribute_list = attribute_id_list[att_class];

	if ( att_class!='A' && att_class!='S' && att_class!='T' ){
		item = 'performer_'+item;
		var local_scene_data = scene_data[item][att_class];
	} else {
		var local_scene_data = scene_data[item][0];
		}

	var buffer = '';

	var last_type = false;
	var type_id_string = 0;
	for( NDX in local_attribute_list ){
		id = local_attribute_list[NDX];

		if ( last_type != attribute_info_list[id]["type"] ){
			type_id_string = attribute_info_list[id]["real_class"]+attribute_info_list[id]["type_id"];
			last_type = attribute_info_list[id]["type"];
			if ( buffer.length!=0 ){
				buffer += '<div class="js_link detail detail_more">More...</div></div>';
				}
			buffer += "<div class=\"detail_block\" id=\""+type_id_string+"\"><div class=\"js_link detail_type\">"
				+"<img src=\"images/icon_cat_minus.png\" alt=\"expand/contract "+last_type+"\" /><strong>"+last_type+"</strong></div>";
			}

		class_list = '';
		if ( !in_array(attribute_short_list[att_class],id) ){
			class_list += ' advanced_detail';
			}
		if ( in_array(local_scene_data,id) ){
			class_list += ' detail_selected';
			}

		buffer += "<div id=\"zz"+id+"\" onclick=\"toggle_attribute("+id+",'"+item+"','"+att_class+"')\" class=\"js_link detail "+class_list+"\">"
			+attribute_info_list[id]["name"]+"</div>";
		}
	if ( buffer.length!=0 ){
		buffer += '<div class="js_link detail detail_more">More...</div></div>';
		}

	if ( att_class!='A' && att_class!='S' && att_class!='T' ){
		buffer = "<div class=\"js_link detail\" onclick=\"remove_performer("+current_performer+")\"> (Remove this Performer) </div>" + buffer;
		}

	return buffer;
	}
function remove_star(id){
	scene_data['star'][0] = array_remove(scene_data['star'][0],id);

	print_attribute_bar();
	}
function toggle_attribute(id, item, att_class){
	if ( !item ){
		item = current_item;
		}
	if ( !att_class ){
		att_class = current_class;
		}
	var type = att_class;
	if ( att_class=='A' || att_class=='S' || att_class=='T'){
		att_class = 0;
		}
	if ( !scene_data[item][att_class] || !scene_data[item][att_class].length ){
		scene_data[item][att_class] = [];
		}
	if ( !in_array(scene_data[item][att_class],id) ){
		scene_data[item][att_class].push(id);
		$("#zz"+id).addClass('detail_selected');
	} else {
		scene_data[item][att_class] = array_remove(scene_data[item][att_class],id);
		$("#zz"+id).removeClass('detail_selected');
		}

	print_attribute_bar();
	}
function get_attribute_string_list(){
	var string_list = [];
	string_list['A'] = [];
	string_list['S'] = [];
	string_list['T'] = [];
	string_list['trans'] = [];
	string_list['trans']['A'] = [];
	string_list['trans']['S'] = [];
	string_list['trans']['T'] = [];


	var att_class = '';
	var seperator = '';


	if ( !scene_data['attribute'][0] ){
		return string_list;
	} else {
		var id_list = scene_data['attribute'][0];
		}
	
	for(key in id_list){
		attribute_id = id_list[key];
		att_class = attribute_info_list[attribute_id]["att_class"];
		type = attribute_info_list[attribute_id]["type"];
		type_id = attribute_info_list[attribute_id]["type_id"];
		if ( !string_list[att_class][type_id] ){
			string_list[att_class][type_id] = '';
			}
		if ( string_list[att_class][type_id].length>0 ){
			seperator = ', ';
		} else {
			seperator = '';
			}
		// [-]
		string_list[att_class][type_id] += seperator + '<span class="js_link" onclick="toggle_attribute(\''+attribute_id+'\', \'attribute\', \''+att_class+'\')">'
			+ attribute_info_list[attribute_id]["name"] + '</span>';
		string_list['trans'][att_class][type_id] = type;
		}
	return string_list;
	}
function get_performer_attribute_string(performer, gender){
	var id_list = [];
	if ( scene_data[performer][gender] && scene_data[performer][gender].length ){
		id_list = scene_data[performer][gender];
		}
	var string_list = [];

	var seperator = '';

	for(key in id_list){
		attribute_id = id_list[key];

		type = attribute_info_list[attribute_id]["type"];
		type_id = attribute_info_list[attribute_id]["type_id"];
		if ( !string_list[type] ){
			string_list[type] = '';
			}
		if ( string_list[type].length>0 ){
			seperator = ', ';
		} else {
			seperator = '';
			}
		// [-]
		string_list[type] += seperator + '<span class="js_link" onclick="toggle_attribute(\''+attribute_id+'\', \''+performer+'\', \''+gender+'\')">'
			+ attribute_info_list[attribute_id]["name"] + '</span>';
		}
	var string = '';
	for( type in string_list ){
		string += '<strong>'+type+':</strong> ';
		string += string_list[type];
		}
	return string;
	}