var yk_url_root = '';
var yk_url_fileSearch = '_admin_media_search.php';

function yk_media()
{
	this.title = '';
	this.description = '';
	this.thumb = '';
	this.tags = '';
	this.id = 0;
	this.code = '';
}

function yk_mediaInput_select(id,imageId)
{
	v = document.getElementById(id);
	if(v)
	{
		i = document.getElementById(id+'_image'+v.value);
		if(i)
		{
			i.className = 'bld_yakasite_mediaInput_element';
		}
		
		i = document.getElementById(id+'_image'+imageId);
		if(i)
		{
			i.className = 'bld_yakasite_mediaInput_elementSelected';
		}
		v.value = imageId;
	}
}

function yk_mediaInput_unselect(id)
{
	v = document.getElementById(id);
	if(v)
	{
		i = document.getElementById(id+'_image'+v.value);
		if(i)
		{
			i.className = 'bld_yakasite_mediaInput_element';
		}
		
		v.value = '';
	}
}

function yk_mediaInput_buildElement(id,image,selected)
{
	var className;
	if(selected) className = 'bld_yakasite_mediaInput_elementSelected';
	else className = 'bld_yakasite_mediaInput_element';
	
	var code = '<table class="'+className+'" id="'+id+'_image'+image.id+'" onclick="yk_mediaInput_select(\''+id+'\',\''+image.id+'\');"><tr>';
	code += "<th><img src=\""+yk_url_root+image.thumb+"\" /></th>";
	code += '<td><div class="title">'+image.title+'</div><div class="description">'+image.description+'</div><div class="tags">'+image.tags+'</div></td>';
	code += '</tr></table>';
	
	return code;
}

function yk_mediaInput_search(id,searchWord)
{
	o = document.getElementById(id+'_list');
	selectedImage = document.getElementById(id).value;
	optionFiletype = document.getElementById(id+'_optionFiletype').value;
	optionSkip = document.getElementById(id+'_optionSkip').value;
	
	var xhr = bldAjax_connexion();
	xhr.onreadystatechange = function()
	{		
		if (xhr.readyState == 4)
		{
			if (xhr.status == 200)
			{
				var doc = xhr.responseXML;
				var element = doc.getElementsByTagName('answerData').item(0);
				
				o.className = 'bld_yakasite_mediaInput_list';
				o.innerHTML = '';
				
				var result = new ajaxResult;
				result.error = bldAjax_error(doc);			
				
				for(var i=0; i < element.childNodes.length; i++)
				{
					if(element.childNodes[i].nodeName == 'element')
					{
						r = new yk_media;
						
						for(var j=0; j < element.childNodes[i].childNodes.length; j++)
						{
							if(!element.childNodes[i].childNodes[j].childNodes[0]) continue;
							
							switch(element.childNodes[i].childNodes[j].nodeName)
							{
								case 'id':
									r.id = element.childNodes[i].childNodes[j].childNodes[0].nodeValue;
									break;
								case 'thumb':
									r.thumb = element.childNodes[i].childNodes[j].childNodes[0].nodeValue;
									break;
								case 'title':
									r.title = element.childNodes[i].childNodes[j].childNodes[0].nodeValue;
									break;
								case 'description':
									r.description = element.childNodes[i].childNodes[j].childNodes[0].nodeValue;
									break;
								case 'tags':
									r.tags = element.childNodes[i].childNodes[j].childNodes[0].nodeValue;
									break;
							}
						}
						
						if(selectedImage == r.id)
						{
							o.innerHTML += yk_mediaInput_buildElement(id,r,true);
						}
						else
						{
							o.innerHTML += yk_mediaInput_buildElement(id,r,false);
						}
					}
				}
			}
			else
			{
				o.className = 'bld_yakasite_mediaList';
				o.innerHTML = '';
			}
		}
		else
		{
			o.className = 'bld_yakasite_ajaxLoad';
			o.innerHTML = '';
		}
	}
	
	var url = yk_url_root+yk_url_fileSearch+'?search='+searchWord;
	
	if(optionSkip != '')
	{
		url += '&skip='+optionSkip;
	}
	if(optionFiletype != '')
	{
		url += '&filetype='+optionFiletype;
	}
	if(document.getElementById(id).value != '')
	{
		url += '&select='+document.getElementById(id).value;
	}

	xhr.open('GET',url,true);
	xhr.send(null);
}

