Element.addMethods({
  visibleRecursive: function(element) {
    element = $(element);
    while (element != document.body)
    {
        if (!element.visible())
            return false;

        element = element.up();
    }

    return true;
  }
});






RatingStars = Class.create({

   initialize: function(container)
   {
	   this.domContainer = $(container);

	   this.domContainer.observe('mouseover', this.onMouseOver.bindAsEventListener(this));
	   this.domContainer.observe('mouseout', this.onMouseOut.bindAsEventListener(this));
   },


   onMouseOver: function(event)
   {
	   domStar = event.element();
	   this.domContainer.addClassName('rating-hover-'+domStar.readAttribute('star_value'));
   },

   onMouseOut: function(event)
   {
	   domStar = event.element();
	   this.domContainer.removeClassName('rating-hover-'+domStar.readAttribute('star_value'));
   }
});







Rating = Class.create({
    initialize: function(container)
    {
        this.domContainer = $(container);
	    this.stars = new RatingStars(this.domContainer.down('.rating'));

	    this.stars.domContainer.observe('click', this.onStarClicked.bindAsEventListener(this));

    },

    onStarClicked: function(event)
    {
        event.stop();

	    var domStar = event.element();
        new Ajax.Request(domStar.href, {method: 'get', onSuccess: this.onFormLoad.bind(this)});
    },

	onFormLoad: function(res)
	{
		new Popup({html: res.responseText});
	}
});


var Popup = Class.create({
    initialize: function(options)
    {
        //$$('.popup').each(function(e) {e.fire('popup:close')});


        this.options = options;
        this.initDom();

        this.onUpdate();
    },

    initDom: function()
    {
        try {

        this.domPopup = new Element('div', {'class': 'popup'});

        /*table workaround*/
        var domTable = new Element('table', {cellspacing: '0', cellpadding: '0', border: '0'}); this.domPopup.appendChild(domTable);
        var domTbody = new Element('tbody'); domTable.appendChild(domTbody);
        var domRow = new Element('tr'); domTbody.appendChild(domRow);
        var domCell = new Element('td'); domRow.appendChild(domCell);


        this.domHeader = new Element('div', {'class': 'p_header'});
        domCell.appendChild(this.domHeader);


        this.domCloseLink = new Element('a', {href:'javascript:void(0)', 'class':'p_close'});
        this.domCloseLink.innerHTML = 'close';
        this.domHeader.appendChild(this.domCloseLink);

        this.domTitle = new Element('span', {'class':'p_title'});
        this.domTitle.update(this.options.title || 'Popup');
        this.domHeader.appendChild(this.domTitle);

        this.domBody = new Element('div', {'class': 'p_body'});
        domCell.appendChild(this.domBody);


        if (this.options.html)
            this.domBody.update(this.options.html); //prevent evaling scripts



        this.domOverlay = new Element('div', {'class': 'overlay'});


        /*workaround to get body height*/
        var domTmp = new Element('div');
        document.body.appendChild(domTmp);
        var tmpOffset = domTmp.cumulativeOffset();
        domTmp.remove();
        /*workaround to get body height |end */
        this.domOverlay.style.height = tmpOffset.top+'px';


        $(document.body).appendChild(this.domOverlay);
        $(document.body).appendChild(this.domPopup);



        this.domCloseLink.observe('click', this.close.bind(this));
        this.domOverlay.observe('click', this.close.bind(this));
        this.domPopup.observe('popup:close', this.close.bind(this));
		this.domPopup.observe('popup:update', this.onUpdate.bind(this));
        
        
        this.onDocumentKeydownBound = this.onDocumentKeydown.bindAsEventListener(this);
        Event.observe(document, 'keydown', this.onDocumentKeydownBound);

        
        }
        catch (e) {console.log(e)}
    },

	onUpdate: function(event)
	{
		var domH1Title = this.domBody.down('h1.popup-title');
		if (domH1Title)
		{
			this.domTitle.update(domH1Title.innerHTML);
			domH1Title.remove();
		}

		if (this.domBody.getHeight() < 100)
        {
            this.domBody.style.paddingTop = '30px';
            this.domBody.style.paddingBottom = '30px';
        }
		else
		{
			this.domBody.style.paddingTop = '';
            this.domBody.style.paddingBottom = '';
		}



		//center
		this.center();
	},


    center: function()
    {
        var viewportSize = document.viewport.getDimensions();

        /* fix the vertical dimension */
//        var domTmp = new Element('div', {style: 'position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 0; padding: 0; border: none;'});
//        document.body.appendChild(domTmp);
//        viewportSize.height = domTmp.getHeight();
//        viewportSize.width = domTmp.getWidth();
//        domTmp.remove();
        /* fix the vertical dimension |end */



        var popupSize = this.domPopup.getDimensions();




        var targetViewportOffset = [(viewportSize.width-popupSize.width)/2, (viewportSize.height-popupSize.height)/2];
        var currentViewportOffset = this.domPopup.viewportOffset();

        var currentDocumentOffset = this.domPopup.cumulativeOffset();
        var targetDocumentOffset = [
        targetViewportOffset[0]-currentViewportOffset[0]+currentDocumentOffset[0],
        targetViewportOffset[1]-currentViewportOffset[1]+currentDocumentOffset[1]
        ];


        if (targetDocumentOffset[1] < 10)
        	targetDocumentOffset[1] = 10; //do not show popup above the screen %)
        
        var styleLeft = targetDocumentOffset[0]+'px';
        var styleTop = targetDocumentOffset[1]+'px';


        this.domPopup.style.left = styleLeft;
        this.domPopup.style.top = styleTop;
    },


    onDocumentKeydown: function(event)
    {
    	if (event.keyCode == 27)
    	{
    		this.close();
    		event.stop(); //prevent closing other popups
    	}
    },
    
    close: function()
    {
        this.domPopup.remove();
        this.domOverlay.remove();
        
        Event.stopObserving(document, 'keydown', this.onDocumentKeydownBound);
    },

    isClosed: function()
    {
    	return !!this.domPopup.parentNode;
    }
});



