/*
 * Initializes all required stuff
 */
Event.observe(window, 'load', function() {

//Initialize tooltips
	$$("a.tt").each( function(link) {
		new Tooltip(link, {mouseFollow: false, backgroundColor: "#535658", textColor: "#FFF", opacity: "1", borderColor: "#FFF"});
	});
});

//Puts the company name of the AutoComplete form in every companyName class innerHTML, so the hidden element gets the name
function setCompanyName(a) {
	
	//Gets the ID
	var companyId = a.id;
	
	if(a.id != 0) {
		company = a.innerHTML;
		$('CompanyName').value = company;
	}
	else {
		company = $('CompanyName').value;
	}
	
	//Busca todos los textos por nombre de clase
	var items = $$('.companyName');
	items.each(function(s, index) {
		s.innerHTML = company;
	}); 
	
}

//Increase count by one (For claims and votes)
function increase_count(id) {
	var count = $(id).innerHTML;
	count++;
	$(id).innerHTML = count;
}


//Share on facebook
function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function credit_discount(item, amt) {
	cr = parseInt($('available_credit').value);
	amt = parseInt(amt);
	if(item.checked) {
		result = cr - amt;
		if(result >= 0) { 
			$('available_credit').value = result;
		}
		else {
			alert("No cuentas con crédito suficiente para seleccionar esta opción. El saldo actual en tu cuenta es de: " + $('available_credit').value + "CR");
			item.checked = false;
		}
	}
	else {
		result = cr + amt;
		$('available_credit').value = result;
	}
}


//Things to do when loading window

Event.observe(window, 'load', function() {
	if($('SearchQuery')) {
		Event.observe('SearchQuery', 'focus', function() {
			fieldToggle('SearchQuery', 'Buscar reclamos, empresas o usuarios');
			$('SearchQuery').removeClassName('default');
			$('SearchQuery').addClassName('write');	
		})
		Event.observe('SearchQuery', 'blur', function() {
			fieldToggle('SearchQuery', 'Buscar reclamos, empresas o usuarios');
			if($('SearchQuery').value == 'Buscar reclamos, empresas o usuarios') {
				$('SearchQuery').removeClassName('write');
				$('SearchQuery').addClassName('default');
			}
			
		})
	}
	
});
