/*** JQUERY ***/
$(document).ready(function(){

	$('#maincategories').change(function(){		
		maincatid = $('#maincategories').attr("value");
		subcatid = 0;
		refresh_categories();
	});
	
	
}); // end of JQUERY


function refresh_categories() {	

	$('#subcategories').empty();
	$('#subcategories').append("<option value='0'>---</option>");

	if (maincatid != 0) {
		
		var results_1 = new Array;
		var counter = 0;
		for (subid in catdata) {
		
			if (catdata[subid][1] == maincatid) {
				// save in array
				results_1[counter++] = catdata[subid][2] + "_" + subid;									
			} // end of if
		} // end of for
		
		/* SORT SUB-CATEGORIES ALPHANUMERICALLY */
		results_1.sort();
		for (var i in results_1) {
			if (i != "indexOf") {
				var subid = results_1[i].replace(/.*_(\d+)/, "$1");
				var subcatname = results_1[i].replace(/(.*)_\d+/, "$1");
				$('#subcategories').append("<option value='" + subid + "'>" + subcatname + "</option>");
				
				var results_2 = new Array;
				var counter_2 = 0;
				for (subsubid in catdata) {
					
					if (catdata[subsubid][1] == subid) {
						// save in array
						results_2[counter_2++]	 = catdata[subsubid][2] + "_" + subsubid;						
					}					
				}
				/* SORT SUBSUB-CATEGORIES ALPHANUMERICALLY */	
				results_2.sort();
				for (var j in results_2) {
					if (j != "indexOf") {
						var subsubid = results_2[j].replace(/.*_(\d+)/, "$1");
						var subsubcatname = results_2[j].replace(/(.*)_\d+/, "$1");
						$('#subcategories').append("<option value='" + subsubid + "'>&raquo; " + subsubcatname + "</option>");
					}
				}
			}
		} // end of for
	} // end of if	

	$('#maincategories').attr("value", maincatid);
	$('#subcategories').attr("value", subcatid);	
}

// start voting
function bwv_set_voting(value, unique) { 

	if (window["_bwv_rate_flag_" + unique] == 1) {

		window["_bwv_rate_flag_" + unique] = 2; // set voting -> WAIT
		xml_request('controller.pl?o=xml_member_directory&a=save_voting&objectid=' + window["_bwv_objectid_" + unique] + "&value=" + value + "&unique=" + unique + '&sess=' + session);
	}		
}	

// xml-request
function xml_request(sendurl) 
{	
	$.get(sendurl, function(xml){				
		xml_processor(xml);					
	});			
}

// xml-processor
function xml_processor(xml) 
{	 
	/* DEBUG FLAG: ON = 1; OFF = 0 */
	var debug = 0;

	if (debug == 1) alert(xml);

	$(xml).find('action').each(function() {
	
		var command = $('command', this).text();
		var this_p = this;

		// #######################
		// VOTING
		if (command == "voting") {
			
			if (debug) { alert("voting"); }

			var tmp_rating = $('rating', this_p).text();
			var tmp_votes = $('votes', this_p).text();
			var tmp_rating_value = $('value', this_p).text();
			var unique = $('unique', this_p).text();
			
			// only if rating is correctly
			if (tmp_rating != '-1' && tmp_votes != '-1' && tmp_rating_value != '-1') {
				
				// save new values
				window["_bwv_rating_" + unique] = tmp_rating;
				window["_bwv_votes_" + unique] = tmp_votes;
				window["_bwv_rating_value_" + unique] = tmp_rating_value;
			}
			
			bwv_set_stars_back(unique);					

			for (var i = 1; i <= 5; i++) {
				
				$("#bwv_star_" + i + "_" + unique).css("cursor", "default");
			}
			
			window["_bwv_rate_flag_" + unique] = 0; // set rating OFF
			bwv_set_votes(unique);
			
		} // end of voting
		// #######################	
	
	}); // end of action
	
	request_active = 0;
	
} // end of xml_processor
		
