﻿function configuraNews() {
	$$('div.chips_riquadro').each(function(element) {
		element.observe('mouseenter', function(event) {
			clearTimeout(timerNews);
			
			/*
			if ($("Progettazione")) {
				$$('div.chips_NewsText').each(function(element) {
					element.style.background = 'url(http://www.chipsmachine.com/chips/immagini/10_over.png) 0 0 repeat';
				})
			}
			*/
			
			if (numeroNews <= 1) {
				return false;
			} else {
				if (newsControlli == 'newsControlli_ON') {
					$("contFrecce").style.display = 'block';
				}
			}
			Event.stop(event);
		})
		element.observe('mouseleave', function(event) {
			clearTimeout(timerNews);
			
			if ($("Progettazione")) {
				$$('div.chips_NewsText').each(function(element) {
					element.style.backgroundImage = '';
					element.style.backgroundColor = '';
					element.style.backgroundPosition = '';
					element.style.backgroundRepeat = '';
					element.style.backgroundAttachment = '';
					element.style.backgroundOrigin = '';
					element.style.backgroundClip = '';
				})
			}
			
			$("contFrecce").style.display = 'none';
			if (newsAutostart == 'newsAutostart_ON') {
				if (numeroNews != 1) {
					timerNews = setTimeout('autoScrollNews();', (newsTime * 1000));
				}
			}
			Event.stop(event);
			//bindAsEventListener(element)
		})
		if ($("Progettazione")) {
			element.observe('click', function(event) {
				//scalaNews();
				Event.stop(event);
			})
		}
	})
	
	if ($("Progettazione")) {  // blocca lo scorrimento con il mouse sulle palette di progettazione
		$$('#Bloque_Add','#Bloque_Media').each(function(element) {
			element.observe('mouseenter', function(event) {
				clearTimeout(timerNews);
			})
			element.observe('mouseleave', function(event) {
				clearTimeout(timerNews);
				$("contFrecce").style.display = 'none';
				if (newsAutostart == 'newsAutostart_ON') {
					if (numeroNews != 1) {
						timerNews = setTimeout('autoScrollNews();', (newsTime * 1000));
					}
				}
				Event.stop(event);
			})
		})
	}
	
	/*
	if ($("Progettazione")) {  // blocca lo scorrimento con il mouse sulle palette di progettazione
		$$('div.chips_NewsText').each(function(element) {
			element.observe('mouseenter', function(event) {
				this.style.background = 'url(http://www.chipsmachine.com/chips/immagini/10_remove.png)  repeat !important';
				Event.stop(event);
			})
			element.observe('mouseleave', function(event) {
				this.style.background = 'url(http://www.chipsmachine.com/chips/immagini/blank.gif)  repeat !important';
				Event.stop(event);
			})
		})
	}
	*/
}


if (window.addEventListener) {
window.addEventListener("load", initNews, false);
} else if (window.attachEvent) {
window.attachEvent("onload", initNews);
} else {
window.onload = initNews;
}



var stopScroll = 0; //non cancellare (per retro compatibilità)
var timerNews;
var numeroNews;
var counterNews;
var bloccoNews;
var w_cont;
var h_cont;
var w_spostam;




