//<![CDATA[
$(document).ready(function(){
	var sjxs_xml;
	var sjxs_object = new Array();
    var width = $('.sjxs_sliding_content').width();
    var slideTotCount = 0;
    var curSlideNum = 0;
    var slideStart = false;
    var GoInterval = false;
    var timerControl;
      
    function sjxs_init(){
    //display load animation
    loadingImage();
    
    //get xml file
	$.ajax({
                 type: "GET",
                 url: "/scripts/simplexmlslideshow/simplexmlslideshow.xml",
                 dataType: "xml",
                 success: function(xml) {
                     sjxs_xml = xml;
                     $(sjxs_xml).find('image').each(function(i,val) {
				          //console.log($(this));
				          var image = $(this);
				          var content = {
				          	'transition':image.attr('transitiontime'),
				          	'content':image.text()
				          	
				          }
				          
				          sjxs_object.push(content);
				    });
				    
				    
				    //file loaded
					//remove load animation, then trigger starting function
					loadingImage();
					
					slideTotCount = sjxs_object.length;
					sjxs_run_setup();
					//console.log(sjxs_object);
                 }
      }); //close $.ajax(
        
    }
    
    
    var loadingImage = function(){
    	//$(".sjxs_group_container").append('<div class="sjxs_status"><img src="images/ajax-loader.gif" alt="loading"/></div>');
    	//$("#sjxs_status").removeClass('sjxs_hide');
    	
    	
    	if($("#sjxs_status").hasClass('sjxs_hide')){
    		//console.log('on');
    		$("#sjxs_status").removeClass('sjxs_hide');
    	}else{
    		//console.log('off');
    		$("#sjxs_status").addClass('sjxs_hide');
    	}
    	
    }
      
      
    sjxs_init();  
    
    /*
    This is triggered once xml file is loaded
    It initializes/starts the slideshow process
    */
    function sjxs_run_setup(){
    	$(".sjxs_group_container").append('<div class="sjxs_col-group current"></div>');
     
	    //cycle through slides
	   	slideNext();
    }
    
    var slideNext = function(){
    
    if(curSlideNum == 0 && !slideStart){//first one
    	$('.sjxs_col-group')
    	.append('<div class="sjxs_current_img"><img></div>')
    	.find('.sjxs_current_img img')
    	.attr(
    		'src',sjxs_object[curSlideNum].content
    		)
    	.hide();
    	
    	$('.sjxs_col-group')
    	.append('<div class="sjxs_next_img"><img></div>')
    	.find('.sjxs_next_img img')
    	.attr(
    		'src',sjxs_object[curSlideNum+1].content
    		)
    	;//.hide();
    	
    	//show first image
    	$('.sjxs_current_img')
    	.fadeIn();
    	
    	//start timer
    	slideStart = true;
    	++curSlideNum
    	startTimer();
    	
    }else{
 		
    	var currentSlide = function(){
	    	$('.sjxs_current_img')
	    	.fadeOut(500,function(){
	    		/*$(this)
	    		//.attr('src',sjxs_object[curSlideNum].content)
	    		.addClass('sjxs_next_img')
	    		.removeClass('sjxs_current_img');
	    		*/
	    	});
    	}
    	
    	
    	var nextSlide = function(){
	    	$('.sjxs_next_img')
	    	.fadeIn(500,function(){
	    		$('.sjxs_current_img')
	    		.addClass('sjxs_next_img')
	    		.removeClass('sjxs_current_img');
	    		$(this)
	    		.addClass('sjxs_current_img')
	    		.removeClass('sjxs_next_img');
	    		
	    	});
    	}
    	
    	$(currentSlide).add($(nextSlide));
    	    	
	}     
   }
   
   
   //initialize timer
    var startTimer = function(){
			$.timer(3000, function (timer) {
			timerControl = timer;
			
			//console.log(curSlideNum);
			if(!GoInterval){
				if(curSlideNum < slideTotCount){
					curSlideNum = curSlideNum+1;
				}else{
					curSlideNum = 0;
				}
				if(curSlideNum == slideTotCount){
					curSlideNum = 0;
				}
			          slideNext();
			          
			          loadNext();
				
				notrun = false;
				timer.reset(3000);
			}else{
				timer.stop();
				//console.log('stop');
			}
				
		      
		   });
	}
	
	var loadNext = function(){
		//console.log(width);
		//$('.sjxs_loading')
		//.stop());
		timerControl.stop();
		loadingImage();
		//console.log('loading next');
		
		var img = new Image();
		$(img).load(function(){
			//$(this).hide();
			$('.sjxs_next_img img').remove();
			loadingImage();
			$('.sjxs_next_img').append(this);
			//$(this).fadeIn();
			/*$('.sjxs_loading')
				.stop()
				.css({
					'width':0,
				}).animate({
					'height':20,
					'width':'5%',
					'opacity':1
				},250,'linear')
				.animate({
					'width':(width)
				},2350,function(){
					$(this).animate({
						'height':0,
						'opacity':0
					},250);
				});*/
		}).error(function(){
		
		})
		.attr('src',sjxs_object[curSlideNum].content);
		
		
		
				
				
	}
/*
//setup slides
	var GoInterval = delayTimer(10000);
	var slideChilds = $('.sliding_content .col-group').length; //get how many groups there are
	var slideW = $('.sliding_content').width();//find width to play with
	$('.sliding_content .group_container').width(slideW*slideChilds); //set width of group container
	var curNum = 1;
	
	//setup positions for groups
	var x = 0;
	$('.sliding_content .col-group').each(function(i){
		
		$(this).css({
			'width':slideW,
			'left':x,
			'display':'block'
		});
		x = x+slideW;
	});
	
	var slideNext = function(){
	
		var nxtChild;
		//find objects
		var curChild = $('.sliding_content .group_container').children('.col-group.current');
	
	//check if it's animating already
	if (!$(curChild).is(':animated') ) {	
		
		//get the position of the placeholder element
 		var pos = curChild.position();  
  		var width = curChild.width();	
		
		//the previous child needs to be the last child of th group, so we can do some cycling
		//console.log((pos.left*-1)+width,slideChilds*slideW);
		if(curChild.attr('id') == $('.sliding_content .group_container').children('.col-group:first').attr('id')){
			nxtChild = curChild.siblings('.col-group:first');
			curNum = 1;
			//console.log('back to the beginning');
		}else{
			nxtChild = curChild.prev('.col-group:first');
			curNum = curNum-1;
			//console.log('prev one');
		}
			
		//check positioning
		nxtChild.animate({
			'left':pos.left+width
		},0,'linear',function(){
			var posN = nxtChild.position();
			
			curChild.removeClass('current');  
			$(this).addClass('current');
			
			//need to set the cur left position, then animate both the current and next group at the same time
			curChild.animate({
				"left":(pos.left-width)
			},1200,'swing');
			
			//need to set the nxt left position, while animating both
			nxtChild.animate({
				"left":(posN.left-width+pos.left)
			},1200,'swing',function(){
				$(this).css({'cursor':'pointer'});
			});
		});
		return false;	
		}else{
			$(this).css({'cursor':'default'});
		}

	};
	
	slidePrev = function(){
	

	};
	
	//setup prev button
	$('.sliding_content .next').click(function() {
				slideNext();					
	});
		
	//setup prev button
	$('.sliding_content .prev').click(function() {
	 
		var nxtChild;
		//find objects
		var curChild = $('.sliding_content .group_container').children('.col-group.current');
	
	//check if it's animating already
	if (!$(curChild).is(':animated') ) {	
		
		//get the position of the placeholder element
 		var pos = curChild.position();  
  		var width = curChild.width();	
		
		//the previous child needs to be the last child of th group, so we can do some cycling
		//console.log((pos.left*-1)+width,slideChilds*slideW);
		if(curChild.attr('id') == $('.sliding_content .group_container').children('.col-group:last').attr('id')){
			nxtChild = curChild.siblings('.col-group:first');
			//console.log('beginn');
			curNum = 1;
		}else{
			nxtChild = curChild.next('.col-group:first');
			//console.log('def');
			curNum = curNum+1;
			//console.log('next one');
		}
			
		//check positioning
		nxtChild.animate({
			'left':pos.left-width
		},0,'linear',function(){
			var posN = nxtChild.position();
			
			curChild.removeClass('current');  
			$(this).addClass('current');
			
			//need to set the cur left position, then animate both the current and next group at the same time
			curChild.animate({
				"left":(pos.left+width)
			},1200,'swing');
			//need to set the nxt left position, while animating both
			nxtChild.animate({
				"left":(posN.left+width+pos.left)
			},1200,'swing',function(){
				$(this).css({'cursor':'pointer'});
			});
			
			
		});
		return false;
	}else{
			$(this).css({'cursor':'default'});
		}								
		});
*/
		
		function delayTimer(delay){
			     var timer;
			     return function(fn){
			          timer=clearInterval(timer);
			          if(fn)
			               timer=setInterval(function(){
			               fn();
			               },delay);
			          return timer;
			     }
			}
		
});