/**
 * EMV ajax paginator base js class.
 */

/**
 * Constructor.
 */
Emv_Paginator_Ajax = function (url, replaceElement, httpMethod, contentType) {

    this.url                   = url;
    this.replaceElement        = replaceElement;
    this.httpMethod            = httpMethod;
    this.contentType           = contentType;
    this.linkedPaginatorSlider = null;
    this.requestedPage         = 1;
    this.currentPage           = 1;
    this.pageCount             = 1;
    this.pageRange             = 10;
    this.id                    = null;
    this.params                = '';

    if (!this.httpMethod) {

        this.httpMethod = 'GET';
    }

    this.yuiConnectionCallback = {

      success: this.handleSuccess,
      failure: this.handleError,
      argument: [ this ]
    };
};

/**
 * Updates the target Url.
 */
Emv_Paginator_Ajax.prototype.setUrl = function (url) {

	this.url = url;
};

/**
 * Updates post params.
 */
Emv_Paginator_Ajax.prototype.setParams = function (params) {

	this.params = params;
};

/**
 * Link with a Emv.Paginator.Slider instance.
 */
Emv_Paginator_Ajax.prototype.linkPaginatorSlider = function (slider) {

	this.linkedPaginatorSlider = slider;
};

/**
 * Paging function.
 */
Emv_Paginator_Ajax.prototype.pageTo = function (page) {

	this.requestedPage = page;

    var request = YAHOO.util.Connect.asyncRequest(
        this.httpMethod,
        this.url + 'seite-' + page,
        this.yuiConnectionCallback,
        this.params);
};

/**
 * Set current page.
 */
Emv_Paginator_Ajax.prototype.setCurrentPage = function(p) {
    
	this.currentPage = p;
};

/**
 * Set page counter.
 */
Emv_Paginator_Ajax.prototype.setPageCount = function(c) {
    
	this.pageCount = c;
};

/**
 * Update the PageRange.
 *
 * @param {Object} pageRange
 */
Emv_Paginator_Ajax.prototype.setPageRange = function(pageRange) {
    
	this.pageRange = pageRange;
};

/**
 * Set paginator id.
 */
Emv_Paginator_Ajax.prototype.setId = function(id) {
    
	this.id = id;
};

/**
 * Rebuild html.
 */
Emv_Paginator_Ajax.prototype.rebuildHtml = function(){

    paginator = document.getElementById('paginator-' + this.id);
    if (paginator) {

        paginatorContainer = YAHOO.util.Dom.getElementsByClassName('paginatorNumbers', null, paginator);
        paginatorContainer[0].innerHTML = '';

        num = this.pageCount;
        if (num > this.pageRange) {

            num = this.pageRange;
        }

        for (i = 0; i < num; i++) {

            paginatorContainer[0].innerHTML += '<a href="javascript:paginator' + this.id + '.pageTo(\'' + (i + 1) + '\');" onfocus="this.blur();" class="paginatorLink">' + (i + 1) + '</a>';
            if (i < num - 1) {

                paginatorContainer[0].innerHTML += ' <small>|</small> ';
            }
        }

        if (num > 1) {

            paginator.style.display = 'block';
        } else {

            paginator.style.display = 'none';
        }
    }

    if (this.linkedPaginatorSlider) {

        this.linkedPaginatorSlider.reinit(parseInt(this.requestedPage,10));
    }
};

/**
 * Next page.
 */
Emv_Paginator_Ajax.prototype.pageNext = function() {
	
	if(this.pageCount <= this.currentPage) {
		
		return;
	}
	this.pageTo(++this.currentPage);
};

/**
 * Previous page.
 */
Emv_Paginator_Ajax.prototype.pagePrevious = function() {
	
	if(this.currentPage <= 1) {
		
		return;
	}
	this.pageTo(--this.currentPage);
};

/**
 * Yui connection manager success callback.
 */
Emv_Paginator_Ajax.prototype.handleSuccess = function (o) {

    paginator = o.argument[0];
    paginator.currentPage = paginator.requestedPage;

    var container = YAHOO.util.Dom.get(paginator.replaceElement);

    if (paginator.contentType == 'html') {

        container.innerHTML = o.responseText;

        //try to execute embedded js code from ajax response
        scriptCodes = container.getElementsByTagName('script');
        for (i = 0; i < scriptCodes.length; i++) {

            try {
                
            	eval(scriptCodes[i].innerHTML);
            } catch (e) {

            }
        }
    }
    if (paginator.contentType == 'json') {
        
    	var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1);
        var data = eval('(' + json + ')');
        //if controller returns no data - do not overwrite playlist with nothing...
        //so error == true is returned to skip this
        if('undefined' != typeof(data.error) && data.error === true) {
	        
        	return;
	    }
        container.innerHTML = data.html;
        eval(data.js);
    }

    if (paginator.linkedPaginatorSlider) {

    	paginator.linkedPaginatorSlider.reinit(parseInt(paginator.requestedPage));
    }
};

/**
 * Yui connection manager failure callback.
 */
Emv_Paginator_Ajax.prototype.handleError = function (o) {};
