var delay = 30;
var increment = 10;

function collapse_main_image() {
	var header_collapse_area = document.getElementById("header_collapse_area");
	var header_height = parseInt(header_collapse_area.style.height);

	if (header_height > 10) {
		header_collapse_area.style.height = (header_height - increment) + "px";
		setTimeout("collapse_main_image();", delay);
	}
	else {
		return true;
	}
}

function set_visible_elements(selected_element_id) {
	/*
	if (selected_element_id == "distance")
		document.getElementById("zipcode").style.display = "inline";
	else
		document.getElementById("zipcode").style.display = "none";
	*/

	if (selected_element_id == "location" || selected_element_id == "snow24" || selected_element_id == "base_lower")
		document.getElementById("locations").style.display = "inline";
	else
		document.getElementById("locations").style.display = "none";
}

// source: http://www.somacon.com/p143.php
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

/*
function update_country_state_select(selected_country) {
	if (selected_country.indexOf("Andorra") != -1)
		

    http.open('get', '/state_select_options.php?country=' + selected_country);
    http.send(null);

    setTimeout("checkState('states',1);", 300);
}
*/

var last_selected_news = null;
function update_home_news(news_category_id, selected_news_category_index) {

	// ignore repeated attempts at receiving the same article
	if (last_selected_news == news_category_id)
		return false;

	last_selected_news = news_category_id;

	// set background image for news category
	var num_news_categories = 4;
	for (var i = 0; i < num_news_categories; i++) {
		if (selected_news_category_index == i) {
			document.getElementById("news_category_" + i).className = "news_background_selected";
			document.getElementById("news_category_link_" + i).className = "news_label_selected";
		}
		else {
			document.getElementById("news_category_" + i).className = "news_background";
			document.getElementById("news_category_link_" + i).className = "news_label";
		}
	}

    http.open('get', '/news/article_only.php?news_category_id=' + news_category_id);
    http.send(null);

    setTimeout("checkState('news_area','http',1);", 300);
}

var last_selected_news_list = null;
function update_home_news_list(news_category_id, selected_news_category_index) {
	//alert("line92");

	// ignore repeated attempts at receiving the same article
	if (last_selected_news_list == news_category_id)
		return false;

	last_selected_news_list = news_category_id;

	// set background image for news category
	var num_news_categories = 4;
	for (var i = 0; i < num_news_categories; i++) {
		if (selected_news_category_index == i) {
			var classname = document.getElementById("news_category_list_" + i).className;
			var new_classname = "";
			if (classname == "news_background_list_leftmost")
				new_classname = "news_background_list_selected_leftmost";
			else if (classname == "news_background_list_rightmost")
				new_classname = "news_background_list_selected_rightmost";
			else
				new_classname = "news_background_list_selected";

			//alert(new_classname);
				
			document.getElementById("news_category_list_" + i).className = new_classname;
			//document.getElementById("news_category_list_" + i).className = "news_background_list_selected";
			//document.getElementById("news_category_link_list_" + i).className = "news_list_label_selected";
		}
		else {
			var classname = document.getElementById("news_category_list_" + i).className;
			var new_classname = "";
			if (classname == "news_background_list_selected_leftmost")
				new_classname = "news_background_list_leftmost";
			else if (classname == "news_background_list_selected_rightmost")
				new_classname = "news_background_list_rightmost";
			else if (classname == "news_background_list_rightmost")
				new_classname = "news_background_list_rightmost";
			else if (classname == "news_background_list_leftmost")
				new_classname = "news_background_list_leftmost";
			else
				new_classname = "news_background_list";

			//alert(new_classname);
				
			document.getElementById("news_category_list_" + i).className = new_classname;
			//document.getElementById("news_category_list_" + i).className = "news_background_list";
			//document.getElementById("news_category_link_list_" + i).className = "news_list_label";
		}
	}

    http_news_categories.open('get', '/news/article_list_only.php?news_category_id=' + news_category_id);
    http_news_categories.send(null);

    setTimeout("checkState('news_area_list','http_news_categories',1);", 300);
}


// AJAX FUNCTIONS
var http = createRequestObject();
var http_news_categories = createRequestObject();
function createRequestObject()
{
    var request_o; //declare the variable to hold the object.
    var browser = navigator.appName; //find the browser name
    if(browser == "Microsoft Internet Explorer"){
        /* Create the object using MSIE's method */
        request_o = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        /* Create the object using other browser's method */
        request_o = new XMLHttpRequest();
    }

    return request_o; //return the object
}
/* Function called to handle the list that was returned from the internal_request.php file.. */
function checkState(destination_element, which_http_instance, iteration){
	var http_instance = null;
	if (which_http_instance == "http_news_categories")
		http_instance = http_news_categories;
	else
		http_instance = http;

    /* Make sure that the transaction has finished. The XMLHttpRequest object
        has a property called readyState with several states:
        0: Uninitialized
        1: Loading
        2: Loaded
        3: Interactive
        4: Finished */
    try
    {
        if(http_instance.readyState == 4){ //Finished loading the response
            /* We have got the response from the server-side script,
                let's see just what it was. using the responseText property of
                the XMLHttpRequest object. */
            var response = http_instance.responseText;

            var destination_element = document.getElementById(destination_element);
            destination_element.innerHTML = response;


			//alert(destination_element);
			//alert(destination_element.innerHTML);
			//destination_element.style.display = "inline";

            //setTimeout("document.getElementById('main_content').style.paddingTop = '79px'; alert('test');", 100);

            // jump to selected page sub section after loading page content
            //window.location.hash = jump_to_anchor;
        }
        else {
            if(iteration % 5 == 0)
                window.status = "loading";

            setTimeout("checkState('" + destination_element + "','" + which_http_instance + "'," + (iteration + 1) + ")", 300);
        }
    }
    catch(Exception)
    {
    }
}

// ADMIN RESORT DATA
function update_resort_method_options(method, resort_url_id, key) {
    http.open('get', '/admin/resort_data/scraping_method_options.php?method=' + method + '&resort_url_id=' + resort_url_id + '&key=' + key);
    http.send(null);

    setTimeout("checkState('method_options','http',1);", 300);
}
