
var		xmlHttp;
var		gl_choice;
var		gl_modal;
var reponse = "";
var loader = "";

function	traite_form(form)
{
	var data = "";
	tableau = new Array();
	
		data += "&ajax=1";
	for (var i=0; i<form.length; i++)
	{
		elt = form.elements[i];
		
		switch(elt.type)
		{
		case "text":	
		case "button":	
		case "submit":
		case "select-one":
		case "textarea":
		case "hidden":
		case "password":
		value = replace_retour(replace_esper(elt.value));
		break;

		case "checkbox" :
		case "radio" :
		value = (elt.checked)?elt.value:"";
		break;

		case "select-multiple" :
		for(a=0;a<elt.length;a++){
		if(elt.options[a].selected==true)
		tableau.push(elt.options[a].value);
		}
		break;
		default :
		value = elt.type+"";
		}
		
	data += "&"+elt.name+"="+value;

	if(tableau.length>0){
	for(a=0;a<tableau.length;a++){
	data += "&"+elt.name+"[]="+tableau[a];
	}
	tableau = new Array();
		
	}
	}
	var data = data.substr(1);
	return (data);
}

function	use_form_ajax(id_form, url, modal, temp_reponse, temp_loader)
{
	var form="";
  var data="";
  
	loader = temp_loader;
	reponse = temp_reponse;
	if (document.getElementById(loader)) {document.getElementById(loader).style.visibility = 'visible';}
	if (id_form != "") {
	form = document.getElementById(id_form);}
	gl_modal = modal;
	if (form != "") {
	data = traite_form(form);}
	send_ajax(url, data, "POST");
}

function	send_ajax(url, data, method)
{
    xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
	    alert('Browser does not support HTTP Request');
	    return (false);
	}
	xmlHttp.onreadystatechange = stateChanged;
    if(method == "GET")
     {
     if(data == 'null')
     {
            xmlHttp.open("GET", url, true); //ouverture asynchrone
     }
     else
     {
            xmlHttp.open("GET", url+"?"+data, true);
     }
        xmlHttp.send(null);
     }
     else if(method == "POST")
     {
        xmlHttp.open("POST", url, true); //ouverture asynchrone
        xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-1');
        xmlHttp.send(data);
     }
}

function  is_ie()
{
if (navigator.appName=="Microsoft Internet Explorer")
{
return 1;
}
return 0;
}

function	stateChanged()
{
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
	if (xmlHttp.responseText[0] == '#' && !is_ie())
	{
		var newchaine = xmlHttp.responseText.substr(1);
		if (document.getElementById(reponse)) {document.getElementById(reponse).innerHTML = newchaine;}
	    if (document.getElementById(loader)) {document.getElementById(loader).style.visibility = 'hidden';}		
	}
	switch (gl_modal)
	{

		case '0':
				if (document.getElementById(reponse)) {document.getElementById(reponse).innerHTML = xmlHttp.responseText;}
	    if (document.getElementById(loader)) {document.getElementById(loader).style.visibility = 'hidden';}
		break;
		
		case '1':
		Modalbox.show(xmlHttp.responseText, {title: 'Your personnal information ! - Step 1/1 -', width: 850, height: 700}); return false;
		break;
		
		case '2':
		Modalbox.show(xmlHttp.responseText, {title: 'Your Brand and Style ! - Step 1/2 -', width: 700, height: 550 });
		break;
		

		case '3':
		Modalbox.show("<div align='center'><b>Welcome !</b><br> <br> A email has been sended to you to activate your account. <br> Please check your email.</div>", {title: 'Your inscription is finished !', width: 700, height: 250}); return false;
		break;

		case '4':
		Modalbox.show("<div align='center'><b><br><br>Favoris Added !</b></div>", {title: 'Your action !', width: 400, height: 150}); return false;
		break;

		case '5':
		Modalbox.show("<div align='center'><b><br>Si vous êtes logguer votre commentaire à été ajouté.<br/>Sinon veuillez vous logguer ou vous inscrire pour pouvoir ajouter un commentaire !</b> </div><script type='text/javascript'>window.location.reload()</script>", {title: 'Votre Commentaire !', width: 400, height: 150}); return false;
		break;
		
		case '6':
		if (document.getElementById(reponse)) {document.getElementById(reponse).innerHTML = xmlHttp.responseText;}
	  if (document.getElementById(loader)) {document.getElementById(loader).style.visibility = 'hidden';}
	  initialize_auto();
		break;
		
		
		case '7':
		alert('Photos deleted');
		location.reload();
		break;
		
		case '8':
		alert('Comment deleted');
		location.reload();
		break;
		
		case '9':
		alert('Favoris deleted');
		location.reload();
		break;
		
		case '10':
		alert('Events deleted');
		location.reload();
		break;
		
		case '11':
		if (document.getElementById(reponse)) {document.getElementById(reponse).innerHTML = xmlHttp.responseText;}
	  if (document.getElementById(loader)) {document.getElementById(loader).style.visibility = 'hidden';}
	  update_autocomplete();
		break;

		case '12':
				if (document.getElementById(reponse)) {document.getElementById(reponse).innerHTML = xmlHttp.responseText;}
	    if (document.getElementById(loader)) {document.getElementById(loader).style.visibility = 'hidden';}
	     shop_indexof = 1;
	    do_pager();
	  //setInterval("cacher_montrer();",2000);
		break;
		
    case '13':
    if (xmlHttp.responseText != "") {
		Modalbox.show("Desole : "+xmlHttp.responseText+".", {title: 'Votre Panier', width: 400, height: 150}); }
		break;


		
		default:
		if (document.getElementById(reponse)) {document.getElementById(reponse).innerHTML = xmlHttp.responseText;}
	    if (document.getElementById(loader)) {document.getElementById(loader).style.visibility = 'hidden';}
		break;
		
	}
	}

}

