function buildInsightsPage(numberOfInsightsToShow, isThisHomePage) {
    var slideEnabled = true;
    var numInsightSlides = 0;
    var allowInsightSlidePlay = true;
    var insightSlideClickCount = 0;
    $('#footer').topZIndex();
    $('#nav-global-line').topZIndex();
    addInsights();
    var ie = navigator.userAgent.toLowerCase();
    var isIE = ie.indexOf("msie") > -1;
    if (isIE) {} else {
        $(".insight-circle-container").css({
            'background-image': 'url(../images/circle-orange-466px.png)',
            'background-color': 'transparent',
            'background-size': 'cover'
        });
    }

    function randOrd() {
        return (Math.round(Math.random()) - 0.5);
    }
    insightArray = new Array(2, 10, 3, 7, 5, 4, 1, 8, 11, 12, 9, 6);
    if (!isThisHomePage) {
        var insightPositionArray = [
            [0, 0],
            [0, 220],
            [0, 440],
            [0, 660],
            [220, 0],
            [220, 220],
            [220, 440],
            [220, 660],
            [440, 0],
            [440, 220],
            [440, 440],
            [440, 660]
        ];
        insightPositionArray.sort(randOrd);
        $('#insight-container-1').addClass('add-margins');
        
        
    } else {
    	if (numberOfInsightsToShow < 5){
        var insightPositionArray = [
            [0, 0],
            [0, 220],
            [0, 440],
            [0, 660]     
        ];
        
        } else {
        
          var insightPositionArray = [
            [0, 0],
            [0, 220],
            [0, 440],
            [0, 660],
            [440, 660],
          	[220, 660],
            [220, 220],
            [440, 440],
            [220, 440],
            [440, 0],
            [440, 220],
            [220, 0]           
        ];

        insightPositionArray.sort(randOrd);
        
        }
        $('#insights-panel').fadeOut(1000).animate({
            height: '0px'
        }, {
            duration: 1000
        }).remove();
        $('#insight-arrow').remove();
        $('#insight-container-1').prepend('<div id="button-more-insights"></div>');
		$('#insight-container-1').prepend('<div id="button-less-insights"></div>');
        $('#insight-container-1').addClass('home-page');
        
        
        
    }
    insightArray.sort(randOrd);

    function addInsights() {
        var iS = 1;
        var InsightId = setTimeout(showInsight, 300);
        var showThisInsighter = '';

        function showInsight() {
            showThisInsighter = $('#insight-' + insightArray[iS - 1]);
            var JSshowThisInsighter = document.getElementById('insight-' + insightArray[iS - 1]);
            JSshowThisInsighter.style.top = insightPositionArray[iS - 1][0] + 'px';
            JSshowThisInsighter.style.left = insightPositionArray[iS - 1][1] + 'px';
            showThisInsighter.fadeIn().delay(500).find('.white-circle-png').animate({
                top: '0px',
                height: '200px',
                width: '200px'
            }, 300, function () {
                $(this).siblings('.text-container-off').show()
            });
            InsightId = setTimeout(showInsight, 300);
            if (iS == numberOfInsightsToShow) {
                abortTimer();
            }
            iS++;
        }

        function abortTimer() {
            clearTimeout(InsightId);
            // THIS GOES THROUGH EACH CIRCLE AND PUTS THE LEFT AND TOP INTO AN ATTRIBUTE.  NOT W3C CERTIFIED BUT THIS IS INTERNAL
            $(".insight-circle-container").each(function (index) {
                $(this).find('.insight-slides').attr('alt', '0');
                var thisCirclePosition = $(this).position();
                var thisCirclePositionLeft = thisCirclePosition.left + 'px';
                var thisCirclePositionTop = thisCirclePosition.top + 'px';
                $(this).attr('left', thisCirclePositionLeft);
                $(this).attr('top', thisCirclePositionTop);
            });
            $('.insight-slides').each(function (nS) {
                var numSld = $(this).find('li').length;
                $(this).css('width', numSld * 480 + 'px')
                $(this).attr('alt', numSld);
                $(this).find('li').each(function (index) {
                    $(this).attr('id', 'insight-slide-' + nS + '-' + index);
                    numInsightSlides++
                });
            });
            
            if (numberOfInsightsToShow < 5) {
        	$('#button-more-insights').fadeIn();
        	
        	} else {
        	$('#button-more-insights').hide();
        	$('#button-less-insights').fadeIn();
        	//$('#insight-container-1').animate({ height: '680' }, {duration: 500});
        	}
        }
    }
    
    if (!isiPad || !isiPhone || !isAndroid ){
    $(".insight-circle-container").hover(function () {
        $(this).find('.white-circle-png').animate({
            width: '192px',
            height: '192px',
            marginTop: '4px'
        }, 200, function () {});
    }, function () {
        $(this).find('.white-circle-png').animate({
            width: '200px',
            height: '200px',
            marginTop: '0px'
        }, 200, function () {});
    });
    
    }
    // GRABS THE CLICK
    $(".insight-circle-container").click(function () {
    	tS = $(this);
		if (tS.hasClass('openCircle')) {
			closeInsightCircle(tS)
		} else {
			openInsightCircle(tS)
		}
 	  });
 	  
 	  $('.slide-advance').click(function(e){
 	  	 sA = $(this);
 	  	 e.stopPropagation();
 	  	 advanceInsightSlides(sA);
 	  });
    
    
 	$('#button-more-insights').click(function(){
 //		$('#insights-homepage-block, #insight-container-1').animate({ height: '650' }, {duration: 500});
 		
 		$('.insight-circle-container').each(function(){
 			$(this).fadeOut(500).remove();
 		});
 		$('#insight-container-1').remove();
 		LoadHomePageInsights(12);
 		
 	}); 

 	$('#button-less-insights').click(function(){
 //		$('#insights-homepage-block, #insight-container-1').animate({ height: '650' }, {duration: 500});
 		
 		$('.insight-circle-container').each(function(){
 			$(this).fadeOut(500).remove();
 		});
 		$('#insight-container-1').remove();
 		LoadHomePageInsights(4);
 		
 	}); 
 	
  
//    $('.insight-circle-container').hover(function () {
/*

*/
//        $('.slide-advance').hover(function () {
       //     slideEnabled = true;
            function advanceInsightSlides(sA) {
            	var thisInsightSlideShow = sA.parent().find('.insight-slides');
            	var thisSlideShowLength = parseFloat(thisInsightSlideShow.attr('alt'));
		        if (thisSlideShowLength == "NaN") {
		            thisSlideShowLength = 0;
		        }
                if (allowInsightSlidePlay) {
                    if (insightSlideClickCount < (thisSlideShowLength - 1)) {
                        allowInsightSlidePlay = false;
                        thisInsightSlideShow.animate({
                            marginLeft: '-=480px'
                        }, 300, function () {
                            allowInsightSlidePlay = true;
                            insightSlideClickCount++;
                        });
                    } else {
                        allowInsightSlidePlay = true;
                //        slideEnabled = false;
                        //  thisInsightSlideShow.css('margin-left','0px');
                        $(".insight-circle-container.openCircle").click();
                    }
                }
            }
//        }, function () {
//            slideEnabled = false;
//        });
 //   });
 	  
function resetInsightSlides(){
    insightSlideClickCount = 0;
    $('.insight-slides').css('margin-left', '0px');

} 	  
 	  
function closeInsightCircle(theCircle){
	resetInsightSlides();
	var theWhiteImage = theCircle.find('img.white-circle-png');
	theCircle.find('.text-container-on').hide();
	var myL = theCircle.attr('left'); // GRABS THE LEFT AND TOP OF THE CIRCLE
	var myT = theCircle.attr('top');
	theCircle.animate({
	    width: '200px',
	    height: '200px',
	    left: myL,
	    top: myT
	}, 100, function () {
	    theCircle.removeClass('openCircle');
	    theWhiteImage.fadeIn('fast');
	    theCircle.find('.text-container-off').show();
	});
 
 
 
}	  
 
 function openInsightCircle(theCircle){
            var theWhiteImage = theCircle.find('img.white-circle-png');
            theCircle.topZIndex(); // SETS THIS CIRCLE TO THE HIGHEST Z-INDEX
            $('#footer').topZIndex();
            $('#nav-global-line').topZIndex();

            
            
                $(".insight-circle-container").not('openCircle').each(function (index) { //LOOPS THROUGH EACH CIRCLE AND PUTS IT IN THE ORIGINAL POSITION
                    var myL = $(this).attr('left');
                    var myT = $(this).attr('top');
                    $(this).find('img.white-circle-png').fadeIn('fast');
                    $(this).animate({
                        width: '200px',
                        height: '200px',
                        left: myL,
                        top: myT
                    }, 100, function () {
                        $(this).removeClass('openCircle');
                        $(this).find('.text-container-off').show();
                        $(this).find('.text-container-on').hide();
                    });
                });
                theWhiteImage.fadeOut('fast', function () {
                    theCircle.find('.text-container-off').hide();
                    theCircle.animate({ //IF THE CIRCLE IS NOT OPEN, IT OPENS IT AND ADDS THE "OPENCIRCLE" CLASS
                        width: '466px',
                        height: '466px',
                        top: '100px',
                        left: '205px'
                    }, 300, 'easeOutBack', function () {
                        theCircle.addClass('openCircle');
                        theCircle.find('.text-container-on').fadeIn(300);
                    });
                });
          //  }
}
/*
        } else {
        
        console.log('slideEnabled= ',slideEnabled)
        
        
        }
*/
  
    
    
    
    
    $.ajax({
        url: "http://api.wunderground.com/api/6756990b71c458f7/geolookup/conditions/q/CO/boulder.json",
        dataType: "jsonp",
        success: function (parsed_json) {
            var location = parsed_json['location']['city'];
            var icon_urlme = parsed_json['current_observation']['icon_url'];
            var weather = parsed_json['current_observation']['weather'];
            var temp_f = parsed_json['current_observation']['temp_f'];
            //  alert("Current temperature in "+location+" is: " + temp_f);
            $(".weather").append('<img src=' + icon_urlme + '><br>' + weather + ' at the corporate<br>offices in Boulder.<br>Current temperature is ' + temp_f + '&#186; :-)');
        }
    });
}
