﻿/*

@requires: DrillSpot

*/

//YAHOO.util.Event.onDOMReady(
//    function(e) 
//    { 
//        DrillSpot.ui.selectTab('btn_desc','tab_desc');
//        Product.init();
//    });

var Product =
{   
    _imageFolder: "../../image/reviews/",
    _namespace: '',
    MainImage: null,
    Images: [],
    
    init : function()
    {
        this.setRating();

        var writeLink = document.getElementById('reviewButton');   
        YAHOO.util.Event.addListener(writeLink, "click", this.writeReview);

        var writeLink2 = document.getElementById('ctl00_c_ContentWindow_ccProductReviews_ccWriteReview2'); 
        YAHOO.util.Event.addListener(writeLink2, "click", this.writeReview);
    },
    
    setRating : function()
    {
        var avg = document.getElementById('ctl00_c_ContentWindow_ccBuyBox_ccRatingBox_ccAverage');   
        
        if (!avg) return;
        
        var s = parseFloat(avg.innerHTML);
        
        var stars = document.getElementById('stars');
        var stars2 = document.getElementById('stars2');
        
        var img = this._imageFolder + s.toString() + '_star.jpg';
        
        stars.src = img;
        stars2.src = img;
    },
    
    writeReview : function(e)
    {
        if (this.href.indexOf('#') < 0) return true;

        YAHOO.util.Event.preventDefault(e);

        e.cancelBubble = true;
        e.returnValue = true;

        DrillSpot.login.show();
        
        return false;
    },
    
    showNotifyWindow: function(e)
    {
        var w = document.getElementById('notifyWindow');
        DrillSpot.Dom.show(w);
        DrillSpot.Dom.setXY(w,e.clientX-150,e.clientY-150); 
    },
    
    hideNotifyWindow: function()
    {
        var w = document.getElementById('notifyWindow');
        DrillSpot.Dom.hide(w);
    }
};

