var ch = new Array();
function chge(id)
{
  ch[id] = document.getElementById(id);
  if (ch[id] != null && ch[id] != '')
  {
    if(ch[id].style.display == "none" || ch[id].style.display == '')
      ch[id].style.display = "block";
    else
      ch[id].style.display = "none";
  }
}

//var imgch = new Array();
function imgchge(id, ione, itwo)
{
  imgch = document.getElementById(id);
  if (imgch != null && imgch != '')
  {
    if(imgch.src.indexOf(ione) > -1)
      imgch.src = itwo;
    else
      imgch.src = ione;
  }
}

var c_el = new Array();
function change(id)
{
  c_el[id] = document.getElementById(id);
  if (c_el[id] != null && c_el[id] != '')
  {
    if(c_el[id].style.display == "none" || c_el[id].style.display == '')
    {
      c_el[id].style.display = "block";
      fadeBoxIn(id,0);
    }
    else
      fadeBoxOut(id,100);
  }
}

function closeb(id)
{
  c_el[id] = document.getElementById(id);
  fadeBoxOut(id,100);
}

function fadeBoxIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeBoxIn('"+objId+"',"+opacity+")", 1);
    }
    if(c_el[objId].style.display == 'none' && opacity >= 99)
    {
      c_el[objId].style.display = 'block';
    }
  }
}

function fadeBoxOut(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity >= 0) {
      setOpacity(obj, opacity);
      opacity -= 10;
      window.setTimeout("fadeBoxOut('"+objId+"',"+opacity+")", 1);
    }
    if(c_el[objId].style.display == 'block' && opacity == 0)
    {
      c_el[objId].style.display = 'none';
    }
  }
}

function on(str)
{
  if(document.getElementById(str))
    document.getElementById(str).style.display = "block";
}
function off(str)
{
  if(document.getElementById(str))
    document.getElementById(str).style.display = "none";
}

function htmlPopup(imageloc, width, height, scroll)
{
  if(width != null && height != null && scroll != null)
  {  
  HTML = "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n<html><head><meta http-equiv='content-type' content='text/html; charset=ISO-8859-1'><style>body, html{margin:0;padding:0;background-color:#FFFFFF;}</style></head><body><img src='"+ imageloc +"'/><br/></body></html>";
  popupImage = window.open("",'_blank','width='+width+',height='+height+',screenX=25,screenY=25,left=25,top=25,toolbar=no,scrollbars='+scroll+',resizable=yes,status=no,menubar=no,directories=no,location=no');
  popupImage.document.open();
  popupImage.document.write(HTML);
  popupImage.document.close();
  }
}
//------------

function scrollMapTo(x, y)
{
  if(map_status != 3)  //3==full map
  { 
    document.getElementById('city_map_div').scrollLeft = x;
    document.getElementById('city_map_div').scrollTop = y;
  }
  else
  {
    document.body.scrollLeft = x;
    document.body.scrollTop = y;
  }
}
//----------------------------------------

function popup(loc, width, height, scroll)
{
  if(width != null && height != null && scroll != null)
  {  
    p = window.open(loc,'','toolbar=no,width='+width+',height='+height+',screenX=25,screenY=25,left=25,top=25,scrollbars='+scroll+',resizable=yes,status=no,menubar=no,directories=no,location=no');
    p.focus();
  }
}

function translate(str)
{
  return "spot_"+str.replace("box_", "");
}

function infolate(str)
{
  return str.replace("box_", "");
}

var last_zindex = null;
function newzIndex(id)
{
  if(x_el[id].style.zIndex < 99999999)
  {
    x_el[id].style.zIndex = 99999999;
    if(last_zindex != null)
      x_el[last_zindex].style.zIndex = 300;
  } 
  last_zindex = id;
}


var IE = document.all?true:false
var xP;
var yP;
document.onmousemove = catchMouseMove;
function catchMouseMove(mov)
{
  if (IE)
  {
    xP = event.clientX + document.body.scrollLeft;
    yP = event.clientY + document.body.scrollTop;
  }
  else
  {
    xP = mov.pageX;
    yP = mov.pageY;
  }
  if (xP < 0){xP = 0;}
  if (yP < 0){yP = 0;}

  if(dragobjekt != null) {
      dragobjekt.style.left = (xP - dragx) + "px";
      dragobjekt.style.top = (yP - dragy) + "px";
  }
}

