var delay = 7000;
window.addEvent('domready', function(){

	$('mainImage').fx = new Fx.Style($('mainImage'), 'opacity', {duration: 1000, link: 'chain'});
	$('homepage_main_image_text').fx = new Fx.Style($('homepage_main_image_text'), 'opacity', {duration: 1000, link: 'chain'});
	$('feature_txtbox').fx = new Fx.Style($('feature_txtbox'), 'background-color', {duration: 1000, link: 'chain'});
	$('linksBlock').fx = new Fx.Style($('linksBlock'), 'opacity', {duration: 500, link: 'chain'});
	$('story_text_idtag').fx = new Fx.Style($('story_text_idtag'), 'opacity', {duration: 500, link: 'chain'});

	/*autoscroller = setTimeout(function(){ 
		advance(1);
		//alert('test');
	},delay);*/
	
	//$('previous_button').addEvent('click', function(){clearTimeout(autoscroller)});
	//$('next_button').addEvent('click', function(){clearTimeout(autoscroller)});
});

var prevImage = new Image;
prevImage.src = ImagePrev.imagepath;

var prevText = new Image;
prevText.src = ImagePrev.headline;
prevText.onload = function(){ $('previous_button').onclick = function(){ advance(-1); } }

var nextImage = new Image;
nextImage.src = ImageNext.imagepath;

var nextText = new Image;
nextText.src = ImageNext.headline;
nextText.onload = function(){ $('next_button').onclick = function(){ advance(1); } }


function advance(dir){
	
	var getID = (dir > 0) ? ImageNext.id : ImagePrev.id;
	$('previous_button').onclick = function(){};
	$('next_button').onclick = function(){};
	
	var ajax = new Json.Remote('/home_info/getStory.php?id=' + getID + '&Rand=' + (Math.random()*10000),
		{ 
			secure: false,
			onFailure: function(){
			
			},
			onComplete: function(Json){
				
				//homepage_main_image_text
				
				$('mainImage').setStyle('opacity', '1');
				$('homepage_main_image_text').setStyle('opacity', '1');

				var changeTo = (dir > 0) ? ImageNext : ImagePrev;
				var changeToImage = (dir > 0) ? nextImage : prevImage;
				var changeToText = (dir > 0) ? nextText : prevText;
				
				ImagePrev = Json.prev;
				ImageNext = Json.next;
						
				prevImage = new Image;
				prevImage.src = ImagePrev.imagepath;
						
				prevText = new Image;
				prevText.src = ImagePrev.headline;
														
				nextImage = new Image;
				nextImage.src = ImageNext.imagepath;
						
				nextText = new Image;
				nextText.src = ImageNext.headline;					
				
				$('feature_photoLocal').setStyle('background-image', 'url(\'' + changeToImage.src + '\')');
				$('feature_txtLocal').setStyle('background-image', 'url(\'' + changeToText.src + '\')');
					
					

				$('story_text_idtag').fx.start(0).chain(
					function(){
						$('story_text_idtag').setHTML(Json.current.story);
						this.start(1);
					}
					
				);
				$('linksBlock').fx.start(0).chain(
					function(){
						$('story_text_idtag').setHTML(stripslashes(Json.current.story));
						$('linksBlock').empty();
						if (Json.current.albumLink != ''){
							var addLink = new Element('A');
							
							addLink.setHTML('<img src="/images/triangle_red.gif" alt="See Photos" /> SEE PHOTOS');
							addLink.setAttribute('href', Json.current.albumLink);
							
							addLink.inject($('linksBlock'));						
						}						
						if (Json.current.storyLink != ''){
							
							var addLink = new Element('A');
							
							addLink.setHTML('<img src="/images/triangle_red.gif" alt="Read More" /> READ MORE');
							addLink.setAttribute('href', Json.current.storyLink);
							
							addLink.inject($('linksBlock'));
						}	

						if (Json.current.videoEnabled != '0'){
							if ((Json.current.storyLink != '') || (Json.current.albumLink != '')) 
							new Element('BR').inject($('linksBlock'));
							var addLink = new Element('A');
							
							addLink.setHTML('<img src="/images/triangle_red.gif" alt="Video" /> VIDEO: ' + Json.current.videoText);
							addLink.setAttribute('href', Json.current.videoLink);
							
							addLink.inject($('linksBlock'));							
							
							
						}						
						
						
						
						this.start(1);
					}
				);
				$('feature_txtbox').fx.start('#' + changeTo.bgcolor);


		

				$('homepage_main_image_text').fx.start(0).chain(
					function(){ 
						$('homepage_main_image_text').src = changeToText.src;
						$('homepage_main_image_text').setAttribute('alt', Json.current.headline_alt);
						$('homepage_main_image_text').setStyle('opacity', '1');
										
					}
				);
				$('mainImage').fx.start(0).chain(
					function(){ 
						$('mainImage').src = changeToImage.src;
						$('mainImage').setAttribute('alt', Json.current.alt);
						$('mainImage').setStyle('opacity', '1');
						
						$('previous_button').onclick = function(){ advance(-1); };
						$('next_button').onclick = function(){ advance(1); };						
						
										
					}
				);
				
				if ($('which') && $('total')){
					var tmpNum = new Number($('which').getText());
					var tmpMax = new Number($('total').getText());
					var currentNum = (tmpNum + dir > tmpMax) ? 1 : tmpNum + dir;
					if (currentNum < 1) currentNum = tmpMax;
					$('which').setText(currentNum);
					
					
				}

				/*autoscroller = setTimeout(function(){ 
					advance(1);
					//alert('test');
				},delay);				*/

			}
			
			
		}).send();
	
	
	
	

	
	
	
	
}

function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}
