/*******************************************************************************
* Attached Events
*
*******************************************************************************/

// This is attached to the body's onload event
window.onload = function() {

	// Show initial TopAd
	view_top_ad("TopJobs");

	// Set text size
	// set_text_size();

	init_dropdowns();
	

	if (document.getElementById('tcontent1')) {
		swap_tabs('tcontent1');
	}
}




function swap_tabs (tab_id) {
	var tabs = 6;

	for (i = 0; i < tabs; i ++) {
		document.getElementById("tcontent" + (i + 1)).style.display = "none";
		document.getElementById("tab-tcontent" + (i + 1)).className = "";
	}
	
	document.getElementById(tab_id).style.display = "block";
	document.getElementById("tab-" + tab_id).className = "selected";
}




/*******************************************************************************
* Top Ad -- view_top_ad()
* This function takes an argument (arg), which is the ID of the HTML block which
* contains the top ad details to display; and hides the others.
*******************************************************************************/
function view_top_ad(arg) {
	var possible_top_ads = new Array(
		"TopAds", "TopJobs", "TopHomes", "TopAutos", "TopRentals",
		"mdw_topAds", "mdw_topJobs", "mdw_topHomes", "mdw_topAutos", 
		"mdw_topRentals", "TopRealEstate"
	);

	for (i = 0; i < possible_top_ads.length; i ++) {
		if (document.getElementById(possible_top_ads[i])) {
			if (arg == document.getElementById(possible_top_ads[i]).id) {
				view_top_ad_display(document.getElementById(possible_top_ads[i]));
			} else {
				document.getElementById(possible_top_ads[i]).style.display = "none";
			}
			
		}
	}
}

function view_top_ad_display(arg) {
	if (arg) {
		arg.style.display = (arg.style.display == "block") 
		? "none"
		: "block"
	}
}


/* Dropdown nav */
function toggle_dropdown() {
	for (var i = 0; i < this.childNodes.length; i ++) {
		if (this.childNodes.item(i).nodeName == "UL") {							
			this.childNodes.item(i).style.display = 
				(this.childNodes.item(i).style.display != "block") 
				? "block" 
				: "none";
			break;            
        }		
    }	
}
function init_dropdowns() {
	//var list_collection = document.getElementsByTagName("li");
	var list_collection = document.getElementById("nav").getElementsByTagName("li");

	for (var i = 0; i < list_collection.length; i ++) {        
		for (var j = 0; j < list_collection[i].childNodes.length; j ++) {
			if (list_collection[i].childNodes.item(j).nodeName == "UL") {
				list_collection[i].onmouseover	= toggle_dropdown;
				list_collection[i].onmouseout	= toggle_dropdown;
            }
        }
    }
}


/*******************************************************************************
* select_thumb()
* This function shows the desired photo and caption box, markes the selected 
* thumb with a different class, and hides the other large photo box.
*******************************************************************************/
function select_thumb(large, thumb) {

	// Collect a list of all large photo containers
	var all_divs 	= document.getElementsByTagName('div');
	var photo_divs 	= new Array();
	for (i = 0; i < all_divs.length; i ++) {
		if (all_divs[i].id.indexOf("photo-id-") == 0) {
			photo_divs.push(all_divs[i].id);
		}
	}

	// Collect a list of all thumbnail images
	var all_divs 	= document.getElementsByTagName('img');
	var thumb_divs	= new Array();
	for (i = 0; i < all_divs.length; i ++) {
		if (all_divs[i].id.indexOf("thumb-id-") == 0) {
			thumb_divs.push(all_divs[i].id);
		}
	}

	// Loop through every photo, as determined by the large photo container
	for (i = 0; i < photo_divs.length; i ++) {

		// Hide all large photo boxes, or show the desired large photo box
		if (document.getElementById(photo_divs[i])) {
			document.getElementById(photo_divs[i]).style.display = 
				(photo_divs[i] == "photo-id-" + large)
				? "block"
				: "none";
		}

		// Unset the class names for all thumbnail images, or set the desired
		// thumbnail image's class name to "selected"
		if (document.getElementById(thumb_divs[i])) {
			document.getElementById(thumb_divs[i]).className = 
				(thumb_divs[i] == "thumb-id-" + thumb)
				? "selected"
				: "";
		}
	}
}