
//code for string compare in javascript
function stristr (haystack, needle, bool) {
    // Finds first occurrence of a string within another, case insensitive
    //
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/stristr    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfxied by: Onno Marsman
    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);    // *     returns 2: 'Kevin '
    var pos = 0;

    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );    if (pos == -1){
        return false;
    } else{
        if (bool) {
            return haystack.substr( 0, pos );        } else{
            return haystack.slice( pos );
        }
    }
}
/*var types = ['DOMMouseScroll', 'mousewheel'];
$.event.special.mousewheel = {
	setup: function() {
		if ( this.addEventListener ) for ( var i=types.length; i; ) this.addEventListener( types[--i], handler, false );
		else this.onmousewheel = handler;
	},
	teardown: function() {
		if ( this.removeEventListener ) for ( var i=types.length; i; ) this.removeEventListener( types[--i], handler, false );
		else this.onmousewheel = null;
	}
};
$.fn.extend({
	mousewheel: function(fn) {
		return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
	},
	unmousewheel: function(fn) {
		return this.unbind("mousewheel", fn);
	}
});
function handler(event) {
	var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
	event = $.event.fix(event || window.event);
	event.type = "mousewheel";
	if ( event.wheelDelta ) delta = event.wheelDelta/120;
	if ( event.detail     ) delta = -event.detail/3;
	args.unshift(event, delta);
	return $.event.handle.apply(this, args);
}*/
/*--- custom scroll ---*/
jQuery.fn.addScroll = function(_options) {
	
var _options = jQuery.extend({
	lineWidth: 25
}, _options);
return this.each(function(){
			
			//alert(this);
						

});
}



function scrollableElement(currobj,showheight){

                if ($(currobj).attr("id") =="menuvalue2")
                    $('#zipcode').val('');
		$(currobj).next("div").removeClass('visibility');
		 
		 var firstDiv=$(currobj).next("div");
		
		 var objer=$(firstDiv).find(".scrollable");
		 
		 var scrollAvailableObject=$(objer).find(".scroll-content");
		   
		 if(scrollAvailableObject.length == 0){
			
			 if(showheight==null){
			 var contentheight=$(objer).children().length*29;

			 if (contentheight <= 200){
		        $(objer).css('height',(contentheight)+'px');
			 }
       else if(contentheight > 200 && contentheight <= 250){
			   $(objer).css('height',(contentheight-34)+'px');
			 }
       else if(contentheight > 250 && contentheight <= 300){
			   $(objer).css('height',(contentheight-48)+'px');
			 }
			 else if(contentheight > 300 && contentheight <= 350){
			   $(objer).css('height','288px');
			 }
			 else{
		        $(objer).css('height','315px');
			  }
			 } else{
				  $("#TB_window").css("margin-top","-420px");
                  $(objer).css('height',showheight+'px');
			 }
	         addscroller(objer);
			
			 if( $(currobj).attr("id") !="within"){
				 
				var imageSize=getImageSize(currobj);
        $(objer).find(".scroll-content a:first-child").toggleClass('scroll-content-hover_'+imageSize);
			 if($.trim($(currobj).html()) == "No" && stristr(window.location, "myemailpreferences", true)){
         $(objer).find(".scroll-content a:first-child").toggleClass('scroll-content-hover_'+imageSize);
         $(objer).find(".scroll-content a:last-child").toggleClass('scroll-content-hover_'+imageSize);
       }
       else if(!stristr(window.location, "myemailpreferences", true))
         {
            $(objer).find(".scroll-content a").each(function()
            {
              if($.trim($(this).html()) == $.trim($(currobj).html()))
                {
                  $(this).addClass('scroll-content-hover_'+imageSize);
                  $(this).siblings().removeClass('scroll-content-hover_'+imageSize);
                }
          });
         }
			 }
	    	 $(currobj).next("div").addClass('display');
		 }
		 else{
		
		  $('.scroll-slider').css('top','0px');  
		  $('.scroll-content').css('top','0px');  
		
	     }
	  
		
}

function scrollableElementInReload(currobj){
		$(currobj).next("div").removeClass('visibility');

		 var firstDiv=$(currobj).next("div");

		 var objer=$(firstDiv).find(".scrollable");

		 var scrollAvailableObject=$(objer).find(".scroll-content");

		 if(scrollAvailableObject.length == 0){

			 var contentheight=$(objer).children().length*29;

			 if (contentheight <= 200){
		        $(objer).css('height',(contentheight)+'px');
			 }
       else if(contentheight > 200 && contentheight <= 250){
			   $(objer).css('height',(contentheight-34)+'px');
			 }
       else if(contentheight > 250 && contentheight <= 300){
			   $(objer).css('height',(contentheight-48)+'px');
			 }
			 else if(contentheight > 300 && contentheight <= 350){
			   $(objer).css('height','288px');
			 }
			 else{
		        $(objer).css('height','315px');
          }
	         addscroller(objer);           
	    	 $(currobj).next("div").addClass('display');
		 }
		 else{

		  $('.scroll-slider').css('top','0px');
		  $('.scroll-content').css('top','0px');

	     }


}