/*brand1 = construct_auto('0', "brand1", "autocomplete_choices1", "indicator1");
brand1.url = "/get/act/cat/list/designers/";
construct_auto('0', "brand2", "autocomplete_choices2", "indicator2");
construct_auto('0', "brand3", "autocomplete_choices3", "indicator3");
construct_auto('0', "brand4", "autocomplete_choices4", "indicator4");
construct_auto('0', "brand5", "autocomplete_choices5", "indicator5");

construct_auto('1', "cat1", "autocomplete_choicescat1", "indicatorcat1");
construct_auto('1', "cat2", "autocomplete_choicescat2", "indicatorcat2");
construct_auto('1', "cat3", "autocomplete_choicescat3", "indicatorcat3");
construct_auto('1', "cat4", "autocomplete_choicescat4", "indicatorcat4");
construct_auto('1', "cat5", "autocomplete_choicescat5", "indicatorcat5");
*/

var br = new Array();
var cat = new Array();

function  initialize_brand(limit)
{

for(var j = 1; j <= limit;j++)
{
br[j] = construct_auto('0', "brand"+j, "autocomplete_choices"+j, "indicator"+j);
}

for(j = 1; j <= limit;j++)
{
cat[j] = construct_auto('1', "cat"+j, "autocomplete_choicescat"+j, "indicatorcat"+j);
}

}

function  update_autocomplete()
{
url = construct_url('0');
for(var j = 1; j <= 10;j++)
{
if (br[j]){
br[j].url = url;}
}

url = construct_url('1');
for(var j = 1; j <= 10;j++)
{
if (cat[j]) {
cat[j].url = url;}
}

}

function  construct_auto(brand_or_cat, champ, champ_div, loader)
{
var url = "";
url = construct_url (brand_or_cat);
a = new_autocompleter(champ, champ_div, url, loader);
return a;
}

/////////utils for ajax completer !

function  construct_url(brand_or_cat)
{
var url = "";
url = "/get/act/";
if (brand_or_cat == '0') {url += "brands/list/designers/";}
if (brand_or_cat == '1') {url += "cat/list/designers/";}

var je = document.getElementById('jewel');
if (je) { url += "cl_or_je/je/";}
var cl = document.getElementById('clothe');
if (cl) { url += "cl_or_je/cl/";}

var is_cl = document.getElementById('je');
var is_je = document.getElementById('cl');
var is_men = document.getElementById('men');
var is_women = document.getElementById('women');
var is_kid = document.getElementById('kid');

if (is_cl) {if (document.getElementById('cl').checked == true ){url += "cl_or_je/cl/";}}
if (is_je) {if (document.getElementById('je').checked == true ){url += "cl_or_je/je/";}}
if (is_men) {if (document.getElementById('men').checked == true){url += "men/1/";}}
if (is_women) {if (document.getElementById('women').checked == true){url += "women/1/";}}
if (is_kid) {if (document.getElementById('kid').checked == true){url += "kid/1/";}}

return url;
}