function jump(id)
{
  if(id != null) 
  {
    //Offset! Beim speichern wird er übernommen!!!
    if(IE) {ofsx=6;ofsy=9} else {ofsx=4;ofsy=7}
    id.style.left = (xP - dragx - ofsx) + "px";
    id.style.top = (yP - dragy - ofsy) + "px";
    id.style.display = "block";
  }
 
  if(document.Show.coord_x != null)
  {
    document.Show.coord_x.value = xP-ofsx;
    document.Show.coord_y.value = yP-ofsy;
  }
}

function in_array(needle, haystack)
{
  if(haystack.toString() == ''){return false;}
  var re = new RegExp(needle, 'g');
  return re.test(haystack);
}


var last_sel_feature = 0;
function rightbox(id)
{
  //var arrCC = new Array("mastercard", "amex", "visa", "dinners");
  if(document.infoboxform != null && document.infoboxform.shopid != null)
  {
    on('infoboxform');
    document.infoboxform.shopid.value = infolate(id);
    document.getElementById('infotitle').innerHTML = shops[infolate(id)][0];
    document.getElementById('infotext').innerHTML = shops[infolate(id)][1];
    if(shops[infolate(id)][2] == '')
      off('linkcontainer');
    else
      on('linkcontainer');

    if(shops[infolate(id)][6] == 1)
      on('boardlink');
    else
      off('boardlink');

    if(shops[infolate(id)][11] == 0)
      off('shopinfobox');
    else
      on('shopinfobox');

    if(shops[infolate(id)][8] != "")
    {
      var ccvals = shops[infolate(id)][8].split(",");
      for(var x=1; x<=ccvals.length; x++ )     
      {
        if(ccvals[x] == "1") on('mastercard'); else if(!in_array("1", ccvals)) off('mastercard');
        if(ccvals[x] == "2") on('amex'); else  if(!in_array("2", ccvals)) off('amex');
        if(ccvals[x] == "3") on('visa'); else  if(!in_array("3", ccvals)) off('visa');
        if(ccvals[x] == "4") on('diners'); else  if(!in_array("4", ccvals)) off('diners');
       }
     }
     else
     {
       off('mastercard');
       off('amex');
       off('visa');
       off('diners');
     }       

    if(shops[infolate(id)][9] > 0)
    {
      last_sel_feature = shops[infolate(id)][9];
      on('feature_'+last_sel_feature);
    }
    else
      off('feature_'+last_sel_feature);

    //Rabatt
    if(shops[infolate(id)][10] == 1)
      on('discount');
    else
      off('discount');
    
    for (var i = 0; i < document.links.length; ++i)
    {
      if( document.links[i].name == 'infohref')
        document.links[i].href = "http://"+shops[infolate(id)][2];
      if( document.links[i].name == 'feature_link')
        document.links[i].href = "javascript:popup('/mannheim/stadtinfos/loewenstark/popup/?id="+infolate(id)+"', 400, 400, 'yes');";
      if( document.links[i].name == 'add_bookmark')
        document.links[i].href = "javascript:savebox('"+id+"');";
        
    }
  }
}


var x_el = new Array(400); //must be set!

function showb(id)
{
  if(id != '' && id != null)
  {
    x_el[id] = document.getElementById(id);
    x_el[id]['wait'] = true;
    setTimeout("show_it('"+id+"')", 500);
  }
}

function showb_notimeout(id)
{
  if(id != '' && id != null)
  {
    x_el[id] = document.getElementById(id);
    x_el[id]['wait'] = true;
    show_it(id);
  }
}

