/**
* module dependences
*/

if(typeof WBSJSBASE == 'undefined')
throw('WBSPRODUCT requires the WBSJSBASE JavaScript Class');

/**
* WBSPRODUCT Class
*/
var WBSPRODUCT = new Class.create();
WBSPRODUCT.prototype = Object.extend(WBSPRODUCT.prototype, WBSJSBASE);
WBSPRODUCT.prototype = Object.extend(WBSPRODUCT.prototype, {
    initialize: function(options) {
        /* setting module's name */
        this.moduleName = 'WBSPRODUCT';
        /* include module's css file */
        this.addCSS();

        /* event references */
        this.events = {
            onload: this.onload.bindAsEventListener(this),
            getDetailsUrl: this.getDetailsUrl.bindAsEventListener(this),
            getPage: this.getPage.bind(this),
            getPremieres: this.getPremieres.bind(this),
            showCommingSoon: this.showCommingSoon.bind(this),
            search: this.search.bind(this),
            showCategory: this.showCategory.bind(this)
        };

        /* default options */
        this.options = {
            container: 'wbsproduct_container',
            section: 'wbsproduct_section',
            pagerClassName: 'wbsproduct_pager',
            oveloadClassName: 'wbsproduct_overload'
        };

        this.setOptions(options);

        this.checkContainer();

        this.pager = new WBSPAGER({classToDraw: this.options.pagerClassName,goPageCallBack: this.events.getPage}) || false;

        this.category = this.rowsPeerPage = false;
        this.page = this.pages = 1;
    },

    checkContainer: function(){
        if((typeof $(this.options.container) != 'undefined') && (!Element.hasClassName(this.options.container,'wbsproduct_container')))
        Element.addClassName(this.options.container,'wbsproduct_container');

        return(typeof $(this.options.container) != 'undefined');
    },

    checkCategoryContainer: function(){
        return(typeof $(this.options.section) != 'undefined');
    },

    getPage: function(page){

        if(!this.checkContainer())
        return false;

        this.pager.options.goPageCallBack=this.events.getPage;

        this.showWaiting();

        page = (isNaN(page))?1:page;
        var pars = 'action=getpage&page='+page+'&id_category='+this.category;

        var myAjax = new Ajax.Updater(
        {success: this.options.container},
        this.getBasePath(),
        {
            method: 'get',
            parameters: pars,
            onComplete: this.afterGetProductsProcess.bind(this),
            onFailure: this.hideWaiting.bind(this)
        });
    },

    afterGetProductsProcess: function(httprequest,jsonrequest){
        this.updateProducts();
        this.addButtonsEvents();
        this.fixDesignLastProduct();
        this.hideWaiting();
        Element.scrollTo(document.body);
        this.pager.showPager(jsonrequest);
    },

    updateProducts: function(){
        /* proccess buttons actions */
        $A(document.getElementsByClassName('wbsproduct_form')).each(function(product){
            this.updateProduct(product);
        }.bind(this));
    },

    addButtonsEvents: function(){
        WBSCart.addButtonsEvents(this.getRentButtons(),'rent');
        WBSCart.addButtonsEvents(this.getNewbuyButtons(),'newbuy');
        WBSCart.addButtonsEvents(this.getUsedbuyButtons(),'usedbuy');
    },

    getRentButtons: function(){
        return $A(document.getElementsByClassName('wbsproduct_btn_rent'));
    },

    getNewbuyButtons: function(){
        return $A(document.getElementsByClassName('wbsproduct_btn_newbuy'));
    },

    getUsedbuyButtons: function(){
        return $A(document.getElementsByClassName('wbsproduct_btn_usedbuy'));
    },

    updateProduct: function(product){
        /* add details actions */
        var imgDetail = $A(Form.getInputs(product, 'image','imgdetails')).first();
        if(imgDetail)
        Event.observe(imgDetail, 'click', this.events.getDetailsUrl, false);

        var btnDetail = $A(Form.getInputs(product, 'image','btndetails')).first();
        if(btnDetail)
        Event.observe(btnDetail, 'click', this.events.getDetailsUrl, false);
    },

    fixDesignLastProduct: function(){
        var products = $A(document.getElementsByClassName('contenmedprod',$(this.options.container)));
        if(Element.hasClassName(products.last(),'contenmedprod'))
        {
            Element.removeClassName(products.last(),'contenmedprod');
            Element.addClassName(products.last(),'contenmedprodver');
        }
    },

    showWaiting: function(){
        this.showOverload(this.options.container);
    },

    hideWaiting: function(){
        this.hideOverload();
    },

    getCategory: function(){
        var result = '';

        var category = location.search.match(/\?.*id_category=([0-9]*)/i);
        if(category && !isNaN(category[1])){
            result = category[1];
        }

        return result;
    },

    onload: function(evt){
        if(Element.hasClassName($(this.options.container),'wbsproduct_showonload'))
        this.showCategory();
    },

    showCategory: function(category){
        if(!this.checkCategoryContainer()) return false;
        this.showWaiting();

        this.category = (isNaN(category))?this.getCategory():category;
        var pars = 'action=getcategoryname&id_category='+this.category;

        var myAjax = new Ajax.Updater(
        {success: this.options.section},
        this.getBasePath(),
        {
            method: 'get',
            parameters: pars,
            onComplete: this.hideWaiting.bind(this),
            onFailure: this.hideWaiting.bind(this)
        });

        this.getPage();
    },

    go2url: function(httprequest,jsonrequest){
        if(typeof jsonrequest != 'undefined' && jsonrequest.href)
        location.href = jsonrequest.href;
    },

    getDetailsUrl: function(evt){
        var btn = Event.element(evt);
        var pars = 'action=getdetailsurl&'+Form.serialize(btn.form);

        var myAjax = new Ajax.Request(
        this.getBasePath(),
        {
            method: 'get',
            parameters: pars,
            onComplete: this.go2url.bind(this)
        });

        Event.stop(evt);
    },

    getButtonActionById: function(operation,id){
        var btn = false;
        $A(document.getElementsByClassName('wbsproduct_form')).each(function(product){
            var btnid = $A(Form.getInputs(product, 'hidden','id')).first();
            if(btnid.value == id)
            {
                switch(operation)
                {
                    case 'rent':
                    btn = $A(Form.getInputs(product, 'image','btnrent')).first();
                    break;
                    case 'newbuy':
                    btn = $A(Form.getInputs(product, 'image','btnnewbuy')).first();
                    break;
                    case 'usedbuy':
                    btn = $A(Form.getInputs(product, 'image','btnusedbuy')).first();
                    break;
                }
            }
        });

        return btn;
    },

    getRefs: function(id){
        if(isNaN(id)) return false;
        this.showWaiting();

        var pars = 'action=getref&id='+id;

        var myAjax = new Ajax.Updater(
        {success: this.options.container},
        this.getBasePath(),
        {
            method: 'get',
            parameters: pars,
            onComplete: this.afterGetProductsProcess.bind(this),
            onFailure: this.hideWaiting.bind(this)
        });
    },

    getPremieres: function(page){
        page = (isNaN(page))?1:page;

        this.pager.options.goPageCallBack=this.events.getPremieres;

        this.showWaiting();

        this.showMonthName($('premiere_start').value.split('/')[1],$('premiere_start').value.split('/')[2],$('premiere_end').value.split('/')[1],$('premiere_end').value.split('/')[2]);

        var premiere_start = $('premiere_start').value.split('/').reverse().join('');
        var premiere_end = $('premiere_end').value.split('/').reverse().join('');

        var pars = 'action=getpremieres&page='+page+'&premiere_start='+premiere_start+'&premiere_end='+premiere_end;

        var myAjax = new Ajax.Updater(
        {success: this.options.container},
        this.getBasePath(),
        {
            method: 'post',
            parameters: pars,
            onComplete: this.afterGetProductsProcess.bind(this),
            onFailure: this.hideWaiting.bind(this)
        });
    },

    showMonthName: function(smonth,syear,emonth,eyear){
        this.showWaiting();

        var pars = 'action=getmonthname&smonth='+smonth+'&syear='+syear+'&emonth='+emonth+'&eyear='+eyear;

        var myAjax = new Ajax.Updater(
        {success: this.options.section},
        this.getBasePath(),
        {
            method: 'get',
            parameters: pars,
            onComplete: this.hideWaiting.bind(this),
            onFailure: this.hideWaiting.bind(this)
        });
    },

    top10: function(){
        this.updateProducts();
        this.addButtonsEvents();
    },

    showCommingSoon: function(page){
        page = (isNaN(page))?1:page;

        this.pager.options.goPageCallBack=this.events.showCommingSoon;

        this.showWaiting();

        var pars = 'action=getcommingsoon&page='+page;

        var myAjax = new Ajax.Updater(
        {success: this.options.container},
        this.getBasePath(),
        {
            method: 'post',
            parameters: pars,
            onComplete: this.afterGetProductsProcess.bind(this),
            onFailure: this.hideWaiting.bind(this)
        });
    },

    search: function(page){
        page = (isNaN(page))?1:page;

        this.pager.options.goPageCallBack=this.events.search;

        this.showWaiting();

        var pars = 'action=search&page='+page+'&'+Form.serialize('wbsproduct_search');

        var myAjax = new Ajax.Updater(
        {success: this.options.container},
        this.getBasePath(),
        {
            method: 'get',
            parameters: pars,
            onComplete: this.afterGetProductsProcess.bind(this),
            onFailure: this.hideWaiting.bind(this)
        });
    }
});

/**
* class initialization
*/
var WBSProduct = new WBSPRODUCT();
Event.observe(window, 'load', WBSProduct.events.onload, false);