/**
 * Tries to get the instance of XmlHttpRequest()
 *
 * @return object XmlHttpRequest or null
 *
 * @throws Exception
 */
function __getXmlHttpRequestObject()
{
  var xmlHttp = null;

  try {
      xmlHttp = new XMLHttpRequest(); // Standards compliant browsers
  } catch (e) {
      try {
          xmlHttp = createObj('Microsoft.XMLHTTP'); // M$IE >= 6
      } catch (e) {
          xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
      }
  }

  return xmlHttp;
}

/**
 * HTTP GET
 *
 * @param   string    URI to GET
 * @param   function  callback
 * @return  void
 *
 * @throws  Exception
 */
function httpRequest(URI, callback, store, divId)
{
	//console.log('request ' + URI +', ' + store + ', ' + divId);
	//alert('request ' + URI +', ' + store + ', ' + divId);

	if(store == true)
	{
		dhtmlHistory.add('' + URI, URI);
	}

	//href can contain div's id after # - for scrolling in price list
	//request with divid is not handled correctly in IE
	var splitted = URI.split('#');

	var xmlHttp = __getXmlHttpRequestObject();
  xmlHttp.open('GET', 'httpget.php?uri=' + splitted[0], true);
  xmlHttp.onreadystatechange = function()
  {
	  if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
	  {
	  	callback(xmlHttp.responseText, xmlHttp.responseXML, divId);
	  }
  }

  xmlHttp.send(null);
}

/**
 * HTTP POST
 *
 * @param   string   URI
 * @param   string   values
 * @return  string   responseText
 *
 * @throws  Exception
 */
function httpSend(URI, values)
{
  var xmlHttp = __getXmlHttpRequestObject();
  xmlHttp.open('POST', URI, false);
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttp.send(values);
  return xmlHttp.responseText;
}

function myCallback(responseText, responseXML, divId)
{
  var idx = 0, ridx = 0, i = 0, j;
  var arr = new Array();
  responseText = new String(responseText);
  while(-1 != (idx = responseText.indexOf("<!--EVAL", idx)) && -1 != (ridx = responseText.indexOf("-->", idx)))
  {
		arr[i++] = responseText.substr(idx + 8, ridx - idx - 8);
		idx = ridx;
  }

  document.getElementById("text").innerHTML = responseText;
  for (j = 0; j < arr.length; j++)
  {
  	eval(arr[j]);
  }

  //scroll to div - used in price list
  if(divId)
  {
  	document.getElementById(divId).scrollIntoView(true);
  }

	/* jQuery - adding click handlers to links in newly loaded content */
	$(function()
	{
		//ie6 png fix on price list - must be scrollable - jquery.pngfix.js is only one working
		//pngfix function is included/defined only for ie6
		if($.fn.pngfix)
		{
			//$("#preise div.jingjang, #preise .gutschein a").pngfix();
			//$("#prices ul li, #pricelist ul li").pngfix({sizingMethod: "scale"});
			$("#prices ul li").pngfix({sizingMethod: "scale"});
		}

		//links
		$('#text a[target!=_blank]')
			.click(function(event)
			{
				jQuery.handleLinkClick($(this).attr('href'));
				event.preventDefault();
			})

		$('#text a[class=showWindow]')
			.click(function(event)
			{
				showWindow($(this).attr('href'), 640, 320);
			})

			.hover(function(event)
			{
				clearGutscheinePopup();
				loadGutscheinePopup($(this).attr('href'), $(this).attr('alt'));
			})
	});
}

function load(page, divId, store)
{
	//load it
	httpRequest(page, myCallback, store == false ? false : true, divId);
}

function showWindow(url, w, h)
{
	window.open(
		url,
  	'',
  	'width=' + w + ', height=' + h + ', ' + 'directories=no, location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no'
   	);
  return false;
}

var popupCloseTimer;

function loadGutscheinePopup(url, alt)
{
	var src = url.substr(0, url.lastIndexOf('_')) + '.jpg';

	 popupCloseTimer = setTimeout('clearGutscheinePopup();', 15000);

	$('body').append('<div id="gutscheinepopup"><div id="topbar"><a class="order" href="/gutschein-formular">Gutscheine gibt’s hier</a> <a class="close" href="#"><span>close</span></a></div><img alt="'+alt+'" src="'+src+'" /></div>');

	$('#topbar a[class=order]')
		.click(function(event)
		{
			jQuery.handleLinkClick($(this).attr('href'));
			clearGutscheinePopup();
			event.preventDefault();
		})

	$('#topbar a[class=close]')
		.click(function(event)
		{
			clearGutscheinePopup();
			event.preventDefault();
		})

	$(function() {
		$("#gutscheinepopup").draggable();
	});

}

function clearGutscheinePopup()
{
	clearTimeout(popupCloseTimer);

	$('#gutscheinepopup').remove();
}