function  new_autocompleter(champ, champ_div, url, loader)
{
a = new Ajax.Autocompleter(champ, champ_div, url, {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionId, indicator: loader});
return a;
}

function getSelectionId(text, li) {
     text.value = li.id;
     go_search_auto();
}

/////nimporte quoi apres cette ligne XD

function  initialize_auto(){

var payant = 0;
var j = document.getElementById("cat2_id");
var h = document.getElementById("cat6_id");
if (j) {payant++;}
if (h) {payant++;}

if (document.getElementById("type").value != '4') {
new Ajax.Autocompleter("br1_id", "autocomplete_choicesbr1_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr1_id, indicator: "indicatorbr1_id"});
if (payant > 0) {
new Ajax.Autocompleter("br2_id", "autocomplete_choicesbr2_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr2_id, indicator: "indicatorbr2_id"});
new Ajax.Autocompleter("br3_id", "autocomplete_choicesbr3_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr3_id, indicator: "indicatorbr3_id"});
new Ajax.Autocompleter("br4_id", "autocomplete_choicesbr4_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr4_id, indicator: "indicatorbr4_id"});
new Ajax.Autocompleter("br5_id", "autocomplete_choicesbr5_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr5_id, indicator: "indicatorbr5_id"});
}
if (payant > 1) {
new Ajax.Autocompleter("br6_id", "autocomplete_choicesbr6_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr6_id, indicator: "indicatorbr6_id"});
new Ajax.Autocompleter("br7_id", "autocomplete_choicesbr7_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr7_id, indicator: "indicatorbr7_id"});
new Ajax.Autocompleter("br8_id", "autocomplete_choicesbr8_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr8_id, indicator: "indicatorbr8_id"});
new Ajax.Autocompleter("br9_id", "autocomplete_choicesbr9_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr9_id, indicator: "indicatorbr9_id"});
new Ajax.Autocompleter("br10_id", "autocomplete_choicesbr10_id", "/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr10_id, indicator: "indicatorbr10_id"});
new Ajax.Autocompleter("br11_id", "autocomplete_choicesbr11_id", "http://rocknstore.com/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr11_id, indicator: "indicatorbr11_id"});
new Ajax.Autocompleter("br12_id", "autocomplete_choicesbr12_id", "http://rocknstore.com/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr12_id, indicator: "indicatorbr12_id"});
new Ajax.Autocompleter("br13_id", "autocomplete_choicesbr13_id", "http://rocknstore.com/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr13_id, indicator: "indicatorbr13_id"});
new Ajax.Autocompleter("br14_id", "autocomplete_choicesbr14_id", "http://rocknstore.com/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr14_id, indicator: "indicatorbr14_id"});
new Ajax.Autocompleter("br15_id", "autocomplete_choicesbr15_id", "http://rocknstore.com/get/act/brands/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdbr15_id, indicator: "indicatorbr15_id"});

}

new Ajax.Autocompleter("cat1_id", "autocomplete_choicescat1_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat1_id, indicator: "indicatorcat1_id"});
if (payant > 0) {
new Ajax.Autocompleter("cat2_id", "autocomplete_choicescat2_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat2_id, indicator: "indicatorcat2_id"});
new Ajax.Autocompleter("cat3_id", "autocomplete_choicescat3_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat3_id, indicator: "indicatorcat3_id"});
new Ajax.Autocompleter("cat4_id", "autocomplete_choicescat4_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat4_id, indicator: "indicatorcat4_id"});
new Ajax.Autocompleter("cat5_id", "autocomplete_choicescat5_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat5_id, indicator: "indicatorcat5_id"});
}
if (payant > 1) {
new Ajax.Autocompleter("cat6_id", "autocomplete_choicescat6_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat6_id, indicator: "indicatorcat6_id"});
new Ajax.Autocompleter("cat7_id", "autocomplete_choicescat7_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat7_id, indicator: "indicatorcat7_id"});
new Ajax.Autocompleter("cat8_id", "autocomplete_choicescat8_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat8_id, indicator: "indicatorcat8_id"});
new Ajax.Autocompleter("cat9_id", "autocomplete_choicescat9_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat9_id, indicator: "indicatorcat9_id"});
new Ajax.Autocompleter("cat10_id", "autocomplete_choicescat10_id", "/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat10_id, indicator: "indicatorcat10_id"});
new Ajax.Autocompleter("cat11_id", "autocomplete_choicescat11_id", "http://rocknstore.com/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat11_id, indicator: "indicatorcat11_id"});
new Ajax.Autocompleter("cat12_id", "autocomplete_choicescat12_id", "http://rocknstore.com/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat12_id, indicator: "indicatorcat12_id"});
new Ajax.Autocompleter("cat13_id", "autocomplete_choicescat13_id", "http://rocknstore.com/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat13_id, indicator: "indicatorcat13_id"});
new Ajax.Autocompleter("cat14_id", "autocomplete_choicescat14_id", "http://rocknstore.com/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat14_id, indicator: "indicatorcat14_id"});
new Ajax.Autocompleter("cat15_id", "autocomplete_choicescat15_id", "http://rocknstore.com/get/act/cat/list/designers/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat15_id, indicator: "indicatorcat15_id"});

}}

else
{
new Ajax.Autocompleter("cat1_id", "autocomplete_choicescat1_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat1_id, indicator: "indicatorcat1_id"});
new Ajax.Autocompleter("cat2_id", "autocomplete_choicescat2_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat2_id, indicator: "indicatorcat2_id"});
new Ajax.Autocompleter("cat3_id", "autocomplete_choicescat3_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat3_id, indicator: "indicatorcat3_id"});
new Ajax.Autocompleter("cat4_id", "autocomplete_choicescat4_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat4_id, indicator: "indicatorcat4_id"});
new Ajax.Autocompleter("cat5_id", "autocomplete_choicescat5_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat5_id, indicator: "indicatorcat5_id"});
if (payant == '1') {
new Ajax.Autocompleter("cat6_id", "autocomplete_choicescat6_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat6_id, indicator: "indicatorcat6_id"});
new Ajax.Autocompleter("cat7_id", "autocomplete_choicescat7_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat7_id, indicator: "indicatorcat7_id"});
new Ajax.Autocompleter("cat8_id", "autocomplete_choicescat8_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat8_id, indicator: "indicatorcat8_id"});
new Ajax.Autocompleter("cat9_id", "autocomplete_choicescat9_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat9_id, indicator: "indicatorcat9_id"});
new Ajax.Autocompleter("cat10_id", "autocomplete_choicescat10_id", "/get/act/cat/list/magazines/", {  paramName: "value", minChars: 1, afterUpdateElement : getSelectionIdcat10_id, indicator: "indicatorcat10_id"});
}}



}

function getSelectionIdbr1_id(text, li) {
     document.getElementById('br1_id').value = li.id;
}

function getSelectionIdbr2_id(text, li) {
     document.getElementById('br2_id').value = li.id;
}

function getSelectionIdbr3_id(text, li) {
     document.getElementById('br3_id').value = li.id;
}

function getSelectionIdbr4_id(text, li) {
     document.getElementById('br4_id').value = li.id;
}

function getSelectionIdbr5_id(text, li) {
     document.getElementById('br5_id').value = li.id;
}

function getSelectionIdbr6_id(text, li) {
     document.getElementById('br6_id').value = li.id;
}

function getSelectionIdbr7_id(text, li) {
     document.getElementById('br7_id').value = li.id;
}

function getSelectionIdbr8_id(text, li) {
     document.getElementById('br8_id').value = li.id;
}

function getSelectionIdbr9_id(text, li) {
     document.getElementById('br9_id').value = li.id;
}

function getSelectionIdbr10_id(text, li) {
     document.getElementById('br10_id').value = li.id;
}

function getSelectionIdbr11_id(text, li) {
     document.getElementById('br11_id').value = li.id;
}

function getSelectionIdbr12_id(text, li) {
     document.getElementById('br12_id').value = li.id;
}

function getSelectionIdbr13_id(text, li) {
     document.getElementById('br13_id').value = li.id;
}

function getSelectionIdbr14_id(text, li) {
     document.getElementById('br14_id').value = li.id;
}

function getSelectionIdbr15_id(text, li) {
     document.getElementById('br15_id').value = li.id;
}


function getSelectionIdcat1_id(text, li) {
     document.getElementById('cat1_id').value = li.id;
}

function getSelectionIdcat2_id(text, li) {
     document.getElementById('cat2_id').value = li.id;
}

function getSelectionIdcat3_id(text, li) {
     document.getElementById('cat3_id').value = li.id;
}

function getSelectionIdcat4_id(text, li) {
     document.getElementById('cat4_id').value = li.id;
}

function getSelectionIdcat5_id(text, li) {
     document.getElementById('cat5_id').value = li.id;
}

function getSelectionIdcat6_id(text, li) {
     document.getElementById('cat6_id').value = li.id;
}

function getSelectionIdcat7_id(text, li) {
     document.getElementById('cat7_id').value = li.id;
}

function getSelectionIdcat8_id(text, li) {
     document.getElementById('cat8_id').value = li.id;
}

function getSelectionIdcat9_id(text, li) {
     document.getElementById('cat9_id').value = li.id;
}

function getSelectionIdcat10_id(text, li) {
     document.getElementById('cat10_id').value = li.id;
}

function getSelectionIdcat11_id(text, li) {
     document.getElementById('cat11_id').value = li.id;
}

function getSelectionIdcat12_id(text, li) {
     document.getElementById('cat12_id').value = li.id;
}

function getSelectionIdcat13_id(text, li) {
     document.getElementById('cat13_id').value = li.id;
}

function getSelectionIdcat14_id(text, li) {
     document.getElementById('cat14_id').value = li.id;
}

function getSelectionIdcat15_id(text, li) {
     document.getElementById('cat15_id').value = li.id;
}


/*
- Ajax.InPlaceSelect.js -
  Creates a <select> control in place of the html element with the id
  specified.  It functions similar to "Ajax.InPlaceEditor" but instead
  of an <input> control, it creates a <select> control with a list of
  <options> from which to choose.  The parameters 'values' and 'labels'
  are arrays (of the same length) from which the <options> are defined.

- Syntax -
  new Ajax.InPlaceSelect('id', 'url', 'values[]', 'labels[]', { options });

- Example -
  new Ajax.InPlaceSelect('someId', 'someURL', [1,2], ['first','second'],
    { paramName: 'asset_type', parameters: "moreinfo=extra info" } );

- Options('default value') -
  - paramName('selected'): name of the default parameter sent
  - hoverClassName(null): class added when mouse hovers over the control
  - hightlightcolor("#FFFF99"): initial color (mouseover)
  - hightlightendcolor("#FFFFFF"): final color (mouseover)
  - parameters(null): additional parameters to send with the request
      (in addition to the data sent by default)

- Modified Feb 22, 2006 by Thom Porter (www.thomporter.com)
  - Modified to use Single Click
  - Added "cancelLink" to options - Defaults to true, set to false to not
    show the cancel link
  - Commented the following Lines:
      //this.menu.onblur = this.onCancel.bind(this);
      //this.menu.onmouseout = this.onCancel.bind(this);
    I found that these lines made it switch back when the user would 
    mouse off of the select and didn't like that feature. =)
   
- Modified Feb 25, 2006 by Thom Porter
  - Modified to include callback function.  Function to accept 2 
    parameters, value & text.  The first being the value=""
    of the choose option, and the second being the text that
    is displayed.
  - IMPORTANT CHANGE: If you don't provide your own call back 
    function, your server-side script should expect the following
    POST variables: 
     newval = Value of the chosen <option>
     newtxt = Text of the chosen <option>

- Example using Call Back Function: 
  new Ajax.InPlaceSelect('someId', 'someURL', [1,2], ['first','second'],
    { 
    	callback: function(value, text) { return 'newval='+value+'&newtxt='+text; }
    	
    } );
  (this example actually does the same things as the default call back function.)
  
*/

Ajax.InPlaceSelect = Class.create();
Ajax.InPlaceSelect.prototype = {
  initialize:function(element,url,values,labels,options) {
    this.element = $(element);
    this.url = url;
    this.values = values;
    this.labels = labels;
    this.options = Object.extend({
      paramName: 'selected',
      highlightcolor: "#FFFF99",
      highlightendcolor: "#FFFFFF",
      onFailure: function(transport) {
        alert("Error: " + transport.responseText.stripTags());
      },
       callback: function(value, text) {
        return 'newval='+value+'&newtxt='+text;
      },
      savingText: "Saving...",
      savingClassName: 'inplaceeditor-saving',
      clickToEditText: "Click to edit",
      cancelLink: true
    }, options || {} );

    this.originalBackground = Element.getStyle(this.element, 'background-color');
    if (!this.originalBackground) {
      this.originalBackground = "transparent";
    }

    this.element.title = this.options.clickToEditText;

    this.ondblclickListener = this.enterEditMode.bindAsEventListener(this);
    this.mouseoverListener = this.enterHover.bindAsEventListener(this);
    this.mouseoutListener = this.leaveHover.bindAsEventListener(this);

    Event.observe(this.element, 'click', this.ondblclickListener);
    Event.observe(this.element, 'mouseover', this.mouseoverListener);
    Event.observe(this.element, 'mouseout', this.mouseoutListener);
  },
  enterEditMode: function(evt) {
    if (this.saving) return;
    if (this.editing) return;
    this.editing = true;
    Element.hide(this.element);
    this.createControls();
    this.element.parentNode.insertBefore(this.menu, this.element);
    
    if (this.options.cancelLink) {
     this.element.parentNode.insertBefore(this.cancelButton, this.element);
    }
    return false;
  },
  createControls: function() {
    var options = new Array();
    for (var i=0;i<this.values.length;i++)
		options[i] = Builder.node('option', {value:this.values[i]}, this.labels[i]);
    this.menu = Builder.node('select', options);
    this.menu.onchange = this.onChange.bind(this);

//	this.menu.onblur = this.onCancel.bind(this);
//	this.menu.onmouseout = this.onCancel.bind(this);

    for (var i=0;i<this.values.length;i++)
      if (this.labels[i]==this.element.innerHTML) {
        this.menu.selectedIndex=i;
        continue;
      }
    if (this.options.cancelLink) {
     this.cancelButton = Builder.node('a', 'cancel');
     this.cancelButton.onclick = this.onCancel.bind(this);
    }
  },
  onCancel: function() {
    this.onComplete();
    this.leaveEditMode();
    return false;
  },
  onChange: function() {
    var value = this.values[this.menu.selectedIndex];
    var text = this.labels[this.menu.selectedIndex];
    this.onLoading();
    new Ajax.Updater(
    	{
    		success:this.element
    	}, 
    	this.url, 
      Object.extend({
        parameters: this.options.callback(value, text),
        onComplete: this.onComplete.bind(this),
        onFailure: this.onFailure.bind(this)
      }, this.options.ajaxOptions)
    );
  },
  onLoading: function() {
    this.saving = true;
    this.removeControls();
    this.leaveHover();
    this.showSaving();
  },
  removeControls:function() {
    if(this.menu) {
      if (this.menu.parentNode) Element.remove(this.menu);
      this.menu = null;
    }
    if (this.cancelButton) {
      if (this.cancelButton.parentNode) Element.remove(this.cancelButton);
      this.cancelButton = null;
    }
  },
  showSaving:function() {
    this.oldInnerHTML = this.element.innerHTML;
    this.element.innerHTML = this.options.savingText;
    Element.addClassName(this.element, this.options.savingClassName);
    this.element.style.backgroundColor = this.originalBackground;
    Element.show(this.element);
  },
  onComplete: function() {
    this.leaveEditMode();
    new Effect.Highlight(this.element, {startcolor: this.options.highlightcolor});
  },
  onFailure: function(transport) {
    this.options.onFailure(transport);
    if (this.oldInnerHTML) {
      this.element.innerHTML = this.oldInnerHTML;
      this.oldInnerHTML = null;
    }
    return false;
  },
  enterHover: function() {
    if (this.saving) return;
    this.element.style.backgroundColor = this.options.highlightcolor;
    if (this.effect) { this.effect.cancel(); }
    Element.addClassName(this.element, this.options.hoverClassName)
  },
  leaveHover: function() {
    if (this.options.backgroundColor) {
      this.element.style.backgroundColor = this.oldBackground;
    }
    Element.removeClassName(this.element, this.options.hoverClassName)
    if (this.saving) return;
    this.effect = new Effect.Highlight(this.element, {
      startcolor: this.options.highlightcolor,
      endcolor: this.options.highlightendcolor,
      restorecolor: this.originalBackground
    });
  },
  leaveEditMode:function(transport) {
    Element.removeClassName(this.element, this.options.savingClassName);
    this.removeControls();
    this.leaveHover();
    this.element.style.backgroundColor = this.originalBackground;
    Element.show(this.element);
    this.editing = false;
    this.saving = false;
    this.oldInnerHTML = null;
  }
}
