/*************************
@Javascript - Extra Functions
@Page: extra_functions.js
**************************/
/***********************************************
@Function: find_DOM()
@Description: Will find an object' DOM and assign a style
attribute if the user wants one assigned.

@Arguments :: object_id, incl_style
	@object_id = id name of the object
	@incl_style = set to 1 to add style, any other # to not
***********************************************/
function find_DOM(object_id,incl_style)
{ 
    if (document.getElementById) { 
        found_object = document.getElementById(object_id); 
    } 
    else if (document.all) { 
        found_object = document.all[object_id]; 
    } 
    else { 
        browser_version = parseInt(navigator.appVersion); 
        if ((navigator.appName.indexOf('Netscape') != 1 && browser_version == 4)) { 
            found_object = document.layers[object_id]; 
            var nav4 = true; 
        }      
    } 

    if (incl_style == 1 && !nav4) { 
        return found_object.style; 
    } else { 
        return found_object; 
    }          
} 

/***********************************************
@Function: find_flash()
@Description: This function will find the all the flash ads on
the page. It will only add an ad to the ad array if it finds out
that it is indeed a flash ad.

@Arguments :: N/A
***********************************************/
function find_flash()
{
	var divs = document.getElementsByTagName("div");
	var div_id = new String(null);
	var dom = new String(null);
	var div_HTML = new String(null);
	var flash_ads = new Array();
	
	//Cycle thru all layers on the page
	for (var x = 0; x < divs.length; x++) {
		div_id = divs[x].id;
		//If the layer id starts with flash_, continue
		if (div_id.indexOf("flash_") != -1) {
			dom = find_DOM(div_id,0);
			div_HTML = dom.innerHTML;
			//If the HTML within the layer contains the flash extension (.swf)
			//Add if to the flash ad layer array to be returned
			if (div_HTML.indexOf(".swf") != -1) {
				flash_ads.push(div_id);
			}
		}
	}

	return flash_ads;
}

/***********************************************
@Function: show_flash()
@Description: This function will show all divs that have flash
in them that have been hidden.

@Arguments :: N/A
***********************************************/
function show_flash()
{
	var flash = new Array();
	var flash = find_flash();
	var dom_style = new String(null);

	//If any flash ads, continue
	if (flash.length >= 1) {
		//show the flash ads again
		for (var i = 0; i < flash.length; i++) {
			dom_style = find_DOM(flash[i],1);
			dom_style.visibility = "visible";
		}
	}
}

/***********************************************
@Function: hide_flash()
@Description: This function will try to hide any ads that are
flash that might interfere with the sub-navigation drop-down
menus.

@Arguments :: obj_r, obj_b, obj_w
	@obj_r = the right cordinate of the object being referenced
	@obj_b = the bottom cordinate of the object being referenced
	@obj_w = the width of the object being referenced
***********************************************/
function hide_flash(obj_r,obj_b,obj_w)
{
	var flash = new Array();
	var flash = find_flash();
	var dom_style = new String(null);
	var dom = new String(null);
	var ad_l = new String(null);
	var ad_t = new String(null);
	var ad_r = new String(null);
	var obj_l = obj_r - obj_w;

	//If there are any flash items, continue
	if (flash.length >= 1) {
		for (var i = 0; i < flash.length; i++) {
			dom = find_DOM(flash[i],0);
			//Find the ad top,left,& right
			ad_l = parseInt(find_cord(dom,"l"));
			ad_t = parseInt(find_cord(dom,"t"));
			ad_r = parseInt(find_dimension(dom,"w")) + ad_l;

			//Uncomment below for testing purposes
			/*alert("AD LEFT: " + ad_l + "\nMENU RIGHT: " + obj_r);
			alert("AD TOP: " + ad_t + "\nOBJ BOTTOM: " + obj_b);
			alert("AD RIGHT: " + ad_r + "\nOBJ LEFT: " + obj_l);*/

			//If the ad top <= subnav bottom
			//And ad left <= subnav right
			//And ad right >= subnav left
			//Hide the ad
			if (ad_t < obj_b) {
				if (ad_l < obj_r) {
					if (ad_r > obj_l) {
						dom_style = find_DOM(flash[i],1);
						dom_style.visibility = "hidden";
					}
				}
			}
		}
	}
}

