/*  -------------------------------------------  //
//  Projekt:   GRAVIS.de                         //
//  Autor:     Clemens Daum                      //
//  Datum:     16.07.10                          //
//                                               //
//  Dateiname: js/slides_home_noflash.js         //
//  -------------------------------------------- */


/*
* 
*
* Liest fileadmin/xml/home_teaser.xml aus und fügt die Bilder in #slides ein, startet Initialisierung der Slideshow
* Startet Ideenstrahl - wird nach jeder Slide neu gestartet
*
* @package  
* @author   Clemens Daum
* @since    27.05.2010
* @change   16.07.2010
* @access   public
* @param    
*/


	$(document).ready(function(){
		// remove noscript fallbacks
		$('.noscript_fallback_hide').removeClass('noscript_fallback_hide');

		$.get("fileadmin/xml/home_teaser.xml",{},function(xml){
	
			$('item',xml).each(function(i) {
				var imgSrc = $(this).find("img").text();
				var linkUrl = $(this).find("url").text();
	
				var output = '<a class="slides_link" href="'+linkUrl+'">';
				output += '<img class="slide_img" src="http://www.gravis.de/'+imgSrc+'" />';
				output += '</a>';
				
				$("#slides").append(output);
			});
				
			(function($) { 
			    $(function() {
				$('#slides').initialize(
					{
					'control': {
						'prev':   '#prev',
						'pause':  '#pause',
						'play':   '#play',
						'next':   '#next',
						'output': '#output'
					},
					'timeout':   11500,
					'speedout':  1000,
					'speednext': 401, // important for state play/pause (see start_all_elements)
					'after': 'start_all_elements'
				});
			});
			})(jQuery);
			
		});

	});
	
	var param = new Array();
		param['pixel_per_step'] = 200;
		param['max_width'] = 900;
		param['pre_name'] = '#ideenstrahl_element';
		param['max_number_elements'] = 10;
	var available_elements = new Array('2','1','3','4','5','6','7','8','9','10','11','12','13','14');
	var available_tempo = new Array(2500, 2800, 3000, 2600, 1600, 1900, 2000, 2700, 3100);
	var number_current_elements = 0;
	var stop_all = 0;

	var var_opacity_number_from = '1';
	var var_opacity_number_to = '0';
	if (!jQuery.support.opacity) {
		var_opacity_number_from = 'none';
		var_opacity_number_to = 'none';
	}

	function go(element_id, tempo) {
    	if (!stop_all && $(param['pre_name'] + element_id).position().left<(param['max_width']-param['pixel_per_step'])) {
			var direction = 120 + (Math.round(Math.random() * 110));
			$(param['pre_name'] + element_id).animate({'left': '+='+param['pixel_per_step'],'top': direction}, {'duration': tempo,'specialEasing': {'left': 'linear','top': 'swing'},'complete': function() {go(element_id, tempo);}});
    	} else {
	    	stop(element_id, tempo);
    	}
	}

	function start(element_id, tempo) {
    	if (!stop_all) {
			var direction = 120 + (tempo/100*3);
			$(param['pre_name'] + element_id).css({left: '0',top: direction, 'opacity': var_opacity_number_from, 'display':'inline'});
			$(param['pre_name'] + element_id).animate({'left': '+='+(param['pixel_per_step']/4)}, {'duration': (tempo/4),'specialEasing': {'left': 'linear'},'complete': function() {go(element_id, tempo);start_new_element();}});
    	}
	}

	function stop(element_id, tempo) {
		// alert(var_opacity+' - 	'+var_opacity_number_from);
		$(param['pre_name'] + element_id).animate({'left': '+='+(param['pixel_per_step']/4), 'opacity': var_opacity_number_to}, {duration: (tempo/6),specialEasing: {left: 'linear'},complete: function() {$(param['pre_name'] + element_id).css({'display':'none'});}});
		available_elements.unshift(element_id);
		number_current_elements--;
	}

	function start_new_element() {
		if (param['max_number_elements']>number_current_elements) {
			var element_id = available_elements.pop();
			if (element_id) {
				number_current_elements++;
				tempo = available_tempo.pop();
				start(element_id, tempo);
				available_tempo.unshift(tempo);
			}
		}
	}

	function start_all_elements(curr,next,opts) {
		if (opts.speedIn != '401') { // when slidesow is paused
			stop_all = 0;
			start_new_element();
			setTimeout("start_new_element()", 400);
			setTimeout("stop_all_elements()", 5500);
		}
	}

	function stop_all_elements() {
		stop_all = 1;
	}
	
