// JavaScript Document
function toggleDiv(objectID) {
	if (document.getElementById(objectID).style.display == 'block') {
		document.getElementById(objectID).style.display = 'none';
	} else {
		document.getElementById(objectID).style.display = 'block';
	}
	return
}

var id;
var type;

function news(newsid) {
	id = newsid;
	type = 'news';
	if(ajax) {
		var q = '_includes/news_refresh.php?id='+id;
		ajax.open('get',q,true);
		ajax.onreadystatechange = handle_news;
		ajax.send(null);
		
	}
}
function blog(newsid) {
	id = newsid;
	type = 'blog';
	if(ajax) {
		var q = '_includes/blog_refresh.php?id='+id;
		ajax.open('get',q,true);
		ajax.onreadystatechange = handle_news;
		ajax.send(null);
		
	}
}
function events(newsid) {
	id = newsid;
	type = 'event';
	if(ajax) {
		var q = '_includes/event_refresh.php?id='+id;
		ajax.open('get',q,true);
		ajax.onreadystatechange = handle_news;
		ajax.send(null);
		
	}
}
function handle_news() {
	if ((ajax.readyState == 4) && (ajax.status == 200)) {
		document.getElementById('news_div').innerHTML = ajax.responseText;
		newsmenu();
	}
}
function newsmenu() {
	if(ajax) {
		var q = '_includes/update_sub_menu.php?id='+id+'&type='+type;;
		ajax.open('get',q,true);
		ajax.onreadystatechange = handle_newsmenu;
		ajax.send(null);
		
	}
}
function handle_newsmenu() {
	if ((ajax.readyState == 4) && (ajax.status == 200)) {
		document.getElementById('archives').innerHTML = ajax.responseText;
	}
}

function swapImage(img){
	document.getElementById('main_photo').innerHTML = "<img src="+img+" />";
}


//jQuery functions
$(document).ready(function(){
	var int_array = Array('honesty','decency','character','integrity');
	var con_array = Array('dependence','reliance','assurance','confidence');
	var tru_array = Array('loyalty','faith','belief','trust');
	var con_active = false;
	var tru_active = false;
	var int_active = false;
	
	var fade = function(id){
		if(id == null) {
			$('#confidence, #integrity, #trust').fadeIn(1000);
		} else {
			if(id == 'confidence' && con_active == false) fadeConfidence();
			if(id == 'trust' && tru_active == false) fadeTrust();
			if(id == 'integrity' && int_active == false) fadeIntegrity();
		}
	}
	
	var fadeImg = function(id, img){
		var cur_src = $('#'+id).css('background-image');
		var usearray;
		var old_id = img-1;
		if(old_id<0) old_id=3;
		if(id == 'integrity') usearray = int_array;
		else if(id == 'confidence') usearray = con_array;
		else usearray = tru_array;
				
		new_src = cur_src.replace(usearray[old_id],usearray[img])
		$('#'+id).fadeOut(1000,function(){
			$('#'+id).css('background-image',new_src);
			$('#'+id).fadeIn(1000);
			img++;
			fade(id,img);
		});
	}
	
	var fadeConfidence = function(img){
		con_active = true;
		var cur_src = $('#confidence').css('background-image');
		if(img == null) img = 0;
		var old_id = img-1;
		if(old_id<0) old_id=3;
		var usearray = con_array;
		new_src = cur_src.replace(usearray[old_id],usearray[img])
		$('#confidence').fadeOut(1000,function(){
			$('#confidence').css('background-image',new_src);
			$('#confidence').fadeIn(1000);
			img++;
			if(img<4)
				fadeConfidence(img);
			else con_active = false;
		});
	}
	var fadeTrust = function(timg){
		tru_active = true;
		var tcur_src = $('#trust').css('background-image');
		if(timg == null) timg = 0;
		var told_id = timg-1;
		if(told_id<0) told_id=3;
		var usearray = tru_array;
		var tnew_src = tcur_src.replace(usearray[told_id],usearray[timg])
		$('#trust').fadeOut(1000,function(){
			$('#trust').css('background-image',tnew_src);
			$('#trust').fadeIn(1000);
			timg++;
			if(timg<4)
				fadeTrust(timg);
			else tru_active = false;
		});
	}
	var fadeIntegrity = function(iimg){
		int_active = true;
		var icur_src = $('#integrity').css('background-image');
		if(iimg == null) iimg = 0;
		var iold_id = iimg-1;
		if(iold_id<0) iold_id=3;
		var iusearray = int_array;
		var inew_src = icur_src.replace(iusearray[iold_id],iusearray[iimg])
		$('#integrity').fadeOut(1000,function(){
			$('#integrity').css('background-image',inew_src);
			$('#integrity').fadeIn(1000);
			iimg++;
			if(iimg<4)
				fadeIntegrity(iimg);
			else int_active = false;
		});
	}
	
	$('#confidence, #integrity, #trust').mouseover(function() {
			fade($(this).attr('id'));
	});
	
	setTimeout(fade(null),1500);
	
	//universal ajax function
	var ajaxFunction = function(dataString,file,replaceID,f) {
		$.ajax({  
		  type: "POST",  
		  url: file,  
		  data: dataString,  
		  success: function(msg) {  
			$('#'+replaceID).html(msg);
			if (typeof f == "function") f(msg);
		  }  
		});
	}	
	//center div function
	jQuery.fn.center = function (position,zIndex) {
		this.css("position",position);
		this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
		this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
		this.css("zIndex",zIndex);
		return this;
	}
	
	//Google maps functions
	$('#gmapform input[type="radio"]').click(function(){
		var ds = '';
		var i =0;
		$('#gmapform input[type="radio"]').each(function(){
			if($(this).is(':checked'))
			ds = ds+$(this).val()+',';
		});
		ds = ds.substr(0,ds.length-1);
		initialize(ds);
	});

});

function showMain() {
document.getElementById('listing_main').style.display="block";
document.getElementById('listing_map').style.display="none";
document.getElementById('listing_calc').style.display="none";
document.getElementById('details').className='select';
document.getElementById('map').className='';
document.getElementById('m_calculator').className='';
}
function showMap() {
	var types = '';
	initialize(types);
document.getElementById('listing_main').style.display="none";
document.getElementById('listing_map').style.display="block";
document.getElementById('listing_calc').style.display="none";
document.getElementById('details').className='';
document.getElementById('map').className='select';
document.getElementById('m_calculator').className='';
}
function showCalc() {
document.getElementById('listing_main').style.display="none";
document.getElementById('listing_map').style.display="none";
document.getElementById('listing_calc').style.display="block";
document.getElementById('details').className='';
document.getElementById('map').className='';
document.getElementById('m_calculator').className='select';
}