/***********************************************
@Function: move_reg()
@Description: Will slide the registration options down over
the search bar. If the reg is shown already, it will slide the 
menu back up. Also this function will show and hide the link
for the user to click.

@Arguments :: N/A
***********************************************/
function move_reg()
{
	var top_home = -50;
	var top_end = 0;
	var regInfo = find_DOM('regInfo',1);
	var regView = find_DOM('regView',1);
	var regClose = find_DOM('regClose',1);
	var t = parseInt(find_cord(regInfo,"t"));

	if (t != top_end) {
		regInfo.top = top_end + "px";
		regView.display = "none";
		regClose.display = "inline";
	} else {
		regInfo.top = top_home + "px";
		regView.display = "inline";
		regClose.display = "none";
	}
}

/***********************************************
@Function: find_cord()
@Description: Will find the left or top position of any object.

@Arguments :: obj, pos
	@obj = The object you want to find cords for
	@pos = Set to either l for left and t for top
***********************************************/
function find_cord(obj,pos)
{
	var f = new String();
	var arr = new Array();
	var found = new Boolean(false);

	//Find the left cords
	//else find the top cords
	//We set all three to an array to find the correct number
	if (pos == "l") {
		arr[0] = obj.left;
		arr[1] = obj.pixelLeft;
		arr[2]= obj.offsetLeft;
	} else {
		arr[0] = obj.top;
		arr[1] = obj.pixelTop;
		arr[2]= obj.offsetTop;
	}

	//Loop thru the array to find the valid number
	//This typically depends on the browser
	for (var i = 0; i < arr.length; i++) {
		if (found == false) {
			if (arr[i] != undefined && arr[i] != null) {
				f = arr[i];
				found = true;
			}
		}
	}

	return f;
}

/***********************************************
@Function: find_dimension()
@Description: Will find the width or height of any object.

@Arguments :: obj, pos
	@obj = The object you want to find cords for
	@pos = Set to either w for width and h for height
***********************************************/
function find_dimension(obj,pos)
{
	var f = new String();
	var arr = new Array();
	var found = new Boolean(false);

	//Find the left cords
	//else find the top cords
	//We set all three to an array to find the correct number
	if (pos == "w") {
		arr[0] = obj.width;
		arr[1] = obj.pixelWidth;
		arr[2]= obj.offsetWidth;
	} else {
		arr[0] = obj.height;
		arr[1] = obj.pixelHeight;
		arr[2]= obj.offsetHeight;
	}

	//Loop thru the array to find the valid number
	//This typically depends on the browser
	for (var i = 0; i < arr.length; i++) {
		if (found == false) {
			if (arr[i] != undefined && arr[i] != null) {
				f = arr[i];
				found = true;
			}
		}
	}

	return f;
}

/***********************************************
@Function: show_sub()
@Description: Will show the sub-nav menu for the current
main nav hovered over. It will also close all other sub-navs
that are open or visible

@Arguments :: obj
	@obj = The main nav layer, called with 'this'
***********************************************/
function show_sub(obj)
{
	//Find the main nav left and top cords.
	//We add 20 pixels to the top so the sub appears
	//in the correct position
	var l = parseInt(find_cord(obj,"l")) - 8;
	var t = parseInt(find_cord(obj,"t")) + 20;
	var sub_id = "sub_" + obj.id;
	var sub_nav = find_DOM(sub_id,1);
	var dom = find_DOM(sub_id,0);
	var w = parseInt(find_dimension(dom,"w"));
	var h = parseInt(find_dimension(dom,"h"));
	var r = w + l;
	var b = h + t;

	//Kill any set timers
	kill_timer();

	//Called to show all flash ads again (if applicable)
//	show_flash();

	//Call the hide_flash to figure if any flash elements
	//are going to interfere with the navigation.
	//If so it will hide them.
//	hide_flash(r,b,w);

	//Get all the layers on the page
	var div_id = new String();
	var dom_style = new String();
	var divs = document.getElementsByTagName("div");

	//Set the sub nav to the correct left and top, and make it visible
	sub_nav.left = l + "px";
	sub_nav.top = t + "px";
	sub_nav.visibility = "visible";

	//Hide all other sub nav layers
	//If the layer id is null or does not have the word sub_
	//do not hide it!
	for (var x = 0; x < divs.length; x++) {
		div_id = divs[x].id;
		 if (div_id != sub_id && div_id.indexOf("sub_") != -1) {
			dom_style = find_DOM(div_id,1);
			dom_style.left = 0 + "px";
			dom_style.top = 0 + "px";
			dom_style.visibility = "hidden";
		}
	}
}

