function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function switchImage(image, target) {
	document.getElementById(target).src = image;
}

function highlightTR(what, class_name) {
	if (document.getElementById(what)) {
		document.getElementById(what).className = class_name;
	}
}

function addCompanyAndClose(company_id, category_id, event_type_id) {
	parent.addCompany(company_id, category_id, event_type_id);
	
	self.parent.tb_remove();
	
	var location = parent.window.location.href.replace('#agenda', '');
	parent.window.location.href = location + '#agenda';
}

function newsletter() {
	tb_show(null, 'registro-newsletter.html?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=360&width=552&modal=true', null);
}
function addEvent() {
	tb_show(null, 'registro-eventos.html?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=500&width=600&modal=true', null);
}

function goUrl(url) {
	self.parent.tb_remove();
	
	parent.window.location.href = url;
}

function changeParent(url) {
	parent.window.location.href = url;
}

function changeImage(src, alt) {
	var img = new Image();
	
	$("#description").html('&nbsp;');
	$("#works_image").fadeOut("fast", function() {
		$("#loading").fadeIn("slow", function() {
			$("#works_image").attr("src", src);
			$("#works_image").attr("alt", alt);
			
			/*
			$("#works_image").onload = function() {
				$("#loading").fadeOut("slow", function() {
					$("#works_image").show();

					if (alt) {
						$("#description").html(alt);
					}else{
						$("#description").html('&nbsp;');
					}
				});
			};
			*/
			
			img.onload = function() {
				$("#loading").fadeOut("slow", function() {
					$("#works_image").show();

					if (alt) {
						$("#description").html(alt);
					}else{
						$("#description").html('&nbsp;');
					}
				});
			}
			
			img.src = src;
		});
	});
}

function addCompany(company_id, category_id, event_type_id) {
	$("#added_companies_info").fadeOut("slow", function() {
		// $("#loading").fadeIn("slow");
		
		$.ajax({
			url: 'add-company.php', 
			type: 'POST', 
			data: 'company_id='+company_id+'&category_id='+category_id+'&event_type_id='+event_type_id, 
			dataType: 'html', 
			
			success: function(html) {
					if (html == "1") {
						$("#companies_counter").html(html + ' empresa agendada');
					}else{
						$("#companies_counter").html(html + ' empresas agendadas');
					}
					$("#added_companies_info").fadeIn("slow");
			}
		});
	});
}

function deleteCompany(event_type, main, category, id, counter) {
	$.ajax({
		url: 'delete-company.php', 
		type: 'POST', 
		data: 'event_type='+event_type+'&main='+main+'&category='+category+'&id='+id, 
		dataType: 'html', 
		
		success: function(html) {
			if (html == "empty") {
				$("#tr_"+counter).slideUp("slow", function() {
					window.location.reload();
				});
			}else{
				$("#tr_"+counter).slideUp();
			}
		}
	});
}

function showCategories(event_type_id) {
	if ($("div.categories:visible").length != 0) {
		do {
			$("div.categories").hide();
		} while ($("div.categories:visible").length != 0);
	}
	
	$("#categories_"+event_type_id).fadeIn("slow");
}

function showHide(toshow, tohide) {
	$("div.categories").hide(function() {
		
	});
	
	if ($("#"+tohide).length) {
		$("#"+tohide).fadeOut("slow", function () {
			$("#"+toshow).fadeIn("slow");
		});
	}else{
		$("#"+toshow).fadeIn("slow");
	}
}

MM_preloadImages('../images/services/icon_articles_f2.gif','../images/services/icon_blueprints_f2.gif','../images/services/icon_churchs_f2.gif','../images/services/icon_faqs_f2.gif','../images/services/icon_gallery_f2.gif','../images/services/icon_greetings_f2.gif','../images/services/icon_testimonials_f2.gif','../images/services/icon_weather_f2.gif');
MM_preloadImages('../images/icon_weather_f2.gif','../images/icon_gallery_f2.gif','../images/icon_greetings_f2.gif','../images/icon_churchs_f2.gif','../images/icon_articles_f2.gif','../images/icon_testimonials_f2.gif','../images/icon_faqs_f2.gif','../images/icon_blueprints_f2.gif');
MM_preloadImages('../images/btn_close_f2.gif');