/*
	Image Cross Fade Redux
	Version 1.0
	Last revision: 02.15.2006
	steve@slayeroffice.com

	Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html
*/

window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false;

var section = '';

var loadingFrame = 1;

var continueAnimation = true;

var boxClosed = -1;

var boxHeightOriginal = 0;

var selSection = 1;
var sectionBlock = 0;

var tabIndex = 1;

function so_init()
{
	if(!d.getElementById || !d.createElement)return;

	imgs = d.getElementById('slideshow').getElementsByTagName('img');
	for(i=1;i<imgs.length;i++){
		imgs[i].xOpacity = 0;
		imgs[i].style.width = "100%";
		imgs[i].style.height = "100%";
	}
	imgs[0].style.display = 'block';
	imgs[0].xOpacity = .99;
	imgs[0].style.width = "100%";
	imgs[0].style.height = "100%";

	d.getElementById('slideshow').style.width = "100%";
	d.getElementById('slideshow').style.height = "100%";
	
	var layer = d.getElementById('secondLayer').xOpacity = 100;

  tabIndex = isNaN(selectedTabMenu.currentTabIndex+1) ? 1 : selectedTabMenu.currentTabIndex+1;
	
	//mid definito in joomla_root/modules/mod_tpajaxtab/tmpl/default.php r.78
	//selectedTabMenu definito in joomla_root/modules/mod_tpajaxtab/ajaxtabs/ajaxtabs.js r.202
  d.getElementById('curr').src = baseUrl+"images/bkg" + mid + "-" + tabIndex + ".jpg";
  
	animateLoading();
	preloadBox();

	var objImage = new Image();

  objImage.onLoad=imageLoaded();
  objImage.src = baseUrl+"images/bkg" + mid + "-" + tabIndex + ".jpg";

}

function imageLoaded(){
	setTimeout("layerOpacity('secondLayer', -1, false)", 500);
	//setTimeout("stopAnimation()", 500);
}

function stopAnimation(){
  continueAnimation = false;
}

function animateLoading() {
	d.getElementById("box_loading_animation").style.top = (loadingFrame * -40) + 'px';
	loadingFrame = (loadingFrame + 1) % 12;
  if(continueAnimation)
    setTimeout("animateLoading()", 66);
}

function preloadBox(){
  var boxW = d.getElementById('img_loading').style.display ='block';
  var boxW = d.getElementById('img_loading').style.width;
  var boxH = d.getElementById('img_loading').style.height;
  boxW = boxW.replace("px", "");
  boxH = boxH.replace("px", "");

  var w = document.body.clientWidth;
  w = (w/2) - (boxW/2);
  var h = document.body.clientHeight;
  h = (h/2) - (boxH/2);

  d.getElementById('img_loading').style.left = w + "px";
  d.getElementById('img_loading').style.top = h + "px";
}

function so_xfade(selectedSection)
{
  setSection(selectedSection);
    
	var layer = d.getElementById('secondLayer');
	layer.xOpacity = 0;
	
	setOpacity(layer);

	layer.style.display = 'block';

	layerOpacity('secondLayer', 1, true);

	nIndex = imgs[current+1]?current+1:0;
	imgs[nIndex].src = baseUrl+"images/bkg" + mid + "-" + (selectedTabMenu.currentTabIndex+1) + ".jpg";


    selSection = 1;
    sectionBlock = 0;
}

function so_xfade2(dir)
{
    var section = (selSection + dir);
    selSection = section;
    var sectionLimit = parseInt(d.getElementById('sectionLimit').value);
    var minLimit = sectionBlock + 1;
    var maxLimit = sectionBlock + sectionLimit;
    if(section > minLimit){
        d.getElementById('pulsante_indietro').style.display = "block";
    }
    else{
        d.getElementById('pulsante_indietro').style.display = "none";
    }
    
    if(section < maxLimit){
        d.getElementById('pulsante_avanti').style.display = "block";
    }
    else{
        d.getElementById('pulsante_avanti').style.display = "none";
    }
    
    setSection("tpajaxtabs" + mid + "divcontainer");
	var layer = d.getElementById('secondLayer');
	layer.xOpacity = 0;
	
	setOpacity(layer);

	layer.style.display = 'block';

	layerOpacity('secondLayer', 1, true);

	nIndex = imgs[current+1]?current+1:0;
    if(section == 1)
        imgs[nIndex].src = baseUrl+"images/bkg" + mid + "-" + (selectedTabMenu.currentTabIndex+1) + ".jpg";
    else 
        imgs[nIndex].src = baseUrl+"images/bkg" + mid + "-" + (selectedTabMenu.currentTabIndex+1) + "-"+ section +".jpg";
        

    if(d.getElementById('pensilineContainer1')){
        var containerId = 1;
        if(section > 3)
            containerId = 2;
        if(section > 5)
            containerId = 3;
        
        for(var i = 1; i<4; i++){
            if(i != containerId)
                d.getElementById('pensilineContainer'+i).className = 'pensilineContainer selected';
            else
                d.getElementById('pensilineContainer'+i).className = 'pensilineContainer';
        }
    }

    articleSection(section);
}

