
/*--------------------------------------------------.
|  Abstraktni trida pro objekty div z DOM           |
`--------------------------------------------------*/
/**
 * //!TODO!// prepsat pomoci Element.addMethods(); 
 *		viz: http://prototypejs.org/learn/extensions
 */

var DivElement = Class.create({

    initialize: function(div){
	if (typeof(div) == "object")
	    this.div = $(div);
    },

    setId: function(id){
	var span = document.createElement("span");
	span = $(span);
	span.setStyle({display : "none"});
	span.className = "object_id";
	span.innerHTML = id;
	this.div.appendChild(span);
    },
    
    setPrice: function(cena, dph){
    var span = document.createElement("span");
	span = $(span);
	span.setStyle({display : "block"});
	span.setStyle({paddingLeft : "50px"});
	span.setStyle({position : "relative"});
	span.setStyle({zIndex : "100"});
    },

    setAttribute : function(name, value){
	var old = $A(this.div.getElementsByClassName("object_" + name));
	if (old.length == 0){
	    var span = $(document.createElement("span"));
	    span.setStyle({display : "none"});
	    span.className = "object_" + name;
	    span.innerHTML = value;
	    this.div.appendChild(span);
	} else {
	    // uz tam neco je
	    old[0].innerHTML = value;
	}
    },

    getAttribute : function(div, name){
	var d = null;
	var n = "";
	if (typeof(div) == "object"){
	    d = $(div);	//!//CHANGE//ADDED//$()
	    n = name;
	} else {
	    d = this.div;
	    n = div;		// pouze jeden parametr (nazev atributu)
	}
	if (!d)
	    return "null";

	var objects = $A(d.getElementsByClassName("object_" + n));
	if (objects.length)
	    return objects.shift().innerHTML;
	return "null";
    },

    getId: function(){
	return (this.div)
	    ? $A(this.div.getElementsByClassName("object_id")).shift().innerHTML
	    : "null";
    },

    show: function(div){
	div.appendChild(this.div);
    },

    removeChildren: function(div){
	var d = null;
	if (typeof(div) == "object")
	    d = div;
	else
	    d = this.div;
	$A(d.childElements()).each(function(c){c.remove()});
    },

    /**
     * Pripoji element pouze jednou (nedela se seznam)
     */
    showOnce: function(div){
	this.removeChildren(div);
	this.show(div);
    },

    // vrati pomer w / h 
    _getProportion : function(w, h){ 
	return (Math.floor(parseFloat(w / h) * 10) / 10); 
    }
    
});



/*--------------------------------------------------.
|  Trida reprezentujici ikonu                       |
`--------------------------------------------------*/

var Icon = Class.create(DivElement, {

    // @param node uzel z xml dom prijateho ze serveru, nebo div z HTML
    initialize: function(node){
	if (!node){
	    this.div = null;
	    this.picture = "";
	} else if (node.nodeName.toLowerCase() == "icon"){

	    // jde o uzel z xml dom prijateho ze serveru
	    this.div = document.createElement("div");
	    this.div = $(this.div);
	    this.div.className = "icon";
	    this.colors = node.getAttribute("colors").evalJSON(true);
	    this.picture = this.colors["01"];
	    //this.picture = node.getAttribute("picture");
	    this.width = node.getAttribute("width");
	    this.height = node.getAttribute("height");
	    this.id = node.getAttribute("id");



	    //!//UPDATE
	    var img = $(document.createElement('img'));
	    img.src = path2pictures + this.picture;
	    var imgstyle = {
		width : this.width + 'px',
		height: this.height + 'px',
		margin : '0',
		padding : '0'
	    };

	    img.setStyle(imgstyle);
	    this.div.appendChild(img);
	    img.makePositioned();
	    this.img = img;

	    this.setId(node.getAttribute("id"));
	    this.setAttribute("picture", this.picture);
	    this.setAttribute("width", this.width);
	    this.setAttribute("height", this.height);
	    this.setAttribute("colors", node.getAttribute("colors"));
	    this.setStyle();
	    //!//UPDATE: zaznamenam pomer
	    //this.div.addClassName("icon-size-" + this.width + "x" + this.height);
	    this.div.addClassName("icon-size-" + this._getProportion(this.width, this.height));

	} else if (node.nodeName.toLowerCase() == "div") {
	    // ikona je vytvarena z jiz existujiciho divu
//	    this.div = $(document.createElement("div"));
	    this.div = new Element("DIV", {});
	    this.div.className = "icon";

	    this.colors = new String(this.getAttribute(node, "colors")).evalJSON(false);

//	    alert(node.innerHTML);
//	    alert($A($A($(node).select(".object_colors")).first().childElements()).inspect());
//	    alert(this.getAttribute(node, "colors"));

	    this.picture = this.getAttribute(node, "picture");
	    //this.picture = this.colors["01"];
	    this.width = this.getAttribute(node, "width");
	    this.height = this.getAttribute(node, "height");
	    this.id = this.getAttribute(node, "id");

	    //!//UPDATE
//	    var img = $(document.createElement('img'));
	    var img = new Element("IMG", {});
	    img.src = path2pictures + this.picture;
	    var imgstyle = {
		width : this.width + 'px',
		height: this.height + 'px',
		margin : '0',
		padding : '0'
	    };

	    img.setStyle(imgstyle);
//	    this.div.appendChild(img);
	    this.div.insert(img);
	    this.img = img;
	    img.makePositioned();

	    this.setAttribute("picture", this.picture);
	    this.setAttribute("width", this.width);
	    this.setAttribute("height", this.height);
	    this.setId(this.getAttribute(node, "id"));
	    this.setAttribute("colors", this.getAttribute(node, "colors"));
	    this.setStyle();

	    //!//UPDATE: zaznamenam pomer
	    //this.div.addClassName("icon-size-" + this.width + "x" + this.height);
	    this.div.addClassName("icon-size-" + this._getProportion(this.width, this.height));
	}
    },


    getPicture: function(){ return this.picture; },
    getWidth: function(){ return this.width + "px"; },
    getHeight: function(){ return this.height + "px"; },

    // ucini ikonu pretahovatelnou
    // @param o dodatecne vlastnosti pro objekt Draggable
    makeDraggable: function(o){
	var opt = {
	    revert : true,
	    ghosting : false
	};
	// pridam dodatecne vlastnosti
	for (var param in o){
	    opt[param] = o[param];
	}
	new Draggable(this.div, opt);
    },

    setStyle: function(o){
	var style = {
	    width : this.width + "px",
	    height : this.height + "px",
	    cursor : "move"
	};
	// pridam dodatecne vlastnosti
	for (var param in o)
	    style[param] = o[param];
	this.div.setStyle(style);
    },

    // zmeni barvu ikony
    changeColor : function(color){
	//!//UPDATE
	this.img.src = path2pictures + this.colors[color];
	this.setAttribute("picture", picture);
    },

    clone : function(){	return new Icon(this.div); }
});

//alert('Classes Loaded');