/*******************************************************************************
* Global Javascript variables
* 
*******************************************************************************/
var site_domain 	= 'shopalaska.com';
var blogs_domain	= 'blogs.shopalaska.com';
var login_domain	= 'shopalaska.com'	// dev environment workaround; this should be the same as site domain in future
var mtl_property	= 'JUNEAU AK';


/*******************************************************************************
* Attached Events
*
*******************************************************************************/
// This is attached to the body's onload event
window.onload = function() {
	// Initialize the dropdown menus
	init_dropdowns();

	// Show initial TopAd
	view_top_ad("mdw_topAds");

	// Set text size
	set_text_size();

	// Initialize the Featured Items block
	featured_item();
}


/* 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("main-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;
            }
        }
    }
}




/*******************************************************************************
* Spotted-related Javascripts
*
*******************************************************************************/
function front_spotted_promo() {
	document.getElementById("submitted-photo").style.backgroundImage 
		= 'url("http://' + site_domain + '/images/submitted/1.jpg")';
}


function spotted_front_gallery(gallery) {
	for (i = 0; i < 6; i ++) {
		document.getElementById("spotted-gallery-" + (i + 1)).className = "gallery";
		document.getElementById("spotted-gallery-link-" + (i + 1)).className = "";
	}
	document.getElementById("spotted-gallery-" + gallery).className = "gallery selected";
	document.getElementById("spotted-gallery-link-" + gallery).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"
	}
}







/*******************************************************************************
* 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"
				: "";
		}
	}
}






























/*******************************************************************************
* Text size management.
*
* The following functions allow the user to change the text size of stories.
* The global variable text_size_class contains the size level. The fu
* change_text_size sets the text size level based on that variable, and 
* either increments or decrements the value.
*
*******************************************************************************/

// The global variable text_size_class contains the size level. This value will 
// Also be cookied if possible.
var text_size_class = default_text_size();


var expdate = new Date ();
expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 365));


/*******************************************************************************
* set_text_size()
* 
* This function sets the text size based on the global variable text_size_class
* and can be called by either the change_text_size() function or directly by
* an event handler (such as the body's onload handler, for example).
*******************************************************************************/
function set_text_size() {
	if (document.getElementById("story")) {
		var story_container = document.getElementById("story");
	
		if (text_size_class) {
			story_container.className = "font-size-" + text_size_class;
		} else {
			story_container.className = "";
		}
	}
}

/*******************************************************************************
* change_text_size()
* 
* This function increments or decrements the global text_size_class variable 
* within the allowed range (0 through 3) based on the argument passed, which 
* should either be '+' or '-'.
*******************************************************************************/
function change_text_size(arg) {

	if (arg == "+") {
		text_size_class += ((text_size_class >= 0) && (text_size_class < 3)) ? 1 : 0;
	} else if (arg == "-") {
		text_size_class -= ((text_size_class <= 3) && (text_size_class > 0)) ? 1 : 0;
	}

	setCookie("text_size", text_size_class, expdate, '/');
	set_text_size();
}


/*******************************************************************************
* default_text_size()
* 
* This function returns the default text size. It first checks the cookie to see
* if the user has selected a text size, otherwise it sets the text size level 
* to 0.
*******************************************************************************/
function default_text_size() {
	var cookied_text_size = 0;
	var cookie_data = parseInt(getcookie("text_size"));

	if ((cookie_data >= 0) && (cookie_data <= 3)) {
		cookied_text_size = parseInt(getcookie("text_size"));
	}

	return cookied_text_size;
}














/*******************************************************************************
* function: getcookie()
*
* This function gets the cookies set on login to properly display the dashboard.
*******************************************************************************/
function getcookie(cookiename) {
	var cookiestring=document.cookie;
	var index1=cookiestring.indexOf(cookiename);
	if (index1==-1 || cookiename=="") return "";
	var index2=cookiestring.indexOf(';',index1);
	if (index2==-1) index2=cookiestring.length;
	return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

/*******************************************************************************
* setCookie()
*
* This function sets a cookie with the desired cookie name.
*******************************************************************************/
function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}





















