// Popup <a href="html-or.jpg" onclick="return popup(this,123,456)" title="..."

var pop = null;

function popdown() {
  if (pop && !pop.closed) pop.close();
}

function popup(obj,w,h) {
  var url = (obj.getAttribute) ? obj.getAttribute('href') : obj.href;
  if (!url) return true;
  w = (w) ? w += 20 : 550;
  h = (h) ? h += 25 : 380;

	if (w < screen.availWidth || h < screen.availHeight) {
		x = (screen.availWidth - w - 12) / 2;
		y = (screen.availHeight - h - 104) / 2;
		if (window.opera) y = 0; // Opera positioniert unter den Symbolleisten
		if (x<0 || y<0) { x=0; y=0; }
	}

  var args = 'left=' + x + ',top=' + y + ',width=' + w + ',height=' + h + ',scrollbars=yes,resizable';
  popdown();
  pop = window.open(url,'',args);
  return (pop) ? false : true;
}

window.onunload = popdown;
window.onfocus = popdown;


/*
window.onload = function() {
  //try {
    //var type = $('searchtype').value;
    //new Ajax.Autocompleter('q', 'autocomplete_choices', 'javascript/mod_search_engine.php?cmd=getSearchSuggestions&searchtype='+type, { paramName: "query", afterUpdateElement : submitSearch });
  //} catch(e) {
  //}
}
*/

/**
 * Kundenstimmenfunktionen:
 */

var voteUrl = "javascript/mod_customer_votes.php";

// Kundenstimme vollständig anzeigen
function expandVote(obj) {
	var id = obj.parentNode.parentNode.id.substr(2);
	new Ajax.Updater(obj.parentNode, voteUrl, {
		parameters:{
			cmd: 'expand',
			voteid: id,
			shop: myShop
		},
		onSuccess: function(transport) {
			obj.parentNode.hide();
			new Effect.Appear(obj.parentNode, { duration: 1.0 });
		},
		onFailure: function(transport) {
			alert("Sorry, zur Zeit steht diese Funktion leider nicht zur Verfügung!");
		}
	});
	return false;
}

// Kundenstimme vollständig anzeigen
function showAllVotes(bstnr, obj) {
  //var newDiv = $div({id:'vote_'+id}, "Loading...");
  //newDiv.addClassName('vote');
  //$('votelist').insertBefore(newDiv, $('votelist').childNodes[0]);

	new Ajax.Updater(obj.parentNode.parentNode, voteUrl, {
		parameters:{
			cmd: 'showMoreVotes',
			bestellnr: bstnr,
			shop: myShop
		},
		onSuccess: function(transport) {
			obj.parentNode.parentNode.hide();
			new Effect.Appear(obj.parentNode.parentNode, { duration: 1.0 });
		},
		onFailure: function(transport) {
			alert("Sorry, zur Zeit steht diese Funktion leider nicht zur Verfügung!");
		}
	});
	return false;
}

// Kundenstimme bewerten
function voting(obj, answer) {
	var id = obj.parentNode.parentNode.id.substr(2);
	new Ajax.Updater(obj.parentNode, voteUrl, {
		parameters:{
			cmd: 'vote',
			voteid: id,
			shop: myShop,
			voting: answer
		},
		onSuccess: function(transport) {
			new Effect.Highlight(obj.parentNode, { duration: 1.0 });
		},
		onFailure: function(transport) {
			alert("Sorry, momentan können keine Kundenstimmen bewertet werden!");
		}
	});
	return false;
}

// Neue Kundenstimme
function addVote(bstnr) {
	new Ajax.Updater($('newvote'), voteUrl, {
		parameters:{
			cmd: 'add',
			shop: myShop,
			bestellnr: bstnr
		},
		onSuccess: function(transport) {
			new Effect.Appear($('newvote'), { duration: 0.5 });
		},
		onFailure: function(transport) {
			alert("Sorry, momentan können keine Kundenstimmen abgegeben werden!");
		}
	});
	return false;
}

function cancelVoteAdd() {
	new Effect.SlideUp('newvote', { duration: 0.5 });
	$('addvote').show();
}

function addVoteSave() {
	var form = $('newvote_form');

	var valid;

	valid = $(form.title).present()
	     && $(form.description).present()
	     && $(form.customer_name).present()
	     && $(form.customer_email).present()
	     && $(form.customer_location).present();

	if ( ! valid ) {
		if ( $('votemsg').style.display == 'none' )
			new Effect.Appear($('votemsg'), { duration: 1.0 });
		else
			new Effect.Highlight($('votemsg'), { duration: 1.0 });
		return false;
	}

	form.cmd.value = 'save';
	form.shop.value = myShop;

	new Ajax.Updater($('newvote'), voteUrl, {
		parameters:$('newvote_form').serialize(),
		onSuccess: function(transport) {
			new Effect.Appear($('newvote'), { duration: 1.0 });
		},
		onFailure: function(transport) {
			alert("Sorry, momentan können keine Kundenstimmen abgegeben werden!");
		}
	});
	return false;
}


/**
 * Produkt-Funktionen:
 */

var productUrl = "javascript/mod_product.php";

// Lagerstatus
function getStock(bnr) {
  $('stockupdater').update('Loading...');

  new Ajax.Request(productUrl, {
		parameters:{
			cmd: 'getStock',
			bestellnr: bnr,
			shop: myShop
		},
 		onSuccess: function(transport) {
      //$('stock').className = transport.responseText;
      var json = transport.responseText.evalJSON();
      $('stock').update(json.amount);
      $('stock').className = json.classname;
      $('stockupdater').update(json.updatedText);
      new Effect.Highlight($('stock'));
    },
		onFailure: function(transport) {
			alert("Sorry, zur Zeit steht diese Funktion leider nicht zur Verfügung!");
		}
  });

	return false;
}

function submitSearch() {
  $('searchengine').submit();
}

// Ajax Responders

Ajax.Responders.register({

  onCreate: function() {
    if($('busy') && Ajax.activeRequestCount > 0)
      Effect.Appear('busy',{duration:0.5,queue:'end'});
  },

  onComplete: function() {
    if($('busy') && Ajax.activeRequestCount == 0)
      Effect.Fade('busy',{duration:1.0,queue:'end'});
  }

});

function submitSearch(text, li) {
    location.href = li.childNodes[0].href;
}



/**
 * Warenkorb Funktionen
 */

// Prüft das Warenkorb Formular und gibt entsprechende Fehlermeldungen aus
function checkCartFormular() {
  new Ajax.Request(document.URL, {
		parameters:{
			cmd: 'checkCartFormular'
		},
 		onSuccess: function(transport) {
      var json = transport.responseText.evalJSON();

      for ( var i=0; i<json.mymessages.length; i++ )
        alert(json.mymessages[i]);

      for ( var j=0; j<json.highlightIds.length; j++ )
        new Effect.Pulsate($(json.highlightIds[j]), { pulses: 3, duration: 1.5 });


      if ( json.checkoutPermission ) {
        $('cart').action = json.formAction;
        $('cart').submit();
      }

      //$('stock').className = transport.responseText;
      /*

      $('stock').update(json.amount);
      $('stock').className = json.classname;
      $('stockupdater').update(json.updatedText);
      new Effect.Highlight($('stock'));
      */
    },
		onFailure: function(transport) {
			alert("Sorry, zur Zeit steht diese Funktion leider nicht zur Verfügung!");
		}
  });
  return false;
}