function initNews() {
	
	configuraNews();
	
	if (newsAutostart == 'newsAutostart_ON') {
		autoScrollNews();
	}
	
	$("newsScrollBox").style.left = "0px";
	$("newsScrollBox").style.top = "0px";
	$("newsScrollBox").style.opacity = "";
	$("contFrecce").style.display = "none";
	$("contFrecce").style.opacity = "";
	$("FrecciaGiu").style.display = "block";
	$("FrecciaSu").style.opacity = "";
	$("FrecciaSu").style.backgroundImage = "";
	$("FrecciaGiu").style.opacity = "";
	$("FrecciaGiu").style.backgroundImage = "";
	
	numeroNews = -1;
	counterNews = 1;
	bloccoNews = 0;
	
	//if (timerNews != null) { clearTimeout(timerNews); }
	
	if ($("Progettazione")) {
		if (newsDirection == "h") { document.newsOptionForm.scrollNewsRadio[1].checked=true; }
	}
	
	if (newsFade == 'newsFade_ON') {
		if ($("Progettazione")) { $("newsFade_cb").checked = true; }
		} else {
		if ($("Progettazione")) { $("newsFade_cb").checked = false; }
		};
		
	if (newsTitle == 'newsTitle_ON') {
		if ($("Progettazione")) { $("newsTitle_cb").checked = true; }
		$("news_title").style.display = "block";
		} else {
		if ($("Progettazione")) { $("newsTitle_cb").checked = false; }
		$("news_title").style.display = "none";
		};
		
	if (newsSchede == 'newsSchede_ON') {
		if ($("Progettazione")) { $("newsSchede_cb").checked = true; }
		$$('#newsQuadro div.chips_NewsText').each(function(element) {
			element.style.cursor = "pointer";
			})
		} else {
		if ($("Progettazione")) { $("newsSchede_cb").checked = false; }
		$$('#newsQuadro div.chips_NewsText').each(function(element) {
			element.style.cursor = "text";
			})
		};
	
	if (newsControlli == 'newsControlli_ON') {
		if ($("Progettazione")) { $("newsController_cb").checked = true; }
		} else {
		if ($("Progettazione")) { $("newsController_cb").checked = false; }
		$("contFrecce").style.display = "none";
		};
	
	if ($("Progettazione")) {
		if (newsAutostart == 'newsAutostart_ON') {
			$("newsAutostart_cb").checked = true;
			} else { $("newsAutostart_cb").checked = false; $('newsLoop_cb').disabled = true; $('newsTimer_input').disabled = true; $('newsController_cb').disabled = true; };
		$('newsTimer_input').value = newsTime;
		//$('newsDuration_input').value = newsTrans;
		if (newsRepeat == 'newsRepeat_ON') { $("newsLoop_cb").checked = true;
			} else { $("newsLoop_cb").checked = false; }
	}
	
	// Centrale
	if ($("newsQuadro").parentNode.parentNode.parentNode.parentNode.id == "contents") {
		//$("newsBox").className = "chips_TextBox";
		w_cont = $("mainBox").getWidth();
		h_cont = $("newsQuadro").getHeight();
		w_spostam = w_cont - 30;
		$("newsBox").style.width = w_cont -28 + "px";
		$("newsBox").style.marginLeft = 10 + "px";
		$("newsQuadro").style.width = w_cont -30 + "px";
			$$('#newsQuadro div.chips_WrapTextBox').each(function(element) {
				element.style.width = w_cont -36 + "px";
				element.style.height = h_cont -8 + "px";
				if (newsDirection == "h") {
					element.style.float = "left";
				} else {
					element.style.float = "none";
				}
			})
			$$('#newsQuadro div.chips_bannerNewsBox').each(function(element) {
				element.style.width = w_cont -44 + "px";
			})
		if (newsDirection == "h") {
			$('FrecciaSu').style.backgroundImage = "url('http://www.chipsmachine.com/chips/immagini/frecceNewsLeft.jpg')";
			$('FrecciaGiu').style.backgroundImage = "url('http://www.chipsmachine.com/chips/immagini/frecceNewsRight.jpg')";
		} else {
			$('FrecciaSu').style.backgroundImage = "url('http://www.chipsmachine.com/chips/immagini/frecceNewsSu.jpg')";
			$('FrecciaGiu').style.backgroundImage = "url('http://www.chipsmachine.com/chips/immagini/frecceNewsGiu.jpg')";
		}
		$('FrecciaSu').style.display = "none";
		for (i=0; i < $("newsScrollBox").childNodes.length; i++) {
			numeroNews++;
		}
		if (numeroNews == 1) {
			$('FrecciaGiu').style.display = "none";
		}
	}
	
	// Colonne
	if (($("newsQuadro").parentNode.parentNode.parentNode.parentNode.id == "leftBox") || ($("newsQuadro").parentNode.parentNode.parentNode.parentNode.id == "rightBox")) {
		if ($("newsQuadro").parentNode.parentNode.parentNode.parentNode.id == "leftBox") {
			w_cont = $("leftBox").getWidth();
		} else {
			w_cont = $("rightBox").getWidth();
		}
		h_cont = $("newsQuadro").getHeight();
		w_spostam = w_cont - 6;
		$("newsBox").style.width = "";
		$("newsBox").style.marginLeft = "";
		$("newsQuadro").style.width = w_cont -6 + "px";
			$$('#newsQuadro div.chips_WrapTextBox').each(function(element) {
				element.style.width = w_cont -12 + "px";
				if (h_cont >= 8) {
				element.style.height = h_cont -8 + "px";
				} else {
				element.style.height = "0px";
				}
				if (newsDirection == "h") {
					element.style.float = "left";
				} else {
					element.style.float = "none";
				}
			})
			$$('#newsQuadro div.chips_bannerNewsBox').each(function(element) {
				element.style.width = w_cont -20 + "px";
			})
		if (newsDirection == "h") {
			$('FrecciaSu').style.backgroundImage = "url('http://www.chipsmachine.com/chips/immagini/frecceNewsLeft.jpg')";
			$('FrecciaGiu').style.backgroundImage = "url('http://www.chipsmachine.com/chips/immagini/frecceNewsRight.jpg')";
		} else {
			$('FrecciaSu').style.backgroundImage = "url('http://www.chipsmachine.com/chips/immagini/frecceNewsSu.jpg')";
			$('FrecciaGiu').style.backgroundImage = "url('http://www.chipsmachine.com/chips/immagini/frecceNewsGiu.jpg')";
		}
		$('FrecciaSu').style.display = "none";
		for (i=0; i < $("newsScrollBox").childNodes.length; i++) {
			numeroNews++;
		}
		if (numeroNews == 1) {
			$('FrecciaGiu').style.display = "none";
		}
	}	
}