function show_it(id)
{
  x_el[id] = document.getElementById(id);
  if(x_el[id]['wait'] == true)
  {
    if(x_el[id]['set'] == null)
    {
    rightbox(id);
    boxpos(id);
    newzIndex(id);
    x_el[translate(id)] = document.getElementById(translate(id));
    if(!isTransparentSpot(id))
      x_el[translate(id)].style.backgroundImage = 'url(/images/spot_'+getSpotType(id)+'_over.gif)';
    //if (window.xajax_updateShopstat)
      updateShopstat("?id="+infolate(id)+"&stat_type=2&city_fk="+city_fk);
    }
  }
}

function hide_it(id)
{
x_el[id] = document.getElementById(id);
x_el[id].style.display = "none";
x_el[translate(id)] = document.getElementById(translate(id));
if(!isTransparentSpot(id))
  x_el[translate(id)].style.backgroundImage = 'url(/images/spot_'+getSpotType(id)+'.gif)';
}


function hide(id)
{
  if(id != '' && id != null)
  {
    x_el[id]['wait'] = null;
    if(x_el[id]['set'] == null)
      setTimeout("hide_it('"+id+"')", 400);
  }
}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
function category_show()
{
change('shop_categories');
//hide_all_spots();

var catres = new Array();
var catresid = new Array();
for (var ii = 0; ii < category_show.arguments.length; ++ii)
{
  catid = category_show.arguments[ii];

  for ( shop_id in cats )
  {
    if(document.getElementById("spot_"+shop_id) != null)
    {
      for ( x in cats[shop_id] )
      {
        if(cats[shop_id][x] == catid)
        {
          //document.getElementById("spot_"+shop_id).style.display = 'block';
          catres[shop_id] = shops[shop_id][0];
          catresid[shop_id] = shop_id;
        }
      }
    }
  }
}

  output = '<div style="border:2px solid #637D91"><table width=190 cellpadding=1 cellspacing=0 border=0><tr style="background:#637D91;cursor:move"><td width=185><b style="color:white;font-size:9px">&nbsp;Ergebnisse zu dieser Kategorie</b></td><td><a href="javascript:;" onClick="chge(\'cat_results\')"><img src=/layout/1/close.gif></a></td></tr><tr><td colspan=2 style="padding:4px 6px;line-height:11px;font-size:10px">';
  var output_s = '';
  for (var uu = 0; uu < catres.length; ++uu)
  {
    if(catres[uu] != null && catres[uu] != 'undefined')
      output_s += "&raquo; <a href=\"javascript:jumptoshop('box_"+catresid[uu]+"')\">"+catres[uu]+"</a><br>";
  }
  if(output_s == '') 
    output_s = 'Leider keine Shops gefunden';
  output += output_s+'</td></tr></table></div>';
  document.getElementById('cat_results').innerHTML = output;
  document.getElementById('cat_results').style.display = 'block';
}



function show_all_spots()
{
  for ( i in shops )
    show_spot(i);
}

function hide_all_spots()
{
  unset_all();
  for ( i in shops )
  {
    if(!isTransparentSpot("box_"+i))
      off("spot_"+i);
  }
}

function show_spot(idnr)
{
  if(document.getElementById("spot_"+idnr) != null && document.getElementById("spot_"+idnr).style.display != null) 
    document.getElementById("spot_"+idnr).style.display = 'block';
}


function set(id)
{
  x_el[id] = document.getElementById(id);
  if(x_el[id]['set'] == null)
  {
    x_el[translate(id)] = document.getElementById(translate(id));
    rightbox(id);
    x_el[id]['set'] = true;
    show_spot(infolate(id));
    if(!isTransparentSpot(id))
      x_el[translate(id)].style.backgroundImage = 'url(/images/spot_'+getSpotType(id)+'_set.gif)';
newzIndex(id);
    boxpos(id);
	//if (window.xajax_updateShopstat)
	   //updateShopstat(infolate(id), 1,city_fk);
	   updateShopstat("?id="+infolate(id)+"&stat_type=1&city_fk="+city_fk);
  }
  else
  {
    unset(id);
  }
}

function getSpotType(id)
{
  return  shops[infolate(id)][5];
}

function isTransparentSpot(id)
{
  if(shops[infolate(id)][12] > 0 && shops[infolate(id)][13] > 0)
    return  true;
  else
    return false;
}

