/*jsl:option explicit*/

// Extensions
YAHOO.util.Dom.setBackgroundImage = function(element, image)
{
    Dom.setStyle(element, 'background-image', 'url(' + image + ')');
};

YAHOO.lang.isInt = function(v)
{
    return lang.isNumber(v) && Math.floor(v) === v && Math.ceil(v) === v;
};

String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g, '');
};

String.prototype.lpad = function(len, ch)
{
    return len > this.length ? new Array(len - this.length + 1).join(ch || '0') + this : this;
};

var 

log = YAHOO.log,
util = YAHOO.util,
lang = YAHOO.lang,
widget = YAHOO.widget,

Dom = util.Dom,
Event = util.Event,
Cookie = util.Cookie,
Cookie = util.Cookie,
Connect = util.Connect,
StyleSheet = util.StyleSheet,
Anim = util.Anim,

JSON = lang.JSON,

Slider = widget.Slider,
DualSlider = widget.DualSlider,
LogReader = widget.LogReader,
Panel = widget.Panel,

constants =
{
    forma_id: 1,
    metal_id: 1,

    pret_lo: 100,
    pret_hi: 99900,

    greutate_lo: 20,
    greutate_hi: 199,

    culoare_lo: 1,
    culoare_hi: 10,

    claritate_lo: 1,
    claritate_hi: 10,

    taietura_lo: 1,
    taietura_hi: 4,

    camp: 1, // 1 = pret; 2 = greutate; 3 = culoare_id; 4 = claritate_id; 5 = taietura_id;
    desc: 0, // 0 = false; 1 = true;

    montura_item_count: 9,
    diamant_item_count: 15,

    diamant_buffer_count: 500,

    masura_lo: 40,
    masura_hi: 70,

    diamant_view_count: 4,
    montura_view_count: 3,

    forma_count: 10,
    metal_count: 3,
    
    session_version: 1,
	
	diamant_cod_length: 4,
	montura_cod_length: 3
},

