Event.observe(window, 'load', function() {
    if ($('christmasForm') != null) {
        $$('.imageSwap').each(function(button) {
            button.observe('click', function() {
                $('mainImage').src = storagePath+"/images/christmas/"+button.rel;
                $('mainLink').href = button.rev;
                $('full-selection-button').href = button.rev;
                
                // Change CSS classes
                $$(".imageSwap-ul li").each(function(elmt) { elmt.removeClassName("selected") });
                $$("a.imageSwap").each(function(elmt) { elmt.removeClassName("selected") });
                button.addClassName('selected');
                button.up().addClassName('selected');
            });
        }); 
        
        // Set handle2 initial position
        $('handle2').setStyle({
            left: '324px'
        });
    }
});

// Init positions
// Array(handle percent position, handle position in pixels from left, the price here)
var tickerPos=new Array(); 
tickerPos[0]= new Array(0,     0,   0); 
tickerPos[1]= new Array(0.216, 70,  1000); 
tickerPos[2]= new Array(0.447, 145, 3000); 
tickerPos[3]= new Array(0.672, 219, 5000); 
tickerPos[4]= new Array(1,     324, 10000); 

// Init start positions
var prev1 = 0;
var prev2 = 324;


function setTrackPosition(currentWidth, xPosition, divWidth) {
    if(currentWidth) {
        // Set handle 1
        $('track-selected').writeAttribute('style', 'background-position: '+xPosition+'px top; width: '+currentWidth+'px');
    } else {
        // Set handle 2
        $('track-selected').writeAttribute('style', 'background-position: '+xPosition+'; width: '+divWidth+'px');
    }
}

// Snap handles
function snapTo(v1,v2, fullWidth) {
    var styleStr1, styleStr2;
    var centre, arrPos;
    var h1str = $('handle1').getStyle('left');
    var h2str = $('handle2').getStyle('left');
    var h1left = h1str.substr(0, h1str.length-2); 
    var h2left = h2str.substr(0, h2str.length-2); 
    
    // Check handles cross slided
    if (parseInt(h2left) > parseInt(h1left)) {
        for(i=0;i< tickerPos.length;i++) {
            
            // Snap handle1
            if (v1 != null && ((v1 > tickerPos[i][0] && v1 < tickerPos[i+1][0]) || v1==0)) {
                var currentWidth = $('track-selected').getWidth();
                centre = ((tickerPos[i+1][0]-tickerPos[i][0])/2)+tickerPos[i][0];
                
                // Set the arrPos to the nearest ticker pos or 0  at the begin. of the track
                if (v1==0) {
                    arrPos = 0;
                } 
                else {
                    arrPos = (v1<=centre) ? i : i+1;
                }

                // if handle1 pos != handle2 pos
                if ($('handle2').getStyle('left') != tickerPos[arrPos][1]+'px') {
                    prev1 = tickerPos[arrPos][1];
                    styleStr1 = tickerPos[arrPos][1]+'px';
                    // Set handle position
                    $('handle1').setStyle({ left: styleStr1 });
                    setTrackPosition(currentWidth, ((tickerPos[arrPos][0]*fullWidth)+8), null);
                    $('price_from').value = tickerPos[arrPos][2];
                } else {
                    setBackHandlePositions();
                }
            }

            // Snap handle2
            if (v2 != null && ((v2 >= tickerPos[i][0] && v2 < tickerPos[i+1][0]) || (v2==1))) {
                var xPosition = $('track-selected').getStyle('background-position');
                centre = ((tickerPos[i+1][0]-tickerPos[i][0])/2)+tickerPos[i][0];
                
                // Set the arrPos to the nearest ticker pos or 1  at the end of the track
                if (v2==1) { 
                    arrPos = tickerPos.length-1;
                } 
                else {
                    arrPos = (v2<=centre) ? i : i+1;
                }

                // if handle1 pos != handle2 pos
                if ($('handle1').getStyle('left') != tickerPos[arrPos][1]+'px') {
                    prev2 = tickerPos[arrPos][1];
                    styleStr2 = tickerPos[arrPos][1]+'px';
                    // Set handle position
                    $('handle2').setStyle({ left: styleStr2 });
                    setTrackPosition(null, xPosition, (fullWidth*tickerPos[arrPos][0])+8);
                    $('price_to').value = tickerPos[arrPos][2];
                } else {
                    setBackHandlePositions();
                }
            }
        }
    } else {
        setBackHandlePositions();
    }
}


function setBackHandlePositions() {
    // Set back previous positions
    var prev1str = prev1+"px";
    var prev2str = prev2+"px";
    $('handle1').setStyle({ left: prev1str });
    $('handle2').setStyle({ left: prev2str });
}