function autoScrollNews() {	
	if (timerNews != null) {
		//alert("n. " + counterNews + " su " + numeroNews);
		if (numeroNews <= 1) {
			return false;
		}
		if (counterNews == 1) { new Effect.Appear("FrecciaSu", {duration:0.6, from: 0.0, to: 1.0}); }
		if (counterNews == numeroNews -1) { new Effect.Fade("FrecciaGiu", {duration:0.6}); }
		
		if (counterNews == numeroNews) {
			if (newsRepeat == 'newsRepeat_OFF') {
				clearTimeout(timerNews);
				return false;
			}
			new Effect.Fade('newsScrollBox', { duration:0.6,
			afterFinish: function() { $("newsScrollBox").style.top = "0px"; $("newsScrollBox").style.left = "0px"; new Effect.Appear("newsScrollBox", {duration:0.6, from: 0.0, to: 1.0}); }
			});
			new Effect.Appear("FrecciaGiu", {duration:0.6, from: 0.0, to: 1.0});
			new Effect.Fade("FrecciaSu", {duration:0.6});
			counterNews = 1;
			clearTimeout(timerNews);
			timerNews = setTimeout('autoScrollNews();', (newsTime * 1000));
			return false;
		}
		clearTimeout(timerNews);
			if (newsDirection == "h") {
				if (newsFade == 'newsFade_ON') {
					var leftBox = $("newsScrollBox").style.left;
					leftBox = leftBox.replace("px","");
					new Effect.Fade('newsScrollBox', { duration:0.6,
					afterFinish: function() { $("newsScrollBox").style.left = leftBox -w_spostam +"px"; setTimeout(appearScrollBox(), 1300) }
					});
				} else {
					MoveRight();
				}
			} else {
				if (newsFade == 'newsFade_ON') {
					var topBox = $("newsScrollBox").style.top;
					topBox = topBox.replace("px","");
					new Effect.Fade('newsScrollBox', { duration:0.6,
					afterFinish: function() { $("newsScrollBox").style.top = topBox -(h_cont-3) +"px"; setTimeout(appearScrollBox(), 1300) }
					});
				} else {
					MoveDown();
				}
			}
		counterNews++;
	}
	timerNews = setTimeout('autoScrollNews();', (newsTime * 1000));
}


function appearScrollBox() {
	new Effect.Appear("newsScrollBox", {duration:0.6, from: 0.0, to: 1.0});
}



function MoveUp() {
	bloccoNews = 1;
	if (counterNews == 2) { new Effect.Fade("FrecciaSu", {duration:0.6}); }
	if (counterNews == numeroNews) { new Effect.Appear("FrecciaGiu", {duration:0.6, from: 0.0, to: 1.0}); }
    new Effect.MoveBy('newsScrollBox', h_cont-3, 0, {
		duration: newsTrans,  
		transition: Effect.Transitions.sinoidal,
		afterFinish: function() { bloccoNews = 0; }
	});
}
function MoveDown() {
	bloccoNews = 1;
	if (counterNews == 1) { new Effect.Appear("FrecciaSu", {duration:0.6, from: 0.0, to: 1.0}); }
	if (counterNews == numeroNews -1) { new Effect.Fade("FrecciaGiu", {duration:0.6}); }
    new Effect.MoveBy('newsScrollBox', -(h_cont-3), 0, {
		duration: newsTrans,  
		transition: Effect.Transitions.sinoidal,
		afterFinish: function() { bloccoNews = 0; }
	});
}