// The encodings should be globally accessible.
forma_encoding = [null, 'Round', 'Princess', 'Emerald', 'Radiant', 'Oval', 'Pear', 'Marquise', 'Heart', 'Asscher', 'Cushion'],
culoare_encoding = [null, 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
claritate_encoding = [null, 'FL', 'IF', 'VVS1', 'VVS2', 'VS1', 'VS2', 'SI1', 'SI2', 'I1', 'I2'],
taietura_encoding = [null, 'EX', 'VG', 'G', 'F', 'P', 'N/A'],
simetrie_encoding = [null, 'EX', 'VG', 'G', 'F', 'P'],
finisaj_encoding = [null, 'EX', 'VG', 'G', 'F', 'P'],
colet_encoding = [null, 'None', 'Very small', 'Small', 'Medium', 'Slightly large', 'Large', 'Very large', 'Extremely large'],
centura_encoding = [null, 'Extremely thin', 'Very thin', 'Thin', 'Medium', 'Slightly thick', 'Thick', 'Very thick', 'Extremely thick'],
fluorescenta_encoding = [null, 'None', 'Faint', 'Medium', 'Strong', 'Very strong'],
fluorescenta_culoare_encoding = [null, 'blue', 'yellow', 'white'],
metal_encoding = [null, 'platină', 'aur 18K', 'aur alb 18K'],
// NU SE FOLOSESC: 0 - Oo- Qq, 1 - Ii - Ll.
cod_encoding = ['2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],

var_dump = function(x)
{
    return typeof x === 'string' ? 'string(' + x.length + ') ' + '"' + x + '"' : (typeof x) + '(' + x + ')';
},

myassert = function(condition, message, source)
{
    if (!condition)
    {
        log("Assertion failed: " + (message ? message : 'No message was provided'), 'error', source);
    }
},

/** Converts a stable value to int. Returns null on failure. */
myint = function(v)
{
    if (lang.isInt(v))
    {
        if (0 <= v && v <= 999999999)
        {
            return v;
        }
    }
    if (lang.isString(v))
    {
        v = v.trim();
        if (1 <= v.length && v.length <= 9 && v.match(/^\d+$/) !== null)
        {
            var w = parseInt(v, 10);
            if (lang.isInt(w) && (v === w + ''))
            {
                return w;
            }
        }
    }
    return null;
},

intervalMapping = function(value, source, destination)
{
    myassert(lang.isNumber(value));
    myassert(lang.isNumber(source[0]));
    myassert(lang.isNumber(source[1]));
    myassert(lang.isNumber(destination[0]));
    myassert(lang.isNumber(destination[1]));
    myassert(source[1] != source[0]);
    return (value - source[0]) * (destination[1] - destination[0]) / (source[1] - source[0]) + destination[0];
},

number_format = function(p)
{
    var 
    s = '',
    v = myint(p),
    groups = [],
	pos = 0;

    if (!lang.isInt(v))
    {
        return null;
    }
    s = v + ''; // convert to string
    while (s.length > 0)
    {
        pos = Math.max(0, s.length - 3);
        groups[groups.length] = s.substring(pos); // substringul de la pos inclusiv pana la sfarsitul sirului
        s = s.substring(0, pos); // substringul de la inceputul sirului pana la pos exclusiv
    }
    groups.reverse();
    return groups.join('.');
},

integerDecimalOne = function(p)
{
    var v = myint(p); // possibly converting from string to number
    if (!lang.isInt(v))
    {
        return null;
    }
    // Converting the last k = 1 digits of the integer number into the fractional part of the decimal number.	
    return (v / 10).toFixed(1).replace('.', ',');
},

integerDecimalTwo = function(p)
{
    var v = myint(p); // possibly converting from string to number
    if (!lang.isInt(v))
    {
        return null;
    }
    // Converting the last k = 2 digits of the integer number into the fractional part of the decimal number.	
    return (v / 100).toFixed(2).replace('.', ',');
},

parsePret = function(p)
{
    if (!lang.isString(p))
    {
        return null;
    }
    var x = +(p.replace('.', '')); // remove the first dot and convert to number 
    return lang.isNumber(x) ? x : null;
},

formatPret = function(p)
{
    if (!lang.isInt(p))
    {
        return null;
    }
    return number_format(p);
},

parseGreutate = function(p)
{
    if (!lang.isString(p))
    {
        return null;
    }
    var x = +(p.replace(',', '.')); // replace the first comma to dot and convert to number 
    return lang.isNumber(x) ? Math.round(x * 100) : null;
},

formatGreutate = function(p)
{
    if (!lang.isInt(p))
    {
        return null;
    }
    return integerDecimalTwo(p);
},

/* Session */

/** Constructor: Unwraps an array into a MonturaSearch. */
MonturaSearch = function(a)
{
    myassert(lang.isArray(a));

    this.metal_id = a[0] ? a[0] : constants.metal_id;

    this.montura_page = a[1] ? a[1] : 1;
    this.montura_view = a[2] ? a[2] : 1;

    /** Returns an array. */
    this.wrap = function()
    {
        var a =
	    [
			this.metal_id ? this.metal_id : null,

		    this.montura_page ? this.montura_page : null,
		    this.montura_view ? this.montura_view : null
	    ];
        return a;
    };

    this.copy = function()
    {
        return new MonturaSearch(this.wrap());
    };
},

/** Constructor: Unwraps an array into a DiamantSearch. */
DiamantSearch = function(a)
{
    myassert(lang.isArray(a));

    this.forma_id = a[0] ? a[0] : constants.forma_id;

    this.diamant_page = a[1] ? a[1] : 1;
    this.diamant_view = a[2] ? a[2] : 1;

    this.camp = a[3] ? a[3] : constants.camp;
    this.desc = a[4] ? a[4] : constants.desc;

    this.pret_min = a[5] ? a[5] : constants.pret_lo;
    this.pret_max = a[6] ? a[6] : constants.pret_hi;
    this.greutate_min = a[7] ? a[7] : constants.greutate_lo;
    this.greutate_max = a[8] ? a[8] : constants.greutate_hi;
    this.culoare_min = a[9] ? a[9] : constants.culoare_lo;
    this.culoare_max = a[10] ? a[10] : constants.culoare_hi;
    this.claritate_min = a[11] ? a[11] : constants.claritate_lo;
    this.claritate_max = a[12] ? a[12] : constants.claritate_hi;
    this.taietura_min = a[13] ? a[13] : constants.taietura_lo;
    this.taietura_max = a[14] ? a[14] : constants.taietura_hi;

    /** Returns an array. */
    this.wrap = function()
    {
        var a =
	    [
		    this.forma_id ? this.forma_id : null,

		    this.diamant_page ? this.diamant_page : null,
		    this.diamant_view ? this.diamant_view : null,

		    this.camp ? this.camp : null,
		    this.desc ? this.desc : null,

		    this.pret_min ? this.pret_min : null,
		    this.pret_max ? this.pret_max : null,
		    this.greutate_min ? this.greutate_min : null,
		    this.greutate_max ? this.greutate_max : null,
		    this.culoare_min ? this.culoare_min : null,
		    this.culoare_max ? this.culoare_max : null,
		    this.claritate_min ? this.claritate_min : null,
		    this.claritate_max ? this.claritate_max : null,
		    this.taietura_min ? this.taietura_min : null,
		    this.taietura_max ? this.taietura_max : null
	    ];
        return a;
    };

    this.copy = function()
    {
        return new DiamantSearch(this.wrap());
    };
},

/** Constructor: Unwraps an array into a MonturaObject. */
MonturaObject = function(a)
{
    myassert(lang.isArray(a));

    this.montura_id = a[0] ? a[0] : null;
    this.metal_id = a[1] ? a[1] : null;
    this.pret = a[2] ? a[2] : null;
    this.template_id = a[3] ? a[3] : null;

    this.montura_search = a[4] ? new MonturaSearch(a[4]) : null;

    /** Returns an array. */
    this.wrap = function()
    {
        var a =
	    [
		    this.montura_id ? this.montura_id : null,
		    this.metal_id ? this.metal_id : null,
		    this.pret ? this.pret : null,
		    this.template_id ? this.template_id : null,

		    this.montura_search ? this.montura_search.wrap() : null
	    ];
        return a;
    };

    this.copy = function()
    {
        return new MonturaObject(this.wrap());
    };

	this.getCod = function() // TODO: Adauga coloana 'cod' in baza de date. ???
	{
		var
		id = this.montura_id,
		base = cod_encoding.length,
		i = 0,
		digit = 0,
		cod = '';

		for (i = 0; i < constants.montura_cod_length; i++)
		{
			digit = id - Math.floor(id / base) * base;
			id = Math.floor(id / base);
			cod = cod_encoding[digit] + cod;
		}
		
		return '#' + cod;
	};

    this.getMetal = function() { return this.metal_id ? metal_encoding[this.metal_id] : null; };
    this.getPret = function() { return this.pret ? formatPret(this.pret) : null; };
},

/** Constructor: Unwraps an array into a DiamantObject. */
DiamantObject = function(a)
{
    myassert(lang.isArray(a));

    this.diamant_id = a[0] ? a[0] : null;
    this.forma_id = a[1] ? a[1] : null;
    this.pret = a[2] ? a[2] : null;
    this.greutate = a[3] ? a[3] : null;
    this.culoare_id = a[4] ? a[4] : null;
    this.claritate_id = a[5] ? a[5] : null;
    this.taietura_id = a[6] ? a[6] : null;

    this.simetrie_id = a[7] ? a[7] : null;
    this.finisaj_id = a[8] ? a[8] : null;

    this.latime = a[9] ? a[9] : null;
    this.lungime = a[10] ? a[10] : null;
    this.adancime = a[11] ? a[11] : null;

    this.procent_tableta = a[12] ? a[12] : null;
    this.procent_adancime = a[13] ? a[13] : null;

    this.colet_id = a[14] ? a[14] : null;
    this.centura_min_id = a[15] ? a[15] : null;
    this.centura_max_id = a[16] ? a[16] : null;

    this.fluorescenta_id = a[17] ? a[17] : null;
    this.fluorescenta_culoare_id = a[18] ? a[18] : null;

    this.laborator_id = a[19] ? a[19] : null;

    this.cert_file = a[20] ? a[20] : null;

    this.diamant_search = a[21] ? new DiamantSearch(a[21]) : null;

    /** Returns an array. */
    this.wrap = function()
    {
        var a =
	    [
		    this.diamant_id ? this.diamant_id : null,
		    this.forma_id ? this.forma_id : null,
		    this.pret ? this.pret : null,
		    this.greutate ? this.greutate : null,
		    this.culoare_id ? this.culoare_id : null,
		    this.claritate_id ? this.claritate_id : null,
		    this.taietura_id ? this.taietura_id : null,

		    this.simetrie_id ? this.simetrie_id : null,
		    this.finisaj_id ? this.finisaj_id : null,

		    this.latime ? this.latime : null,
		    this.lungime ? this.lungime : null,
		    this.adancime ? this.adancime : null,

		    this.procent_tableta ? this.procent_tableta : null,
		    this.procent_adancime ? this.procent_adancime : null,

		    this.colet_id ? this.colet_id : null,
		    this.centura_min_id ? this.centura_min_id : null,
		    this.centura_max_id ? this.centura_max_id : null,

		    this.fluorescenta_id ? this.fluorescenta_id : null,
		    this.fluorescenta_culoare_id ? this.fluorescenta_culoare_id : null,

		    this.laborator_id ? this.laborator_id : null,

		    this.cert_file ? this.cert_file : null,

		    this.diamant_search ? this.diamant_search.wrap() : null
	    ];
        return a;
    };

    this.copy = function()
    {
        return new DiamantObject(this.wrap());
    };

	this.getCod = function() // TODO: Adauga coloana 'cod' in baza de date. ???
	{
		var
		id = this.diamant_id,
		base = cod_encoding.length,
		i = 0,
		digit = 0,
		cod = '';

		for (i = 0; i < constants.diamant_cod_length; i++)
		{
			digit = id - Math.floor(id / base) * base;
			id = Math.floor(id / base);
			cod = cod_encoding[digit] + cod;
		}
		
		return '#' + cod;
	};
	
    this.getForma = function() { return this.forma_id ? forma_encoding[this.forma_id] : null; };

    this.getPret = function() { return this.pret ? formatPret(this.pret) : null; };
	this.getPretCarat = function() { return this.pret && this.greutate ? formatPret(Math.round(this.pret * 100 / this.greutate)) : null; }; 

    this.getGreutate = function() { return this.greutate ? formatGreutate(this.greutate) : null; };
    this.getCuloare = function() { return this.culoare_id ? culoare_encoding[this.culoare_id] : null; };
    this.getClaritate = function() { return this.claritate_id ? claritate_encoding[this.claritate_id] : null; };
    this.getTaietura = function() { return this.taietura_id ? taietura_encoding[this.taietura_id] : null; };

    this.getSimetrie = function() { return this.simetrie_id ? simetrie_encoding[this.simetrie_id] : null; };
    this.getFinisaj = function() { return this.finisaj_id ? finisaj_encoding[this.finisaj_id] : null; };

    this.getLungime = function() { return this.lungime ? (this.lungime / 100).toFixed(2) : null; };
    this.getLatime = function() { return this.latime ? (this.latime / 100).toFixed(2) : null; };
	this.getAdancime = function() { return this.adancime ? (this.adancime / 100).toFixed(2) : null; };
	
    this.getDimensiuni = function() { return this.getLungime() && this.getLatime() && this.getAdancime ? this.getLungime() + 'x' + this.getLatime() + 'x' + this.getAdancime() : null; };
	this.getRaportLungimeLatime = function() { return this.lungime && this.latime ? (this.lungime / this.latime).toFixed(2) : null; };
	
    this.getProcentTableta = function() { return this.procent_tableta ? (this.procent_tableta / 10).toFixed(1) : null; };
    this.getProcentAdancime = function() { return this.procent_adancime ? (this.procent_adancime / 10).toFixed(1) : null; };

    this.getColet = function() { return this.colet_id ? colet_encoding[this.colet_id] : null; };
    this.getCentura = function() { return this.centura_min_id && this.centura_max_id ? (this.centura_min_id === this.centura_max_id ? centura_encoding[this.centura_min_id] : centura_encoding[this.centura_min_id] + ' to ' + centura_encoding[this.centura_max_id]) : null; };

    this.getFluorescenta = function() { return this.fluorescenta_id ? (fluorescenta_encoding[Math.round(this.fluorescenta_id / 10)]) : null; };

    /* TODO: Implement descriere. */
    //this.getCuloareDescriere = function() { return this.culoare_id ? culoare_descriere[this.culoare_id] : null; };
    //this.getClaritateDescriere = function() { return this.claritate_id ? claritate_descriere[this.claritate_id] : null; };
    //this.getTaieturaDescriere = function() { return this.taietura_id ? taietura_descriere[this.taietura_id] : null; };
},

/** Constructor: Unwraps an array into a InelObject. */
InelObject = function(a)
{
    myassert(lang.isArray(a));

    this.diamant_object = a[0] ? new DiamantObject(a[0]) : null;
    this.montura_object = a[1] ? new MonturaObject(a[1]) : null;
    this.masura = a[2] ? a[2] : null;

    /** Returns an array. */
    this.wrap = function()
    {
        var a =
	    [
		    this.diamant_object ? this.diamant_object.wrap() : null,
		    this.montura_object ? this.montura_object.wrap() : null,
		    this.masura ? this.masura : null
	    ];
        return a;
    };

    this.copy = function()
    {
        return new InelObject(this.wrap());
    };
},

/** Constructor: Unwraps an array into a CartObject. */
CartObject = function(a)
{
    myassert(lang.isArray(a));

    var i = 0;

    this.inel_list = [];
    if (a[0])
    {
        for (i = 0; i < a[0].length; i++)
        {
            this.inel_list[i] = a[0][i] ? new InelObject(a[0][i]) : null;
        }
    }
    this.diamant_list = [];
    if (a[1])
    {
        for (i = 0; i < a[1].length; i++)
        {
            this.diamant_list[i] = a[1][i] ? new DiamantObject(a[1][i]) : null;
        }
    }

    /** Returns an array. */
    this.wrap = function()
    {
        var 
        i = 0,
        a = [[], []];

        if (this.inel_list)
        {
            for (i = 0; i < this.inel_list.length; i++)
            {
                a[0][i] = this.inel_list[i] ? this.inel_list[i].wrap() : null;
            }
        }
        if (this.diamant_list)
        {
            for (i = 0; i < this.diamant_list.length; i++)
            {
                a[1][i] = this.diamant_list[i] ? this.diamant_list[i].wrap() : null;
            }
        }

        return a;
    };

    this.copy = function()
    {
        return new CartObject(this.wrap());
    };
},

/** Constructor: Unwraps an array into a SessionObject. */
SessionObject = function(a)
{
    myassert(lang.isArray(a));

    this.version = a[0] ? a[0] : constants.session_version;
    
    this.montura_object = a[1] ? new MonturaObject(a[1]) : null;
    this.diamant_object = a[2] ? new DiamantObject(a[2]) : null;
    
    this.cart_object = a[3] ? new CartObject(a[3]) : new CartObject([]);

    /** Returns an array. */
    this.wrap = function()
    {
        var a =
	    [
	        this.version ? this.version : null,
	        
		    this.montura_object ? this.montura_object.wrap() : null,
		    this.diamant_object ? this.diamant_object.wrap() : null,

		    this.cart_object ? this.cart_object.wrap() : null
	    ];
        return a;
    };
},

session = null, // Default session object (no diamant, no montura, empty cart).

/** Wraps the session object into an array, stringifies the array and saves the resulting string into the cookie. */
saveSession = function()
{
    Cookie.set('session', JSON.stringify(session.wrap()));
},

initSession = function()
{
    log('begin initSession');
	
	session = new SessionObject(Cookie.get('session') ? JSON.parse(Cookie.get('session')) : []); 
	
	log('session: ' + lang.dump(session));
	
	if (session.diamant_object)
	{
		Dom.get('diamant_ultima_selectie_link').innerHTML = 'Diamant ' + session.diamant_object.getForma() + ' - ' + session.diamant_object.getPret() + ' RON';
		Dom.removeClass('diamant_ultima_selectie', 'hidden');
	}
	if (session.montura_object)
	{
		Dom.get('montura_ultima_selectie_link').innerHTML = 'Inel ' + session.montura_object.getMetal() + ' - ' + session.montura_object.getPret() + ' RON';
		Dom.removeClass('montura_ultima_selectie', 'hidden');
	}
	
	var
	inel_count = session.cart_object.inel_list.length,
	diamant_count = session.cart_object.diamant_list.length; // var
	
	if (inel_count === 0 && diamant_count === 0)
	{
		Dom.get('produse_cos').innerHTML = '0 produse';
	}
	else if (inel_count > 0 && diamant_count === 0)
	{
		Dom.get('produse_cos').innerHTML = (inel_count == 1 ? '1 inel' : inel_count + ' inele');
	}
	else if (diamant_count > 0 && inel_count === 0 )
	{
		Dom.get('produse_cos').innerHTML = (diamant_count == 1 ? '1 diamant' : diamant_count + ' diamante');
	}
	else if (inel_count > 0 && diamant_count > 0)
	{
		Dom.get('produse_cos').innerHTML = (inel_count + diamant_count) + ' produse';
	}
	
    log('end initSession');
},

addCartInel = function(diamant_object, montura_object, masura)
{
    myassert(diamant_object);
    myassert(montura_object);
    myassert(masura);

    var inelObject = new InelObject([]);
    inelObject.diamant_object = diamant_object.copy();
    inelObject.montura_object = montura_object.copy();
    inelObject.masura = masura;

    session.cart_object.inel_list[session.cart_object.inel_list.length] = inelObject;
	session.diamant_object = null;
	session.montura_object = null;
    saveSession();
},

addCartDiamant = function(diamant_object)
{
    myassert(diamant_object);

    session.cart_object.diamant_list[session.cart_object.diamant_list.length] = diamant_object.copy();
	session.diamant_object = null;
	session.montura_object = null;
    saveSession();
},

removeCartInel = function(i)
{
    session.cart_object.inel_list.splice(i, 1);
    saveSession();
},

removeCartDiamant = function(i)
{
    session.cart_object.diamant_list.splice(i, 1);
    saveSession();
},

initMenu = function()
{
    log('begin initMenu');

    var // begin var statement initMenu 
    showOver = function(button)
    {
        Dom.addClass(button, 'over');
        Dom.removeClass(button + '_submenu', 'hidden');
        // log('in');
    },

    hideOver = function(button)
    {
        Dom.removeClass(button, 'over');
        Dom.addClass(button + '_submenu', 'hidden');
        // log('out');
    },

    mainInitMenu = function()
    {
        Event.addListener('diamante_button', 'mouseover', function() { showOver('diamante_button'); });
        Event.addListener('diamante_button', 'mouseout', function() { hideOver('diamante_button'); });
        Event.addListener('logodna_button', 'mouseover', function() { showOver('logodna_button'); });
        Event.addListener('logodna_button', 'mouseout', function() { hideOver('logodna_button'); });
        Event.addListener('indrumare_button', 'mouseover', function() { showOver('indrumare_button'); });
        Event.addListener('indrumare_button', 'mouseout', function() { hideOver('indrumare_button'); });
        Event.addListener('cum_cumpar_button', 'mouseover', function() { showOver('cum_cumpar_button'); });
        Event.addListener('cum_cumpar_button', 'mouseout', function() { hideOver('cum_cumpar_button'); });

        Event.addListener('diamante_button_submenu', 'mouseover', function() { showOver('diamante_button'); });
        Event.addListener('diamante_button_submenu', 'mouseout', function() { hideOver('diamante_button'); });
        Event.addListener('logodna_button_submenu', 'mouseover', function() { showOver('logodna_button'); });
        Event.addListener('logodna_button_submenu', 'mouseout', function() { hideOver('logodna_button'); });
        Event.addListener('indrumare_button_submenu', 'mouseover', function() { showOver('indrumare_button'); });
        Event.addListener('indrumare_button_submenu', 'mouseout', function() { hideOver('indrumare_button'); });
        Event.addListener('cum_cumpar_button_submenu', 'mouseover', function() { showOver('cum_cumpar_button'); });
        Event.addListener('cum_cumpar_button_submenu', 'mouseout', function() { hideOver('cum_cumpar_button'); });
    }; // end var statement initMenu

    mainInitMenu();

    log('end initMenu');
},


getMonturaImage = function(metal_id, template_id, forma_id, montura_view, size)
{
    var 
	metal_transform = [null, 1, 2, 1],
	frame = (template_id - 1) * 30 + (forma_id - 1) * 3 + (montura_view - 1),
	path = 'images/inele/' + size + '/' + metal_transform[metal_id] + '_' + (frame + '').lpad(4, '0') + '.jpg';
    return path;
},

getDiamantImage = function(forma_id, diamant_view, size)
{
    var 
	path = 'images/diamante/' + size + '/' + diamant_view + '_' + (forma_id + '').lpad(2, '0') + '.png';
    return path;
},

/** urls: array of string. onComplete: callback */
preloadImages = function(urls, onComplete)
{
    log('begin preloadImages()');
    myassert(lang.isFunction(onComplete), 'lang.isFunction(onComplete)');
    myassert(lang.isArray(urls) && urls.length > 0, 'lang.isArray(urls) && urls.length > 0');

    var 
    i = 0,
    count = 0,
    images = [],
	
    onLoad = function()
    {
        count++;
        if (count === urls.length)
        {
            onComplete();
        }
    }; // var

    for (i = 0; i < urls.length; i++)
    {
        myassert(lang.isString(urls[i]), 'lang.isString(urls[i])');

        images[i] = new Image();
        Event.addListener(images[i], 'load', onLoad);
        images[i].src = urls[i];
    }

    log('end preloadImages');
},

/** Constructor. */
Fader = function(style_sheet, style_rule, seconds_out, seconds_in)
{
    this.anim_in = new Anim(null, null, seconds_in);
    this.anim_out = new Anim(null, null, seconds_out);

    // Modify the existing style node's stylesheet.
    var sheet = new StyleSheet(style_sheet);

    // Update the StyleSheet on each frame of the animation.
    this.anim_in.onTween.subscribe(function(type, data)
    {
        var opacity = (data[0].duration / (seconds_in * 1000));
        sheet.set(style_rule, { opacity: opacity }); // normalized across browsers
    });

    // Update the StyleSheet on each frame of the animation.
    this.anim_out.onTween.subscribe(function(type, data)
    {
        var opacity = 1 - (data[0].duration / (seconds_out * 1000));
        sheet.set(style_rule, { opacity: opacity }); // normalized across browsers
    });
},


showDiamantView = function(diamant_view)
{
	myassert(diamant_view, 'diamant_view');

	var i = 0;

	for (i = 1; i <= constants.diamant_view_count; i++)
	{
		Dom.addClass('diamant_detail_' + i, 'hidden');
		Dom.removeClass('diamant_view_' + i, 'selected');
	}
	Dom.addClass('diamant_view_' + diamant_view, 'selected');
	Dom.removeClass('diamant_detail_' + diamant_view, 'hidden');
},

selectDiamantView = function(diamant_view, diamant_search)
{
	myassert(diamant_view, 'diamant_view');
	myassert(diamant_search, 'diamant_search');

	log('begin selectDiamantView(diamant_view: ' + lang.dump(diamant_view) + ', diamant_search: ' + lang.dump(diamant_search) + ')');

	if (lang.isInt(diamant_view) && 1 <= diamant_view && diamant_view <= constants.diamant_view_count)
	{
		if (diamant_search.diamant_view != diamant_view)
		{
			diamant_search.diamant_view = diamant_view;
			showDiamantView(diamant_search.diamant_view);
		}
	}

	log('end selectDiamantView');
},

showDiamantInstructions = function()
{
	Dom.addClass('diamant_box', 'hidden');
	Dom.removeClass('diamant_instructions_box', 'hidden');
},

showDiamant = function(diamant_object)
{
	myassert(diamant_object, 'diamant_object');

	Dom.addClass('diamant_instructions_box', 'hidden');
	Dom.removeClass('diamant_box', 'hidden');

	var i = 0;
		
	for (i = 1; i <= constants.diamant_view_count; i++)
	{
		Dom.setBackgroundImage('diamant_view_' + i, getDiamantImage(diamant_object.forma_id, i, 'view'));
		Dom.setBackgroundImage('diamant_detail_' + i, getDiamantImage(diamant_object.forma_id, i, 'detail'));
	}
		
	Dom.get('diamant_cod_produs').innerHTML = diamant_object.getCod();
		
	Dom.get('text_forma').innerHTML = diamant_object.getForma();
	Dom.get('text_greutate').innerHTML = diamant_object.getGreutate();
	Dom.get('text_taietura').innerHTML = diamant_object.getTaietura();
	Dom.get('text_culoare').innerHTML = diamant_object.getCuloare();
	Dom.get('text_claritate').innerHTML = diamant_object.getClaritate();
	Dom.get('text_pret').innerHTML = diamant_object.getPret();

	Dom.get('diamant_cod').innerHTML = diamant_object.getCod();

	Dom.get('diamant_forma').innerHTML = diamant_object.getForma();

	Dom.get('diamant_pret').innerHTML = diamant_object.getPret() + ' RON';
	Dom.get('diamant_pret_carat').innerHTML = diamant_object.getPretCarat() + ' RON';
	
	
	Dom.get('diamant_greutate').innerHTML = diamant_object.getGreutate() + ' carate';
	Dom.get('diamant_culoare').innerHTML = diamant_object.getCuloare();
	Dom.get('diamant_claritate').innerHTML = diamant_object.getClaritate();
	Dom.get('diamant_taietura').innerHTML = diamant_object.getTaietura();

	Dom.get('diamant_simetrie').innerHTML = diamant_object.getSimetrie();
	Dom.get('diamant_finisaj').innerHTML = diamant_object.getFinisaj();

	Dom.get('diamant_dimensiuni').innerHTML = diamant_object.getDimensiuni();
	Dom.get('diamant_raport_lungime_latime').innerHTML = diamant_object.getRaportLungimeLatime();

	Dom.get('diamant_procent_tableta').innerHTML = diamant_object.getProcentTableta();
	Dom.get('diamant_procent_adancime').innerHTML = diamant_object.getProcentAdancime();

	Dom.get('diamant_colet').innerHTML = diamant_object.getColet();
	Dom.get('diamant_centura').innerHTML = diamant_object.getCentura();

	Dom.get('diamant_fluorescenta').innerHTML = diamant_object.getFluorescenta();

	Dom.get('detail_tableta').innerHTML = diamant_object.getProcentTableta() + '%';
	Dom.get('detail_adancime').innerHTML = diamant_object.getProcentAdancime() + '%';

	Dom.get('detail_colet').innerHTML = diamant_object.getColet();
	Dom.get('detail_centura').innerHTML = diamant_object.getCentura();

	Dom.get('detail_latime').innerHTML = diamant_object.getLatime() + ' mm';
	Dom.get('detail_lungime').innerHTML = diamant_object.getLungime() + ' mm';
	
	Dom.get('diamant_certificat').innerHTML = diamant_object.cert_file;
	Dom.get('diamant_certificat').href = 'http://www2.gia.edu/reportcheck/index.cfm?fuseaction=home.showReportVerification&reportno=' + diamant_object.cert_file + '&weight=' + diamant_object.getGreutate().replace(',', '.');
	
	// TODO: clean
	//alert(diamant_object.cert_file);
	
	Dom.get('panel_cod_value').innerHTML = diamant_object.getCod();
	Dom.get('panel_forma_value').innerHTML = diamant_object.getForma();
	Dom.get('panel_pret_value').innerHTML = diamant_object.getPret();
	Dom.get('panel_greutate_value').innerHTML = diamant_object.getGreutate();
	Dom.get('panel_pret_carat_value').innerHTML = diamant_object.getPretCarat();
	Dom.get('panel_culoare_value').innerHTML = diamant_object.getCuloare();
	Dom.get('panel_claritate_value').innerHTML = diamant_object.getClaritate();
	Dom.get('panel_taietura_value').innerHTML = diamant_object.getTaietura();
	Dom.get('panel_procent_adancime_value').innerHTML = diamant_object.getProcentAdancime();
	Dom.get('panel_procent_tableta_value').innerHTML = diamant_object.getProcentTableta();
	Dom.get('panel_simetrie_value').innerHTML = diamant_object.getSimetrie();
	Dom.get('panel_finisaj_value').innerHTML = diamant_object.getFinisaj();
	Dom.get('panel_colet_value').innerHTML = diamant_object.getColet();
	Dom.get('panel_centura_value').innerHTML = diamant_object.getCentura();
	Dom.get('panel_fluorescenta_value').innerHTML = diamant_object.getFluorescenta();
	Dom.get('panel_dimensiuni_value').innerHTML = diamant_object.getDimensiuni();
	Dom.get('panel_raport_lungime_latime_value').innerHTML = diamant_object.getRaportLungimeLatime();

	Dom.get('forma_descriere').innerHTML = forma_descriere[diamant_object.forma_id];
	Dom.get('culoare_descriere').innerHTML = culoare_descriere[diamant_object.culoare_id];
	Dom.get('claritate_descriere').innerHTML = claritate_descriere[diamant_object.claritate_id];
	Dom.get('taietura_descriere').innerHTML = taietura_descriere[diamant_object.taietura_id];
	
	// Dom.get('panel_certificat_image_element').src = 'images/ajax/ajax_loading.gif'; // TODO: Make this better.
	// if (diamant_object.cert_file.substring(0, 4) === 'http')
	//	Dom.get('panel_certificat_image_element').src = diamant_object.cert_file;
	// else
	//	Dom.get('panel_certificat_image_element').src = 'http://89.39.46.130/' + diamant_object.getForma().toLowerCase() + '/' + diamant_object.cert_file + '.jpg';
	
	Dom.removeClass('diamant_box', 'hidden');
},


showMonturaView = function(montura_view)
{
	myassert(montura_view, 'montura_view');

	var i = 0;

	for (i = 1; i <= constants.montura_view_count; i++)
	{
		Dom.addClass('montura_zoom_' + i, 'hidden');
		Dom.addClass('montura_detail_' + i, 'hidden');
		Dom.removeClass('montura_view_' + i, 'selected');
	}
	Dom.addClass('montura_view_' + montura_view, 'selected');
	Dom.removeClass('montura_detail_' + montura_view, 'hidden');
	Dom.removeClass('montura_zoom_' + montura_view, 'hidden');
},

selectMonturaView = function(montura_view, montura_search)
{
	myassert(montura_view, 'montura_view');
	myassert(montura_search, 'montura_search');

	log('begin selectMonturaView(montura_view: ' + lang.dump(montura_view) + ', montura_search: ' + lang.dump(montura_search) + ')');

	if (lang.isInt(montura_view) && 1 <= montura_view && montura_view <= constants.montura_view_count)
	{
		if (montura_search.montura_view != montura_view)
		{
			montura_search.montura_view = montura_view;
			showMonturaView(montura_search.montura_view);
		}
	}

	log('end selectMonturaView');
},	

showMonturaInstructions = function()
{
	Dom.addClass('montura_box', 'hidden');
	Dom.removeClass('montura_instructions_box', 'hidden');
},

showMontura = function(montura_object, forma_id)
{
	myassert(montura_object, 'montura_object');
	myassert(montura_object, 'forma_id');

	Dom.removeClass('montura_box', 'hidden');
	Dom.addClass('montura_instructions_box', 'hidden');

	var	i = 0;

	for (i = 1; i <= constants.montura_view_count; i++)
	{
		Dom.setBackgroundImage('montura_view_' + i, getMonturaImage(montura_object.metal_id, montura_object.template_id, forma_id, i, 'view'));
		Dom.setBackgroundImage('montura_detail_' + i, getMonturaImage(montura_object.metal_id, montura_object.template_id, forma_id, i, 'detail'));
		Dom.setBackgroundImage('montura_zoom_' + i, getMonturaImage(montura_object.metal_id, montura_object.template_id, forma_id, i, 'zoom'));
	}

	// TODO: Add more properties here.
	Dom.get('montura_pret').innerHTML = montura_object.getPret();
	Dom.get('montura_cod_produs').innerHTML = montura_object.getCod();
	Dom.get('montura_metal').innerHTML = montura_object.getMetal();
	
	Dom.removeClass('montura_box', 'hidden');
},

active_panel = null,
panel_width = '300px',

hideActivePanel = function()
{
	if (active_panel)
	{
		active_panel.hide();
	}
},

initPanels = function()
{
	log('begin initPanels()');
	var
	panels = {
		panel_cod: new Panel('panel_cod', { visible: false, width: panel_width }),
		panel_forma: new Panel('panel_forma', { visible: false, width: panel_width }),
		panel_pret: new Panel('panel_pret', { visible: false, width: panel_width }),
		panel_greutate: new Panel('panel_greutate', { visible: false, width: panel_width }),
		panel_pret_carat: new Panel('panel_pret_carat', { visible: false, width: panel_width }),
		panel_culoare: new Panel('panel_culoare', { visible: false, width: panel_width }),
		panel_claritate: new Panel('panel_claritate', { visible: false, width: panel_width }),
		panel_taietura: new Panel('panel_taietura', { visible: false, width: panel_width }),
		panel_procent_adancime: new Panel('panel_procent_adancime', { visible: false, width: panel_width }),
		panel_procent_tableta: new Panel('panel_procent_tableta', { visible: false, width: panel_width }),
		panel_simetrie: new Panel('panel_simetrie', { visible: false, width: panel_width }),
		panel_finisaj: new Panel('panel_finisaj', { visible: false, width: panel_width }),
		panel_colet: new Panel('panel_colet', { visible: false, width: panel_width }),
		panel_centura: new Panel('panel_centura', { visible: false, width: panel_width }),
		panel_fluorescenta: new Panel('panel_fluorescenta', { visible: false, width: panel_width }),
		panel_dimensiuni: new Panel('panel_dimensiuni', { visible: false, width: panel_width }),
		panel_raport_lungime_latime: new Panel('panel_raport_lungime_latime', { visible: false, width: panel_width }),
		panel_certificat: new Panel('panel_certificat', { visible: false, width: panel_width }),
		panel_certificat_image: new Panel('panel_certificat_image', 	            
				{ fixedcenter:false,  
	              close:true,  
	              draggable:false,  
	              zindex:100, 
	              modal:true, 
	              visible:false })
	},
	
	showPanel = function(attribute, context_element)
	{
		var 
			x = 0,
			p = panels['panel_' + attribute];
		// alert(lang.dump(p, 1));
		p.cfg.setProperty('context', [context_element, 'tr', 'tl']);
		x = p.cfg.getProperty('x');
		p.cfg.setProperty('x', x - 10);
		log(p.cfg.getProperty('x'));
		if (active_panel !== p)
		{
			hideActivePanel();
		}
		p.show();
		active_panel = p;
	}; // var	

	panels.panel_cod.render();
	panels.panel_forma.render();
	panels.panel_pret.render();
	panels.panel_greutate.render();
	panels.panel_pret_carat.render();
	panels.panel_culoare.render();
	panels.panel_claritate.render();
	panels.panel_taietura.render();
	panels.panel_procent_adancime.render();
	panels.panel_procent_tableta.render();
	panels.panel_simetrie.render();
	panels.panel_finisaj.render();
	panels.panel_colet.render();
	panels.panel_centura.render();
	panels.panel_fluorescenta.render();
	panels.panel_dimensiuni.render();
	panels.panel_raport_lungime_latime.render();
	panels.panel_certificat.render();

	panels.panel_certificat_image.render(document.body);

	Event.addListener('label_cod', 'click', function() { showPanel('cod', 'label_cod'); });
	Event.addListener('label_forma', 'click', function() { showPanel('forma', 'label_cod'); });
	Event.addListener('label_pret', 'click', function() { showPanel('pret', 'label_cod'); });
	Event.addListener('label_greutate', 'click', function() { showPanel('greutate', 'label_cod'); });
	Event.addListener('label_pret_carat', 'click', function() { showPanel('pret_carat', 'label_cod'); });
	Event.addListener('label_culoare', 'click', function() { showPanel('culoare', 'label_cod'); });
	Event.addListener('label_claritate', 'click', function() { showPanel('claritate', 'label_cod'); });
	Event.addListener('label_taietura', 'click', function() { showPanel('taietura', 'label_cod'); });
	Event.addListener('label_procent_adancime', 'click', function() { showPanel('procent_adancime', 'label_procent_adancime'); });
	Event.addListener('label_procent_tableta', 'click', function() { showPanel('procent_tableta', 'label_procent_adancime'); });
	Event.addListener('label_simetrie', 'click', function() { showPanel('simetrie', 'label_procent_adancime'); });
	Event.addListener('label_finisaj', 'click', function() { showPanel('finisaj', 'label_procent_adancime'); });
	Event.addListener('label_colet', 'click', function() { showPanel('colet', 'label_procent_adancime'); });
	Event.addListener('label_centura', 'click', function() { showPanel('centura', 'label_procent_adancime'); });
	Event.addListener('label_fluorescenta', 'click', function() { showPanel('fluorescenta', 'label_procent_adancime'); });
	Event.addListener('label_dimensiuni', 'click', function() { showPanel('dimensiuni', 'label_procent_adancime'); });
	Event.addListener('label_raport_lungime_latime', 'click', function() { showPanel('raport_lungime_latime', 'label_procent_adancime'); });
	Event.addListener('label_certificat', 'click', function() { showPanel('certificat', 'label_cod'); });

	/*
	Event.addListener('click_aici', 'click', function() {
		var p = panels.panel_certificat_image;
		p.center();
		p.show();
		p.center();
	} );
	*/
	
	Event.addListener('panel_certificat_image_element', 'click', function() { panels.panel_certificat_image.hide(); });
	
	Dom.removeClass('panel_container', 'hidden'); // All panels got hidden by now by their initialization.
	log('end initPanels');
},

initMontura = function(ultima_selectie, search_array)
{
	myassert(lang.isBoolean(ultima_selectie));
	myassert(lang.isArray(search_array));
    log('begin initMontura(ultima_selectie: ' + lang.dump(ultima_selectie) + ', search_array: ' + lang.dump(search_array) + ')');

    var 
    montura_object = ultima_selectie && session && session.montura_object && session.montura_object.montura_search ? session.montura_object.copy() : null,
    montura_search = ultima_selectie && session && session.montura_object && session.montura_object.montura_search ? session.montura_object.montura_search.copy() : new MonturaSearch(search_array),

	montura_item_list = [], // array of montura object - the visible ones from montura_cache

    getMonturaForma = function()
    {
        return session && session.diamant_object && session.diamant_object.forma_id ? session.diamant_object.forma_id : constants.forma_id;
    },
	
	isDefaultMonturaForma = function()
	{
        return session && session.diamant_object && session.diamant_object.forma_id ? false : true;
	},

    showSelectedMetal = function()
    {
        var i = 0;

        for (i = 1; i <= constants.metal_count; i++)
        {
            Dom.removeClass('metal_' + i, 'selected');
        }
        Dom.addClass('metal_' + montura_search.metal_id, 'selected');
		
		Dom.get('instructions_metal_selectat').innerHTML = metal_encoding[montura_search.metal_id];
		Dom.get('instructions_forma_selectata').innerHTML = forma_encoding[getMonturaForma()];
    },

    selectMetal = function(metal_id)
    {
        log('begin selectMetal(metal_id: ' + lang.dump(metal_id) + ')');

		if (lang.isInt(metal_id) && 1 <= metal_id && metal_id <= constants.metal_count)
		{
			if (montura_search.metal_id != metal_id)
			{
				montura_search.metal_id = metal_id;
				showSelectedMetal();

				montura_object = null;
				showMonturaInstructions();

				// Show montura list.
				showMonturaList();
			}
		}

        log('end selectMetal');
    },

    showSelectedPage = function()
    {
        var i = 0;

        for (i = 1; i <= 10; i++)
        {
            Dom.removeClass('page_a_' + i, 'page_selected');
            Dom.removeClass('page_b_' + i, 'page_selected');
        }
        Dom.addClass('page_a_' + montura_search.montura_page, 'page_selected');
        Dom.addClass('page_b_' + montura_search.montura_page, 'page_selected');
    },

    selectPage = function(montura_page)
    {
        log('begin selectPage(montura_page: ' + montura_page + ')');

		if (lang.isInt(montura_page) && 1 <= montura_page && montura_page <= 10)
		{
			if (montura_search.montura_page !== montura_page)
			{
				montura_search.montura_page = montura_page;
				showSelectedPage();
				
				// Show montura list.
				showMonturaList();
			}
		}

        log('end selectPage');
    },

	selectMontura = function(i)
	{
		log('begin selectMontura(i:' + lang.dump(i) + ')');
		
		if (montura_item_list[i])
		{
			montura_object = montura_item_list[i];
			montura_object.montura_search = montura_search.copy();
			
			showMonturaListSelection();
			showMontura(montura_object, getMonturaForma());
		}
	},
	
	showMonturaListSelection = function()
	{
		var i = 0;
		
		for (i = 1; i <= constants.montura_item_count; i++)
		{
			if (montura_item_list && montura_item_list[i] && montura_object && montura_object.montura_id === montura_item_list[i].montura_id)
			{
				Dom.addClass('montura_item_' + i, 'montura_item_selected');
			}
			else
			{
				Dom.removeClass('montura_item_' + i, 'montura_item_selected');
			}
		}
	},
	
    showMonturaList = function()
    {
        // Cache is 0-indexed, page is 1-indexed.
		log('begin showMonturaList()');

		var 
		i = 0,
		k = 0,
		pos = -1,
		alpha = montura_search.montura_page - 1,
		omega = montura_search.montura_page + constants.montura_item_count - 1,
		metal_id = montura_search.metal_id,
		obj = null; // var

		montura_item_list = [null]; // reseting
		
		log(lang.dump(montura_cache));
		
		for (i = alpha; i < omega; i++)
		{
			k++;
			if (montura_cache[metal_id][i])
			{
				obj = new MonturaObject(montura_cache[metal_id][i]);
				
				montura_item_list[k] = obj;
						
				Dom.get('montura_cod_' + k).innerHTML = obj.getCod();
				Dom.get('montura_pret_' + k).innerHTML = obj.getPret();
				Dom.setBackgroundImage('montura_image_' + k, getMonturaImage(obj.metal_id, obj.template_id, getMonturaForma(), 2, 'item'));
						
				Dom.removeClass('montura_item_' + k, 'hidden');
			}
			else
			{
				Dom.addClass('montura_item_' + k, 'hidden');
				
				if (i < montura_cache.length)
				{
					pos = i;
				}
			}
		}
		showMonturaListSelection();

        log('end showMonturaList');
		return pos;
    },

    /* Magnifying glass */

    hideMagnifyingGlass = function()
    {
        Dom.addClass('montura_zoom_container', 'hidden');
    },

    updateMagnifyingGlass = function(e)
    {
		var 
		montura_view = montura_search.montura_view,
		a = Event.getXY(e),
		b = Dom.getXY('montura_detail_container'),
		x = a[0] - b[0],
		y = a[1] - b[1], // x si y intre 1 si 250.
		xx = Math.round((600 - 300) / (250 - 1) * x),
		yy = Math.round((600 - 300) / (250 - 1) * y); // var

        Dom.removeClass('montura_zoom_container', 'hidden');
        Dom.setStyle('montura_zoom_' + montura_view , 'background-position', (-xx) + 'px ' + (-yy) + 'px');
    },

	saveMontura = function()
	{
		session.montura_object = montura_object.copy();
		session.montura_object.montura_search.montura_view = montura_search.montura_view;
		saveSession();
		// alert('saved');
	},
	
	buttonAlegeDiamant = function()
	{
		log('begin buttonAlegeDiamant()');
		saveMontura();
		log('end buttonAlegeDiamant');
	},
	
	buttonSchimbaDiamant = function()
	{
		log('begin buttonSchimbaDiamant()');
		saveMontura();
		log('end buttonSchimbaDiamant');
	},
	
	buttonFinalizeazaInel = function()
	{
		log('begin buttonFinalizeazaInel()');
		saveMontura();
		log('end buttonFinalizeazaInel');
	},

    initMonturaMain = function()
    {
        log('begin initMonturaMain()');

        Event.addListener('metal_1', 'click', function() { selectMetal(1); });
        Event.addListener('metal_2', 'click', function() { selectMetal(2); });
        Event.addListener('metal_3', 'click', function() { selectMetal(3); });

        Event.addListener('page_a_1', 'click', function() { selectPage(1); });
        Event.addListener('page_a_10', 'click', function() { selectPage(10); });
        Event.addListener('page_b_1', 'click', function() { selectPage(1); });
        Event.addListener('page_b_10', 'click', function() { selectPage(10); });

        Event.addListener('montura_view_1', 'click', function() { selectMonturaView(1, montura_search); });
        Event.addListener('montura_view_2', 'click', function() { selectMonturaView(2, montura_search); });
        Event.addListener('montura_view_3', 'click', function() { selectMonturaView(3, montura_search); });

        Event.addListener('montura_item_1', 'click', function() { selectMontura(1); });
        Event.addListener('montura_item_2', 'click', function() { selectMontura(2); });
        Event.addListener('montura_item_3', 'click', function() { selectMontura(3); });
        Event.addListener('montura_item_4', 'click', function() { selectMontura(4); });
        Event.addListener('montura_item_5', 'click', function() { selectMontura(5); });
        Event.addListener('montura_item_6', 'click', function() { selectMontura(6); });
        Event.addListener('montura_item_7', 'click', function() { selectMontura(7); });
        Event.addListener('montura_item_8', 'click', function() { selectMontura(8); });
        Event.addListener('montura_item_9', 'click', function() { selectMontura(9); });

        Event.addListener('montura_detail_container', 'mouseout', hideMagnifyingGlass);
        Event.addListener('montura_detail_container', 'mousemove', updateMagnifyingGlass);

        Event.addListener('montura_button_alege_diamant', 'click', buttonAlegeDiamant);
        Event.addListener('montura_button_schimba_diamant', 'click', buttonSchimbaDiamant);
        Event.addListener('montura_button_finalizeaza_inel', 'click', buttonFinalizeazaInel);

        log('montura_object: ' + lang.dump(montura_object));
        log('montura_search: ' + lang.dump(montura_search));

        Dom.removeClass('montura_option_' + (session.diamant_object ? 'finalizeaza_inel' : 'alege_diamant'), 'hidden');
		if (session.diamant_object)
		{
			Dom.get('option_forma_selectata').innerHTML = session.diamant_object.getForma();
			Dom.get('option_greutate_selectata').innerHTML = session.diamant_object.getGreutate();
		}
		
		
        showSelectedMetal();
        showSelectedPage();

        showMonturaView(montura_search.montura_view);

		if (montura_object)
		{
			showMontura(montura_object, getMonturaForma());
		}
		else
		{
			showMonturaInstructions();
		}

		// Show montura list.
		showMonturaList();

        log('end initMonturaMain');
    }; // var

    initMonturaMain();

    log('end initMontura');
},

initDiamant = function(ultima_selectie, search_array)
{
	myassert(lang.isBoolean(ultima_selectie));
	myassert(lang.isArray(search_array));
    log('begin initDiamant(ultima_selectie: ' + lang.dump(ultima_selectie) + ', search_array: ' + lang.dump(search_array) + ')');

    var 
    diamant_object = ultima_selectie && session && session.diamant_object && session.diamant_object.diamant_search ? session.diamant_object.copy() : null,
    diamant_search = ultima_selectie && session && session.diamant_object && session.diamant_object.diamant_search ? session.diamant_object.diamant_search.copy() : new DiamantSearch(search_array),
    
	diamant_cache = [], // array of diamant array
    diamant_buffers = [], // array of int
    diamant_item_list = [], // array of diamant object - the visible ones from diamant_cache

    showSelectedForma = function()
    {
        var i = 0;

        for (i = 1; i <= constants.forma_count; i++)
        {
            Dom.removeClass('forma_' + i, 'selected');
			Dom.removeClass('diamant_detail_container', 'forma_' + i);
        }
        Dom.addClass('forma_' + diamant_search.forma_id, 'selected');
		Dom.addClass('diamant_detail_container', 'forma_' + diamant_search.forma_id);
		
		Dom.get('instructions_forma_selectata').innerHTML = forma_encoding[diamant_search.forma_id];
		// Dom.get('instructions_forma_selectata_image').src = 'images/photos/forma_' + (diamant_search.forma_id + '').lpad(2, '0') + '.png';
    },

    selectForma = function(forma_id)
    {
        log('begin selectForma(forma_id: ' + lang.dump(forma_id) + ')');

        if (lang.isInt(forma_id) && 1 <= forma_id && forma_id <= constants.forma_count)
        {
            if (diamant_search.forma_id != forma_id)
            {
                diamant_search.forma_id = forma_id;
                showSelectedForma();

                diamant_object = null;
                showDiamantInstructions();
				
				hideActivePanel(); // Help panel should be hidden.

				// Show diamant list.
                performSearch(true);
            }
        }

        log('end selectForma');
    },

    showSelectedCamp = function()
    {
        var 
		i = 0,
		camp_transform = [null, 4, 0, 1, 2, 3],
		desc_transform = [null, false, false, true, true, true],
		x = camp_transform[diamant_search.camp] * 72,
		tmp = desc_transform[diamant_search.camp] ? diamant_search.desc : 1 - diamant_search.desc, // tmp este 0 sau 1.
		y = (tmp + 1) * 25; // y este 25 sau 50.

        for (i = 1; i <= 5; i++)
        {
			Dom.setStyle('camp_' + i, 'background-position', (-camp_transform[i] * 72) + 'px 0');
        }

		Dom.setStyle('camp_' + diamant_search.camp, 'background-position', (-x) + 'px ' + (-y) + 'px');
    },

    selectCamp = function(camp)
    {
        log('begin selectCamp(camp: ' + lang.dump(camp) + ')');

        if (lang.isInt(camp) && 1 <= camp && camp <= 5)
        {
            if (diamant_search.camp != camp)
            {
                diamant_search.camp = camp;
                diamant_search.desc = 0;
            }
            else
            {
                diamant_search.desc = 1 - diamant_search.desc;
            }
            showSelectedCamp();
			
			// Show diamant list.
            performSearch(true);
        }
		
		log('end selectCamp');
    },

    selectDiamant = function(i)
    {
        log('begin selectDiamant(i: ' + lang.dump(i) + ')');

        if (diamant_item_list[i])
        {
            diamant_object = diamant_item_list[i];
            diamant_object.diamant_search = diamant_search.copy();

            showDiamantListSelection();
            showDiamant(diamant_object);
        }

        log('end selectDiamant');
    },

    showDiamantListSelection = function()
    {
        var i = 0;
		
        for (i = 1; i <= constants.diamant_item_count; i++)
        {
            if (diamant_item_list && diamant_item_list[i] && diamant_object && diamant_object.diamant_id === diamant_item_list[i].diamant_id)
            {
                Dom.addClass('diamant_item_' + i, 'diamant_item_selected');
            }
            else
            {
                Dom.removeClass('diamant_item_' + i, 'diamant_item_selected');
            }
        }
    },

    showDiamantList = function()
    {
        // Cache is 0-indexed, page is 1-indexed.
	
        log('begin showDiamantList()');
        var 
		i = 0,
		k = 0,
		pos = -1,
		alpha = diamant_search.diamant_page - 1,
		omega = diamant_search.diamant_page + constants.diamant_item_count - 1,
		obj = null; // var
		
        diamant_item_list = [null]; // reseting
		
        Dom.removeClass('diamant_item_container', 'diamant_item_container_odd');
        Dom.removeClass('diamant_item_container', 'diamant_item_container_even');
        Dom.addClass('diamant_item_container', 'diamant_item_container_' + (diamant_search.diamant_page % 2 ? 'odd' : 'even'));
        
		for (i = alpha; i < omega; i++)
        {
			k++;
            if (diamant_cache[i])
            {
                obj = new DiamantObject(diamant_cache[i]);
				
				diamant_item_list[k] = obj;
				
                Dom.get('diamant_pret_' + k).innerHTML = obj.getPret() + ' <span style="font-size:9px">RON</span>';
                Dom.get('diamant_greutate_' + k).innerHTML = obj.getGreutate() + ' <span style="font-size:9px">ct.</span>';
                Dom.get('diamant_culoare_' + k).innerHTML = obj.getCuloare();
                Dom.get('diamant_claritate_' + k).innerHTML = obj.getClaritate();
                Dom.get('diamant_taietura_' + k).innerHTML = obj.getTaietura();
				
				Dom.removeClass('diamant_item_' + k, 'hidden');
            }
            else
            {
				Dom.addClass('diamant_item_' + k, 'hidden');

                if (i < diamant_cache.length)
                {
                    pos = i;
                }
            }
        }
        showDiamantListSelection();
		
        if (pos != -1)
        {
            Dom.removeClass('release_button_container', 'hidden');
        }
        else
        {
            Dom.addClass('release_button_container', 'hidden');
        }
		
        log('end showDiamantList: ' + pos);
        return pos;
    },

    searchSuccess = function(response)
    {
        log('begin searchSuccess: ' + response.responseText.length);

		var
		i = 0,
		datastore = null,
		count = null,
		offset = null,
		diamants = null,
		reload = response.argument.reload;
		
        // Parsing JSON strings can throw a SyntaxError exception.
        try
        {
            datastore = JSON.parse(response.responseText);
            count = datastore[0];
            offset = datastore[1];
            diamants = datastore[2];
            
			log('count: ' + lang.dump(count));
        }
        catch (e)
        {
            // TODO: Clean this up.
            log('error parsing JSON: ' + response.responseText, 'error', 'JSON');
        }

        if (reload)
        {
            diamant_cache = new Array(count);
            diamant_buffers = new Array(Math.ceil(diamant_cache.length / constants.diamant_buffer_count));
            // Refresh pagina_slider because cache.length has changed.
            pagina_slider.setRealValue(pagina_slider.getSearchValue(), true, true);
            Dom.get('total_diamante').innerHTML = diamant_cache.length;
            Dom.get('forma_selectata').innerHTML = forma_encoding[diamant_search.forma_id];
        }
        for (i = 0; i < diamants.length; i++)
        {
            diamant_cache[offset + i] = diamants[i];
            diamant_buffers[Math.floor((offset + i) / constants.diamant_buffer_count)]++;
        }

        showDiamantList(); // Should allways return -1 here.

        Dom.addClass('loading_container', 'hidden');
        if (diamant_cache.length === 0)
        {
            Dom.removeClass('notfound_container', 'hidden');
        }
        else
        {
            Dom.addClass('notfound_container', 'hidden');
        }

        log('end searchSuccess');
    },

    searchFailure = function(response)
    {
        log('begin searchFailure', 'error');
        log('end searchFailure', 'error');
    },

    rezultate_transaction = null,

    performSearch = function(reload)
    {
        log('begin performSearch(reload: ' + lang.dump(reload) + ')');

        var 
		pos = 0,
		buffer_min = 0,
		buffer_max = 0,
		params = '',
		callback = null;

        if (reload)
        {
            pagina_slider.setRealValue(1, true, true);
            diamant_cache = [];
            diamant_buffers = [];
            pos = 0;
        }
        else
        {
            pos = showDiamantList();
        }

        log('pos: ' + lang.dump(pos));

        if (pos > -1)
        {
            buffer_min = buffer_max = Math.floor(pos / constants.diamant_buffer_count);
            if (buffer_min > 0 && !diamant_buffers[buffer_min - 1])
            {
                buffer_min--;
            }
            if (!diamant_buffers[buffer_max + 1])
            {
                buffer_max++;
            }

            params = params + 'forma_id=' + diamant_search.forma_id;
            params = params + '&pret_min=' + diamant_search.pret_min + '&pret_max=' + diamant_search.pret_max;
            params = params + '&greutate_min=' + diamant_search.greutate_min + '&greutate_max=' + diamant_search.greutate_max;
            params = params + '&culoare_min=' + diamant_search.culoare_min + '&culoare_max=' + diamant_search.culoare_max;
            params = params + '&claritate_min=' + diamant_search.claritate_min + '&claritate_max=' + diamant_search.claritate_max;
            params = params + '&taietura_min=' + diamant_search.taietura_min + '&taietura_max=' + diamant_search.taietura_max;
            params = params + '&camp=' + diamant_search.camp + '&desc=' + diamant_search.desc;
            params = params + '&buffer_min=' + buffer_min + '&buffer_max=' + buffer_max;

            log('params: ' + lang.dump(params));

            callback =
		    {
		        success: searchSuccess,
		        failure: searchFailure,
		        argument: { reload: reload } // retrieve this object using response.argument in success handler
		    };

            Connect.abort(rezultate_transaction);

            Dom.addClass('release_button_container', 'hidden');
            Dom.removeClass('loading_container', 'hidden');
            rezultate_transaction = Connect.asyncRequest('POST', 'rezultate.php', callback, params); // returns an object.
            log('transaction: ' + rezultate_transaction);
        }

        log('end performSearch');
    },



    pret_slider = null,
    greutate_slider = null,
    culoare_slider = null,
    claritate_slider = null,
    taietura_slider = null,

    initDualSliders = function()
    {
        log('begin initDualSliders');

		var
        max_range = 360,
        thumb_width = 15,

        culoare_tick_size = 36,
        claritate_tick_size = 36,
        taietura_tick_size = 90,

        pret_min_range = 1,
        greutate_min_range = 1,
        culoare_min_range = culoare_tick_size - thumb_width + 1,
        claritate_min_range = claritate_tick_size - thumb_width + 1,
        taietura_min_range = taietura_tick_size - thumb_width + 1; // var

        DualSlider.prototype.real_lo = null;
        DualSlider.prototype.real_hi = null;
        DualSlider.prototype.real_min_value = null;
        DualSlider.prototype.real_max_value = null;
        DualSlider.prototype.highlight_element = null;
        DualSlider.prototype.search_min_parameter = null;
        DualSlider.prototype.search_max_parameter = null;
        DualSlider.prototype.parse_function = null;
        DualSlider.prototype.format_function = null;
        DualSlider.prototype.input_min_element = null;
        DualSlider.prototype.input_max_element = null;

        /** The actual minimum distance in pixels between the reported values of the thumbs. */
        DualSlider.prototype.getTrueMinRange = function() { return this.minRange + thumb_width - 1; };
        /** The actual maximum distance in pixels between the reported values of the thumbs. */
        DualSlider.prototype.getTrueMaxRange = function() { return max_range - this.getTrueMinRange(); };

        log('begin Ui accessors');

        DualSlider.prototype.getUiMinValue = function()
        {
            var 
			min_val = this.minVal,
            real_min_value = Math.round(intervalMapping(min_val, [0, this.getTrueMaxRange()], [this.real_lo, this.real_hi])); // var
			
            if (this.highlight_element === 'pret_highlight') // Hack, avoiding the use of a dedicated member.
            {
                real_min_value = Math.round(real_min_value / 100) * 100;
            }
            return real_min_value;
        };

        DualSlider.prototype.getUiMaxValue = function()
        {
            var
			max_val = this.maxVal - this.getTrueMinRange(),
            real_max_value = Math.round(intervalMapping(max_val, [0, this.getTrueMaxRange()], [this.real_lo, this.real_hi])); // var
			
            if (this.highlight_element === 'pret_highlight') // Hack, avoiding the use of a dedicated member.
            {
                real_max_value = Math.round(real_max_value / 100) * 100;
            }
            return real_max_value;
        };

        DualSlider.prototype.setUiMinValue = function(real_min_value)
        {
            var min_val = Math.round(intervalMapping(real_min_value, [this.real_lo, this.real_hi], [0, this.getTrueMaxRange()]));
            this.setMinValue(min_val); // Will fire the change event, but no code will get executed in the change handler.
            this.updateHighlight(this.minVal, this.maxVal); // Updating the highlight here because no code was was executed in the change handler.
        };

        DualSlider.prototype.setUiMaxValue = function(real_max_value)
        {
            var max_val = Math.round(intervalMapping(real_max_value, [this.real_lo, this.real_hi], [0, this.getTrueMaxRange()]));
            this.setMaxValue(max_val + this.getTrueMinRange()); // Will fire the change event, but no code will get executed in the handler.
            this.updateHighlight(this.minVal, this.maxVal); // Updating the highlight here because no code was was executed in the change handler.
        };

        log('end Ui accessors');

        log('begin Real accessors');

        /** public: use only get/set Real Min/Max Value externally. */
        DualSlider.prototype.getRealMinValue = function()
        {
            myassert(this.real_min_value, 'real_min_value is not set', 'DualSlider');
            return this.real_min_value;
        };

        /** public: use only get/set Real Min/Max Value externally. */
        DualSlider.prototype.getRealMaxValue = function()
        {
            myassert(this.real_max_value, 'real_max_value is not set', 'DualSlider');
            return this.real_max_value;
        };

        /** public: use only get/set Real Min/Max Value externally. */
        DualSlider.prototype.setRealMinValue = function(real_min_value, set_ui, set_input)
        {
            myassert(lang.isInt(real_min_value), 'set real_min_value to an int', 'DualSlider');
            this.real_min_value = real_min_value;
            this.setSearchMinValue(real_min_value);

            if (set_ui)
            {
                this.setUiMinValue(real_min_value);
            }
            if (set_input)
            {
                this.setInputMinValue(real_min_value);
            }
        };

        /** public: use only get/set Real Min/Max Value externally. */
        DualSlider.prototype.setRealMaxValue = function(real_max_value, set_ui, set_input)
        {
            myassert(lang.isInt(real_max_value), 'set real_max_value to an int', 'DualSlider');
            this.real_max_value = real_max_value;
            this.setSearchMaxValue(real_max_value);

            if (set_ui)
            {
                this.setUiMaxValue(real_max_value);
            }
            if (set_input)
            {
                this.setInputMaxValue(real_max_value);
            }
        };

        log('end Real accessors');

        log('begin Search accessors');

        DualSlider.prototype.getSearchMinValue = function()
        {
            return diamant_search[this.search_min_parameter];
        };

        DualSlider.prototype.getSearchMaxValue = function()
        {
            return diamant_search[this.search_max_parameter];
        };

        DualSlider.prototype.setSearchMinValue = function(real_min_value)
        {
            diamant_search[this.search_min_parameter] = real_min_value;
            saveSession();
        };

        DualSlider.prototype.setSearchMaxValue = function(real_max_value)
        {
            diamant_search[this.search_max_parameter] = real_max_value;
            saveSession();
        };

        log('end Search accessors');

        log('begin Input accessors');

        DualSlider.prototype.getInputMinValue = function(s)
        {
            return this.parse_function ? this.parse_function(s) : s;
        };

        DualSlider.prototype.getInputMaxValue = function(s)
        {
            return this.parse_function ? this.parse_function(s) : s;
        };

        DualSlider.prototype.setInputMinValue = function(real_min_value)
        {
            if (this.input_min_element)
            {
                Dom.get(this.input_min_element).value = this.format_function ? this.format_function(real_min_value) : real_min_value;
            }
        };

        DualSlider.prototype.setInputMaxValue = function(real_max_value)
        {
            if (this.input_max_element)
            {
                Dom.get(this.input_max_element).value = this.format_function ? this.format_function(real_max_value) : real_max_value;
            }
        };

        DualSlider.prototype.enterInputMinValue = function(s)
        {
            var 
			v = this.getInputMaxValue(s),
            validation = lang.isInt(v) && this.real_lo <= v && v <= this.getRealMaxValue(),
            real_min_value = validation ? v : this.getRealMinValue(); // var
            
			this.setRealMinValue(real_min_value, true, true);
            performSearch(true, true);
        };

        DualSlider.prototype.enterInputMaxValue = function(s)
        {
            var 
			v = this.getInputMaxValue(s),
            validation = lang.isInt(v) && this.getRealMinValue() <= v && v <= this.real_hi,
            real_max_value = validation ? v : this.getRealMaxValue(); // var
			
            this.setRealMaxValue(real_max_value, true, true);
            performSearch(true, true);
        };

        DualSlider.prototype.blurInputMinValue = function()
        {
            this.setRealMinValue(this.getRealMinValue(), false, true);
        };

        DualSlider.prototype.blurInputMaxValue = function()
        {
            this.setRealMaxValue(this.getRealMaxValue(), false, true);
        };

        log('end Input accessors');

        log('begin event handlers');

        DualSlider.prototype.updateHighlight = function(min_val, max_val)
        {
            var
			left = min_val + thumb_width,
            width = max_val - min_val - thumb_width; // var
			
            if (this.highlight_element)
            {
                Dom.setStyle(this.highlight_element, 'left', left + 'px');
                Dom.setStyle(this.highlight_element, 'width', width + 'px');
            }
        };

        DualSlider.prototype.executeReady = function()
        {
            myassert(this.search_min_parameter && this.search_max_parameter);

            this.minSlider.animate = false;
            this.maxSlider.animate = false;

            // Restore the search values.
            this.setRealMinValue(this.getSearchMinValue(), true, true);
            this.setRealMaxValue(this.getSearchMaxValue(), true, true);

            this.subscribe('slideStart', this.executeSlideStart);
            this.subscribe('change', this.executeChange);
            this.subscribe('slideEnd', this.executeSlideEnd);
        };

        DualSlider.prototype.executeSlideStart = function()
        {
            if (this.activeSlider.valueChangeSource == Slider.SOURCE_UI_EVENT)
            {
                // The inputs will not call the onBlur handler unless they have focus.
                // This is a fix for FireFox, because the inputs don't get blurred when the sliders are clicked.
                Dom.get('pret_min_input').blur();
                Dom.get('pret_max_input').blur();
                Dom.get('greutate_min_input').blur();
                Dom.get('greutate_max_input').blur();
            }
        };

        DualSlider.prototype.executeChange = function()
        {
            if (this.activeSlider.valueChangeSource == Slider.SOURCE_UI_EVENT)
            {
                if (this.activeSlider === this.minSlider)
                {
                    this.setRealMinValue(this.getUiMinValue(), false, true);
                }
                if (this.activeSlider === this.maxSlider)
                {
                    this.setRealMaxValue(this.getUiMaxValue(), false, true);
                }
                this.updateHighlight(this.minVal, this.maxVal);
            }
        };

        DualSlider.prototype.executeSlideEnd = function()
        {
            if (this.activeSlider.valueChangeSource == Slider.SOURCE_UI_EVENT)
            {
                if (this.activeSlider === this.minSlider)
                {
                    this.setRealMinValue(this.getUiMinValue(), false, true);
                }
                if (this.activeSlider === this.maxSlider)
                {
                    this.setRealMaxValue(this.getUiMaxValue(), false, true);
                }
                performSearch(true, true);
            }
        };

        log('end event handlers');

        var createDualSlider = function(slider_element, min_thumb_element, max_thumb_element, highlight_element,
		    max_range, min_range, tick_size,
		    real_lo, real_hi, search_min_parameter, search_max_parameter,
		    parse_function, format_function, input_min_element, input_max_element)
        {
            log('begin createDualSlider: ' + slider_element);

            // The dual slider constructor expects both thumbs to be placed at left coordinates 0.
            Dom.setStyle(min_thumb_element, 'left', '0');
            Dom.setStyle(max_thumb_element, 'left', '0');

            var slider = Slider.getHorizDualSlider(slider_element, min_thumb_element, max_thumb_element, max_range, tick_size);

            slider.minRange = min_range;
            slider.highlight_element = highlight_element;

            slider.real_lo = real_lo;
            slider.real_hi = real_hi;
            slider.search_min_parameter = search_min_parameter;
            slider.search_max_parameter = search_max_parameter;

            slider.parse_function = parse_function;
            slider.format_function = format_function;
            slider.input_min_element = input_min_element;
            slider.input_max_element = input_max_element;

            // FF BUG: FireFox does not trigger the blur event of the input elements if the user drags a slider.
            if (slider.input_min_element)
            {
                Event.addListener(input_min_element, 'blur', function() { log('blur min', null, 'events'); slider.blurInputMinValue(); });
                Event.addListener(input_min_element, 'keyup', function(e) { log('keyup min', null, 'events'); if (Event.getCharCode(e) === 13) { slider.enterInputMinValue(this.value); } });
            }
            if (slider.input_max_element)
            {
                Event.addListener(input_max_element, 'blur', function() { log('blur max', null, 'events'); slider.blurInputMaxValue(); });
                Event.addListener(input_max_element, 'keyup', function(e) { log('keyup max', null, 'events'); if (Event.getCharCode(e) === 13) { slider.enterInputMaxValue(this.value); } });
            }

            slider.subscribe('ready', slider.executeReady);

            log('end createDualSlider');
            return slider;
        };

        pret_slider = createDualSlider('pret_slider', 'pret_min_thumb', 'pret_max_thumb', 'pret_highlight',
		    max_range, pret_min_range, null,
		    constants.pret_lo, constants.pret_hi, 'pret_min', 'pret_max',
		    parsePret, formatPret, 'pret_min_input', 'pret_max_input');

        greutate_slider = createDualSlider('greutate_slider', 'greutate_min_thumb', 'greutate_max_thumb', 'greutate_highlight',
		    max_range, greutate_min_range, null,
		    constants.greutate_lo, constants.greutate_hi, 'greutate_min', 'greutate_max',
		    parseGreutate, formatGreutate, 'greutate_min_input', 'greutate_max_input');

        culoare_slider = createDualSlider('culoare_slider', 'culoare_min_thumb', 'culoare_max_thumb', 'culoare_highlight',
		    max_range, culoare_min_range, culoare_tick_size,
		    constants.culoare_lo, constants.culoare_hi, 'culoare_min', 'culoare_max');

        claritate_slider = createDualSlider('claritate_slider', 'claritate_min_thumb', 'claritate_max_thumb', 'claritate_highlight',
		    max_range, claritate_min_range, claritate_tick_size,
		    constants.claritate_lo, constants.claritate_hi, 'claritate_min', 'claritate_max');

        taietura_slider = createDualSlider('taietura_slider', 'taietura_min_thumb', 'taietura_max_thumb', 'taietura_highlight',
		    max_range, taietura_min_range, taietura_tick_size,
		    constants.taietura_lo, constants.taietura_hi, 'taietura_min', 'taietura_max');


        log('end initDualSliders');
    },

    pagina_slider = null,

    initMonoSlider = function()
    {
        log('begin initMonoSlider');

        var pagina_max_range = 255;

        Slider.prototype.real_value = null;
        Slider.prototype.search_parameter = null;
        Slider.prototype.interval_handle = null;

        Slider.prototype.getCandidates = function() { return diamant_cache.length - constants.diamant_item_count + 1; };

        log('begin Ui accessors');

        /** Valorile reale sunt in [1, this.getCandidates()]. */
        Slider.prototype.getUiValue = function()
        {
            var 
			value = this.getValue(),
			real_value = this.getCandidates() > 1 ? Math.round(intervalMapping(value, [0, pagina_max_range], [1, this.getCandidates()])) : 1; // var
        
			return real_value;
        };

        /** Valorile interne sunt in [0, pagina_max_range]. */
        Slider.prototype.setUiValue = function(real_value)
        {
            var value = this.getCandidates() > 1 ? Math.round(intervalMapping(real_value, [1, this.getCandidates()], [0, pagina_max_range])) : 0;
            this.setValue(value); // Will fire the change event, but no code will get executed in the change handler.
        };

        log('end Ui accessors');

        log('begin Real accessors');

        /** public: use only get/set Real Value externally. */
        Slider.prototype.getRealValue = function()
        {
            myassert(this.real_value, 'real_value is not set', 'Slider');
            return this.real_value;
        };

        /** public: use only get/set Real Value externally. */
        Slider.prototype.setRealValue = function(real_value, set_ui, set_input)
        {
            myassert(lang.isInt(real_value), 'set real_value to an int', 'Slider');
            this.real_value = real_value;
            this.setSearchValue(real_value);

            if (set_ui)
            {
                this.setUiValue(real_value);
            }
        };

        log('end Real accessors');

        log('begin Search accessors');

        Slider.prototype.getSearchValue = function()
        {
            return diamant_search[this.search_parameter];
        };

        Slider.prototype.setSearchValue = function(real_value)
        {
            diamant_search[this.search_parameter] = real_value;
        };

        log('end Search accessors');

        Slider.prototype.executeReady = function()
        {
            myassert(this.search_parameter);

            this.animate = false;

            this.setRealValue(this.getSearchValue(), true, true);
			
            this.subscribe('slideStart', this.executeSlideStart);
            this.subscribe('change', this.executeChange);
            this.subscribe('slideEnd', this.executeSlideEnd);
        };

        Slider.prototype.executeSlideStart = function()
        {
            if (this.valueChangeSource == Slider.SOURCE_UI_EVENT)
            {
                Dom.get('pagina_thumb_image').src = 'images/slider/thumb_press.png';
            }
        };

        Slider.prototype.executeChange = function()
        {
            if (this.valueChangeSource == Slider.SOURCE_UI_EVENT)
            {
                this.setRealValue(this.getUiValue(), false, true);
                showDiamantList();
            }
        };

        Slider.prototype.executeSlideEnd = function()
        {
            if (this.valueChangeSource == Slider.SOURCE_UI_EVENT)
            {
                Dom.get('pagina_thumb_image').src = 'images/slider/thumb.png';
                performSearch(false, false);
            }
        };

        log('end event handlers');

        log('begin additional buttons');

        Slider.prototype.increaseValue = function()
        {
            if (this.getRealValue() < this.getCandidates())
            {
                this.setRealValue(this.getRealValue() + 1, true, true);
                showDiamantList();
            }
        };

        Slider.prototype.decreaseValue = function()
        {
            if (this.getRealValue() > 1)
            {
                this.setRealValue(this.getRealValue() - 1, true, true);
                showDiamantList();
            }
        };

        Slider.prototype.handleDecreaseValue = function(e)
        {
            var self = this;

            Dom.get('pagina_sus').src = 'images/slider/up_press.png';
            this.decreaseValue();
            clearInterval(this.interval_handle);
            this.interval_handle = setInterval(function() { self.decreaseValue(); }, 100);
            if (e.preventDefault)
            {
                e.preventDefault();
            }
            else
            {
                e.returnValue = false;
            }
            return false;
        };

        Slider.prototype.handleIncreaseValue = function(e)
        {
            var self = this;

            Dom.get('pagina_jos').src = 'images/slider/down_press.png';
            this.increaseValue();
            clearInterval(this.interval_handle);
            this.interval_handle = setInterval(function() { self.increaseValue(); }, 100);
            if (e.preventDefault)
            {
                e.preventDefault();
            }
            else
            {
                e.returnValue = false;
            }
            return false;
        };

        Slider.prototype.handleRelease = function(e)
        {
            Dom.get('pagina_jos').src = 'images/slider/down.png';
            Dom.get('pagina_sus').src = 'images/slider/up.png';
            clearInterval(this.interval_handle);
            performSearch(false, false);
            if (e.preventDefault)
            {
                e.preventDefault();
            }
            else
            {
                e.returnValue = false;
            }
            return false;
        };

        log('end additional buttons');

        var createMonoSlider = function(slider_element, slider_thumb, max_range, search_parameter)
        {
            log('begin createMonoSlider: ' + slider_element);

            var slider = Slider.getVertSlider('pagina_slider', 'pagina_thumb', 0, max_range);

            slider.search_parameter = search_parameter;

            Event.addListener('pagina_sus', 'mousedown', function(e) { slider.handleDecreaseValue(e); });
            Event.addListener('pagina_sus', 'mouseup', function(e) { slider.handleRelease(e); });
            Event.addListener('pagina_sus', 'mouseout', function(e) { slider.handleRelease(e); });
            Event.addListener('pagina_jos', 'mousedown', function(e) { slider.handleIncreaseValue(e); });
            Event.addListener('pagina_jos', 'mouseup', function(e) { slider.handleRelease(e); });
            Event.addListener('pagina_jos', 'mouseout', function(e) { slider.handleRelease(e); });

            slider.executeReady(); // There is no 'ready' event to subscribe to.

            log('end createMonoSlider');
            return slider;
        };

        pagina_slider = createMonoSlider('pagina_slider', 'pagina_thumb', pagina_max_range, 'diamant_page');

        log('end initMonoSlider');
    },

	saveDiamant = function()
	{
	    session.diamant_object = diamant_object.copy();
		session.diamant_object.diamant_search.diamant_view = diamant_search.diamant_view;
        saveSession();
        // alert('saved');
	},
	
    buttonAlegeMontura = function()
    {
        log('begin buttonAlegeMontura()');
		saveDiamant();
        log('end buttonAlegeMontura');
    },

	buttonSchimbaMontura = function()
	{
		log('begin buttonSchimbaMontura()');
		saveDiamant();
		log('end buttonSchimbMontura');
	},
	
	buttonFinalizeazaInel = function()
	{
		log('begin buttonFinalizeazaInel()');
		saveDiamant();
		log('end buttonFinalizeazaInel()');
	},
	
	buttonCumparaDiamant = function()
	{
		log('begin buttonCumparaDiamant()');
		addCartDiamant(diamant_object);
		log('end buttonCumparaDiamant');
	},

	active_forma_label = null,
	
	hideActiveFormaLabel = function()
	{
		if (active_forma_label)
		{
			Dom.addClass(active_forma_label, 'hidden');
			active_forma_label = null;
		}
	},
	
	showFormaLabel = function(forma_label)
	{
		hideActiveFormaLabel();
		Dom.removeClass(forma_label, 'hidden');
		active_forma_label = forma_label;
	},
	
    initDiamantMain = function()
    {
        log('begin initDiamantMain()');

        Event.addListener('forma_1', 'mouseover', function() { showFormaLabel('forma_label_1'); });
        Event.addListener('forma_2', 'mouseover', function() { showFormaLabel('forma_label_2'); });
        Event.addListener('forma_3', 'mouseover', function() { showFormaLabel('forma_label_3'); });
        Event.addListener('forma_4', 'mouseover', function() { showFormaLabel('forma_label_4'); });
        Event.addListener('forma_5', 'mouseover', function() { showFormaLabel('forma_label_5'); });
        Event.addListener('forma_6', 'mouseover', function() { showFormaLabel('forma_label_6'); });
        Event.addListener('forma_7', 'mouseover', function() { showFormaLabel('forma_label_7'); });
        Event.addListener('forma_8', 'mouseover', function() { showFormaLabel('forma_label_8'); });
        Event.addListener('forma_9', 'mouseover', function() { showFormaLabel('forma_label_9'); });
        Event.addListener('forma_10', 'mouseover', function() { showFormaLabel('forma_label_10'); });

        Event.addListener('forma_1', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_2', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_3', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_4', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_5', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_6', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_7', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_8', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_9', 'mouseout', function() { hideActiveFormaLabel(); });
        Event.addListener('forma_10', 'mouseout', function() { hideActiveFormaLabel(); });

		
        Event.addListener('forma_1', 'click', function() { selectForma(1); });
        Event.addListener('forma_2', 'click', function() { selectForma(2); });
        Event.addListener('forma_3', 'click', function() { selectForma(3); });
        Event.addListener('forma_4', 'click', function() { selectForma(4); });
        Event.addListener('forma_5', 'click', function() { selectForma(5); });
        Event.addListener('forma_6', 'click', function() { selectForma(6); });
        Event.addListener('forma_7', 'click', function() { selectForma(7); });
        Event.addListener('forma_8', 'click', function() { selectForma(8); });
        Event.addListener('forma_9', 'click', function() { selectForma(9); });
        Event.addListener('forma_10', 'click', function() { selectForma(10); });

        Event.addListener('camp_1', 'click', function() { selectCamp(1); });
        Event.addListener('camp_2', 'click', function() { selectCamp(2); });
        Event.addListener('camp_3', 'click', function() { selectCamp(3); });
        Event.addListener('camp_4', 'click', function() { selectCamp(4); });
        Event.addListener('camp_5', 'click', function() { selectCamp(5); });

        Event.addListener('diamant_view_1', 'click', function() { selectDiamantView(1, diamant_search); });
        Event.addListener('diamant_view_2', 'click', function() { selectDiamantView(2, diamant_search); });
        Event.addListener('diamant_view_3', 'click', function() { selectDiamantView(3, diamant_search); });
        Event.addListener('diamant_view_4', 'click', function() { selectDiamantView(4, diamant_search); });

        Event.addListener('diamant_item_1', 'click', function() { selectDiamant(1); });
        Event.addListener('diamant_item_2', 'click', function() { selectDiamant(2); });
        Event.addListener('diamant_item_3', 'click', function() { selectDiamant(3); });
        Event.addListener('diamant_item_4', 'click', function() { selectDiamant(4); });
        Event.addListener('diamant_item_5', 'click', function() { selectDiamant(5); });
        Event.addListener('diamant_item_6', 'click', function() { selectDiamant(6); });
        Event.addListener('diamant_item_7', 'click', function() { selectDiamant(7); });
        Event.addListener('diamant_item_8', 'click', function() { selectDiamant(8); });
        Event.addListener('diamant_item_9', 'click', function() { selectDiamant(9); });
        Event.addListener('diamant_item_10', 'click', function() { selectDiamant(10); });
        Event.addListener('diamant_item_11', 'click', function() { selectDiamant(11); });
        Event.addListener('diamant_item_12', 'click', function() { selectDiamant(12); });
        Event.addListener('diamant_item_13', 'click', function() { selectDiamant(13); });
        Event.addListener('diamant_item_14', 'click', function() { selectDiamant(14); });
        Event.addListener('diamant_item_15', 'click', function() { selectDiamant(15); });

        Event.addListener('diamant_button_alege_montura', 'click', buttonAlegeMontura);
        Event.addListener('diamant_button_schimba_montura', 'click', buttonSchimbaMontura);
        Event.addListener('diamant_button_finalizeaza_inel', 'click', buttonFinalizeazaInel);
        Event.addListener('diamant_button_cumpara_diamant', 'click', buttonCumparaDiamant);
		
		Event.addListener('pret_slider_help', 'mouseout', function() { Dom.addClass('pret_slider_instructions', 'hidden'); });
		Event.addListener('pret_slider_help', 'mouseover', function() { Dom.removeClass('pret_slider_instructions', 'hidden'); });
		Event.addListener('pret_slider_instructions', 'mouseout', function() { Dom.addClass('pret_slider_instructions', 'hidden'); });
		Event.addListener('pret_slider_instructions', 'mouseover', function() { Dom.removeClass('pret_slider_instructions', 'hidden'); });
		Event.addListener('greutate_slider_help', 'mouseout', function() { Dom.addClass('greutate_slider_instructions', 'hidden'); });
		Event.addListener('greutate_slider_help', 'mouseover', function() { Dom.removeClass('greutate_slider_instructions', 'hidden'); });
		Event.addListener('greutate_slider_instructions', 'mouseout', function() { Dom.addClass('greutate_slider_instructions', 'hidden'); });
		Event.addListener('greutate_slider_instructions', 'mouseover', function() { Dom.removeClass('greutate_slider_instructions', 'hidden'); });
		Event.addListener('culoare_slider_help', 'mouseout', function() { Dom.addClass('culoare_slider_instructions', 'hidden'); });
		Event.addListener('culoare_slider_help', 'mouseover', function() { Dom.removeClass('culoare_slider_instructions', 'hidden'); });
		Event.addListener('culoare_slider_instructions', 'mouseout', function() { Dom.addClass('culoare_slider_instructions', 'hidden'); });
		Event.addListener('culoare_slider_instructions', 'mouseover', function() { Dom.removeClass('culoare_slider_instructions', 'hidden'); });
		Event.addListener('claritate_slider_help', 'mouseout', function() { Dom.addClass('claritate_slider_instructions', 'hidden'); });
		Event.addListener('claritate_slider_help', 'mouseover', function() { Dom.removeClass('claritate_slider_instructions', 'hidden'); });
		Event.addListener('claritate_slider_instructions', 'mouseout', function() { Dom.addClass('claritate_slider_instructions', 'hidden'); });
		Event.addListener('claritate_slider_instructions', 'mouseover', function() { Dom.removeClass('claritate_slider_instructions', 'hidden'); });
		Event.addListener('taietura_slider_help', 'mouseout', function() { Dom.addClass('taietura_slider_instructions', 'hidden'); });
		Event.addListener('taietura_slider_help', 'mouseover', function() { Dom.removeClass('taietura_slider_instructions', 'hidden'); });
		Event.addListener('taietura_slider_instructions', 'mouseout', function() { Dom.addClass('taietura_slider_instructions', 'hidden'); });
		Event.addListener('taietura_slider_instructions', 'mouseover', function() { Dom.removeClass('taietura_slider_instructions', 'hidden'); });
		
		f = function(el)
		{
			Event.addListener(el, 'mouseover', function() { Dom.addClass(el, 'hover'); });
			Event.addListener(el, 'mouseout', function() { Dom.removeClass(el, 'hover'); });
		}
		
		Dom.getElementsByClassName('cursor_pointer', null, null, f);
		Dom.getElementsByClassName('cursor_help', null, null, f);
		
		
		initPanels();

        log('diamant_object: ' + lang.dump(diamant_object));
        log('diamant_search: ' + lang.dump(diamant_search));

		Dom.removeClass('diamant_option_cumpara_diamant', 'hidden');
        Dom.removeClass('diamant_option_' + (session.montura_object ? 'finalizeaza_inel' : 'alege_montura'), 'hidden');
		if (session.montura_object)
		{
			Dom.get('option_model_selectat').innerHTML = session.montura_object.getCod();
			Dom.get('option_metal_selectat').innerHTML = session.montura_object.getMetal();
		}

		
        initDualSliders();
        initMonoSlider();

        showSelectedForma();
        showSelectedCamp();
        
		showDiamantView(diamant_search.diamant_view);
		
		if (diamant_object)
		{
			showDiamant(diamant_object);
		}
		else
		{
			showDiamantInstructions();
		}

		// Show diamant list.
        performSearch(true);

        log('end initDiamantMain');
    }; // end var statement

    initDiamantMain(); // Entry point for initDiamant.

    log('end initDiamant');
},

initFinalizare = function()
{
	log('begin initFinalizare()');
	
	var
	diamant_object = session && session.diamant_object && session.diamant_object.diamant_search ? session.diamant_object : null,
	montura_object = session && session.montura_object && session.montura_object.montura_search ? session.montura_object : null,
		
	buttonCumparaInel = function(e)
	{
		log('begin buttonCumparaInel(e)');
		
		var masura = myint(Dom.get('masura').options[Dom.get('masura').selectedIndex].value);
		if (masura)
		{
			addCartInel(diamant_object, montura_object, masura);
		}
		else
		{
			alert('Selectati o masura');
			Event.preventDefault(e);
		}

		log('end buttonCumparaInel');
	},
		
	initFinalizareMain = function()
	{
		myassert(diamant_object, 'diamant_object');
		myassert(montura_object, 'montura_object');
		
		log('begin initFinalizareMain()');
		
        Event.addListener('diamant_view_1', 'click', function() { selectDiamantView(1, diamant_object.diamant_search); diamant_object.diamant_search.diamant_view = 1; saveSession(); });
        Event.addListener('diamant_view_2', 'click', function() { selectDiamantView(2, diamant_object.diamant_search); diamant_object.diamant_search.diamant_view = 2; saveSession(); });
        Event.addListener('diamant_view_3', 'click', function() { selectDiamantView(3, diamant_object.diamant_search); diamant_object.diamant_search.diamant_view = 3; saveSession(); });
        Event.addListener('diamant_view_4', 'click', function() { selectDiamantView(4, diamant_object.diamant_search); diamant_object.diamant_search.diamant_view = 4; saveSession(); });
		
        Event.addListener('montura_view_1', 'click', function() { selectMonturaView(1, montura_object.montura_search); montura_object.montura_search.montura_view = 1; saveSession(); });
        Event.addListener('montura_view_2', 'click', function() { selectMonturaView(2, montura_object.montura_search); montura_object.montura_search.montura_view = 2; saveSession(); });
        Event.addListener('montura_view_3', 'click', function() { selectMonturaView(3, montura_object.montura_search); montura_object.montura_search.montura_view = 3; saveSession(); });

		showDiamantView(diamant_object.diamant_search.diamant_view);
		showMonturaView(montura_object.montura_search.montura_view);
		log('a');
		showDiamant(diamant_object);
		log('b');
		showMontura(montura_object, diamant_object.forma_id);
		log('c');
        Dom.removeClass('diamant_option_schimba_diamant', 'hidden');
        Dom.removeClass('montura_option_schimba_montura', 'hidden');
		
		Event.addListener('finalizare_button_cumpara_inel', 'click', buttonCumparaInel);
		
		initPanels();
		
		log('end initFinalizareMain');
	}; //var
	
	
	
	initFinalizareMain(); // Entry point for initFinalizare.
	
	log('end initFinalizare');
},

initCart = function()
{
	log('begin initCart()');

	var 
	i = 0,
	removeCartInelClosure = function(i) { this.execute = function() { removeCartInel(i); }; },
	removeCartDiamantClosure = function(i) { this.execute = function() { removeCartDiamant(i); }; },
	
	buttonCopiaza = function()
	{
		log('begin buttonCopiaza()');
		Dom.get('input_livrare_adresa1').value = Dom.get('input_adresa1').value;
		Dom.get('input_livrare_adresa2').value = Dom.get('input_adresa2').value;
		Dom.get('input_livrare_localitate').value = Dom.get('input_localitate').value;
		Dom.get('input_livrare_cod_postal').value = Dom.get('input_cod_postal').value;
		Dom.get('input_livrare_judet').selectedIndex = Dom.get('input_judet').selectedIndex;
		log('end buttonCopiaza');
	}; // var

	for (i = 0; i < session.cart_object.inel_list.length; i++)
	{
		Event.addListener('remove_inel_' + i, 'click', (new removeCartInelClosure(i)).execute);
	}

	for (i = 0; i < session.cart_object.diamant_list.length; i++)
	{
		Event.addListener('remove_diamant_' + i, 'click', (new removeCartDiamantClosure(i)).execute);
	}
	
	Event.addListener('button_copiaza', 'click', buttonCopiaza);

	var panel_instructiuni_bancare = new Panel('panel_instructiuni_bancare', { visible: false, width: '450px' });
	
	panel_instructiuni_bancare.render();
	
	Dom.removeClass('panel_container', 'hidden');
	
	Event.addListener('label_instructiuni_bancare', 'click', function() { panel_instructiuni_bancare.show(); });
	
	
	log('end initCart');
},

myLogConfig =
{
    width: '500px',
    height: '300px',
    verboseOutput: false,
    newestOnTop: false
},

initConsole = function()
{
    var myLogReader = new LogReader(null, myLogConfig);
},

showDiamantCuloare = function(i)
{
	var j = 0;
	Dom.setStyle('culoare_diamante', 'background-position', (-i * 320) + 'px 0px');

	for (j = 0; j <= 10; j++)
	{
		Dom.removeClass('buton_culoare_diamant_' + j, 'buton_culoare_diamant_selectat');
	}
	

	Dom.addClass('buton_culoare_diamant_' + i, 'buton_culoare_diamant_selectat');
},

showDiamantClaritate = function(i)
{
	var j = 0;
	Dom.setStyle('claritate_diamante', 'background-position', (-i * 243) + 'px 0px');	
	
	for (j = 0; j <= 10; j++)
	{
		Dom.removeClass('buton_claritate_diamant_' + j, 'buton_claritate_diamant_selectat');
	}
	

	Dom.addClass('buton_claritate_diamant_' + i, 'buton_claritate_diamant_selectat');
},

initClient = function()
{
    if (Cookie.get('console'))
	{
		initConsole();
	}

    log('begin initClient');
    initMenu();
    initSession();
    log('end initClient');
}; // var


