/*
Copyright (c) 2007 Mediasparx (http://www.mediasparx.com)
Notes: Teaser display
*/
$.teaserObjArray = new Array();
$.fn.teaser = function(passedOptions) {
    var lastCounter = -1;
	var options = {
	    teaserID: '',
		delay: 1000,
		source: '',
		speed: 'slow',
        type: 'random',
		contentcounter: -1,
		ArrayIndex: -1,
		dataXML: {
		},
		timerid: -1, tempid: -1,
		setXML: function(ArrayIndex) {
			$.get(options.source, function(data){
				options.dataXML = data;
				options.FillSlide();
			});
		},
		FillSlide: function() {
			clearTimeout(options.tempid);
            if ( options.type == 'random' ) {
                while (options.contentcounter == lastCounter) {
                    options.contentcounter = Math.floor ( Math.random ( ) * ( $("item", options.dataXML).length ) );
                }
                lastCounter = options.contentcounter;
            } else {
			    options.contentcounter++;
		    	if (options.contentcounter == $("item", options.dataXML).length) {
	    			options.contentcounter = 0;
    			}
            }
			var Item = $('item', options.dataXML).get(options.contentcounter);
            var Content = $('content', Item).text();
//   			var teaserHTML = '<div class="teaserInner">'+ Content +'</div>';
   			var teaserHTML = ''+ Content +'';
            $('#'+ options.teaserID).empty().append(teaserHTML);
			options.EnterSlide();
		},
	    ExitSlide: function() {
			clearTimeout(options.timerid);
			$('#'+ options.teaserID).fadeOut(options.speed, options.ExitSlideStep2());
	    },
	    ExitSlideStep2: function() {
			options.tempid = setTimeout('$.teaserObjArray['+ options.ArrayIndex +'].FillSlide();', 500);
		},
	    EnterSlide: function() {
			$('#'+ options.teaserID).fadeIn(options.speed, options.EnterSlideStep2());
	    },
	    EnterSlideStep2: function() {
			options.timerid = setTimeout('$.teaserObjArray['+ options.ArrayIndex +'].ExitSlide();', options.delay);
		}
	};
	if (passedOptions) {
		$.extend(options, passedOptions);
	}
	return this.each(function(){
		options.teaserID = this.id;
		$.teaserObjArray.push(options);
		options.ArrayIndex = $.teaserObjArray.length - 1;
		$('#'+ options.teaserID).hover(function() {
			clearTimeout(options.timerid);
		}, function() {
			options.timerid = setTimeout('$.teaserObjArray['+ options.ArrayIndex +'].ExitSlide();', 500);
		});
		$.teaserObjArray[options.ArrayIndex].setXML(options.ArrayIndex);
	});
};

$(function() {
    $('#slogon').teaser({
   		delay: 12000,
	    source: '/templates/visitkragero/Xml/teaser.xml',
    	speed: 'slow'
   	});
});