
function clusters_update() {
    var vclusters = $('#zabbix-switch p:last').text();
    vclusters = String(vclusters);
    while (vclusters.length < 5) {
        vclusters = '0' + vclusters;
    }
    $('.stats-counters .clusters span').hide('slide', {
        'direction': 'down'
    }, 400, function () {
        $(this).text(vclusters).show('slide', {
            'direction': 'up'
        }, 700);
    });
}

function zabbix_update(data, target, padding, convert) {
    $.get("http://" + document.location.host + "/wp-content/themes/catn/zabbix/stats.xml", function (XML) {
        
        var stats = 0,
			val,
			counter = 0;

        $.each($(XML).find('data[type="' + data + '"] group'), function (i) {
            counter += 1;
            stats += parseInt($(this).text(), 10);
        });

        val = stats;
        if (convert === 'MB') {
            val = Math.round(val / 10000);
        } else if (convert === 'GB') {
            val = Math.round(val / 100000000);
        }
        val = String(val);
        while (val.length < padding) {
            val = '0' + val;
        }

        $(target).hide('slide', {
            'direction': 'down'
        }, 400, function () {
            $(this).text(val).show('slide', {
                'direction': 'up'
            }, 700);
        });

    });
}


// Slider function. Called with interval.
function homeSlider(slideInterval){
	
	"use strict";
		
	// Create array of slides
	var slides = $('.carousel .slide'),
	// Array counter
	i = 0,
	// Slide controls
	switches,
	// Handle for clearing current interval
	intHandle,
	// Cache sunburst selector
	sunburst;

	// Cache sunburst selector
	sunburst = $('.carousel .backing');
			
	// Return next cycled slide			  
	function increment (){
		var nextTick = ( i === 2 ) ? 0 : i += 1;
		return nextTick;
	}
	
	// Add and define controlls
	$('.carousel').append('<div id="controls"><a></a><a></a><a></a></div>');
	switches = $('#controls a');


		// Slide animation
	function slide (){
		
		// Animate controls
		$(switches[i]).animate({'background-position-x': '0px'}, slideInterval, 'linear');
		
		// Set slider animation on delay and supply handle
		intHandle = setTimeout(function(){		
			sunburst.fadeOut(300, function(){
				// Hide on sunburst hidden
				$(slides[i]).fadeOut( 1000 );
				
				// Increment slide
				i = increment();
				if( i === 0 ){ $(switches).css('background-position', '-65px 0'); }
	
				// Show.
				sunburst.fadeIn(300);
				$(slides[i]).fadeIn(1000, 'linear', function(){
					// Schedule again
					slide();
				});
			});
			
		}, slideInterval);
	}

	
		
	// Add click handler to controlls			  
	$(switches).click(function (){
		
		// Check animation is not busy
		if($(sunburst).is(':animated') || $(slides).is(':animated')){
			return false;
		}
		
		// Stop next slide
		clearTimeout(intHandle);
		
		// Get clicked element with same index as slide
		var clicked = $(switches).index(this);
		
		// Stop and complete controls animation
		$(switches).stop(1,1);		
		
		// Change controls
		$(this).css('background-position', '0 0').prevAll().css('background-position', '0 0');
		$(this).nextAll().css('background-position', '-65px 0');
		
		// Toggle current slide and clicked
		$(slides[i]).hide(0);
		i = clicked;
		$(slides[i]).show(0);
		
		// Restart animation
		slide();
	});
		
	// Start animation
	slide();
	
}


// Implementation
$("document").ready(function () {
		
	// Start slider and set interval
	homeSlider(3000);
        
    setTimeout(function () {
        clusters_update();
        setInterval(function () {
            clusters_update();
        }, 30000);
    }, 1500);
    setTimeout(function() {
        zabbix_update('Outgoing traffic on interface $1', '.database span', 6, 'MB');
        setInterval(function () {
            zabbix_update('Outgoing traffic on interface $1', '.database span', 6, 'MB');
        }, 30000);
    }, 2500);
    setTimeout(function () {
        zabbix_update('Total memory', '.traffic span', 4, 'GB');
        setInterval(function () {
            zabbix_update('Total memory', '.traffic span', 4, 'GB');
        }, 30000);
    }, 3500);
        
        
    // Format tweets
	(function(){
	
		var tString,
			accountPre = 'CatNHosting:',
			accountPost = '<span class="title">' + accountPre + '</span>',
			tags = '<span class="tag">',
			tagsArr;
		
		$('.aside .tweets ul li a').each(function(){
		
			$(this).attr('target', '_blank');
			
			tString = $(this).html().replace(accountPre, '');
			
			$(this).before('<a class="title" href="http://twitter.com/CatNHosting" target="_blank">' + accountPre + '</a> ');
			
			// Bold tags
			tString = tString.replace(/[@#](\w+)/gi, '<strong class="tag">$&</strong>');
			$(this).html(tString);
		});
			
	}());

       
});

