Emv.Videofilm = {};

/*****************************/
/*****************************/

/**
 * Variations update class
 * Using Ajax to fire a search and update resultpage
 *
 * @param {string} url
 * @param {string} blockId
 * @param {string} carrouselId
 */
Emv.Videofilm.variations = function(url, blockId, carrouselId) {

	this.url         = url;
	this.blockId     = blockId;
	this.carrouselId = carrouselId;
	this.lastChecked = null;
	this.allOpen     = false;

    this.yuiConnectionCallback = {

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

/**
 * Starts the search
 *
 * @param {element} lastChecked
 */
Emv.Videofilm.variations.prototype.update = function(lastChecked) {

	this.lastChecked = lastChecked;

	varations_dvd    = document.getElementById(this.blockId + 'varations_dvd').checked;
	varations_bluray = document.getElementById(this.blockId + 'varations_bluray').checked;
	varations_other  = document.getElementById(this.blockId + 'varations_other').checked;
	varations_sell   = document.getElementById(this.blockId + 'varations_sell').checked;
	varations_rent   = document.getElementById(this.blockId + 'varations_rent').checked;

    var request = YAHOO.util.Connect.asyncRequest(
            'POST',
            this.url,
            this.yuiConnectionCallback,
            'dvd='     + varations_dvd +
            '&bluray=' + varations_bluray +
            '&other='  + varations_other +
            '&sell='   + varations_sell +
            '&rent='   + varations_rent
    );
};

/**
 * Ajax succes callback
 *
 * @param {Object} o
 */
Emv.Videofilm.variations.prototype.handleSuccess = function (o) {

    _this = o.argument[0];

    carrousel = Emv.Registry.getObject(_this.carrouselId);

    if (o.responseText != '') {

        carrousel.clearItems();
        carrousel.addItems(o.responseText);
    } else {

    	_this.lastChecked.checked = !_this.lastChecked.checked;
        Emv.VideodeJsoverlay.get('jsOverlay' + _this.blockId).show('Hinweis', 'Einschr&auml;nkung nicht m&ouml;glich, keine Fassungen gefunden.');
    }
};

/*****************************/
/*****************************/


/**
 * all releases search helper class
 *
 * @param {string} filterUrl
 * @param {string} filterReplaceId
 * @param {string} termField
 * @param {string} resultListId
 */
Emv.Videofilm.allreleasessearch = function(filterUrl, filterReplaceId, termField, resultListId) {

    this.filterUrl       = filterUrl;
    this.searchPaginator = null;
    this.termField       = termField;
    this.filterReplaceId = filterReplaceId;
    this.resultListId    = resultListId;
    this.filters         = new Array();
    this.lastFilters     = new Array();
    this.lastTerm        = '';
    this.order           = '';

    this.yuiConnectionFilterCallback = {

          success: this.handleFilterSuccess,
          argument: [ this ]
        };
};

/**
 * update form based on filters over ajax
 */
Emv.Videofilm.allreleasessearch.prototype.update = function(){

    element = document.getElementById(this.filterReplaceId);
    if (element) {

        element.innerHTML = element.innerHTML + '<img class="ajaxLoader" src="/jslib/int/ajax-loader.gif" />';
    }

    var request = YAHOO.util.Connect.asyncRequest(
            'POST',
            this.filterUrl,
            this.yuiConnectionFilterCallback,
            'filter=' + this.filters
    );
};

/**
 * Sets the paginator
 * @param {Object} searchPaginator
 */
Emv.Videofilm.allreleasessearch.prototype.setPaginator = function(searchPaginator) {

    this.searchPaginator = searchPaginator;
};

/**
 * Updates the order
 */
Emv.Videofilm.allreleasessearch.prototype.updateOrder = function(order){

    this.order = order;

    params = 'term=' + this.lastTerm + '&filter=' + this.lastFilters + '&order=' + this.order;

    if (this.searchPaginator) {

        element = document.getElementById(this.resultListId);
        if (element) {

            element.innerHTML = '<img class="ajaxLoader" src="/jslib/int/ajax-loader.gif" />' + element.innerHTML;
        }

        this.searchPaginator.setParams(params);
        this.searchPaginator.pageTo('1');
    } else {

        alert('searchPaginator not set');
    }
};

/**
 * starts the search
 */
Emv.Videofilm.allreleasessearch.prototype.search = function(){

    element = document.getElementById(this.termField);

    term = '';
    if (element) {

        term = element.value;
    }

    this.lastFilters = new Array();
    for(i = 0; i < this.filters.length; i++) {

        this.lastFilters[this.lastFilters.length] = this.filters[i];
    }

    this.lastTerm    = term;

    params = 'term=' + term + '&filter=' + this.filters + '&order=' + this.order;

    if (this.searchPaginator) {

        element = document.getElementById(this.resultListId);
        if (element) {

            element.innerHTML = '<img class="ajaxLoader" src="/jslib/int/ajax-loader.gif" />' + element.innerHTML;
        }

        this.searchPaginator.setParams(params);
        this.searchPaginator.pageTo('1');

        // Set tracking pixel
        Video.Statistics.setCountPixel();
    } else {

        alert('searchPaginator not set');
    }

    return false;
};

/**
 * Add a filter to stack.
 *
 * @param {string} cat
 * @param {int} id
 */
Emv.Videofilm.allreleasessearch.prototype.addFilter = function(cat, id){

    for (i = 0; i < this.filters.length; i++) {

        if ((this.filters[i][0] == cat) && (this.filters[i][1] == id)) {

            return false;
        }
    }

    this.filters[this.filters.length] = new Array(cat, id);
};

/**
 * Remove a filter from stack.
 *
 * @param {string} cat
 * @param {int} id
 */
Emv.Videofilm.allreleasessearch.prototype.removeFilter = function(cat, id){

    for (i = 0; i < this.filters.length; i++) {

        if ((this.filters[i][0] == cat) && (this.filters[i][1] == id)) {

            this.filters.splice(i,1);
        }
    }
};

/**
 * selects on filter and add to stack
 *
 * @param {string} cat
 * @param {int} id
 */
Emv.Videofilm.allreleasessearch.prototype.select = function(cat, id){

    this.addFilter(cat, id);
    this.update();
    this.search();
};

/**
 * remove element from stack
 *
 * @param {Object} cat
 * @param {Object} id
 */
Emv.Videofilm.allreleasessearch.prototype.remove = function(cat, id){

    this.removeFilter(cat, id);
    this.update();
    this.search();
};

/**
 * Ajax succes callback for filter
 *
 * @param {Object} o
 */
Emv.Videofilm.allreleasessearch.prototype.handleFilterSuccess = function (o) {

    _this = o.argument[0];

    element = document.getElementById(_this.filterReplaceId);
    if (element) {

        element.innerHTML = o.responseText;
    }
};

/*****************************/
/*****************************/

/**
 * all series search helper class
 *
 * @param {string} filterUrl
 * @param {string} filterReplaceId
 * @param {string} termField
 * @param {string} resultListId
 */
Emv.Videofilm.allseriesSearch = function(filterUrl, filterReplaceId, termField, resultListId) {

    this.filterUrl       = filterUrl;
    this.searchPaginator = null;
    this.termField       = termField;
    this.filterReplaceId = filterReplaceId;
    this.resultListId    = resultListId;
    this.filters         = new Array();
    this.lastFilters     = new Array();
    this.lastTerm        = '';
    this.order           = '';

    this.yuiConnectionFilterCallback = {

          success: this.handleFilterSuccess,
          argument: [ this ]
        };
};

/**
 * update form based on filters over ajax
 */
Emv.Videofilm.allseriesSearch.prototype.update = function(){

    element = document.getElementById(this.filterReplaceId);
    if (element) {

        element.innerHTML = element.innerHTML + '<img class="ajaxLoader" src="/jslib/int/ajax-loader.gif" />';
    }

    var request = YAHOO.util.Connect.asyncRequest(
            'POST',
            this.filterUrl,
            this.yuiConnectionFilterCallback,
            'filter=' + this.filters
    );
};

/**
 * Sets the paginator
 * @param {Object} searchPaginator
 */
Emv.Videofilm.allseriesSearch.prototype.setPaginator = function(searchPaginator) {

    this.searchPaginator = searchPaginator;
};

/**
 * Updates the order
 */
Emv.Videofilm.allseriesSearch.prototype.updateOrder = function(order){

    this.order = order;

    params = 'term=' + this.lastTerm + '&filter=' + this.lastFilters + '&order=' + this.order;

    if (this.searchPaginator) {

        element = document.getElementById(this.resultListId);
        if (element) {

            element.innerHTML = '<img class="ajaxLoader" src="/jslib/int/ajax-loader.gif" />' + element.innerHTML;
        }

        this.searchPaginator.setParams(params);
        this.searchPaginator.pageTo('1');
    } else {

        alert('searchPaginator not set');
    }
};

/**
 * starts the search
 */
Emv.Videofilm.allseriesSearch.prototype.search = function(){

    element = document.getElementById(this.termField);

    term = '';
    if (element) {

        term = element.value;
    }

    this.lastFilters = new Array();
    for(i = 0; i < this.filters.length; i++) {

        this.lastFilters[this.lastFilters.length] = this.filters[i];
    }

    this.lastTerm    = term;

    params = 'term=' + term + '&filter=' + this.filters + '&order=' + this.order;

    if (this.searchPaginator) {

        element = document.getElementById(this.resultListId);
        if (element) {

            element.innerHTML = '<img class="ajaxLoader" src="/jslib/int/ajax-loader.gif" />' + element.innerHTML;
        }

        this.searchPaginator.setParams(params);
        this.searchPaginator.pageTo('1');

        Video.Statistics.setCountPixel();
    } else {

        alert('searchPaginator not set');
    }

    return false;
};

/**
 * Add a filter to stack.
 *
 * @param {string} cat
 * @param {int} id
 */
Emv.Videofilm.allseriesSearch.prototype.addFilter = function(cat, id){

    for (i = 0; i < this.filters.length; i++) {

        if ((this.filters[i][0] == cat) && (this.filters[i][1] == id)) {

            return false;
        }
    }

    this.filters[this.filters.length] = new Array(cat, id);
};

/**
 * Remove a filter from stack.
 *
 * @param {string} cat
 * @param {int} id
 */
Emv.Videofilm.allseriesSearch.prototype.removeFilter = function(cat, id){

    for (i = 0; i < this.filters.length; i++) {

        if ((this.filters[i][0] == cat) && (this.filters[i][1] == id)) {

            this.filters.splice(i,1);
        }
    }
};

/**
 * selects on filter and add to stack
 *
 * @param {string} cat
 * @param {int} id
 */
Emv.Videofilm.allseriesSearch.prototype.select = function(cat, id){

    this.addFilter(cat, id);
    this.update();
    this.search();
};

/**
 * remove element from stack
 *
 * @param {Object} cat
 * @param {Object} id
 */
Emv.Videofilm.allseriesSearch.prototype.remove = function(cat, id){

    this.removeFilter(cat, id);
    this.update();
    this.search();
};

/**
 * Ajax succes callback for filter
 *
 * @param {Object} o
 */
Emv.Videofilm.allseriesSearch.prototype.handleFilterSuccess = function (o) {

    _this = o.argument[0];

    element = document.getElementById(_this.filterReplaceId);
    if (element) {

        element.innerHTML = o.responseText;
    }
};

/*****************************/
/*****************************/

/**
 * new releases update class
 * Using Ajax to fire a search and update resultpage
 *
 * @param {string} urlPrefix
 * @param {string} urlSuffix
 * @param {string} paginatorId
 */
Emv.Videofilm.newreleases = function(urlPrefix, urlSuffix, paginatorId) {

    this.urlPrefix   = urlPrefix;
    this.urlSuffix   = urlSuffix;
    this.paginatorId = paginatorId;
};

/**
 * switch to sort xy
 *
 * @param {string} sort
 */
Emv.Videofilm.newreleases.prototype.gotoSort = function(sort){

    eval(this.paginatorId + ".setUrl('" + this.urlPrefix + sort + this.urlSuffix + "');");
    eval(this.paginatorId + ".pageTo('1');");
};

/**
 * Switch to kw.
 *
 * @param {string} kw
 * @param {string} sortOrder
 */
Emv.Videofilm.newreleases.prototype.gotoKw = function(kw, sortOrder){

    eval(this.paginatorId + ".setUrl('" + this.urlPrefix + kw + '/' + sortOrder + this.urlSuffix + "');");
    eval(this.paginatorId + ".pageTo('1');");

    // If change in kw is possible/done: reset tracking pixel
    if( 'undefined' != typeof(kw) && kw != '' && kw != null ) {

        Video.Statistics.setCountPixel();
    }
};

/**
 * Moves one element backwards in selectbox.
 *
 * @param {object} selectBox
 * @param {string} sortOrder
 */
Emv.Videofilm.newreleases.prototype.gotoPrevKw = function(selectBox, sortOrder) {

    select = document.getElementById(selectBox);
    if (select.selectedIndex > 0) {

        this.gotoKw(select.options[select.selectedIndex - 1].value, sortOrder);
    }
};

/**
 * Moves one element forward in selectbox.
 *
 * @param {object} selectBox
 * @param {string} sortOrder
 */
Emv.Videofilm.newreleases.prototype.gotoNextKw = function(selectBox, sortOrder) {

    select = document.getElementById(selectBox);
    if (select.selectedIndex < select.options.length - 1) {

        this.gotoKw(select.options[select.selectedIndex + 1].value, sortOrder);
    }
};