function addscroller(scrollerobj){
	
		var _box = jQuery($(scrollerobj));
	
	
	//if(_box.is(':visible')){
           
		if(_box.children('.scroll-content').length == 0){
			//var line_w = _options.lineWidth;
			var line_w = 25;
			//  alert('Inside'+line_w);
			/*--- init part ---*/
			var scrollBar = jQuery('<div class="scroll-bar"><div class="scroll-up"></div><div class="scroll-line"><div class="scroll-slider"><div class="scroll-slider-inner">&nbsp;</div></div></div><div class="scroll-down"></div></div>');
			_box.wrapInner('<div class="scroll-content"><div class="scroll-hold"></div></div>').append(scrollBar);
			var scrollContent = _box.children('.scroll-content');
			var scrollSlider = scrollBar.find('.scroll-slider');
			var scrollSliderH = scrollSlider.parent();
			var scrollUp = scrollBar.find('.scroll-up');
			var scrollDown = scrollBar.find('.scroll-down');
			/*--- different variables ---*/
			var box_h = _box.height();
			var slider_h = 0;
			var slider_f = 0;
			var cont_h = scrollContent.height();
			var _f = false;
			var _f1 = false;
			var _f2 = true;
			var _f3 = false;
			var _t1, _t2, _s1, _s2;
			/*--- set styles ---*/
			_box.css({
				position: 'relative',
				overflow: 'hidden',
				height: box_h
			});
			scrollContent.css({
				position: 'absolute',
				top: 0,
				left: 0,
				zIndex: 1,
				height: 'auto'
			});
			scrollBar.css({
				position: 'absolute',
				top: 0,
				right: 0,
				zIndex:2,
				width: line_w,
				height: box_h,
				overflow: 'hidden'
			});
			scrollUp.css({
				width: line_w,
				height: line_w,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			scrollDown.css({
				width: line_w,
				height: line_w,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			slider_h = scrollBar.height();
			if(scrollUp.is(':visible')) slider_h -= scrollUp.height();
			if(scrollDown.is(':visible')) slider_h -= scrollDown.height();
			scrollSliderH.css({
				position: 'relative',
				width: line_w,
				height: slider_h,
				overflow: 'hidden',
				zIndex: 4
			});
			slider_h = 0;
			scrollSlider.css({
				position: 'absolute',
				top: 0,
				left: 0,
				width: '21px',
				height: slider_h,
				overflow: 'hidden',
				'margin-left':'2px',
				cursor: 'pointer'
			});
			box_h = _box.height();
			cont_h = scrollContent.height();
			if(box_h < cont_h){
				_f = true;
				slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
				if(slider_h < 5) slider_h = 5;
				scrollSlider.height(slider_h);
				slider_h = scrollSlider.outerHeight();
				slider_f = (cont_h - box_h)/(scrollSliderH.height() - slider_h);
				_s1 = (scrollSliderH.height() - slider_h)/15;
				_s2 = (scrollSliderH.height() - slider_h)/3;
				scrollContent.children('.scroll-hold').css('padding-right', scrollSliderH.width());
			}
			else{
				_f = false;
				scrollBar.hide();
				scrollContent.css({width: _box.width(), top: 0, left:0});
				scrollContent.children('.scroll-hold').css('padding-right', 0);
			}
			var _top = 0;
			/*--- element's events ---*/
			scrollUp.mousedown(function(){
				_top -= _s1;
				scrollCont();
				_t1 = setTimeout(function(){
					_t2 = setInterval(function(){
						_top -= 4/slider_f;
						scrollCont();
					}, 20);
				}, 500);
			}).mouseup(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			}).mouseleave(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			});
			scrollDown.mousedown(function(){
				_top += _s1;
				scrollCont();
				_t1 = setTimeout(function(){
					_t2 = setInterval(function(){
						_top += 4/slider_f;
						scrollCont();
					}, 20);
				}, 500);
			}).mouseup(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			}).mouseleave(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			});
			scrollSliderH.click(function(e){
                    //  alert('sdf'+e.pageY);
				if(_f2){
					if(scrollSlider.offset().top + slider_h < e.pageY){
						_top += _s2;
					}
					else if(scrollSlider.offset().top > e.pageY){
						_top -= _s2;
					}
					scrollCont();
				}
				else{
					_f2 = true;
				}
			});
			var t_y = 0;
			scrollSlider.mousedown(function(e){
				t_y = e.pageY - $(this).position().top;
				_f1 = true;
			}).mouseup(function(){
				_f1 = false;
			});
			$('body').mousemove(function(e){
				if(_f1){
					 _f2 = false;
					 _top = e.pageY - t_y;
					// console.debug(e.pageY);
					 scrollCont(_top);
				}
			}).mouseup(function(){
				_f1 = false;
				_f3 = false;
			});
			scrollSliderH.mousedown(function(){
				_f3 = true;
			}).mouseup(function(){
				_f3 = false;
			});
			if(window.attachEvent) document.body.attachEvent("onselectstart", function(){
				if(_f1 || _f3) return false;
			});
			/*_box.bind('mousewheel', function(event, delta){
				if(_f){
					_top -=delta*_s1;
					scrollCont();
					if((_top > 0) && (_top+slider_h < scrollSliderH.height())) {
						return false;
						}else{
							return false;
							}
				}
			});*/		
		
			function scrollCont(){
				if(_top < 0) _top = 0;
				else if(_top+slider_h > scrollSliderH.height()) _top = scrollSliderH.height() - slider_h;
				scrollSlider.css('top', _top);
				scrollContent.css('top', -_top*slider_f);
			}
			
		//}
		
	}


}

function getImageSize(selectedObj){
	
   	   var imageSize;
       var objImgInfo=$(selectedObj).attr("onmouseout");
	   
	  // alert('dfg'+String(objImgInfo).indexOf("231"));
	   
   if(String(objImgInfo).indexOf("231")!= -1){
       imageSize="231";
   }
   if(String(objImgInfo).indexOf("160")!= -1){
       imageSize="160";
   }
   if(String(objImgInfo).indexOf("130")!= -1){
       imageSize="130";
   }
   if(String(objImgInfo).indexOf("112")!= -1){
       imageSize="112";
   }
   if(String(objImgInfo).indexOf("250")!= -1){
       imageSize="250";
   }
  return imageSize;
}