function MoveLeft() {
	bloccoNews = 1;
	if (counterNews == 2) { new Effect.Fade("FrecciaSu", {duration:0.6}); }
	if (counterNews == numeroNews) { new Effect.Appear("FrecciaGiu", {duration:0.6, from: 0.0, to: 1.0}); }
	
    new Effect.MoveBy('newsScrollBox', 0, w_spostam, {
		duration: newsTrans,  
		transition: Effect.Transitions.sinoidal,
		afterFinish: function() { bloccoNews = 0; }
	});
}
function MoveRight() {
	bloccoNews = 1;
	if (counterNews == 1) { new Effect.Appear("FrecciaSu", {duration:0.6, from: 0.0, to: 1.0}); }
	if (counterNews == numeroNews -1) { new Effect.Fade("FrecciaGiu", {duration:0.6}); }
	
    new Effect.MoveBy('newsScrollBox', 0, -(w_spostam), {
		duration: newsTrans,  
		transition: Effect.Transitions.sinoidal,
		afterFinish: function() { bloccoNews = 0; }
	});
}


function scrollNewsV() {
	if (counterNews == numeroNews) {
		return false;
	}
	if (bloccoNews == 1) {
		return false;
	}
	if (newsDirection == "h") {
		MoveRight();
	} else {
		MoveDown();
	}
	counterNews++;
	//alert(counterNews);
}
function scrollNewsDownV() {
	//alert(counterNews);
	if (counterNews == 1) {
		return false;
	}
	if (bloccoNews == 1) {
		return false;
	}
	if (newsDirection == "h") {
		MoveLeft();
	} else {
		MoveUp();
	}
	counterNews--;
}

// questi in realtà sono gli stop anche se si chiamano scroll...
function scrollNews() {
	if (timerNews != null) {
		clearTimeout(timerNews);
	}
}
function scrollNewsDown() {
	if (timerNews != null) {
		clearTimeout(timerNews);
	}
}


var altezzaBoxNews;
var memoDirection;
var memoAutostart;
var org_News = 0;

function orgNews() {
	org_News = 1;
	altezzaBoxNews = $('newsQuadro').getStyle('height');
	memoDirection = newsDirection;
	memoAutostart = newsAutostart;
	
	$('ModifNewsButton').disabled = true;
	$('OrgNewsButton').value = '      Ok      ';
	$('OrgNewsButton').setAttribute('onclick', "orgNews_Ok()");
	
	newsDirection = "v";
	newsAutostart = "newsAutostart_OFF";
	initNews;
	clearTimeout(timerNews);
	
	/////////////////////////////////////alert($('intmenu1236858352488').getStyle("font-size"));
	
	var s = 0;  
	$$('#newsQuadro div.chips_WrapTextBox').each(function(element) {
			element.style.height = "";
			var h = element.getStyle('height');
			h = h.replace('px', '') * 1;
			s += h;
	})
	
	//s = s +'px';
	$('newsScrollBox').style.top = "0px";
	$('newsQuadro').style.border = "1px dashed #00ff00";
  	$('newsQuadro').style.left = "-1px";
  	$('newsQuadro').style.top = "-1px";
  	//show_div();
  	rendisortable();
  	header_dragabile();
  	new Effect.Morph('newsQuadro', {style:'height:'+ s +'px', duration:1.1});
  	new Effect.Morph('newsScrollBox', {style:'height:'+ s +'px', duration:1.1});
}


function orgNews_Ok() {
	
	$('ModifNewsButton').disabled = false;
	$('OrgNewsButton').value = 'Organizza';
	$('OrgNewsButton').setAttribute('onclick', "orgNews()");
	
	newsDirection = memoDirection;
	newsAutostart = memoAutostart;
	
	$('newsQuadro').style.border = "";
	$('newsQuadro').style.left = "";
  	$('newsQuadro').style.top = "";
	
	new Effect.Morph('newsQuadro', {style:'height:'+ altezzaBoxNews, duration:1.1});
  	new Effect.Morph('newsScrollBox', {style:'height:'+ altezzaBoxNews, duration:1.1});
  	$$('#newsQuadro div.chips_WrapTextBox').each(function(element) {
			element.style.height = altezzaBoxNews;
	})
  	initNews;
  	org_News = 0;
}