/***********************************************
@Function: kill_subnav()
@Description: This will close the current open/visible sub-nav
once a user mouses out of it. This function is called from the
start_timer() function and should not be called directly. We
don't want the menu to close right away, so we pause for
a quarter of a second or 250 milliseconds.

@Arguments :: obj
	@obj = This object is going to be called from 'this'. Since
	this obj could be the main or the sub_nav we must first
	check to make sure we are closing the sub_nav and not
	the main nav. If the sub_nav is not selected, we need
	to select it, then hide the layer.
***********************************************/
function kill_subnav(obj)
{
	var sub_nav = obj;

	//If the object does not have the word sub_, prepend it
	if (sub_nav.indexOf("sub_") == -1) {
		sub_nav = "sub_" + sub_nav;
	}

	//Find the sub_nav DOM
	//Hide the sub_nav and reset the left/top
	sub_nav = find_DOM(sub_nav,1);
	sub_nav.left = 0 + "px";
	sub_nav.top = 0 + "px";
	sub_nav.visibility = "hidden";
	
	//Called to show all flash ads again (if applicable)
	show_flash();
}

/***********************************************
@Function: start_timer()
@Description: This function is called on a mouse out form 
the main nav layer. If the user has not moused to any
other nav object and not entered their mouse over the
sub-nav the sub-nav will close in a quarter second.

@Arguments :: obj
	@obj = The layer obj to be passed to the kill_subnav()
	function.
***********************************************/
function start_timer(obj)
{
	//Set the timer
	timer = setTimeout("kill_subnav('" + obj.id + "')",250);
}

/***********************************************
@Function: kill_timer()
@Description: Will kill any timers associated with the nav. This
is called when the user mouses over a sub-nav. The timer
called on the main nav mouseout will be cancelled. This will
enable the sub-nav to stay open until the user mouses out=
of it.

@Arguments :: N/A
***********************************************/
function kill_timer()
{
	if (typeof timer != 'undefined' && timer) {
		timer = clearTimeout(timer);
	}
}

/***********************************************
@Function: change_tab()
@Description: This function will change the tab and content
for the user.

@Arguments :: obj
	@obj = The object which should be shown as selected
***********************************************/
function change_tab(obj)
{
	var sel = "tabBGSel";
	var nor = "tabBG";
	var c_class = obj.className;
	var c_id = obj.id;
	var td = document.getElementsByTagName("td");
	var td_id = new String();
	var dom_style = new String();
	var pos = new String();
	var positions = new Array("_upper","_lower","_mid");

	//If the table class is set to not selected run the code
	//We check this because we don't want the user to hide
	//everything they are looking at. If they were to click on
	//the tab they are currently on, and this precaution is not
	//set it would hide everything.
	if (c_class == nor) {
		obj.className = sel;

		//Figure out if the tab positions are in upper, lower, or mid
		//region of the page, and set the pos variable
		for (var x = 0; x < positions.length; x++) {
			if (c_id.indexOf(positions[x]) != -1) {
				pos = positions[x];
			}
		}

		//Cycle thru all the table columns and find the id param
		//If the id is not equal to the current id and the word tab_
		//and variable pos are found in the id, we change the class
		//to look like it has been indexed back one level
		for(var i = 0; i < td.length; i++) {
			td_id = td[i].id;
			if (td_id != c_id && td_id.indexOf("tab_") != -1 && td_id.indexOf(pos) != -1) {
				dom_style = find_DOM(td_id,0);
				dom_style.className = nor;
			}
		}

		//Call the tab_content function
		tab_content(obj,pos);
	}
}

/***********************************************
@Function: tab_content()
@Description: This function will change the content that
is supposed to show with the correct content tab.

@Arguments :: obj, pos
	@obj = The object which should be shown as selected
	@pos = the position of the tab from the function called
	above.
***********************************************/
function tab_content(obj,pos)
{
	var sel = "tabContentSel";
	var nor = "tabContent";
	var c_id = obj.id + "_content";
	var c_style = find_DOM(c_id,0);
	var span = document.getElementsByTagName("span");
	var span_id = new String();

	//Set the content for the selected tab to show
	c_style.className = sel;
	
	//Cycle thru all other spans on the page, if the span
	//is not equal to the one we just displayed and the
	//word _tab and the pos variable are found in the id,
	//we will hide the content.
	for(var i = 0; i < span.length; i++) {
		span_id = span[i].id;
		if (span_id != c_id && span_id.indexOf("tab_") != -1 && span_id.indexOf(pos) != -1) {
			dom_style = find_DOM(span_id,0);
			dom_style.className = nor;
		}
	}
}

