/* To prevent Prototype conflict */
var $j = jQuery.noConflict();

/* Active */
var current = 'introduction';

function active(name) {
    $j('*[name="' + current + '"]').removeClass('active');
    $j('*[name="' + name + '"]').addClass('active');
    current = name;
}
  
/* AJAX Content */
function dynContent(page) {
    $j.get(  'ex/content.php?' + page,
            function(data) {
                $j('#contentfield').html(data);
            },
            'html'
    );

    active(page);
}

/* Dynamic banner */
var currPos = 8;
var amtBanners = 8;
var stopRotate = false;
var highlight = "";
var currState = [];

function rotate() {
    if(!stopRotate) {
        if(currPos == 1) {
            $j('#ban' + amtBanners).fadeIn(function() {
                $j('#bannerContainer').find('img').each(function() {
                    $j(this).show();
                });
            });
            
            currPos = amtBanners;
        }
        else {
            $j('#ban' + currPos).fadeOut();
            currPos -= 1;
        }
    } 

    setTimeout("rotate()", 5000);
} 

function setStatic() {
    var i = 0;

    $j('#bannerContainer').find('img').each(function() {                   
        currState[i] = $j(this).is(':visible');
        i++;
        
        $j(this).hide();
    });
    
    $j('#ban' + highlight).show();            
}

function removeStatic() {
    var i = 0;
    
    $j('#bannerContainer').find('img').each(function() {
        if(currState[i])
            $j(this).show();
        else
            $j(this).hide();
        
        i++;
    });
}

/* Random jQuery functionality */
$j(document).ready(function() {               
    /* Start banner and slideshow */
    rotate();
    so_init();
    
    /* Menu item hover */
    $j('#menu li[id]').hover(
        /* Handler in */
        function(e) {
            stopRotate = true;
            highlight = $j(this).attr('id');
            
            setStatic();
        },
        /* Handler out */
        function(e) {
            stopRotate = false;
            
            removeStatic();
        }
    );
    
    /* Menu click */
    $j('#menu li[name]').click(function(e) {
        dynContent($j(this).attr('name'));
        
        var correspondingDt = $j(this).parent().parent().siblings('dt[name]');
        
        if(correspondingDt.length)
            active(correspondingDt.attr('name'));
    });
    
    /* Prevent <a href="#"> links */
    $j('a[href="#"]').live('click', function(e) {
        e.preventDefault();
    });
    
    /* Latest file header */
    $j('.latestfile').click(function(e) {
        dynContent($j(this).attr('name'));
        
        active('downloads');       
    });
    
    /* Latest news link */
    $j('#latestnews').click(function(e) {
        dynContent($j(this).attr('title'));
        
        active('profile');
    });
    
});

/* Cross-fade image slideshow */
var d = document, imgs = new Array(), zInterval = null, currentSlide = 0, pause = false;
var bannerImgs = new Array(), bCurrentSlide = 0;

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

    /* Dynamic image slideshow */
	imgs = d.getElementById("slideshow").getElementsByTagName("img");
    
	for(i = 1; i < imgs.length; i++) 
        imgs[i].xOpacity = 0;
        
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	setTimeout(so_xfade, 2000);
}

function so_xfade() {
	cOpacity = imgs[currentSlide].xOpacity;
	nIndex = imgs[currentSlide + 1] ? (currentSlide + 1) : 0;

	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity -= .05; 
	nOpacity += .05;
	
	imgs[nIndex].style.display = "block";
	imgs[currentSlide].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpac(imgs[currentSlide]); 
	setOpac(imgs[nIndex]);
	
	if(cOpacity <= 0) {
		imgs[currentSlide].style.display = "none";
		currentSlide = nIndex;
		setTimeout(so_xfade, 2000);
	} 
    else
		setTimeout(so_xfade, 50);
	
	function setOpac(obj) {
		if(obj.xOpacity > .99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity * 100) + ")";
	}
}

/* Slider menu */
var DDSPEED = 10;
var DDTIMER = 15;

function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.top = '-' + c.offsetHeight + 'px';
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.top = parseInt(c.style.top.replace('px','')) - parseInt(dist * d) + 'px';
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}


