// ajax engine
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(keyword, total_result, search_type, ordering) {
	swapLoading();
    http.open('get', mosConfig_live_site+'/modules/mod_instantsearch.ajax.php?q='+keyword+'&tr='+total_result+'&st='+search_type+'&o='+ordering);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
		swapLoading();
        var response = http.responseText;
        PopupContent('instantsearch_result', showCloseButton, CloseButtonPos, response, width, height, padding_to, padding_width);
    }
}

// display engine
// is Netscape?
var ns = (navigator.appName.indexOf("Netscape") != -1);
// is IE?
var ie = (navigator.appVersion.indexOf("MSIE") != -1);

// Get window`s width
var window_width = 0;
var window_height = 0;
if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	window_width = window.innerWidth;
	window_height = window.innerHeight;
} else if( document.documentElement &&
	  ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	window_width = document.documentElement.clientWidth;
	window_height = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	window_width = document.body.clientWidth;
	window_height = document.body.clientHeight;
}

// Remove all child nodes of an element
function clearElement(node) {
	while (node.hasChildNodes()) {
		node.removeChild(node.firstChild);
	}
	node.innerHTML = '';
}

// Set position for Popup Div
function PopupPosition(obj, width, height, padding_to, padding_width) {
	obj.style.position = 'absolute';
	obj.style.width = width + 'px';
	obj.style.height = '100%';
	if (padding_to == 'left') {
		obj.style.left = padding_width + 'px';
	} else if (padding_to == 'right') {
		if (ie) {
			obj.style.left = String(Math.round(window_width - width - padding_width - 3)) + 'px';
		} else {
			obj.style.left = String(Math.round(window_width - width - padding_width - 20)) + 'px';
		}
	}
	if (ie) {
		obj.style.marginTop = '20px';
	}
	obj.style.display = 'block';
}

// Set content for Popup Div
function PopupContent(id, showCloseButton, CloseButtonPos, content, width, height, padding_to, padding_width) {
	var current_form = document.getElementById(id);
	if (!document.getElementById(id+'_list')) {
		// Create new elements
		var hiddenDivContent = document.createElement('div'); //div
		var att1 = document.createAttribute('class');
		att1.value = 'hiddenDivContent';
		hiddenDivContent.setAttributeNode(att1);
		
		var hiddenDivTable = document.createElement('table'); //table
		var att2 = document.createAttribute('class');
		att2.value = 'hiddenDivTable';
		hiddenDivTable.setAttributeNode(att2);
		hiddenDivTable.setAttribute('border','0');
		hiddenDivTable.setAttribute('cellspacing','0');
		hiddenDivTable.setAttribute('cellpadding','0');
		hiddenDivTable.style.width = '100%';
		
		if (showCloseButton) {
			var hiddenDivTableCloseTR = document.createElement('tr'); //close button row
			var hiddenDivTableCloseTD = document.createElement('td'); //close button col
			var att4 = document.createAttribute('class');
			att4.value = 'hiddenDivTableHeader';
			hiddenDivTableCloseTD.setAttributeNode(att4);
			if (CloseButtonPos == 'topleft' || CloseButtonPos == 'bottomleft') {
				hiddenDivTableCloseTD.style.textAlign = 'left';
			} else if (CloseButtonPos == 'topright' || CloseButtonPos == 'bottomright') {
				hiddenDivTableCloseTD.style.textAlign = 'right';
			}
		}
		
		var hiddenDivTableBodyTR = document.createElement('tr'); //content row
		var hiddenDivTableBodyTD = document.createElement('td'); //content col
		var hiddenDivTableBodyDiv = document.createElement('div'); // body div
		var att6 = document.createAttribute('class');
		att6.value = 'hiddenDivTableBody';
		hiddenDivTableBodyDiv.setAttributeNode(att6);
		hiddenDivTableBodyDiv.setAttribute('id',id+'_list');
		hiddenDivTableBodyDiv.style.height = height+'px';
		
		// Append new elements
		clearElement(document.getElementById('instantsearch_temporary'));
		document.getElementById('instantsearch_temporary').appendChild(hiddenDivContent);
		hiddenDivContent.appendChild(hiddenDivTable);
		
		if (showCloseButton && (CloseButtonPos == 'topleft' || CloseButtonPos == 'topright')) {
			hiddenDivTable.appendChild(hiddenDivTableCloseTR);
			hiddenDivTableCloseTR.appendChild(hiddenDivTableCloseTD);
		}
		
		hiddenDivTable.appendChild(hiddenDivTableBodyTR);
		hiddenDivTableBodyTR.appendChild(hiddenDivTableBodyTD);
		hiddenDivTableBodyTD.appendChild(hiddenDivTableBodyDiv);
		
		if (showCloseButton && (CloseButtonPos == 'bottomleft' || CloseButtonPos == 'bottomright')) {
			hiddenDivTable.appendChild(hiddenDivTableCloseTR);
			hiddenDivTableCloseTR.appendChild(hiddenDivTableCloseTD);
		}
		
		// Input content for new elements
		if (showCloseButton) {
			var close_link = document.createElement('a');
			close_link.setAttribute('href','javascript: void(0)');
			close_link.setAttribute('onClick','document.getElementById(\''+id+'\').style.display = \'none\'');
			close_link.setAttribute('title','Close');
			
			var close_img = document.createElement('img');
			close_img.setAttribute('src',mosConfig_live_site+'/modules/mod_instantsearch.close.png');
			close_img.setAttribute('width','16');
			close_img.setAttribute('height','16');
			close_img.setAttribute('border','0');
			close_img.style.margin = '2px';
			
			hiddenDivTableCloseTD.appendChild(close_link);
			close_link.appendChild(close_img);
		}
		document.getElementById(id+'_list').innerHTML = content;
		
		clearElement(current_form);
		current_form.innerHTML = document.getElementById('instantsearch_temporary').innerHTML;
		clearElement(document.getElementById('instantsearch_temporary'));
	} else if (content != null && content != '') {
		document.getElementById(id+'_list').innerHTML = content;
	}
	PopupPosition(current_form, width, height, padding_to, padding_width);
}

// show/hide loading status
function swapLoading() {
	if (!document.getElementById('instantsearch_loading')) {
		var DivToPlace = document.createElement('div');
		DivToPlace.setAttribute('id', 'instantsearch_loading');
		var att = document.createAttribute('class');
		att.value = 'loading_text';
		DivToPlace.setAttributeNode(att);
		DivToPlace.style.position = 'absolute';
		DivToPlace.style.display = 'none';
		DivToPlace.style.textAlign = 'center';
		DivToPlace.style.width = '70px';
		DivToPlace.style.lineHeight = '20px';
		DivToPlace.style.left = String(window_width - 105) + 'px';
		document.getElementById('instantsearch_container').appendChild(DivToPlace);
		DivToPlace.innerHTML = '<b>Loading...</b>';
	} else {
		var DivToPlace = document.getElementById('instantsearch_loading');
	}
	var pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
	var top = (pY > 15) ? (pY + 15) : 15;
	DivToPlace.style.top = top+'px';
	if (DivToPlace.style.display == 'none') {
		DivToPlace.style.display = 'block';
	} else {
		DivToPlace.style.display = 'none';
	}
}
