﻿var urlParam;
var round;
var nr;

$(document).ready(function() {
	
	$.get( 'fileadmin/dynamic_ajax_php_banner.php', 
		{
			first: 0,
			last: 1,
		},
		function (data) {
			sumItems = parseInt(jQuery('total', data).text());
			round(sumItems);
			
		}, 'xml'
	);

	function round (num){
		
		nr = Math.floor(Math.random()*(num+1));

		initSlider(nr);
	};
	
	function initSlider(startId) {		
	
		$('#mycarousel').jcarousel({
			// Uncomment the following option if you want items
			// which are outside the visible range to be removed
			// from the DOM.
			// Useful for carousels with MANY items.
	
			//itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
			vertical: true,
			scroll: 1,
			auto:	3,
			start: 0,
			//animation: 3000,
			wrap: 'circular',
			itemLoadCallback: mycarousel_itemLoadCallback,
			itemLastInCallback:{
	            onAfterAnimation: afterAnimation
	        }
		});
	
	};

	activeMenu = $('#navcont').find('a.active').attr('title');
	
	if( activeMenu == 'Fertighaus' ) {
		haus = 'fertig';
	} else if ( activeMenu == 'Passivhaus' ) {
		haus = 'passiv';
	} else if ( activeMenu == 'Minergie-P-Standard' ) {
		haus = 'passiv';
	} else if ( activeMenu == 'Effizienzhaus 40' ) {
		haus = 'passiv';
	} else if ( activeMenu == 'Blockhaus' ) {
		haus = 'block';
	} else {
		haus = 'alle';
	}

	urlParam = 'fileadmin/dynamic_ajax_php_banner.php?typ=' + haus;
	
});


function mycarousel_itemLoadCallback(carousel, state)
{
	// Check if the requested items already exist
	if (carousel.has(carousel.first, carousel.last)) {
		return;
	}	
	
	jQuery.get(
			urlParam,
		{
			first: carousel.first,
			last: carousel.last
		},
		function(xml) {
			mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
		},
		'xml'
	);
};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
	// Set the size of the carousel
	carousel.size(parseInt(jQuery('total', xml).text()));

	jQuery('image', xml).each(function(i) {

		imgInfo = jQuery(this).text().split(',');

		img = imgInfo[0];
		
		link = imgInfo[1]
		
		info = imgInfo[2];
		
		carousel.add(first + i, mycarousel_getItemHTML(img, info, link));

	});
	
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(url, text, link)
{
	return '<a href='+ link +'><span class="hide">'+ text +'</span></a><div class="item"><img src="' + url + '" width="154" height="131" alt="" /></div><div class="info">' + text + '</div>';
};

function afterAnimation(carousel){
    if(carousel.autoStopped){
        carousel.startAuto()
    }
}


