function jumpTo(anchor){
    $(anchor).scrollTo();
    pos = document.viewport.getScrollOffsets();
    //check if we're at page's bottom
    window.scrollTo(0, pos.top+1)
    checkPos = document.viewport.getScrollOffsets();
    if(checkPos.top != pos.top){
	    pos = pos.top - 100;
	    window.scrollTo(0,pos);
    }
}

function zoomListImage(listImage, zoomSrc)
{
    if($('listZoomImageContainer'))
    {
        $('listZoomImageContainer').remove();
    }
    
    offset = listImage.cumulativeOffset();
    listImageDim = listImage.getDimensions();
    
    zoomImageContainer = new Element('div', {id:'listZoomImageContainer'});
    
    zoomImage = new Element('img', {id:'listZoomImage', src:zoomSrc});
    zoomImageContainer.appendChild(zoomImage);
    
    document.body.appendChild(zoomImageContainer);
    
    zoomImageDim = zoomImage.getDimensions();
    
    //fit to list image
    zoomImage.style.width = listImageDim.width+'px';
    zoomImage.style.height = listImageDim.height+'px';
    
    zoomImageContainer.style.width = parseInt(listImageDim.width+5)+'px';
    zoomImageContainer.style.height = parseInt(listImageDim.height+6)+'px';
    
    zoomImageContainer.style.left = offset[0] + 'px';
    zoomImageContainer.style.top = offset[1] + 'px';
    
    //morph
    new Effect.Parallel([
      new Effect.Morph(zoomImage, { sync: true, style: 'width:' + parseInt(zoomImageDim.width - 6) + 'px; height:' + parseInt(zoomImageDim.height - 6) + 'px', duration: 0.2 }), 
      new Effect.Morph(zoomImageContainer, { sync: true, style: 'left:' + parseInt(offset[0] + listImageDim.width - zoomImageDim.width - 2) + 'px; top:' + parseInt(offset[1] + listImageDim.height - zoomImageDim.height - 2) + 'px; width:' + parseInt(zoomImageDim.width) + 'px; height:' + parseInt(zoomImageDim.height) + 'px', duration: 0.2 }) 
    ], { 
      duration: 0.2
    });

    zoomImageContainer.observe('mouseout', function(){if(this.parentNode){this.remove()}}); //FF fix
    zoomImageContainer.observe('click', function(){window.location.href=listImage.href});
}


//############### Indicator-stuff #################

document.observe('dom:loaded', function()
{
    $$('.indicator').each(function(obj)
    {
       obj.observe('click', function()
       {
            showIndicator();
       });
    });
});

function showIndicator()
{
  if($('indicatorImg').style.display == 'none')
  {
    $('indicatorImg').show();
    centerElement('indicatorImg');
    window.setTimeout(function(){$('indicatorImg').src = $('indicatorImg').src + "?reload=" + Math.round(1000);}, 10);
  }
}

function centerElement(elementId)
{
  viewportDim = document.viewport.getDimensions();
  viewportScroll = document.viewport.getScrollOffsets();
  elementDim = $(elementId).getDimensions();
  
  if($(elementId).style.position == 'absolute'){
      $(elementId).style.top = parseInt(viewportScroll.top + ((viewportDim.height - elementDim.height) / 3))+'px';
  } else if($(elementId).style.position == 'fixed'){
      $(elementId).style.top = parseInt((viewportDim.height - elementDim.height) / 3)+'px';
  }
  
  if(viewportDim.width < 950){
  $(elementId).style.left = parseInt((viewportDim.width - elementDim.width) / 2)+'px';
}else{
  $(elementId).style.left = parseInt((950 - elementDim.width) / 2)+'px';
}
}

//############### custDim ####################

function setCustSize(dim, value)
{
  $$('[rel="cust'+dim+'"]').each(function(obj)
  {
    obj.value = value;
  });
}

//############### clear dimension #############
document.observe('dom:loaded', function()
{
    $$('.clearCustDim').each(function(obj)
    {
       obj.observe('click', function()
       {
            setCustSize('Width', '');
            setCustSize('Height', '');
            //submit their form
            obj.up('form').submit();
       });
    });
});

//################ mouse over ###################
document.observe('dom:loaded', function()
{
    $$('.toggleMouseMove').each(function(obj)
    {
       obj.observe('mouseover', function()
       {
            obj.src = obj.src.replace(/\.([a-z]+)$/, '_in.$1');
       });
       obj.observe('mouseout', function()
       {
            obj.src = obj.src.replace(/_in\.([a-z]+)$/, '.$1');
       });
    });
});

//################ size validation ###################
$(document).observe('dom:loaded', function() {
    var replaceInvalidChars = function () {
        // allow only 
        //  - numbers and comma, 
        //  - one number after comma
        var newValue = this.value.replace(/[^0-9,\.]/g, '').replace(/([0-9]+,?[0-9]?).*/, '$1');
        if (newValue !== this.value) {
            this.value = newValue;
        }
    };
    $$('.sizeInput').each(function (input) {
        input.observe('keyup', replaceInvalidChars);
        input.observe('change', replaceInvalidChars);
    });
});