var isJumptoFct = null;
function boxpos(id)
{
	//Sonderformat für 2
	if(shops[infolate(id)][7] == "2")
	{
	  offset_w = 205;
	  offset_h = 90;
	}
	else
	{
	  offset_w = 245;
	  offset_h = 160;
	}
	
    t_offset_w = 0;
    t_offset_h = 0;
	if(isTransparentSpot(id) && isJumptoFct == 1)
	{
	  t_offset_w = shops[infolate(id)][12]-15;
	  t_offset_h = shops[infolate(id)][13]-15;
	}

	realHeight = document.body.clientHeight + document.body.scrollTop;
	realWidth = document.body.clientWidth + document.body.scrollLeft;
	
	if(xP >= realWidth-offset_w)
	  x_el[id].style.left = (xP - offset_w - 2 + t_offset_w) + "px";
	else
	  x_el[id].style.left = (xP + 20 + t_offset_w) + "px";
	  
	if(yP >= realHeight-offset_h)
	  x_el[id].style.top = (yP - offset_h - 15 + t_offset_h) + "px";
	else
	  x_el[id].style.top = (yP + 5 + t_offset_h) + "px";

	x_el[id].style.display = "block";
}

function unset(id)
{
  x_el[id] = document.getElementById(id);
  x_el[id]['set'] = null;
  x_el[translate(id)] = document.getElementById(translate(id));
  if(!isTransparentSpot(id))
    x_el[translate(id)].style.backgroundImage = 'url(/images/spot_'+getSpotType(id)+'.gif)';
  x_el[id].style.display = "none";
}


function unset_all()
{
  for (var i = 0; i < x_el.length; ++i)
  {
    if(x_el["box_"+i] != null)
	  unset("box_"+i);
  }
}

//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
var lastlink = null;
function linkshow(id)
{
  change('shoplinks');
  jumptoshop(id);
}

function jumptoshop(id)
{
  unset_all();
  nrid = infolate(id);
//  show_spot(nrid);
  rightbox(id);
  yS = shops[nrid][3]-200;
  xS = shops[nrid][4]-230;
  scrollMapTo(xS, yS);

  xP = shops[nrid][4] +160 - document.getElementById('city_map_div').scrollLeft;
  yP = shops[nrid][3] +90+34 - document.getElementById('city_map_div').scrollTop;
  showb_notimeout(id, shops[nrid][5]);
  set(id, shops[nrid][5]);
  blink("jarrow", shops[nrid][3],shops[nrid][4]);
  //alert(document.getElementById("spot_"+nrid).style.backgroundImage);
}

function blink(str, x, y)
{
  document.getElementById(str).style.top = (x - 15) + "px";
  document.getElementById(str).style.left = (y - 20) + "px";
    
	setTimeout("blink_element('"+str+"', 'on')", 250);
	setTimeout("blink_element('"+str+"', 'off')", 350);
	setTimeout("blink_element('"+str+"', 'on')", 450);
	setTimeout("blink_element('"+str+"', 'off')", 550);
	setTimeout("blink_element('"+str+"', 'on')", 650);
	setTimeout("blink_element('"+str+"', 'off')", 750);
	setTimeout("blink_element('"+str+"', 'on')", 850);
	setTimeout("blink_element('"+str+"', 'off')", 950);
}

function blink_element(str, on_off)
{
  if(on_off == 'on')
    on(str);
  else
    off(str);
}
//-------------------------
document.onmouseup = dragstop;
var dragobjekt = null;

// Position, an der das Objekt angeklickt wurde.
var dragx = 0;
var dragy = 0;


function drag(id) {
//Wird aufgerufen, wenn ein Objekt bewegt werden soll.
  simpledrag(id);
  newzIndex(id)
  rightbox(id);
}

function simpledrag(id)
{
  x_el[id] = document.getElementById(id);
  dragobjekt = x_el[id];
  dragx = xP - dragobjekt.offsetLeft;
  dragy = yP - dragobjekt.offsetTop;
}

