/**** my functions ****/
function addBehaviours()
{
	evenup();
	
	$(".start-hidden").toggle();
	
	$("a img").parent().addClass('imglink');

	$("a.delete").click(function(event)
	{
		event.preventDefault(); //we never want this to go through in a js enabled browser
		attr = $(this).attr("href");
		cut = attr.indexOf("?")+1; 
		attr = attr.substr(cut);
		attrs = getParameters(attr);
		type = attrs[0];
		del_id = attrs[1];
		name = unescape(attrs[3]);
		//ask for confirmation
		if(confirm('Delete '+type+' '+name+'?'))
		{
			//put to form and submit
			$("#del_id").val(del_id);
			$("#form_delete").submit();
		}		
	}
	)
	
	// alert('behaviours');
}

function getParameters(querystring)
{
	results = new Array();
	breakup = querystring.split("&");
	i=0;
	for(x in breakup)
	{
		y = breakup[x].split("=");
		results[i] = y[0];
		results[i+1] = y[1];
		i+=2;
	}
	return results;
}

function evenup()
{
	//get an array containing all columns which need to be matched at the first level
	i = 1;
	matches = $('.match-'+i);
	
	//limit the number of iterations
	// can be done with a 'while' or with a 'for', if the identifiers are not sequential
	while(matches.size() > 0)
	{	
		//find the tallest column
		var maxHeight = 0;
		matches.each(function(){
			maxHeight = Math.max($(this).outerHeight(), maxHeight);
			}
		);
		
		//set all the columns to this height
		matches.each(function(){
			outers = $(this).outerHeight() - $(this).height();
			set = maxHeight - outers;
			$(this).height(set);
			});
			
		//next set	
		i++;
		matches = $('.match-'+i);
	}
}

function price_total(val)
{
	val=parseFloat(val);
	if(val<=0 || isNaN(val))
	{
		val = 0;
	}
	else
	{
		// var price_drum declared in page
		price = val * Math.floor(price_drum*100+0.50000000001);
		cents = price%100;
		if(cents<10)
		{
			cents = "0" + cents;
		}
		price = Math.floor(price/100).toString();
		//add thousands separator
		for (var i = 0; i < Math.floor((price.length-(1+i))/3); i++)
		{
			price = price.substring(0,price.length-(4*i+3))+','+ price.substring(price.length-(4*i+3));
		}
		
		document.getElementById('tot_price').innerHTML = '$ ' + price +'.'+ cents;
	}
	return;
}

function clearTextField(f,d)
{
	if($(f).val()==d) // is default value
	{
		$(f).val(''); // empty field
		// add onblur function with default value
		$(f).blur(function()
		{
			if($(f).val()=='') // is empty
			{
				$(f).val(d); // replace default value
				$(f).focus(function()
				{
					clearTextField(f,d); // add onfocus function
				})
				$(f).blur(function(){}) // remove blur function
				return;
			}
		})
		
		$(f).focus(function(){}) // remove onfocus function
	}
	return;	
}

/**** Set it all in motion *****/
$(document).ready(
	function()
	{
		addBehaviours();
	}
)