/***********************************************
@Function: cookie_finder()
@Description: This function will return the value set for the
cookie name you are searching for. If no cookie exists by
that name, it will return a value of null.

@Arguments :: nm
	@nm = The name of the cookie you are searching for.
***********************************************/
function cookie_finder(nm)
{
	if (navigator.cookieEnabled == false) {
		return null;
	}
	else {
		var result = new String(null);
		var cookies = document.cookie.split("; ");

		for (var i = 0; i < cookies.length; i++) {
			if (cookies[i].split("=")[0] == nm) {
				result = cookies[i].split("=")[1];
			}
		}
		return result;
	}
}

/***********************************************
@Function: save_tab()
@Description: This function will save a cookie for the user's
default tabs. It will write the upper and lower selections to
the same cookie variable.

@Arguments :: cb
	@cb = This is the checkbox value being clicked
***********************************************/
function save_tab(cb)
{
	if (navigator.cookieEnabled == false) {
		alert('You must enable your cookies to use this feature!');
	} else {
		var cookie_name = new String(null);
		var pos = new String(null);
		var cb_name = new String(null);
		var cb_val = cb.value;
		var this_cb = cb_val + "_cb";
		var inputs = document.getElementsByTagName("input");
		var expireDate = new Date();
		expireDate.setMonth(expireDate.getMonth()+120);

		if (cb_val.indexOf("_upper") != -1) {
			cookie_name = "upper_tab";
			pos = "_upper";
		} else {
			cookie_name = "lower_tab";
			pos = "_lower";
		}

		if (cb.checked == true) {
			for (var i = 0; i < inputs.length; i++) {
				if (inputs[i].type == "checkbox") {
					cb_name = inputs[i].id;
					if (cb_name != this_cb && cb_name.indexOf(pos) != -1) {
						inputs[i].checked = false;
					}
				}
			}
		} else {
			cb_val = "";
		}

		document.cookie = cookie_name + "=" + cb_val + "; expires=" + expireDate.toGMTString() + "; path=/";
	}
}

function select_tabs()
{
	var upper_tab = cookie_finder("upper_tab");
	var lower_tab = cookie_finder("lower_tab");
	var obj = new String(null);
	var cb = new String(null);

	if (upper_tab != "" && upper_tab != null) {
		obj = find_DOM(upper_tab,0);
		cb = upper_tab + "_cb";
		cb = find_DOM(cb,0);
		change_tab(obj);
		cb.checked = true;
	}

	if (lower_tab != "" && lower_tab != null) {
		obj = find_DOM(lower_tab,0);
		cb = lower_tab + "_cb";
		cb = find_DOM(cb,0);
		change_tab(obj);
		cb.checked = true;
	}
}

/***********************************************
@Function: hide_layer()
@Description: This function is used to be called from within
a flash richmedia campaign. When it is called, the function
searches for a layer named 'richmedia' and hides it.

@Arguments :: N/A
***********************************************/
function hide_layer() {
	 var dom_style = find_DOM('richmedia',1);
	dom_style.visibility = "hidden"
}

/***********************************************
@Function: select_nav()
@Description: This function is used to highlight the prelim
nav element that the user is at depending on the current
URL. We did it this way so we don't have to have multiple
nav collections or a large script to control nav.

@Arguments :: N/A
***********************************************/
function select_nav()
{
	var url = location.href;
	var host = location.protocol + "//" +  location.host;
	var root = host + "/";
	var anchors = document.getElementsByTagName("a");	
	var dom_style = new String();
	for (var i = 0; i < anchors.length; i++) {
		if (anchors[i].className == "nav") {
			if (url.indexOf(anchors[i].href) != -1 && anchors[i].href != root) {
				anchors[i].className = "navSel";
			}
		}
	}
}

/***********************************************
@Function: preload_images()
@Description: This function will attempt to preload images
that will be used in roll-overs.

@Arguments :: Image List
***********************************************/
function preload_images()
{ 
    if (document.images) { 
        var args = preload_images.arguments; 
        document.imageArray = new Array(args.length); 

        for(var i=0; i<args.length; i++) { 
            document.imageArray[i] = new Image; 
            document.imageArray[i].src = args[i]; 
        } 
    } 
} 

/***********************************************
@Function: rollover()
@Description: This function will attempt to change the current
image to the roll image.

@Arguments :: c, img
***********************************************/
function rollover(c,img)
{ 
	if (document.images) {
		c.src = img; 
	}
} 

