var QuickView = Class.create();
QuickView.prototype = {
    initialize: function(container, urlView, viewWidth, viewHeight) {
        this.container = container;
        this.urlView = urlView;
        this.loadWaiting = false;
        this.response = [];
        
        var heightBody = 0;
        var widthBody = 0;
        
        if(typeof(window.innerHeight) == 'number') {
            heightBody = Math.round(window.innerHeight);
            widthBody = Math.round(window.innerWidth);
        } else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) {
            heightBody = Math.round(document.documentElement.clientHeight);
            widthBody = Math.round(document.documentElement.clientWidth);
        } else if (document.body && ( document.body.clientWidth || document.body.clientHeight )) {
            heightBody = Math.round(document.body.clientHeight);
            widthBody = Math.round(document.body.clientWidth);
        }
        
        var top = Math.round((heightBody - (viewHeight+20))/2);
	var left = Math.round((widthBody - (viewWidth+20))/2);
        
        $$('#' + container + ' div.magefox-quickview').each(function(element){
                element.setStyle({
                        left: left + 'px',
                        top: top + 'px'
                    });
            });
        
        $('quickview-ajax-loader').setStyle({
                left: ((widthBody-180)/2) + 'px',
                top: ((heightBody-40)/2) + 'px'
            });
    },
    
    viewProduct: function(id) {
        $$('#' + this.container + ' div.magefox-quickview').each(function(element){
                element.hide();
            });
        
        if(this.response[id]) {
            $('magefox-quickview-' + id).show();
        } else {
            this.setLoadWaiting(true);
            var url = this.urlView + 'id/' + id;
            new Ajax.Request(url, {
                    method: 'get',
                    onComplete: this.resetLoadWaiting.bind(this),
                    onSuccess: this.processRespone.bind(this)
                });
        }
    },
    
    hideProduct: function(id) {
        $('magefox-quickview-' + id).hide();
    },
    
    setLoadWaiting: function(flag) {
        this.loadWaiting = flag;
        if(flag == true) {
            $('quickview-ajax-loader').show();
        } else {
            $('quickview-ajax-loader').hide();
        }
    },
    
    resetLoadWaiting: function() {
        this.setLoadWaiting(false);
    },
    
    processRespone: function(transport) {
        var response;
        if (transport && transport.responseText){
            try{
                response = eval('(' + transport.responseText + ')');
            }
            catch (e) {
                response = {};
            }
        }
        
        if(response.id) {
            this.response[response.id] = response.html;
            $('magefox-quickview-' + response.id).update(response.html);
            $('magefox-quickview-' + response.id).show();
        }
    },
    
    changeMedia: function(id, title, smallImage, largeImage) {
        var smallImageContainer = $('quickview-media-' + id);
        smallImageContainer.setAttribute('title', title);
        smallImageContainer.setAttribute('src', smallImage);
        var popString = 'popWin("' + largeImage + '", "gallery", "width=400,height=600,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes"); return false;';
        $('quickview-tool-' + id).setAttribute('onclick', popString);
    },
    
    addToCart: function(id) {
        var productAddToCartForm = new VarienForm(id);
        var validator = new Validation(id);
        if(validator.validate()) {   
            productAddToCartForm.submit();
        }
    }
}
