// 
//  selector.js
//  ipb
//  
//  Created by Tim Smart on 2009-02-21.
//  Copyright 2009 Tim Smart. All rights reserved.
// 

// =====================
// = Hardware Selector =
// =====================
function Hardware_selector()
{
	this.step=1;
	this.details=[];
	this.details_name=['','','hwCa','hwMa','hwSC'];
	this.eid="";
	this.ajax_url=location.href.match(new RegExp('.*hardware/'))[0]+'includes/hw/ajax.php';
	this.show=function()
	{
		this.step=1;
		$('#hardware_pop_content').html('');
		$('#hardware_pop_loading').show();
		hardware_view.show();
		$.ajax({
			type:"POST",
			url:this.ajax_url,
			data:"act=select&step="+this.step,
			success:function(resp)
			{
				hardware_view.details(resp);
				selector.add_events(selector.step);
				return;
			}
		});
		return;
	};
	this.add_events=function(step)
	{
		$('.hardware_selector_link').click(function()
		{
			$('#hardware_pop_wrap').hide();
			var item={};
			item.id=parseInt(this.getAttribute('hwid'),0);
			item.name=this.getAttribute('hwname');
			selector.insert_item(item);
			$('#hardware_pop_content').html('');
			$('#hardware_pop_loading').show();
			return;
		});
		$('#hardware_selector_submit').click(function()
		{
			selector.step++;
			selector.details[selector.step]=$('#hardware_selector_select').val();
			$('#hardware_pop_content').html('');
			$('#hardware_pop_loading').show();
			$.ajax({
				type:"POST",
				url:selector.ajax_url,
				data:selector.ajax_nvp(selector.step),
				success:function(resp)
				{
					hardware_view.details(resp);
					selector.add_events(selector.step);
					return;
				}
			});
			return;
		});
		$('#hardware_selector_back').click(function()
		{
			selector.step=1;
			$('#hardware_pop_content').html('');
			$('#hardware_pop_loading').show();
			$.ajax({
				type:"POST",
				url:selector.ajax_url,
				data:selector.ajax_nvp(selector.step),
				success:function(resp)
				{
					hardware_view.details(resp);
					selector.add_events(selector.step);
					return;
				}
			});
			return;
		});
		$('#hardware_selector_back_step').click(function()
		{
			selector.step--;
			$('#hardware_pop_content').html('');
			$('#hardware_pop_loading').show();
			$.ajax({
				type:"POST",
				url:selector.ajax_url,
				data:selector.ajax_nvp(selector.step),
				success:function(resp)
				{
					hardware_view.details(resp);
					selector.add_events(selector.step);
					return;
				}
			});
			return;
		});
		if(step!=1)
		{
			
		}
		return;
	};
	this.ajax_nvp=function(step)
	{
		step=parseInt(step,0);
		nvp="act=select&step="+(step)+"&";
		for(var i=2;i<=step;i++)
		{
			nvp+=encodeURIComponent(this.details_name[i])+"="+this.details[i]+"&";
		}
		return nvp;
	};
	this.insert_item=function(item)
	{
		var div=document.createElement('div');
		div.className='hardware_related';
		var input=document.createElement('input');
		input.type='hidden';
		input.value=item.id;
		div.appendChild(input);
		div.appendChild(document.createTextNode(item.name+' '));
		var link=document.createElement('a');
		link.href="#";
		$(link).click(function()
		{
			selector.remove_item(this);
			return false;
		});
		link.appendChild(document.createTextNode('[Remove]'));
		div.appendChild(link);
		document.getElementById('hardware_related_wrap').appendChild(div);
		selector.update_items();
		return;
	};
	this.update_items=function()
	{
		$('.hardware_related input').each(function(i)
		{
			this.name='related'+i;
			return;
		});
		return;
	};
	this.remove_item=function(link)
	{
		var div=link.parentNode;
		div.parentNode.removeChild(div);
		selector.update_items();
		return;
	};
	return;
};

// Init everything
var selector=new Hardware_selector();
$(document).ready(function()
{
	$('.hardware_selector').click(function()
	{
		selector.show();
		return false;
	});
	$('.hardware_remove').click(function()
	{
		selector.remove_item(this);
		return false;
	});
	return;
});