// globals
var IE = document.all?true:false;
var activeLinks = new Array;
var oldactiveLinks = new Array;
var mouseX = 0;
var mouseY = 0;

/** Initialize all of our objects now */
function startup(locale, controller, action, id) {

  window.historyStorage.init();
  window.dhtmlHistory.create();

  dhtmlHistory.initialize();

  var loc='';
  if (locale) {
    loc='/'+locale;
  }
  if (controller) {
    loc=loc+'/'+controller;
    if (action) {
      loc=loc+'/_'+action;
      if (id) {
	loc=loc+'/'+id;
      }
    }
  }
  var initialLocation = dhtmlHistory.getCurrentLocation();
  if (initialLocation==null || initialLocation=='' || initialLocation=='start') {
    dhtmlHistory.add('start', {update : {'content' : loc}});
  } else {
    newHash={update:{}};
    ar=initialLocation.split('--');
    for (var i=0; i<ar.size(); i++) {
      up=ar[i].split(':');
      if (up[1]!=null) {
	newHash['update'][up[0]]=up[1].split('&&').join('?');
      }
    }
    navigate(newHash, true);
  }
  dhtmlHistory.addListener(handleHistoryChange);

  // init autocompleter after dhtmlhistory!!! otherwise ac, doesn't work!
  var code_menu_auto_completer =
    new Ajax.Autocompleter('menu_article', 'menu_article_auto_complete', '/search/auto_complete_for_article_search', {frequency:0.1});

  // add event listener for mouse position
  document.onmousemove=handleMousePosition;

  // add html for cart updates
  var newdiv = document.createElement('div');
  //newdiv.setAttribute('id','cart_helper');
  //newdiv.setAttribute('style','width:120px; height:30px; background-color:#DADFE4; float:none; display:none; z-index:5; position:absolute; left:0px; top:0px;');
  //newdiv.innerHTML = '<img alt="" src="/images/de/basket_ok.gif">';
  newdiv.innerHTML = '<div id="cart_helper" style="width:165px; height:60px; background-color:#DADFE4; float:none; display:none; z-index:5; position:absolute; left:0px; top:0px;"><img alt="" src="/images/de/basket_ok1.gif"></div>';
  var body = document.getElementById('body');
  body.appendChild(newdiv);

// init highslide after dhtmlhistory!!! otherwise highslide, doesn't work!
//  hs.addEventListener(window, 'load', hs.preloadImages);
//  hs.addEventListener(document, 'mouseup', hs.mouseClickHandler);
//  hs.addEventListener(document, 'mousedown', hs.mouseClickHandler);
//  hs.graphicsDir = '/images/highslide/';
}

// add to basket
function cart_helper_start() {
  wait_cursor();
  var carthelper = document.getElementById('cart_helper');
  if (carthelper.style) {carthelper=carthelper.style;}
  carthelper.left = (mouseX-170) + "px";
  carthelper.top = (mouseY-65) + "px";
  new Effect.Appear('cart_helper');
  setTimeout('new Effect.Fade(\'cart_helper\')', 4000);
}
// Mouse Event Handler
function handleMousePosition(event) {
  if (!event)
    event=window.event;

  if (event.pageX) {
    mouseX = event.pageX;
    mouseY = event.pageY;
  } else if (event.clientX) {
    mouseX = event.clientX + (document.documentElement.scrollLeft ?
			      document.documentElement.scrollLeft :
			      document.body.scrollLeft);
    mouseY = event.clientY + (document.documentElement.scrollTop ?
			      document.documentElement.scrollTop :
			      document.body.scrollTop);
  } else {
    mouseX = 0;
    mouseY = 0;
  }
}

