/*
Copyright (c) 2007, Mixam Com. All rights reserved.
Author: Effie Nadiv
version: 0.88.2
*/

/**
 * The display module provides a way to print a detaild invoice to the user.
 * @module display
 * @requires yahoo, event, dom, animation
 */

/**
 * @class MIXAM.Display
 * display.show(price)
 * display.hide([price])
 * display.hint(quantity, price, name)
 ********************************************************************************
 */

MIXAM.Display = function(owner, container, price, hint, errorDivElem){
    var tt = $("displayToolTip");
    this.owner = owner;
	this.element = container;
	this.panel = price;
    this.hint = hint;
    this.errorDiv = errorDivElem;
	this.weight = 0;
	this.copies = 0;
	this.name = "";
    if (tt){
        $D.removeClass(tt, "hidden");
        this.tt = new YAHOO.widget.Tooltip(tt, { context: container, autodismissdelay: 10000, zIndex: 101 });
    }
};

MIXAM.Display.prototype.show = function(value, weight, copies, name) {
	this.weight = weight;
	this.copies = copies;
	this.name = name;
	this.hide(value);
};

MIXAM.Display.prototype.dispError = function(message){
    var target = YAHOO.util.Dom.getXY(this.element),
	anim1 = new YAHOO.util.Motion(this.panel, { points: { to: [target[0], target[1] + 65] } }, 0.5),
        anim2 = new YAHOO.util.Motion(this.errorDiv, { points: { to: [target[0], target[1] + 26] } }, 0.75);

    if (message){
        $D.addClass(this.hint, "hidden");
        $D.addClass("loading", "hidden");
        anim1.setAttribute('useSeconds', false);
        anim1.animate();

        this.errorDiv.innerHTML = message;
        $D.removeClass(this.errorDiv, "hidden");
        $D.setXY(this.errorDiv, [target[0] + 180, target[1]]);
        anim2.setAttribute('useSeconds', false);
        anim2.animate();
    } else {
        $D.addClass(this.errorDiv, "hidden");
    }
};

MIXAM.Display.prototype.hide = function(value) {
	var target = YAHOO.util.Dom.getXY(this.element),
		self = this,
		anim = new YAHOO.util.Motion(this.panel, { points: { to: [target[0], target[1] + 65] } }, 0.25);

    this.dispError();
    $D.removeClass("loading", "hidden");
	if (value){
		anim.onComplete.subscribe( function() {
			self._show(value);
		});
	}
	anim.animate();
};

MIXAM.Display.prototype.promote = function(quantity, price, name) {
	var a = [MIXAM.regional.display.promote["get"], "quantity.1", "name.2", MIXAM.regional.display.promote["for"], "price.3"],
		target = YAHOO.util.Dom.getXY(this.element),
		anim = new YAHOO.util.Motion(this.hint, { points: { to: [target[0], target[1] + 48] } }, 0.25),
		rate = this.owner.getCurrency().rate,
		symbol = this.owner.getCurrency().symbol,
		formatedQuantity = MIXAM.util.Format.formatNumber(quantity, true);

	$D.setXY(this.hint, [target[0] + 180, target[1]]);
    this.dispError();

	a[1] = "<a id='jumpto' title='Offer " + formatedQuantity + " " + name.toLowerCase() + "' href='#'>" + formatedQuantity + "</a>";
	a[2] = name.toLowerCase();
	a[4] = symbol + MIXAM.util.Format.formatNumber(this.round(price/rate));
	this.hint.innerHTML = a.join(" ");
	anim.animate();
	$E.on('jumpto', "click", function(e){
		$E.stopEvent(e);
		this.owner.setQuantity(quantity);
	}, this, true);
};

MIXAM.Display.prototype._show = function(value) {
	var anim = new YAHOO.util.Motion(this.panel, { points: { to: YAHOO.util.Dom.getXY(this.element) } }, 0.5, YAHOO.util.Easing.easeOut),
		rate = this.owner.getCurrency().rate,
		symbol = this.owner.getCurrency().symbol,
		price = MIXAM.util.Format.formatNumber(this.round(value/rate));


	if (this.copies && this.tt){
		$('tt-name').innerHTML = this.name + MIXAM.regional.display.tooltip.multipostfix;
		$('tt-name2').innerHTML = this.name;
		$('tt-copies').innerHTML = MIXAM.util.Format.formatNumber2(this.copies, 0);
		$('tt-price').innerHTML = symbol + price;
		$('tt-price-single').innerHTML = symbol + MIXAM.util.Format.formatNumber2(value/rate/this.copies, 2);
		$('tt-weight').innerHTML = "" + MIXAM.util.Format.formatNumber2(this.weight, 2) + MIXAM.regional.display.tooltip.weight;
		$('tt-weight-single').innerHTML = "" + MIXAM.util.Format.formatNumber2(this.weight/this.copies, 3) + MIXAM.regional.display.tooltip.weight;
	}
	this.hint.innerHTML = "";
    $D.removeClass(this.hint, "hidden");
	$D.addClass("loading", "hidden");
    	$D.setY(this.panel, -65);
	this.panel.innerHTML = "<a id='flipCurrency' href='#' " +
		"title='" +  MIXAM.regional.display.panel.title + "' >" +
		 symbol + "</a>" + price ;
	anim.animate();
	$E.on('flipCurrency', "click", function(e){
		$E.stopEvent(e);
		this.owner.flipCcurrency();
	}, this, true);
};

MIXAM.Display.prototype.round = function(text){
	var f = parseFloat(text);

	return Math.round(f * 10) / 10;
};