/*******************************************************************************
* ajaxinclude()
*
* This uses the XMLHTTPRequest object to fetch data. Used for AJAX.
*******************************************************************************/
$good_ajax = (window.XMLHttpRequest) ? 1 : 0;
function ajaxinclude(url, container) {
	var A = null;
	try {
		A = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			A = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (oc) {
			A = null;
		}
	}
	if(!A && typeof XMLHttpRequest != "undefined")
		A = new XMLHttpRequest();


	var page_request = A;

	var delimiter = (url.indexOf('?') > -1) ? '&' : '?';

	page_request.open('GET', "http://" + window.location.hostname + url + delimiter + Math.random(), false)
	page_request.send(null)

	if (container) {
		if (window.location.href.indexOf("http") == -1 || page_request.status == 200)
			document.getElementById(container).innerHTML = page_request.responseText;
	} else {
		return page_request.responseText;
	}
}













/*******************************************************************************
* display_featured_item()
*
* This shows the desired featured item, or the first item if none are specified.
*******************************************************************************/
function featured_item(item) {
	/* TODO
		* Need to get the "this" value of the link that originally called this 
		function, if it was a link. This is so we can set the className value
		of that link to "selected", so that it's highlighted.
	*/

	if (document.getElementById("featured-items")) {
		var temp_list = document.getElementById("featured-items").getElementsByTagName("div");
		var item_list = new Array();
		
		for (i = 0; i < temp_list.length; i ++) {
			if (temp_list[i].className == "featured-item") {
				item_list.push(temp_list[i]);
				temp_list[i].style.display = "none";
			}
		}

		// Get the item to show. Use the first item in the block as a default if
		// none is given.
		item = (item) ? document.getElementById(item) : item_list[0];
		
		item.style.display = "block";
	}
}

/*******************************************************************************
* print_dashboard()
*
* This shows the user dashboard.
*******************************************************************************/
function print_dashboard() {
	var username = getcookie("mdwuser");
	if (getcookie("mdwac") && username != ''){ 	// user is logged in
		if(avatar_url_init && avatar_url_init != '') { 	// if the cookie is already set render the dashboard picutre since the cookie is setup already
			document.write('<a href="#"><img src="' + avatar_url_init + '" width="30" height="30" alt="" /></a>');			
		} else {								// see if the user has an avatar set in blogs, if so, set the blogs_avatar cookie and render the dashboard
			blogs_avatar_url = get_avatar_url();
			if(blogs_avatar_url && blogs_avatar_url != '') {	// user has an avatar in blogs, set the cookie and render accordingly
				setCookie('blogs_avatar', blogs_avatar_url)
				avatar_url = blogs_avatar_url;
				document.write('<a href="#"><img src="' + avatar_url + '" width="30" height="30" alt="" /></a>');
			} else { 							// user has not set an avatar so render the default one
				document.write('<a href="#"><img src="/images/TEMP/avatar1.gif" width="30" height="30" alt="" /></a>');
			}
		}
		document.write('<a href="http://' + site_domain + '/account.shtml">' + username + '</a>');
		document.write('|');
		document.write('<a href="http://register.' + login_domain + '/forge/dataentry?tp=' + mtl_property + '&tl=2&temp_type=LOGIN&category=REGISTRATION&reg_redirect=http://dev.' + login_domain + '"> log out </a>');
	} else{ 									// user is not logged in so display login link instead
		document.write('<a href="#"><img src="/images/TEMP/avatar1.gif" width="30" height="30" alt="" /></a>');
		document.write('<a href="http://register.' + login_domain + '/forge/dataentry?tp=' + mtl_property + '&temp_type=DATA ENTRY&category=registration&reg_redirect=http://dev.' + login_domain + '"> register </a>');
		document.write('|');
		document.write('<a href="http://register.' + login_domain + '/forge/dataentry?tp=' + mtl_property + '&temp_type=DATA ENTRY&category=registration&reg_redirect=http://dev.' + login_domain + '"> log in </a>');
	}
}