// history things
function handleHistoryChange(newLocation, historyData) {
  navigate(historyData);                      
}
function navigate(newHash, add, nojump) {
  if (newHash!=undefined && newHash.length!=0) {
    wait_cursor();
    oldactiveLinks=activeLinks;
    activeLinks=new Array;    
  }
  newLocation=new Array;
  for (var i in newHash) {
    if (i=='update') {
      x=0;
      for (var l in newHash[i]) {
	newLocation.push(l+':'+newHash[i][l].split('?').join('&&'));
	if (x<newHash[i].length) {
	  new Ajax.Updater(l, newHash[i][l], {asynchronous:true, evalScripts:true, method:'get'});
	} else {
	  new Ajax.Updater(l, newHash[i][l], {asynchronous:true, evalScripts:true, method:'get', onComplete:function(){if (getPosY()!=0 && !nojump) {window.scrollTo(0,0);} activate_links(); default_cursor(); }});
	}
	x=x+1;
      }
    } else if (i=='activate') {
      activeLinks.push(newHash[i]['name']);
    } else {
      alert('NotImplementedError');
    }
  }
  if (add)
    dhtmlHistory.add(newLocation.join('--'), newHash);
}

// category browsing
function open_main_cat(locale, id) {
  if (locale!='') {
    navigate({ update : {category : '/'+locale+'/category/_nav/'+id, content : '/'+locale+'/category/_view/'+id} }, true);
  } else {
    navigate({ update : {category : '/category/_nav/'+id, content : '/category/_view/'+id} }, true);
  }
}

// search
function submitSearch() {
  wait_cursor();
  new Ajax.Updater('content', '/search/_execute', {asynchronous:true, evalScripts:true, onComplete:function(request){default_cursor()}, parameters:document.getElementById('menu_article').serialize()});

  loc='/search/_execute/'+document.getElementById('menu_article').value;
  dhtmlHistory.add('content:'+loc, {update : {'content' : loc}});
}

// links
function activate_links() {
  if (activeLinks.size() == 0) {
    activeLinks=oldactiveLinks;
    return;
  }
  deactivate_links();
  for (var i=0; i<activeLinks.size(); i++) {
    els=document.getElementsByName(activeLinks[i]);
    for (var j=0; j<els.length; j++) {
      els[j].style.color='#C22812';
    }
  }
}
function deactivate_links() {
  for (var i=0; i<oldactiveLinks.size(); i++) {
    els=document.getElementsByName(oldactiveLinks[i]);
    for (var j=0; j<els.length; j++) {
      els[j].style.color='#586A7C';
    }
  }
}

// get Scroll Position
function getPosY() {
  var x,y;
  if (self.pageYOffset) // all except Explorer
    {
      //x = self.pageXOffset;
      y = self.pageYOffset;
    }
  else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
      //x = document.documentElement.scrollLeft;
      y = document.documentElement.scrollTop;
    }
  else if (document.body) // all other Explorers
    {
      //x = document.body.scrollLeft;
      y = document.body.scrollTop;
    }
  return(y);
}

// mouse cursor
function wait_cursor() {
  w=document.body.clientWidth;
  document.getElementById('load').style.left=((w-202)/2)+'px';
  document.getElementById('load').style.visibility='visible';
  document.body.style.cursor='wait';
}
function default_cursor() {
  document.getElementById('load').style.visibility='hidden';
  document.body.style.cursor='default';
}

// popups
function openPic(id) {
  var newWindow=window.open("/popups/bigpic/"+id,"bigpic","width=500,height=500,scrollbars=yes,resizable=yes");
  newWindow.focus();
}
function resizeWindow() {
  window.resizeTo(document.images[0].width+100,document.images[0].height+100);
}

// order helpers
function clear_order_del() {
  document.getElementById('delivery_country_id').options[0].selected=1;
  document.getElementById('register_del_div').style.display='none';
  ar=['delivery[firstname]', 'delivery[name]', 'delivery[street]', 'delivery[zip]', 'delivery[city]'];
  for (i=0; i<ar.size(); i++) {
    document.getElementsByName(ar[i])[0].value="";
  }
  new Ajax.Updater('payment_div', '/order/_payment', {asynchronous:true, evalScripts:true, parameters:'delivery_country_id='});
}
