﻿	// set global
    var counter = getCounter();
    //counter = counter - 1000;
	var intival = 1;
	var countHeight = 50;
	var count1, count2, count3, count4, count5, count6, count7, count8;

	$(document).ready( function() {
		//get units
		count8 = Math.floor(counter / 10000000);
		count7 = Math.floor(counter / 1000000);
		count6 = Math.floor(counter / 100000);
		count5 = Math.floor(counter / 10000);
		count4 = Math.floor(counter / 1000);
		count3 = Math.floor(counter / 100);
		count2 = Math.floor(counter / 10);
		count1 = Math.floor(counter / 1);

		//clean units
		count8 = parseInt(count8.toString().substring(count8.toString().length-1));
		count7 = parseInt(count7.toString().substring(count7.toString().length-1));
		count6 = parseInt(count6.toString().substring(count6.toString().length-1));
		count5 = parseInt(count5.toString().substring(count5.toString().length-1));
		count4 = parseInt(count4.toString().substring(count4.toString().length-1));
		count3 = parseInt(count3.toString().substring(count3.toString().length-1));
		count2 = parseInt(count2.toString().substring(count2.toString().length-1));
		count1 = parseInt(count1.toString().substring(count1.toString().length-1));
		
		//set count heights
		$('#count8').css({ height: (count8 * countHeight) + countHeight});
		$('#count7').css({ height: (count7 * countHeight) + countHeight});
		$('#count6').css({ height: (count6 * countHeight) + countHeight});
		$('#count5').css({ height: (count5 * countHeight) + countHeight});
		$('#count4').css({ height: (count4 * countHeight) + countHeight});
		$('#count3').css({ height: (count3 * countHeight) + countHeight});
		$('#count2').css({ height: (count2 * countHeight) + countHeight});
		$('#count1').css({ height: (count1 * countHeight) + countHeight});
		
		//set update intival
		//setInterval( "increaseCounter()", intival );
		setTimeout("increaseCounter()", intival);
	});
	
	function increaseCounter()
	{
	    //set the new intival
	    var currentCounter = getCounter();
	    var counterLag = currentCounter - counter;

	    /*if (counterLag < 100) {
	        intival = 20000;
	    } else if (counterLag < 500) {
	        intival = 15000;
	    } else {
	        intival = 10000;
	    }*/
	    intival = 10000;

	    count1++;
	    $('#count1').animate({ height: '+=' + countHeight }, intival, 'linear', function () {
	        $('#count1').css({ height: (count1 * countHeight) + countHeight });
	        $('#count2').css({ height: (count2 * countHeight) + countHeight });
	        $('#count3').css({ height: (count3 * countHeight) + countHeight });
	        $('#count4').css({ height: (count4 * countHeight) + countHeight });
	        $('#count5').css({ height: (count5 * countHeight) + countHeight });
	        $('#count6').css({ height: (count6 * countHeight) + countHeight });
	        $('#count7').css({ height: (count7 * countHeight) + countHeight });
	        $('#count8').css({ height: (count8 * countHeight) + countHeight });
	        increaseCounter();
        });

	    if (count1 == 10) {
	        count1 = 0;
	        count2++;
	        $('#count2').animate({ height: '+=' + countHeight }, intival, 'linear');

	        if (count2 == 10) {
	            count2 = 0;
	            count3++;
	            $('#count3').animate({ height: '+=' + countHeight }, intival, 'linear');

	            if (count3 == 10) {
	                count3 = 0;
	                count4++;
	                $('#count4').animate({ height: '+=' + countHeight }, intival, 'linear');

	                if (count4 == 10) {
	                    count4 = 0;
	                    count5++;
	                    $('#count5').animate({ height: '+=' + countHeight }, intival, 'linear');

	                    if (count5 == 10) {
	                        count5 = 0;
	                        count6++;
	                        $('#count6').animate({ height: '+=' + countHeight }, intival, 'linear');

	                        if (count6 == 10) {
	                            count6 = 0;
	                            count7++;
	                            $('#count7').animate({ height: '+=' + countHeight }, intival, 'linear');

	                            if (count7 == 10) {
	                                count7 = 0;
	                                count8++;
	                                $('#count8').animate({ height: '+=' + countHeight }, intival, 'linear');

	                                if (count8 == 10) {
	                                    count8 = 0;
	                                }
	                            }
	                        }
	                    }
	                }
	            }
	        }
	    }
    }

    function getCounter() {
        var counter = $.ajax({ url: "/counter.aspx", async: false }).responseText;
        counter = counter.substring(counter.indexOf('#') + 1, counter.indexOf('~'));
        if (isNaN(counter)) counter = 0;
        if (parseInt(counter) < 700000) counter = 700000;
        return parseInt(counter);
    }




//image preloader
(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

//when ready
$(document).ready(function () {
    var mood = jQuery('.carouselSlide ul').moodular({
            auto: false,
            speed: 500,
            api: true
    });

    //apply carousel controls to buttons
    jQuery('a.carouselButton1').bind('click', function () {
        mood.moveTo(0);
        $('a.carouselButton1').addClass('selected');
        $('a.carouselButton2').removeClass('selected');
        $('a.carouselButton3').removeClass('selected');
        return false; });
    jQuery('a.carouselButton2').bind('click', function () {
        mood.moveTo(1);
        $('a.carouselButton1').removeClass('selected');
        $('a.carouselButton2').addClass('selected');
        $('a.carouselButton3').removeClass('selected');
        return false; });
    jQuery('a.carouselButton3').bind('click', function () {
        mood.moveTo(2);
        $('a.carouselButton1').removeClass('selected');
        $('a.carouselButton2').removeClass('selected');
        $('a.carouselButton3').addClass('selected');
        return false; });

    //add links to panel divs
    $(".panelSquare").click(function () {
        window.location = $(this).find("a").attr("href");
    });
    $(".panelLandscape").click(function () {
        window.location = $(this).find("a").attr("href");
    });

    //preload images
    jQuery.preLoadImages("image0", "_client/images/home/carousel-button-1.gif");
    jQuery.preLoadImages("image1", "_client/images/home/carousel-button-1-hover.gif");
    jQuery.preLoadImages("image2", "_client/images/home/carousel-button-2-hover.gif");
    jQuery.preLoadImages("image3", "_client/images/home/carousel-button-3-hover.gif");

    jQuery.preLoadImages("image4", "_client/images/container/footer-button-find-hover.gif");
    jQuery.preLoadImages("image5", "_client/images/container/footer-button-contact-hover.gif");
    jQuery.preLoadImages("image6", "_client/images/container/footer-button-download-hover.gif");
});
