/**
 * @version  1.00
 * @updated  2008/04/24
 */


Searches = {
	init: function () {
		Searches.searchbox();
		if ($('#facebox input').size()) {
			$('#facebox input').val($('#artist_search_fm').val());
			Searches.load();
		}

	},	
	
		
	searchbox: function () {
	
		$('div#facebox  ul.sb_categories a').click(function () {
			var on = $('div#facebox ul.sb_categories a.on').get(0);
			this.className = (this.className == '') ? 'on' : '';
			if (on) on.className = '';

			Searches.searchbox_btn();
			return false;
		});
		
		$('div#facebox ul.sb_keywords a').click(function () {
			this.className = (this.className == '') ? 'on' : '';
			
			Searches.searchbox_btn();
			return false;
		});
		
		$('div#facebox a.sb_search_btn, a.sb_research_btn').click(function () {
			if (this.parentNode.className == '') {
				Searches.load();
			}
			return false;
		});
	},



	searchbox_btn: function () {
		var btn = $('div#facebox a.sb_search_btn').get(0);
		if ($('div#facebox ul.sb_keywords a.on').length == 0 && $('div#facebox ul.sb_categories a.on').length == 0) {
			btn.parentNode.className = 'dis';
			$(btn).removeAttr('href');
		} else {
			btn.parentNode.className = '';
			$(btn).attr('href', '#');
		}
	},
	

	
	load: function () {
		Searches.loaderStart();
		Searches.xml();
		
		jQuery.ajax({
			url     : xml_url,
			type    : 'GET',
			dataType: 'xml',
			cache   : false,
			success : function (xml) {
				sb_data = Searches.toArray(xml.lastChild);
				Searches.list(sb_data);
			}
		});
	},
	
	
	
	xml: function () {
		var nm     = (!$('#facebox input.artist_search').get(0) || $('#facebox input.artist_search').val() == '') ? '-' : encodeURIComponent($('#facebox input').val());
		
		var cat_on = $('div#facebox ul.sb_categories a.on');
		if (cat_on.get(0)) var cat_id = cat_on.get(0).parentNode.id.replace('c_','');
		
		if (cat_on.get(0)) {
			switch (cat_id){
				case '2-M': var cat = '2';    var gen = 'm'; break;
				case '2-F': var cat = '2';    var gen = 'f'; break;
				default   : var cat = cat_id; var gen = '-';
			}
		} else {
			var cat = '-'; var gen = '-';
		}		

		var kw_num = [];
		var kw_on  = $('div#facebox ul.sb_keywords a.on');
		if (kw_on.length > 0) {
			$(kw_on).each(function (i) {
				if (i == kw_on.length-1) {
					kw_num.push(this.parentNode.id.replace('kw_',''));
				} else {
					kw_num.push(this.parentNode.id.replace('kw_','') + ',');
				}
			});
		}
		var kw = (kw_num.join('') == '') ? '-' : kw_num.join('');
				
		xml_url = '/artist/api/search/n/' + nm + '/g/' + gen + '/c/' + cat + '/k/' + kw;
	},
	
	
	
	toArray: function (node, self) {
		var r    = {};
		var self = self || this.toArray;
		for (var attr, i = 0; attr = node.attributes[i]; i++)
			r[attr.name] = node.getAttribute(attr.name);
		
		var tags = [];
		jQuery(node).children().each(function () {
			if (!r[this.nodeName])
				r[this.nodeName] = [], tags.push(this.nodeName);
			
			var data = { text: jQuery(this).text() };
			for (var attr, i = 0; attr = this.attributes[i]; i++)
				data[attr.name] = this.getAttribute(attr.name);
			
			r[this.nodeName].push(jQuery(this).children().size() ? self(this, self) : data);
		});
		
		for (var tag, i = 0; tag = tags[i]; i++) {
			if (r[tag].length == 1) {
				for (var key in r[tag][0])
					r[tag][key] = r[tag][0][key];
			}
		}
		
		return r;
	},



	loaderStart: function () {
			$('div#facebox .sb_loader div.sb_inside p').show();
			$('div#facebox .sb_loader').show();
			$('div#facebox .sb_result').hide();
	},
	

	
	loaderEnd: function () {
		$('div#facebox .sb_loader div.sb_inside p').animate(
			{ height: $('div#facebox .sb_result').height() }, 'slow'
		).fadeOut('normal', function () {
			$('div#facebox .sb_loader').hide();
			$('div#facebox .sb_result').show();
		});
	},	

		
	
	list: function (sb_data) {
		if (!sb_data.artist || $('div#facebox input.artist_search').val() == '') {
			$('div#facebox .result_title').html('<p class="sb_result">' + Searches.lang1 + '</p>');
			$('div#facebox ul.sb_artist').html('');
			$('div#facebox ul.sb_pages').css({ display : 'none' });
		} else {
			var html = [];
				$(sb_data.artist).each(function (i) {
					html.push(((i + 1) % 18 == 1) ? '<div><li>' : '<li>');
					html.push('<a href="' + this.href + '" class="sb_img"><span style="background-image: url(' + ((!this.src) ? '/artist/img/anonymous/artist_tmb.gif' : this.src) + ');"></span></a>');
					html.push('<p class="sb_name"><a href="' + this.href + '">' + this.text + '</a></li>');
					html.push(((i + 1) % 18 == 0) ? '</li></div>' : '</li>');
				});
				
			$('div#facebox ul.sb_artist').html(html.join(''));
		
			Searches.title();
			Searches.pages();
		}
		
		Searches.loaderEnd();
	},
	

	
	title: function () {
		var html = [];
		var on   = $('div#facebox ul.sb_categories a.on, div#facebox ul.sb_keywords a.on');
		
		html.push('<p class="sb_words">');
		if ($('#facebox input.artist_search').get(0)) {
			html.push('<strong>[' + $('div#facebox input.artist_search').val() +']</strong> ');
		} else {
			$(on).each(function (i) {
				if (i == on.length-1) {
					html.push('<strong>[' + this.innerHTML +']</strong> ');
				} else {
					html.push('<strong>[' + this.innerHTML +']</strong> + ');
				}
			});
		}
		html.push(Searches.lang2);
		html.push('</p>');
		html.push('<p class="sb_result"><strong>' + $('div#facebox ul.sb_artist li').length + Searches.lang3);
		html.push('</p>');
		
		$('div#facebox .result_title').html(html.join(''));
	},
	
	
	
	pages: function () {
		$('div#facebox ul.sb_pages').css({ display : '' });
		
		var html = [];
			html.push('<li id="back"><a href="#">' + Searches.lang4 + '</a></li>');
			$('div#facebox ul.sb_artist div').each(function (i) {
				if (i == 0) this.className = 'current';
				$(this).get(0).id = 'pg_set_' + (i + 1);
				html.push('<li id="pg_' + (i + 1) + '"><a href="#" ' + ((i == 0) ? ' class="current"' : '') + '>' + (i + 1) + '</a></li>');
			});
			html.push('<li id="next"><a href="#">' + Searches.lang5 + '</a></li>');
		$('div#facebox ul.sb_pages').html(html.join(''));
		
		
		$('div#facebox ul.sb_pages li a').click(function () {
			if (this.className == 'current') return false; 
			var current    = parseFloat($('div#facebox ul.sb_pages li a.current').html());
			var list       = this.parentNode.id.replace('pg_','');
			var back_list  = current - 1;
			var next_list  = current + 1;
			var pg_length  = $('div#facebox ul.sb_pages li a').size();
			
			
			
			if (this.parentNode.id == 'back' && back_list == 0) {
				return false;
			} else if (this.parentNode.id == 'next' && next_list > pg_length - 2) {
				return false;
			} else {
				Searches.loaderStart();
						
				$('div#facebox ul.sb_artist div.current').get(0).className = '';
				switch (this.parentNode.id) {
					case 'back' : $('div#facebox ul.sb_artist div#pg_set_' + back_list).get(0).className = 'current'; break;
					case 'next' : $('div#facebox ul.sb_artist div#pg_set_' + next_list).get(0).className = 'current'; break;
					default     : $('div#facebox ul.sb_artist div#pg_set_' + list).get(0).className = 'current';
				}
			
				var div_current = $('div#facebox ul.sb_artist div.current').get(0).id.replace('pg_set_','');
				$('div#facebox ul.sb_pages li a.current').get(0).className = '';
				$('div#facebox ul.sb_pages li#pg_' + div_current + ' a').get(0).className = 'current';
				Searches.loaderEnd();
					
				return false;
			}
		});
	}
}