function so_xfade2plus(section, limit)
{
    selSection = section;
    sectionBlock = section;
    so_xfade2(1);
    d.getElementById('sectionLimit').value = limit;
}

function makeFade(){
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;
	nOpacity = imgs[nIndex].xOpacity;

    sOpacity = section.xOpacity;

	cOpacity-=.05;
	nOpacity+=.05;

	sOpacity+=.05;

	imgs[nIndex].style.display = 'block';
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
    section.xOpacity = sOpacity;

	setOpacity(imgs[current]);
	setOpacity(imgs[nIndex]);

	setOpacity(section);
    

	if(cOpacity<=0)
	{
		imgs[current].style.display = 'none';
		current = nIndex;
		layerOpacity('secondLayer', -1, true);
	}
	else
	{
		setTimeout(makeFade,50);
	}

	d.getElementById('slideshow').style.width = "100%";
	d.getElementById('slideshow').style.height = "100%";
	
	imgs[current].style.width = "100%";
	imgs[current].style.height = "100%";
	
}

function layerOpacity(element, direction, fadeActive){
	var layer = d.getElementById(element);
	
	if(direction == 1){
		layer.xOpacity += .05;
    }
	if(direction == -1){
		layer.xOpacity -= .05;
	}
	
	setOpacity(layer);

	if(direction == 1)
	{
		if (layer.xOpacity<.99)
			setTimeout("layerOpacity('"+element+"', "+direction+", "+fadeActive+")", 50);
		else if(fadeActive) makeFade();
	}
	if(direction == -1)
	{
		if (layer.xOpacity>0)
			setTimeout("layerOpacity('"+element+"', "+direction+", "+fadeActive+")", 50);
		else{
			layer.style.display = 'none';
		}
	}
}

function layerOpacity2(element, direction){
	var layer = '';
  var sections = document.getElementById('article_sections'+(selectedTabMenu.currentTabIndex+1));
  var sectionsCount = sections.children.length;
	for(var i=1;i<sectionsCount;i++){
    var secId = sections.children[i].id; 
    if(secId == element){
      layer = sections.children[i];
    }
	}
	
	if(direction == 1){
		layer.xOpacity += .05;
    }
	if(direction == -1){
		layer.xOpacity -= .05;
	}
	
	setOpacity(layer);

	if(direction == 1)
	{
		if (layer.xOpacity<.99)
			setTimeout("layerOpacity2('"+element+"', "+direction+")", 50);
	}
	if(direction == -1)
	{
		if (layer.xOpacity>0)
			setTimeout("layerOpacity2('"+element+"', "+direction+")", 50);
		else{
			layer.style.display = 'none';
		}
	}
}

function layerOpacity3(element, direction){
	var layer = d.getElementById(element);
	
	if(direction == 1){
		layer.xOpacity += .05;
    }
	if(direction == -1){
		layer.xOpacity -= .05;
	}
	
	setOpacity(layer);

	if(direction == 1)
	{
		if (layer.xOpacity<.99)
			setTimeout("layerOpacity3('"+element+"', "+direction+")", 50);
	}
	if(direction == -1)
	{
		if (layer.xOpacity>0)
			setTimeout("layerOpacity3('"+element+"', "+direction+")", 50);
		else{
			layer.style.display = 'none';
		}
	}
}

