var ID = 0;

$(function() 
{
	$.ajax(
	{
		type: "GET",
		url: "jquery/data.xml",
		dataType: "xml",
		success: dataXml
	});
	
	$('.pre').click(function()
	{
		$('.slideshow').cycle('prev'); 
		return false;
	});
	
	$('.next').click(function()
	{
		$('.slideshow').cycle('next'); 
		return false;
	});
	
});

function dataXml(xml)
{
	var f = 0;
	$(xml).find("swf").each(function()
	{
		var title = $(this).attr("title");
		ID = f;		
		var link = $(this).attr("link");
		var swf = $(this).text();
		var swf = "jquery/img/"+ swf.replace(".swf",".png");
		
		
		$('.slideshow').append
		(
			'<a href="'+link+'" target="_new"><img src="'+ swf + '"></a>'
		);
			
		// $('.slidename').append
		// (
			// '<img src="img/dot.png" class="margin">'+
			// '<a href="#" id="'+ID+'">' + title + '</a>'
		// );
		setLinks(ID);
		f++;
	});
	initSlideshow();
	// $('.slidename').append
	// (
		// '<img src="img/dot.png" class="margin">'
	// );	
}

function initSlideshow()
{
	$('.slideshow').cycle(
	{
		fx: 'fade', 
		timeout:15000, 
		speed:3000,
		timeoutFn: calculateTimeout
	});
	$('.page').append('');
	$('.page').append('');
}

function calculateTimeout(currElement, nextElement, opts, isForward) 
{ 
	var timeout = 15000;
    var index = opts.currSlide;
	deactivated();
	setactivision(index);
	return timeout;
} 

function setactivision(index)
{
	var array = new Array();
	for(var i = 0; i<=ID;i++)
	{
		array[i] = i;
	}
	$("#"+array[index]).attr('class',"activ");
}
function deactivated()
{
	for(var i = 0;i <= ID;i++)
	{
		$("#"+i).attr('class',"");
	}
}

function setLinks(ID)
{
	$('#'+ID).click(function()
	{
		deactivated();
		$("#"+ID).attr('class',"activ");
		$('.slideshow').cycle(ID); 
	});
}