/***********************************************
@Function: ss_rollover()
@Description: This function will attempt to change the current
image to the roll image. It is the same as the rollover function
except we need to see if the current item we are rolling over
is the active stylesheet. If it is, do not load the new image.

@Arguments :: c, img
***********************************************/
function ss_rollover(c,img)
{ 
	var nm = c.name;
	if (document.images) {
		var small_style = find_DOM("storySmallCSS",0);
		var medium_style = find_DOM("storyMedCSS",0);
		var large_style = find_DOM("storyLargeCSS",0);
		if (nm.indexOf("Small") != -1) {
			if (small_style.disabled == true) {
				c.src = img;
			}
		}
		else if (nm.indexOf("Med") != -1) {
			if (medium_style.disabled == true) {
				c.src = img;
			}
		}
		else {
			if (large_style.disabled == true) {
				c.src = img;
			}
		}
	}
} 

/***********************************************
@Function: change_style()
@Description: This function will attempt to change the style
sheet for the user. The user can view the story in small,
medium, or large print.

@Arguments :: ssTitle
***********************************************/
function change_style(ssTitle)
{
	//Set some variables
	var img_root = "/images/allnews3/icons/textsizes/";
	var small_sel = "small_selected.gif";
	var medium_sel = "medium_selected.gif";
	var large_sel = "large_selected.gif";
	var small_gif = "small.gif";
	var medium_gif = "medium.gif";
	var large_gif = "large.gif";
	var small_style = find_DOM("storySmallCSS",0);
	var medium_style = find_DOM("storyMedCSS",0);
	var large_style = find_DOM("storyLargeCSS",0);
	var c_style = new String(null);

	//Set the expiration date for the cookie
	var expireDate = new Date();
	expireDate.setMonth(expireDate.getMonth()+120);

	//Find the cookie
	var thisCookie = document.cookie.split("; ");
	var ssCookie =  new String(null);


	//Check to see if we should try to extract the cookie
	if (ssTitle == "") {
		for (i = 0; i < thisCookie.length; i++) {
			if (thisCookie[i].split("=")[0] == "defaultCSS") {
				ssCookie = thisCookie[i].split("=")[1];
				ssTitle = ssCookie;
			}
		}
	}

	//Set the cookie
	if (ssTitle != "") {
		document.cookie = "defaultCSS=" + ssTitle + "; expires=" + expireDate.toGMTString() + "; path=/";
	}

	//Find out what style sheet to use
	//We must enable the correct style sheet and disable the others
	//We must also set the correct images to show the user
	//what style sheet they are using.
	if (ssTitle.indexOf("storySmall") != -1) {
		small_style.disabled = false;
		medium_style.disabled = true;
		large_style.disabled = true;

		if (document.images) {
			document.SmallTextIcon.src = img_root + small_sel;
			document.MedTextIcon.src = img_root + medium_gif;
			document.LargeTextIcon.src = img_root + large_gif;
		}
	}
	else if (ssTitle.indexOf("storyLarge") != -1) {
		large_style.disabled = false;
		small_style.disabled = true;
		medium_style.disabled = true;

		if (document.images) {
			document.SmallTextIcon.src = img_root + small_gif;
			document.MedTextIcon.src = img_root + medium_gif;
			document.LargeTextIcon.src = img_root + large_sel;
		}
	}
	else { //DEFAULT (Medium Text)
		medium_style.disabled = false;
		small_style.disabled = true;
		large_style.disabled = true;

		if (document.images) {
			document.SmallTextIcon.src = img_root + small_gif;
			document.MedTextIcon.src = img_root + medium_sel;
			document.LargeTextIcon.src = img_root + large_gif;
		}
	}

}

/***********************************************
@Function: get_random()
@Description: This function will return a randon number from
1 to million.

@Arguments :: N/A
***********************************************/
function get_random() {
	return Math.floor((Math.random() + 1) * 1000000);
}

/***********************************************
@Function: track_size()
@Description: This function will attempt to keep track of what
sizes the users are clicking.

@Arguments :: s
***********************************************/
function track_size(s) {
	if (document.images) {
		var img = new Image;
		img.src = 'http://www.mcallcommunity.com/tracking/textsize/execute.php?a=1&s=' + s + '&rn=' + get_random();
		document.size_tracker.src = img.src;
	}
}

/***********************************************
@Function: track_tabs()
@Description: This function will attempt to keep track of what
tabs the users are clicking.

@Arguments :: n
***********************************************/
function track_tabs(n) {
	if (document.images) {
		var img = new Image;
		img.src = 'http://www.mcallcommunity.com/tracking/tabs/execute.php?n=' + n + '&rn=' + get_random();
		document.tab_tracker.src = img.src;
	}
}