﻿//Special offers

var offerCurrentItem = 1;
var offerListCount = 0;
var i = 0;
var offerList, offerListItem, offerListHeight, offerViewHeight, offerViewCount;
    

Event.observe(window, 'load', function() {
    offerListHeight = 0;
    offerViewHeight = 0;
    
    offerListCount = $('offerList').childElements().length; 
    offerListHeight = ($('offerList').down('li')) ? $('offerList').down('li').getHeight() : 1;
    offerViewHeight = ($('specialOffersView')) ? $('specialOffersView').getHeight() : 1;
    offerViewCount = Math.floor(offerViewHeight / offerListHeight);
});

var isMovingUp = null;
function scrollUp(offerScrollCount) {
    if (isMovingUp == null) {
        if (offerCurrentItem > 1){
            if (offerCurrentItem - offerScrollCount < 1) {
                offerScrollCount = offerCurrentItem - 1;
            }
            var moveHeight = offerScrollCount * offerListHeight;
            isMovingUp = new Effect.Move('offerList', {x:0, y:moveHeight, mode: 'relative', afterFinish:function(){isMovingUp = null}, queue: { position: 'end', scope: 'offerListScope' }});
          
            offerCurrentItem -= offerScrollCount;
        }
    }
}

var isMovingDown = null;
function scrollDown(offerScrollCount) {
    if (isMovingDown == null) {
        if (offerCurrentItem < offerListCount){
            //Make sure the scroller won't go past the last item in the list
            if (offerCurrentItem + offerScrollCount + offerViewCount - 1 > offerListCount){
                offerScrollCount = offerListCount - (offerCurrentItem + offerViewCount) + 1;
            }
            var moveHeight = 0 - (offerScrollCount * offerListHeight);
            
            isMovingDown = new Effect.Move('offerList', {x:0, y:moveHeight, mode: 'relative', afterFinish:function(){isMovingDown = null}, queue: { position: 'end', scope: 'offerListScope' }});
            offerCurrentItem += offerScrollCount;
        }        
    }
}

// Mini Basket
var basketMoving;
var basketOpen;

Event.observe(window, 'load', function() {
    $('miniBasket-open').style.top = -($('miniBasket-open').offsetHeight) + "px";
    basketOpen = false;
    basketMoving = false;
    //new Effect.Move('miniBasket-open', {x:0, y:-($('miniBasket-open').offsetHeight), mode:'relative', queue:{position:'end', scope:'minibasket'}});
    
    // Open mini basket
    $('miniBasketShow').observe('click', function(event) {
        event.stop();
        if (basketOpen == false && basketMoving == false) {
            basketMoving = true;
            basketOpen = true;
            $('miniBasket-wrap').style.visibility = 'visible';
            $('miniBasket-wrap').style.display = 'block';
            new Effect.Move('miniBasket-open', {afterFinish: function(){basketMoving=false;}, duration:0.75, x:0, y:(document.getElementById('miniBasket-open').offsetHeight), mode:'relative', queue:{position:'end', scope:'minibasket'}});
        }    
    });
    
    //Close mini basket
    $('miniBasketHide').observe('click', function(event){
        event.stop();
        if (basketOpen == true && basketMoving == false) {
            basketMoving = true;
            basketOpen = false;
            new Effect.Move('miniBasket-open', {afterFinish: function(){basketMoving=false;}, duration:0.75, x:0, y:-($('miniBasket-open').offsetHeight), mode:'relative', queue:{position:'end', scope:'minibasket'}});
            new Effect.BlindUp('miniBasket-wrap');
        }        
    });
    
    //Product details thumbnail switcher
    $$('div#productImageThumbs img').each(function(sel){
        Event.observe(sel, 'click', function(event){
            event.stop();
            var fullSizeURL = sel.up('a').href;
            var mediumSizeURL = sel.up('a').rel;
            
            $('productImage').down('a').href = fullSizeURL;
            $('productImage').down('img').src = mediumSizeURL;            
        });
    });
    
    
});
