productViewer = Class.create();
Object.extend(productViewer.prototype, {

    initialize: function(elementid, cache, lang) {
        this.hash = document.location.hash.substring(1);
        this.href = document.location.href;
        if(this.href.indexOf('#') > 0)
        {
            this.href = this.href.substring(0,this.href.indexOf('#'));
        }
        this.language = lang;
        this.elementid = elementid;
        this.categoriecontent = null;
        this.products = new Array();
        this.productIds = new Array();
        this.productNrs = new Array();
        this.activeProductId = null;
        this.cache = cache;
        this.init();
    },

    isProductLoaded: function(productid)
    {
        if(this.productIds[productid] > -1)
        {
            return true;
        }
        return false;
    },

    addProduct: function(productid)
    {
        var posnr = this.productNrs.length;
        this.productNrs[posnr] = productid;
        this.productIds[productid] = posnr;
    },

    init: function()
    {
        var hashs = this.hash.toQueryParams();
        if(hashs['productid'])
        {
            this.showProduct(hashs['productid']);
        }
    },

    showNext: function(currentProductid)
    {
        var posnr = this.productIds[currentProductid];
        var nextid = 0;

        nextid = this.productNrs[posnr+1]
        if(!nextid)
        {
            nextid = this.productNrs[0];

        }
        this.showProduct(nextid);

    },

    showPrevious: function(currentProductid)
    {
        var posnr = this.productIds[currentProductid];
        var previd = 0;
        if(posnr > -1)
        {
            if(posnr == 0)
            {
                previd = this.productNrs[this.productNrs.length-1];
            }
            else
            {
                previd = this.productNrs[posnr-1];
            }

            this.showProduct(previd);
        }
    },

    showProduct: function(productid)
    {
        if(this.activeProductId)
        {
             document.getElementById('link_'+ this.activeProductId).className = '';
        }
        if(document.getElementById('link_'+ productid))
        {

            document.getElementById('link_'+ productid).className = 'activeProduct';
            this.activeProductId = productid;
        }

        if(productid == -1)
        {
            // restore first content (product-categorie description)
            if(this.categoriecontent != null)
            {
                document.getElementById(this.elementid).innerHTML = this.categoriecontent;
                document.location.replace(this.href+ '#');

                document.getElementById('pathtocategorie').style.display = '';
                document.getElementById('pathtoproduct').style.display = 'none';

                return true;
            }
            return false;
        }

        if(this.categoriecontent == null)
        {
            // save first content (product-categorie description)
            this.categoriecontent = document.getElementById(this.elementid).innerHTML;
        }

        if(!this.products[productid] || !this.cache)
        {
            // caching the result
            this.products[productid] = this.getProduct(productid);
        }

        document.getElementById(this.elementid).innerHTML = this.products[productid];
        document.getElementById(this.elementid).innerHTML.evalScripts();

        document.getElementById('pathproducttitle').innerHTML = document.getElementById('producttitle').innerHTML;
        document.getElementById('pathproducttitle').innerHTML = document.getElementById('producttitle').innerHTML;

        document.getElementById('pathtocategorie').style.display = 'none';
        document.getElementById('pathtoproduct').style.display = '';

        if(this.hash != '#productid='+ productid &&   this.hash != 'productid='+ productid)
        {
            document.location.replace(this.href + '#productid='+ productid);
        }
    },

    getProduct: function (productid)
    {
        var url  = absCorrect + languagedir + '/cnt_ajax_tpl_productdetail.html';
        var pars =  "objects.productid=" + productid +"&objects.language="+ this.language;
		var myAjax = new Ajax.Request(url,
                                        {method: 'post',
                                         parameters: pars,
                                         asynchronous: false
                                        }
                                      );
        return myAjax.transport.responseText;
    },

    getHashParams:function(){
        if(!this.hashParams || location.hash != this.hashStr){
            this.hashStr=location.hash;
            this.hashParams= new Array();
            if(location.hash.charAt(0)=="#"){
                var nv,params=location.hash.substring(1).split("&");
                for(var i=0;i<params.length;i++){
                    nv=params[i].split("=");
                    this.hashParams[nv[0]]=nv[1];
                }
            }
        }
        return this.hashParams;
    }


});

