var bootstrap = function (){
	this._initHomepage = function (){
		if ($(".home-slider").length>0){
			var slide_delay = 15; //seconds
			var slide_no = 1;
			var slider_to;
			var animating = false;
			var max_slides = $(".home-slider .slide").length;
			var slideTransition = function (slide){
				clearTimeout(slider_to);
				slider_to =setTimeout(slideTransition, (slide_delay*1000+(animating ? 1000 : 0)));
				if (animating) return;
				var prev = slide_no;
				if (slide!=null){
					if (slide_no == slide+1) return;
					slide_no = slide+1;
				}else{
					slide_no++;
				}
				if (slide_no>max_slides) slide_no = 1;
				$(".home-slider .transition_slide").addClass('slide-bg-'+prev);
				$(".home-slider .transition_slide .slide_new").addClass('slide-bg-'+slide_no);
				$(".home-slider .slide:eq("+(prev-1)+")").fadeOut('normal');
				$(".home-slider .slide:eq("+(slide_no-1)+")").fadeIn('slow');
				$(".nav-home-slider ul li:eq("+(slide_no-1)+")").addClass('selected').siblings().removeClass('selected');
				animating = true;
				$(".home-slider .transition_slide").show().animate({left:-980}, 'slow', function (){
					$(".home-slider .transition_slide").css({left:0});
					$(".home-slider").addClass('slide-bg-'+slide_no);
					$(".home-slider").removeClass('slide-bg-'+prev);
					$(".home-slider .transition_slide").hide()
					$(".home-slider .transition_slide").removeClass('slide-bg-'+prev);
					$(".home-slider .transition_slide .slide_new").removeClass('slide-bg-'+slide_no);
					animating = false;
				});
			}

			$(".home-slider *").css('z-index', 10);
			$(".home-slider").append('<div class="transition_slide"><div class="slide_new"></div></div>');
			$(".nav-home-slider").append('<ul></ul>');
			for(var i=1; i<=max_slides; i++){
				$(".nav-home-slider ul").append('<li'+(i==1 ? ' class="selected"' : '')+'><a href="#"></a></li>');
			}
			$(".nav-home-slider ul li a").click(function (){slideTransition($(this).parent().index());})
			setTimeout(slideTransition, (slide_delay*1000));
		}

	};
        //new slider logic as of 2/2/2012 tm
        this._initSlider = function (){
                var curIndex = 1;
		var delay = 7000;
		var animspeed = 1000;//'slow';
		var toSlider;
		var slider_bg = [
                        {url:'', image: 'images/home-slider-bg-1.jpg', h1:'Welcome to All American Semiconductor', h2:'All American is a leading distributor of electronic components with over 40 years of industry experience. The company\'s complete product offering includes a broad range of active, passive, interconnect, power, and electromechanical components.'},
			{url:'', image: 'images/home-slider-bg-2.jpg', h1:'Over 250,000 parts in stock.', h2:'Hundreds of thousands of off the shelf product to choose from. See our <a style="background: rgb(201, 224, 245); padding: 0 5px; text-shadow: none;" href="http://www.allamerican.com/linecard">line card</a> for more.'},
                        {url:'', image: 'images/home-slider-bg-3.jpg', h1:'Design Solutions', h2:'Checkout our solutions <a style="background: rgb(201, 224, 245); padding: 0 5px; text-shadow: none;" href="http://www.allamerican.com/solutions">here'}   	
		];
		
		var sliderChange = function (target, force){
			if (force==(curIndex-1) || (force==4 && curIndex==0)) return;
			if (typeof(force)=='number') curIndex = force;
			clearTimeout(toSlider);
			
			var cur = slider_bg[curIndex];
                        
			//$(target).find('a.btn-slider').attr('href', cur.url); //used for URL.. not used in this application
                        //$(target).find('h1').replaceWith('<h1>'+cur.h1+'</h1>');
                        //$(target).find('h2').replaceWith('<h2>'+cur.h2+'</h2>');
 			$(target).find('ul.navigation li a').removeClass('active');
			$(target).find('ul.navigation li:eq('+(curIndex)+') a').addClass('active');			
			
			$(target).find('div.temp-slider').css({'background-image':'url('+cur.image+')', left:980, display:'block'}).animate({left:1}, animspeed, 'swing', function (){ //Slide from right
			//$(target).find('div.temp-slider').css({'background-image':'url('+cur.image+')', opacity:0, display:'block'}).stop().animate({opacity:1}, animspeed, 'swing', function (){ //Cross fade
				$(target).css({'background-image':'url('+cur.image+')'});
				$(this).hide();
				toSlider = setTimeout(function (){sliderChange(target)}, delay);
                                $(target).find('h1').replaceWith('<h1>'+cur.h1+'</h1>');
                                $(target).find('h2').replaceWith('<h2>'+cur.h2+'</h2>');
				curIndex++;
				if (curIndex>=slider_bg.length) curIndex = 0;
			});
		};
		
		$('div.slider-content').each(function (){
			var that = this;
			$(this).css('overflow', 'hidden');
			$(this).find("*").css('z-index', '10101');
			$(this).append('<div class="temp-slider" style="z-index:10100; width:980px; height:288px; position:absolute; top:1px; left:1px; display:none; background-position:-1px -1px"></div>');
			var first = true;
			for(var i in slider_bg){
				$(this).find('ul.navigation').append('<li><a href="#"'+(first ? ' class="active"':'')+'></a></li>');
				first = false;
			}
			$(this).find('ul.navigation a').click(function (){
				sliderChange(that, $(this).parent().index());
			});
			//$(this).find('a.btn-slider').attr('href', slider_bg[0].url); //used for URL.. not used in this application
                        $(this).find('h1').replaceWith('<h1>'+slider_bg[0].h1+'</h1>');
                        $(this).find('h2').replaceWith('<h2>'+slider_bg[0].h2+'</h2>');
 			toSlider = setTimeout(function (){sliderChange(that)}, (delay-500));
		});
	}
        
	this._initNavigation = function(){
		var nav_timeout;
		$("header nav .navigation").find('ul').siblings('a').click(function(){$(this).parent().mouseover();return true;}).parent().mouseover(function (){
			$(this).children('ul').show();
			clearTimeout(nav_timeout);
		}).mouseout(function (){
			var that = this;
			nav_timeout = setTimeout(function (){
				$(that).children('ul').hide();
			},200);
		});
		$("header nav .navigation").find('ul').find('li a').each(function (){
			$(this).html($(this).html().replace(/\s/ig, '&nbsp;'));
		});
	}

	this._initDialogs = function (){
		if ($("#partsImport").length>0){
			$("#partsImport").dialog({
				autoOpen: false,
				width:465,
				height:250,
				modal:true,
				beforeClose: function (){
					if ($('#import_progress').is(':visible')){
						if (!confirm("Closing this dialog will stop upload in progress.\nAre you sure you want to close this dialog?")) return false;
					}

				}
			});
		}
	};
	
	this._initSearchbar = function(){
		addHintText("#searchbar");
	}


	this._initMap = function (){
		if ($('.us-map-full').hasClass('map-ready')) return;
		$('.us-map-full').find('.state').append('<div class="image"></div>').mouseover(function (){
			$(this).children('.image').show();
			var cls =$(this).attr('class').replace(/(state|\s*)/gi, '');
			$('.location'+cls).addClass('selected');
		}).mouseout(function (){
			var cls =$(this).attr('class').replace(/(state|\s*)/gi, '');
			$(this).children('.image').hide();
			$('.location'+cls).removeClass('selected');
		});
		$('.locations').find('.location').mouseover(function (){
			var cls =$(this).attr('class').replace(/(location|\s*)/gi, '');
			$('.state'+cls).children('.image').show();
			$(this).addClass('selected');

		}).mouseout(function (){
			var cls =$(this).attr('class').replace(/(location|selected|\s*)/gi, '');
			$('.state'+cls).children('.image').hide();
			$(this).removeClass('selected');
		});
		$('.us-map-full').addClass('map-ready');
	};

	for(prop in this){if (prop.match(/^_init/i)) this[prop]();}
};

$(function(){
	var bs = new bootstrap();
});

function addHintText(target){
	var title = $(target).attr('title');
	$(target).focus(function (){
		if ($(this).val()==title) $(this).val("").removeClass("textfield-hint");
	}).blur(function (){
		if ($(this).val()=="" || $(this).val()==title){
			$(this).val(title);
			$(this).addClass("textfield-hint");
		}
	}).blur();
}
