function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}

function grabFile(file) {
  var request = getHTTPObject();
  if (request) {
    request.onreadystatechange = function() {
      parseResponse(request);
    };
    request.open("GET", file, true);
    request.send(null);
  }
}

function video_getData() {
	//We can use one Request object many times.
	var req = new Request.HTML({url:'ajax-infos-video.php', 
		onRequest: function() { $('CoordsVideoActive').set('html', '<img src="/images/lytebox/loading.gif">');  },
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('CoordsVideoActive').set('text', '');
			//Inject the new DOM elements into the results div.
			$('CoordsVideoActive').adopt(html);
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('CoordsVideoActive').set('text', 'The request failed.');
		}
	});
	req.send();
}

function getEntreprises(p,z,pag) {
	//We can use one Request object many times.
	var req = new Request.HTML({url:'get_entreprises.php?activite='+p+'&zone='+z+'&page='+pag, 
		onRequest: function() { $('getEntreprises').set('html', '<img src="/images/lytebox/loading.gif">');  },
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('getEntreprises').set('text', '');
			//Inject the new DOM elements into the results div.
			$('getEntreprises').adopt(html);
			generatePagination();
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('getEntreprises').set('text', 'The request failed.');
		}
	});
	req.send();
}

function generatePagination() {
	//We can use one Request object many times.
	var req = new Request.HTML({url:'generatePagination.php', 
		onRequest: function() { $('generatePagination').set('html', '<img src="/images/lytebox/loading.gif">');  },
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('generatePagination').set('text', '');
			//Inject the new DOM elements into the results div.
			$('generatePagination').adopt(html);
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('generatePagination').set('text', 'The request failed.');
		}
	});
	req.send();
}

function parseResponse(request) {
  if (request.readyState == 4) {
    if (request.status == 200 || request.status == 304) {
      
		var data = request.responseXML;
		
		var swfData = data.getElementsByTagName("swfData")[0];
		
		//Get SWF params from XML
		var swf_src = swfData.getAttribute("src");
		var swf_w = swfData.getAttribute("width");
		var swf_h = swfData.getAttribute("height");
		var swfMinVersion = swfData.getAttribute("minflashversion");
		
		//Get variables to pass to SWF
		var swfVar_var1 = getNodeValue(swfData, "myVar1");
		
		var so = new SWFObject(swf_src, "Video", swf_w, swf_h, swfMinVersion);
		so.addParam("wmode", "transparent");
		so.addVariable("file", swfVar_var1);
		so.addParam("allowfullscreen","true");
		so.addVariable("autostart","true");
//		so.addVariable("skin","/shadowbox/skin/linkeo/SkinOverPlaySeekFullscreen.swf");
		so.addVariable("skin","/shadowbox/skin/linkeo/SkinOverPlayStopSeekFullVol.swf");
		so.write("VideoMain");
		video_getData();
		
    }
  }
}

function getNodeValue(parent, target) {
	var node = parent.getElementsByTagName(target)[0];
	if(node.firstChild && node.firstChild.nodeValue){
		//Escaping to help eliminate errors with quotes and other characters
		return escape(node.firstChild.nodeValue);
	}
	else return false;
}