varienSlider = Class.create();
    varienSlider.prototype = {
         template:null,
         loadedProduct:null,
         container:null,
         currentCategory:null,
         products:null,
         _products:null,
         currentPage:0,
         loadDataUrl:'',
         currentProduct:null,
         currentCategoryProductIds:'',
         templatesPattern: /(^|.|\r|\n)(__(.*?)__)/,
         onLoad:0,
         productIdsPosition:0,
         baseUrl:'',
         initialize: function(cur_product, curr_product_ids, load_url, base_url) {
            this.currentProduct=cur_product;
            this.idsArr = new Array();
            this.currId = 0;
            this.currCnt = 1;
            this.container = $('product_container');
            this.currentCategoryProductIds = curr_product_ids;
            this.baseUrl = base_url;
            this.loadDataUrl = load_url;

            this._products = '';
            this.products = $H(this._products);
            this.template = new Template(
                this.getInnerElement('template').innerHTML,
                this.templatesPattern
            );

            for (var i=0; i<this.currentCategoryProductIds.length; i++) {
                this.idsArr.push(this.currentCategoryProductIds[i]);
            }
            this.currId = this.idsArr[0];

         },

         insertItem :function (data) {
            Element.insert(this.container, {bottom: this.template.evaluate(data)});
         },

         getInnerElement: function(elementName) {
            return $('slider_item' + '-' + elementName);
         },

         click: function (id) {
            var product = this.products.get(id);
            $('prod_preview_image').style.backgroundImage = "url("+product.image+")";
            $('prod_desc').innerHTML = product.short_description;
            $('prod_name').innerHTML = product.name;

            // To adjust a bit glassbox height
            if ($('prod_name').offsetHeight > 30) {
                boxHeight = 150 - 26 + $('prod_name').offsetHeight + 'px';
            } else { 
                boxHeight = '150px';
            }
            $('contentBox').style.height = boxHeight;
            $('left').style.height = boxHeight;
            $('right').style.height = boxHeight;

            $('prod_price').innerHTML = product.price;
            infoGlassBox.appear();
            $('btn_addtocart').onclick=function(){ drawBox(product.base_url + 'cm/checkout_cart/addtocartpopup/product_id/'+id); }
            $('btn_moreinfo').onclick=function(){ setLocation(product.view_product_url); }
            this.currentProduct = id;

         },

         getPrevId : function (curr_id) {
            var nid = curr_id;
            for(n=0;n<this.idsArr.length;n++){
                if(this.idsArr[n] == curr_id && this.idsArr[n-1] > 0){
                    nid = this.idsArr[n-1]; this.currCnt -= 1;
                    break;
                }
            }
            $('p-count').innerHTML = this.currCnt+'-'+(this.currCnt+1);
            return nid;
         },

         next: function(){
            if(this.productIdsPosition < (this.idsArr.length-1)) {
                if((this.productIdsPosition+2) < this.idsArr.length) {
                    // retrieve next hiden product id
                    var hiddenNextProductId = this.idsArr[this.productIdsPosition+2];
                    // update current glider position
                    this.productIdsPosition++;
                    // update current product id
                    this.currId = this.idsArr[this.productIdsPosition];
                    if(this.products.get(hiddenNextProductId)) {
                        // if product exists
                        var glider = new Glider('glider', {duration:0.5});
                        this.currCnt++;
                        $('p-count').innerHTML = this.currCnt+'-'+(this.currCnt+1);
                        glider.next('sldr', this.currId);

                    } else {
                        // if product not exists
                        // load product data
                        this.getDataIds(Array(hiddenNextProductId));
                    }
                }
            }
            // alert(this.productIdsPosition);
            if(this.productIdsPosition == 0) {
                $('glider_prev').addClassName('disabled');
            } else {
                $('glider_prev').removeClassName('disabled');
            }
            if(this.productIdsPosition == (this.idsArr.length-2)) {
                $('glider_next').addClassName('disabled');
            } else {
                $('glider_next').removeClassName('disabled');
            }
         },
         prev: function() {
            if(this.productIdsPosition > 0) {
                this.productIdsPosition--;
                var prevId = this.getPrevProductId(this.currId);
                if(prevId != this.currId) {
                    var glider = new Glider('glider', {duration:0.5});
                    this.currId = this.getPrevId(this.currId);
                    glider.previous('sldr', this.currId);
                }
            }
            // alert(this.productIdsPosition);
            if(this.productIdsPosition == 0) {
                $('glider_prev').addClassName('disabled');
            } else {
                $('glider_prev').removeClassName('disabled');
            }
            if(this.productIdsPosition == (this.idsArr.length-2)) {
                $('glider_next').addClassName('disabled');
            } else {
                $('glider_next').removeClassName('disabled');
            }
         },
         getNextProductId: function(curr_id){
            var nid = curr_id;
            for(n=0;n<this.idsArr.length;n++){
                if(this.idsArr[n] == curr_id && this.idsArr[n+1] > 0){
                    nid = this.idsArr[n+1];
                    break;
                }
            }
            return nid;
         },
         getPrevProductId: function(curr_id){
            var nid = curr_id;
            for(n=this.idsArr.length-1; n>=0; n--){
                if(this.idsArr[n] == curr_id && this.idsArr[n-1] > 0){
                    nid = this.idsArr[n-1];
                    break;
                }
            }
            return nid;
         },


        getData: function(url) {
             new Ajax.Request(url, {
               parameters: false,
               method:'post',
               onComplete: this.insertData.bind(this)
            });
        },
        insertData: function(transport) {
             var result = $H(transport.responseText.evalJSON());
             result.each(function(item) {
                 if(typeof(item.value) != 'function'){
                    this.products.set(item.value.id,item.value);
                    this.insertItem(item.value);
                    if(this.onLoad<=0) {
                        this.currCnt++;
                        $('p-count').innerHTML = this.currCnt+'-'+(this.currCnt+1);
                        var glider = new Glider('glider', {duration:0.5});
                        glider.next('sldr', this.currId);
                    }
                 }
             }.bind(this));
             this.onLoad--;
        },
        disableButton: function (buttonId){

        },
        renderProducts: function() {
            var cnt = 0;
            this.products.each(function(item) {
            if(typeof(item.value) != 'function'){
               this.idsArr.push(item.value.id);
               this.products.set(item.value.id,item.value);
               this.insertItem(item.value);
            }
            }.bind(this));
            if(this.idsArr && this.currId == 0) this.currId = this.idsArr[0];
        },
        getDataIds: function(ids) {
                new Ajax.Request(this.loadDataUrl, {
                   parameters: {
                   product_ids: ids
                   },
                   method:'post',
                   onComplete: this.insertData.bind(this)
                });
        },
        loadFirstTwo: function() {
            this.onLoad = 1;
            this.getDataIds(this.currentCategoryProductIds[0]+','+this.currentCategoryProductIds[1]);
        }
    }