function dragstop() 
{
//Wird aufgerufen, wenn ein Objekt nicht mehr bewegt werden soll.
  if(dragobjekt != null)
  {
    dragobjekt.style.left = (xP - dragx) + "px";
    dragobjekt.style.top = (yP - dragy) + "px";
    dragobjekt=null;
  }
}


var arrLinks = new Array();
function markBold(id)
{
  //reset all
  unmarkAll();
  //set clicked link
  arrLinks[id] = document.getElementById(id);
  arrLinks[id].style.fontWeight = "bold";
}

function unmarkAll()
{
  unmarkCatLinks();
  if(lastbold != null && lastbold.style != null)
    lastbold.style.fontWeight = "normal";
}

function unmarkCatLinks()
{
  for(i in arrLinks)
  {
    if(arrLinks[i].style != null)
      arrLinks[i].style.fontWeight = "normal";
  }
}

var lastbold = null;
function changeBold(object)
{
  unmarkCatLinks();
  if(lastbold != null)
    lastbold.style.fontWeight = "normal";
  if(object.style.fontWeight == 'bold')
    object.style.fontWeight = "normal";
  else
    object.style.fontWeight = "bold";
  lastbold = object;
}


function textCounter(field, cntfield, maxlimit) 
{
  if(field != null && cntfield != null)
  {
    if (field.value.length > maxlimit) 
      field.value = field.value.substring(0, maxlimit);
    else 
      cntfield.innerHTML = maxlimit - field.value.length;
  }
}

var arrSavedBoxes = new Object();
function savebox(id)
{
  var str = '';
  nrid = infolate(id);
  arrSavedBoxes[nrid] = nrid;
  for ( i in arrSavedBoxes )
  {
    str += "<a href=\"javascript:jumptoshop('box_"+i+"');change('saved_shops');color_unset('bm_td')\">"+shops[i][0]+"</a> | ";
  }
  str += "<a href=\"javascript:delbox();change('saved_shops');\" title=\"Lesezeichen löschen\" onClick=\"return confirm('Alle Lesezeichen löschen?')\"><b>x</b></a>";

  document.getElementById('saved_shops').innerHTML = '<div style="padding:6px 10px">'+str+'</div>';
  unset(id);
    setTimeout("color_ch('bm_td','#637D91')", 150);
    setTimeout("color_ch('bm_td','#89A2B6')", 250);
    setTimeout("color_ch('bm_td','#637D91')", 350);
    setTimeout("color_ch('bm_td','#89A2B6')", 450);
    setTimeout("color_ch('bm_td','#637D91')", 550);
    setTimeout("color_ch('bm_td','#89A2B6')", 650);
}
function color_ch(element, color)
{
  document.getElementById(element).bgColor=color;
}

function delbox()
{
  arrSavedBoxes = new Object();
  document.getElementById('saved_shops').innerHTML = '<div style="padding:6px 10px">Lesezeichen sind leer</div>';
}


function color_unset(element)
{
  document.getElementById(element).bgColor='#89A2B6';
}


function isBlock(e)
{
 if(document.getElementById(e).style.display == 'block')
   return true;
 else
   return false;
}

function showSpotType(type)
{
  if(document.getElementById('le_sp_'+type) == lastbold)
  {
    lastbold.style.fontWeight = "normal";
    show_all_spots();
    lastbold = null;
  }
  else
  {
    changeBold(document.getElementById('le_sp_'+type));
  for ( i in shops )
  {
    if(document.getElementById("spot_"+i) != null && shops[i][5] == type)
      on("spot_"+i);
    else if(isTransparentSpot("box_"+i)) 
      on("spot_"+i);
    else
      off("spot_"+i);
  }
  
  }
}

//Shop-Tracking
var ajaxrequest = false;
function updateShopstat(post_request) 
{
  if (window.XMLHttpRequest) 
    ajaxrequest = new XMLHttpRequest();

  if (window.ActiveXObject) 
    ajaxrequest = new ActiveXObject("Microsoft.XMLHTTP");

  ajaxrequest.open("GET", "/shoppingplaner/update_shop_stat.php"+post_request); 
  ajaxrequest.send(null);
}