function Gallery(mainImgID,thumbPanel,prevBtn,nextBtn,anim)
{
    this.MainImage = document.getElementById(mainImgID);
    this.ThumbPanel = document.getElementById(thumbPanel);
    this.PrevButton = document.getElementById(prevBtn);
    this.NextButton = document.getElementById(nextBtn);
    this.ThumbPanelXY = YAHOO.util.Dom.getXY(this.ThumbPanel);
    this.MainIndex = 0;
    this.MainSize = 600;
    this.Thumbs = [];
    this.ThumbIndex = 0;
    this.Sources = [];
    this.RowSize = 8;
    
//    YAHOO.util.Event.addListener(this.MainImage, 'load', function(e)
//    {
////        DrillSpot.Dom.visible(this);
////        DrillSpot.ui.fadeImageIn(this.id);
//    });
    
    this.setMain = function(src)
    {
        var g = this.gallery || this;
        var img = g.MainImage;
        
        //DrillSpot.Dom.invisible(img);
        img.src = src.replace(/_\d+\./, '_' + g.MainSize + '.');
    };
    
    this.hideThumbs = function()
    {
        var thumbs = this.Thumbs;
        for (var i = 0; i < thumbs.length; i++)
            DrillSpot.Dom.hide(thumbs[i]);
    };
    
    this.showThumbs = function(index)
    {
        var g = this;
        
        if (index >= g.RowSize) return;

        var thumbs = g.Thumbs;
        var srcs = g.Sources;
        var offset = g.ThumbIndex + index;

        if (!thumbs[index]) return;

        if (offset < g.Sources.length)
        {
            thumbs[index].src = srcs[offset];
            DrillSpot.Dom.visible(thumbs[index].container);
            DrillSpot.Dom.show(thumbs[index]);
        }
        else
        {
            DrillSpot.Dom.invisible(thumbs[index].container);
        }

        index++;
        Product.images.Gallery = g;
        window.setTimeout("Product.images.Gallery.showThumbs(" + index + ")", 100);
    };
    
    var scrollAnimate = function(adv)
    {
        var g = this;

        var xy = YAHOO.util.Dom.getXY(g.ThumbPanel);

        var x = xy[0] - (100 * adv);
        var y = xy[1];

        var w = DrillSpot.Dom.getWidth(g.ThumbPanel);

        var pad = 250;

        if (x + w >= (g.ThumbPanelXY[0] + pad) && x <= (w + g.ThumbPanelXY[0] - pad))
        {
            var scroll = new YAHOO.util.Motion(g.ThumbPanel, { points: { to: [x, y] } });
            scroll.duration = 0.5; 
            scroll.method = YAHOO.util.Easing.easeOut;
            scroll.animate();
        }
        else
        {
            var x1 = xy[0] - (20 * adv);
            var x2 = xy[0];
        
            var easeFn = function()
            {
                var ease = new YAHOO.util.Motion(g.ThumbPanel, { points: { to: [x2, y] } });
                ease.duration = 0.3; 
                ease.method = YAHOO.util.Easing.easeOut;
                ease.animate();
            };
        
            var ease1 = new YAHOO.util.Motion(g.ThumbPanel, { points: { to: [x1, y] } });
            ease1.duration = 0.3; 
            ease1.method = YAHOO.util.Easing.easeOut;
            ease1.onComplete.subscribe(easeFn);
            ease1.animate();
        }
    };
    
    var scrollReload = function(adv)
    {
        var g = this;

        var index = g.ThumbIndex + adv;

        if (index < 0) index = g.Sources.length - 1;

        index = index % g.Sources.length;

        g.ThumbIndex = index;

        g.hideThumbs();
        g.showThumbs(0);
    };
    
    if (anim)
    {
        this.advanceThumbIndex = scrollAnimate;
        this.RowSize = 20;
    }
    else
    {
        this.advanceThumbIndex = scrollReload;
        this.RowSize = 6;
    }
    
    if (this.PrevButton && this.NextButton)
    {
        this.PrevButton.gallery = this;
        YAHOO.util.Event.addListener(this.PrevButton, 'click', function(e) { this.gallery.advanceThumbIndex(-1); });
    
        this.NextButton.gallery = this;
        YAHOO.util.Event.addListener(this.NextButton, 'click', function(e) {  this.gallery.advanceThumbIndex(1); });
    }
}
Gallery.prototype.load = function(sources)
{
    for (var i = 0; i < sources.length; i++)
    {
        this.Sources[i] = sources[i];
        
        if (!this.ThumbPanel) continue;
        
        if (i < this.RowSize)
        {
            var div = document.createElement('div');

            DrillSpot.Dom.setClass(div,'product_thumbBlock');

            var img = document.createElement('img');
            img.setAttribute('src',sources[i]);
            img.container = div;

            div.appendChild(img);

            this.Thumbs[i] = img;
            this.ThumbPanel.appendChild(div);
            
            img.gallery = this;
            
            YAHOO.util.Event.addListener(img, 'mouseover', function(e)
            {
                this.gallery.setMain(this.src);
            });
        }
    }
    
    if (this.ThumbPanel)
    {
        if (sources.length <= this.RowSize)
        {
            DrillSpot.Dom.hide(this.PrevButton);
            DrillSpot.Dom.hide(this.NextButton);
        }
    }
}
Gallery.prototype.setThumbHandler = function(e,h)
{
    var thumbs = this.Thumbs;
    for (var i = 0; i < thumbs.length; i++)
        YAHOO.util.Event.addListener(thumbs[i], e, h);
}
Gallery.prototype.show = function(src)
{
    if (src) this.setMain(src);
    else this.setMain(this.Sources[this.MainIndex]);
}


Product.images =
{
    Gallery : null,
    
    init: function(sources)
    {
        var swin = document.getElementById('imageSuperWin');
        
        var big = new Gallery('imageSuper','superThumbs','superThumbPrev','superThumbNext',false);
        big.MainSize = 600;
        big.load(sources);
        
        YAHOO.util.Event.addListener(big.MainImage, 'click', function(e) { DrillSpot.Dom.hide(swin); });
        
        var showBig = function(e)
        {
            var x = DrillSpot.Dom.getCenterX(swin);
            var y = DrillSpot.Dom.getCenterY(swin);
            
            DrillSpot.Dom.setXY(swin, x, y);
            DrillSpot.Dom.show(swin);
            
            big.show(this.src);
        };
        
        var main = new Gallery('ctl00_c_ContentWindow_ccBuyBox_ccMainImage','mainThumbs','mainThumbPrev','mainThumbNext',false);
        main.MainSize = 300;
        main.load(sources);
        main.setThumbHandler('click', showBig); 
        
        if (sources.length > 1)
            YAHOO.util.Event.addListener(main.MainImage, 'click', showBig);
        
        main.show();
    }
};


YAHOO.util.Event.onDOMReady(
    function(e) 
    { 
        DrillSpot.ui.selectTab('btn_desc','tab_desc');
        Product.init();
        //Product.images.init();
    });