function setOpacity(obj)
{
	if(obj.xOpacity>.99)
	{
		obj.xOpacity = 1;
	}
	if(obj.xOpacity<-0.1)
	{
		obj.xOpacity = 0;
	}

	obj.style.opacity = obj.xOpacity;
	obj.style.MozOpacity = obj.xOpacity;
	obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
}


function setSection(selectedSection){
    section = d.getElementById(selectedSection);
    section.xOpacity = 0;
    setOpacity(section);
}

function articleSection(section){
  var sections = document.getElementById('article_sections'+(selectedTabMenu.currentTabIndex+1));
  var sectionsCount = sections.children.length;
  var selectedDiv='';
  var selectedId='';
	for(var i=1;i<sectionsCount;i++){
    var secId = sections.children[i].id; 
    if(secId.indexOf("article_section") === 0){
      if(secId != 'article_section'+section){
        var sectionDiv = sections.children[i];
      	sectionDiv.style.display = 'none';
      }
      else {
        selectedDiv = sections.children[i];
        selectedId = secId;
      }
    }
	}
	selectedDiv.style.display = 'block';
  sections.style.height = (40 + selectedDiv.clientHeight) +"px";
}

	//richiamato in joomla_root/modules/mod_tpajaxtab/ajaxtabs/ajaxtabs.js r.74
function initArticleSections(){
  var sections = document.getElementById('article_sections'+(tabIndex));
  if(sections != null){
    var sectionsCount = sections.children.length;
  	for(i=1;i<sectionsCount;i++){
      if(sections.children[i].id == "article_section1"){
        var sectionDiv = sections.children[i];
        sections.style.height = (40 + sectionDiv.clientHeight) +"px";
      }
  	}
    sections.style.height = (40 + sectionDiv.clientHeight) +"px";
  }
}

	//richiamato in joomla_root/templates/ja_purity2/index.php r.314
function initArticleSections2(){
  var sections = document.getElementById('article_sections');
  if(sections != null){
    var sectionsCount = sections.children.length;
  	for(i=0;i<sectionsCount;i++){
      if(sections.children[i].id == "article_section1"){
        var sectionDiv = sections.children[i];
      }
  	}
    sections.style.height = (64 + sectionDiv.clientHeight) +"px";
  }
}

function articleSection2(section){
  var sections = d.getElementById('article_sections');
  var sectionsCount = sections.children.length - 1;
  var sectionDiv = d.getElementById('article_section' + section);
	for(i=1;i<=sectionsCount;i++){
    if(i != section){
    	d.getElementById('article_section' + i).xOpacity = 100;
      layerOpacity3('article_section' + i, -1);
    }
	}
	sectionDiv.xOpacity = 0;
  layerOpacity3('article_section' + section, 1);
	sectionDiv.style.display = 'block';
  sections.style.height = (64 + sectionDiv.clientHeight) +"px";
}

function closeBox(){
    //devo togliere il padding che viene conteggiato
    var fakeBox = d.getElementById("fakeBox");
    var divContainer = d.getElementById("tpajaxtabs" + mid + "divcontainer");
        boxHeightOriginal = divContainer.clientHeight;
    if(boxClosed == -1){
        fakeBox.boxHeight = boxHeightOriginal;
        fakeBox.style.height = fakeBox.boxHeight+"px";
        fakeBox.style.display = "block";
        divContainer.style.visibility = "hidden";
    }
    boxHeight("fakeBox", boxClosed);
    boxClosed = boxClosed * (-1);
}

function boxHeight(element, direction){
	var layer = d.getElementById(element);
    	
	if(direction == 1){
		layer.boxHeight = layer.boxHeight + 5;
    }
	if(direction == -1){
		layer.boxHeight = layer.boxHeight - 5;
	}
	
    layer.style.height = layer.boxHeight+"px";
    
	if(direction == 1)
	{
		if (layer.boxHeight<(boxHeightOriginal))
			setTimeout("boxHeight('"+element+"', "+direction+")", 50);
        else{
			layer.style.display = 'none';
            d.getElementById("tpajaxtabs" + mid + "divcontainer").style.visibility = "visible";
        }
	}
	if(direction == -1)
	{
		if (layer.boxHeight>0)
			setTimeout("boxHeight('"+element+"', "+direction+")", 50);
	}
}

function setCurrentTabIndex(index){
  tabIndex = index;  
}








