/**
 * JavaScript by Morten Fangel // fangel
 * Copyright (C) IDG Denmark 2006
 *
 */

var PCW_UserRatingHandler = Class.create();
PCW_UserRatingHandler.prototype = {
    cdivs: null, select: null, form: null, container: null, placeholder: null,  no_comments_div: null,
    postback_url: null, nav_div: null, per_page: 5, pages: new Array(), currentlySelected: 0, currentPage: 1,

    initialize: function(cdivs, sel, form, container, postback_url, no_comments_div) {
        this.cdivs = $(cdivs);
        this.container = $(container);
        this.no_comments_div = $(no_comments_div);
        this.postback_url = postback_url;

        this.placeholder = window.document.createElement('div');
        //this.placeholder.setAttribute('id', 'mercuryUserCommentPlaceholder');
        this.placeholder.style.display = 'none';

        this.select = $(sel);
        this.form = $(form);
        if( this.form !== null && this.select !== null && cdivs !== null) {
            this.form.onsubmit = this.handleSubmit.bindAsEventListener(this);
            this.select.style.display = 'none';
            this.setupRatingDivs();
        }

        this.setupPages();
    },

    setupPages: function() {
        uc = new Array(); c = this.container;
        $A(c.getElementsByTagName('div')).each( function(div) { if(div.parentNode == c) { uc[uc.length] = div; } } );

        cp = 0;
        for(i=0;i<uc.length;i++) {
            if( i % this.per_page == 0 || i == 0 ) {
                this.pages[++cp] = window.document.createElement('div');
                if( i == 0 ) { this.pages[cp].appendChild(this.placeholder); }
            }
            this.pages[cp].appendChild(uc[i]);
        }

        if( this.pages.length == 0 ) { c.appendChild(this.placeholder); }

        for(i=1;i<this.pages.length;i++) {
            if(i != this.currentPage) { this.pages[i].style.display = 'none'; }
            c.appendChild(this.pages[i]);
        }

        this.createNavDiv(1);
    },

    setupRatingDivs: function() {
        inst = this;
        $A(this.cdivs.getElementsByTagName('div')).each( function(div) { div.onclick = inst.handleClick.bindAsEventListener(inst); div.style.cursor = 'pointer'; } );

    },

    handleClick: function(evt) {
        var div = evt.target ? evt.target : evt.srcElement;
        children = this.cdivs.getElementsByTagName('div');
        passed = false;

        for(i=0;i<children.length;i++) {
            children[i].className = (passed) ? 'rating-empty' : 'rating-filled';

            if( children[i] == div ) {
                this.currentlySelected = (i + 1);
                this.select.selectedIndex = this.currentlySelected;
                passed = true;
            }
        }
    },

    handleSubmit: function(evt) {
        if( $F(this.select) == 0 ) {
            alert('Du har ikke valgt nogen bedømmelse. Vælg venligst en bedømmelse ved at klikke på de røde kasser, placeret oven over tekstfeltet');
            return false;
        }

        var myAjax = new Ajax.Request(
					this.postback_url,
					{
						method: 'post',
						postBody: Form.serialize(this.form) + '&ajax=1',
						onSuccess: this.handleResponse.bind(this)

					});

        return false;
    },

    handleResponse: function(response) {
        if( response.getResponseHeader('Content-Type') != 'text/javascript' ) {
            this.placeholder.innerHTML = response.responseText;
            this.placeholder.style.display = 'block';

            new fx.Height(this.form, {duration: 1000}).toggle();

            if( this.no_comments_div !== null ) {
                new fx.Height(this.no_comments_div, {duration: 100}).toggle();
            }

            if( this.currentPage == 1 ) {
                cmnt = new fx.Height(this.placeholder, {duration: 2000});
                cmnt.hide();
                cmnt.toggle();
            } else {
                this.jumpToPage(1);
            }
        } else {
            new fx.Height(this.form, {duration: 1000}).toggle();
        }
    },

    handleNextPage: function() {
        if( this.currentPage <= (this.pages.length - 1) ) {
            this.jumpToPage(this.currentPage + 1);
        }
    },

    handlePrevPage: function() {
        if( this.currentPage != 1) {
            this.jumpToPage(this.currentPage - 1);
        }
    },

    jumpToPage: function(page) {
        if( this.pages[page] !== undefined ) {
            if( page != this.currentPage ) {
                this.pages[page].style.display = 'block';
                new_page = new fx.Height(this.pages[page], {duration: 1000});
                new_page.hide();

                new fx.Height(this.pages[this.currentPage], {duration: 1000, onComplete: function() { new_page.toggle(); }}).toggle();

                this.createNavDiv(page);
                this.currentPage = page;
            }
        }
    },

    createNavDiv: function(page) {
        if( this.nav_div === null ) {
            this.nav_div = window.document.createElement('div');
            this.nav_div.style.textAlign = 'center';
            this.container.appendChild(this.nav_div);
        }

        while( this.nav_div.hasChildNodes() ) { this.nav_div.removeChild(this.nav_div.firstChild); }

        if( page > 1 ) {
            prev_a  = window.document.createElement('a');
            prev_a.onclick = this.handlePrevPage.bindAsEventListener(this);
            prev_a.setAttribute('href','javascript:void(null);');
            prev_a.innerHTML = '&laquo; Forrige side';
            this.nav_div.appendChild(prev_a);
        }

        if( page > 1 && page < (this.pages.length - 1)) {
            this.nav_div.appendChild(window.document.createTextNode(' - '));
        }

        if( page < (this.pages.length - 1) ) {
            next_a  = window.document.createElement('a');
            next_a.onclick = this.handleNextPage.bindAsEventListener(this);
            next_a.setAttribute('href','javascript:void(null);');
            next_a.innerHTML = 'Næste side &raquo;';
            this.nav_div.appendChild(next_a);
        }
    }
}