/**
 * Custom JS Functions
 */
 
// ** global variables ** //

/**
 * Product Browse dattributeid
 */
var pbDid = 875593;

/**
 * Attribute Search dattributeid
 */
var asDid = 875594;

/**
 * Function used to get our absolute path
 * @param {string}
 * @return {string}
 */
function get_abs_path(site) {
	return '';
	
	/*
	var host = window.location.host;
	
	switch (host) {
		case "dev.insidehli.com":
		case "webq1.insidehli.com":
			//now we have to check to see if we're on the dev or test site (dev is our default db connection)
			if (window.location.pathname.match(/_test/g)) {
				//we're on testing site	
				return 'http://' + host + '/' + site + '_test/';						
			} else {
				//we're on dev (or we're defaulting to dev)
				return 'http://' + host + '/' + site + '_dev/';								
			}
			break;
		case "qa.hubbelloutdoor.com":
			//we're on qa site
			return 'http://qa.hubbelloutdoor.com/';
			break;
		default:
			//we're on production site
			return 'http://www.hubbelloutdoor.com/';		
			break;
	}
	*/
}

/**
 * Function used to display product selected by
 * user from our product browse list
 * @param {int} did
 * @param {string} siteurl
 * @param {object} product_data
 */
function show_product(did, siteurl, product_data) {
	if (product_data == "") {
		alert("No data available for the selected product. Please try your request again.");
		product_browse('');
		return;
	}
	
	//set our product_data (parsing our JSON string)
	var product_data = json_decode(unescape(product_data));

	//check if siteurl is empty, if so its coming from xml, build relpath here
	if (siteurl == '') {var siteurl = get_abs_path('outdoor');}

	//begin our product listing
	var prod_div = 	'<div id="content-header"><h4>Product Browse</h4></div>';
	prod_div += '<div class="section">';
	prod_div += ' <div class="box">';
	
	//product image	
	if (product_data.productImage != '' && product_data.productImage != 'null') {
		prod_div += '  <img class="thumbnail float-left" alt="product image" src="' + siteurl + 'content/products/images/thumb/' + product_data.productImage + '" />';
	}

	prod_div += '  <div class="box-text float-left">';
	//product name	
	if (product_data.realProductName != 'null') {
		prod_div += '  <h3>' + product_data.realProductName + '</h3>'; 
	}

	//subtitle
	if (product_data.subtitle != 'null') {
		prod_div += '  <h4>' + product_data.subtitle + '</h4>'; 
	}

	//description
	if (product_data.productDescription != 'null') {
		prod_div += '  <p>' + product_data.productDescription + '</p>'; 
	}
	
	//end of content div
	prod_div += '  </div>';
	prod_div += ' </div>';
	prod_div += '</div>';
	
	$('content').style.display = 'none';
	$('content-browse').innerHTML = prod_div;
	$('content-browse').style.display = 'block';
}

/**
 * Function used to validate our
 * expressline distributors form
 * @return {boolean}
 */
function validate_expressline_form(the_form) {
	if (the_form.state.selectedIndex == 0) {
		alert('Please choose a state from the drop down menu Or click a state located on the map.');
		the_form.state.focus();
		return false;
	} else {
		return true;
	}
}

// ** QUICK SEARCH FUNCTIONS **//

/**
 * Function used to show our search results
 * @param int start
 * @param int end
 */
function show_search_results(start, end) {
	var str_html = '';
	var siteurl = get_abs_path(site_prefix);
	
	for (var i = start; i < end; i++) {
		str_html += '<div class="box">';
		str_html += '<img class="thumbnail float-left" src="' + siteurl + 'content/products/' + results[i].productImage + '" alt="' + results[i].productName + '" />';
		str_html += '<div class="box-text float-left">';
		str_html += '<h3>' + results[i].productName + '</h3>';
		str_html += '<h4>' + results[i].subtitle + '</h3>';
		str_html += '<p>' + results[i].productDescription + '</p>';
		str_html += '<p><a href="' + siteurl + results[i].filename + '">View Product Page</a></p>';
		str_html += '</div>';
		str_html += '<div class="clear"></div>';
		str_html += '</div>';
	}
	
	document.getElementById('search-contents').innerHTML = str_html;
}