var scrollMin = 0;
var scrollMax = 0;
var textMax = 0;
var scrubberOrgY = 0;
var mouseOrgY = 0;

$(document).ready(function(){
    var textH = $('#text').height();
    var textConH = $('#con_text').height();
    textMax = textH - textConH;
    var scrollH = $('#scroller').height();
    var scrubberH = scrollH * (textConH / textH);
    scrollMin = parseInt($('#scrubber').css('top'));
    scrollMax = Math.round((parseInt($('#scrubber').css('top')) + scrollH) - scrubberH);
    
    //Min height for scroller
    if (scrubberH < 15) scrubberH = 15;
    
    //Set the scroller's height
    $('#scrubber').height(Math.round(scrubberH));
    
    //Set the scroller's drag-n-drop
    $('#scroller').mousedown(onMouseDown);
    $('body').mouseleave(onMouseUpLeave);
    $('#scroller').bind('selectstart',function(){return false;});
    
    //For iPhone/iPad
    $('#scroller').bind('touchstart', onTouchStart);
    $('body').bind('touchend', onTouchEnd);
    
    //adding the event listerner for Mozilla
    if(window.addEventListener) document.getElementById('custom_textarea').addEventListener('DOMMouseScroll', onMouseWheel, false);
    //for IE/OPERA etc
    document.getElementById('custom_textarea').onmousewheel = onMouseWheel;
    
    //For the popups
    $('#zip_input').keyup(function() {
        this.value = this.value.replace(/[^0-9\.]/g,'');
    })
    $('#submit').click(onInAreaClick);
    $('#gift_text').click(onGiftTextClick);
});

function onInAreaClick(e) {
    var zip = $('#zip_input').attr("value");
    $('.contentContainer').append('<div id="map_frame"><iframe src="http://wacoal.com.storelocatorsoftware.com/?event=1&btemptd=0&address=' + zip + '"></iframe><div id="map_frame_ext">Close <div id="ext_x">X</div></div></div>');
    $('#map_frame_ext').click(onMapFrameExtClick);
}

function onGiftTextClick(e) {
    $('#stage').append('<div id="gift_frame"><img id="handbag" src="/wcsstore/WacoalStorefrontAssetStore/upload/img/handbag_2.jpg" /><div id="handbag_text">Wacoal Into The Wild Tote Bag <p>A gift to you with a purchase of $80 or<br />more. Available only at a <span class="pink">"Fit for the Cure"</span><br />event, while supplies last.</p></div><div id="map_frame_ext">Close <div id="ext_x">X</div></div></div>');
    $('#map_frame_ext').click(onGiftFrameExtClick);
}

function onMapFrameExtClick(e) {
    $('#map_frame').remove();
}

function onGiftFrameExtClick(e) {
    $('#gift_frame').remove();
}

function onMouseDown(e) {
    mouseOrgY = parseInt(e.pageY);
    scrubberOrgY = $('#scrubber').position().top;
    
    $('body').mousemove(onMouseMove);
    $('body').mouseup(onMouseUpLeave);
    return false;
}

function onMouseUpLeave(e) {
    $('body').unbind('mousemove');
    $('body').unbind('mouseup');
    return false;
}

function onMouseMove(e) {
    var mouseCurY = parseInt(e.pageY);
    var mouseOffset = mouseCurY - mouseOrgY;
    
    scroller(mouseOffset);
}

function onTouchStart(e) {
    e.preventDefault();
    var orig = e.originalEvent;
    mouseOrgY = parseInt(orig.changedTouches[0].pageY);
    scrubberOrgY = $('#scrubber').position().top;
    
    $('body').bind('touchmove', onTouchMove);
}

function onTouchEnd(e) {
    $('body').unbind('touchmove');
}

function onTouchMove(e) {
    e.preventDefault();
    var orig = e.originalEvent;
    var mouseCurY = parseInt(orig.changedTouches[0].pageY);
    var mouseOffset = mouseCurY - mouseOrgY;
    
    scroller(mouseOffset);
}

function onMouseWheel(e) {
  var delta = 0;
  
  if (!e) e = window.event;
  // normalize the delta
  if (e.wheelDelta) { // IE & Opera
    if (window.opera) delta = -e.wheelDelta / 40;
    else delta = -e.wheelDelta / 28;
  } else if (e.detail) { // W3C 
    delta = e.detail;
  }
  
  scrubberOrgY = $('#scrubber').position().top;
  
  scroller(delta * 5);
  return false;
}

function scroller(offset) {
    var scrubberTop = scrubberOrgY + offset;
    
    if (scrubberTop <= scrollMin) scrubberTop = scrollMin;
    else if (scrubberTop >= scrollMax) scrubberTop = scrollMax;
    
    $('#scrubber').css("top", (scrubberTop)+"px");
    
    var textTop = -(textMax * (scrubberTop / scrollMax));
    
    $('#text').css("top", (textTop)+"px");
}

/*********************************
 *
 *For Flash
 *
 ********************************/

function fadeTextarea() {
    $('#custom_textarea').fadeOut(1000);
    $('#in_area').fadeOut(1000);
    $('#watch').fadeOut(1000);
}

function fadeInTextarea() {
    $('#custom_textarea').fadeIn(1000);
    $('#in_area').fadeIn(1000);
    $('#watch').fadeIn(1000);
}
