var cur_twit = 1;
var twit_arr = new Array();
var delay = 8000;
var timerID = null; 
var timerRunning = false;

function fadebox(the_box_id) {
		$("#content_box").css({
  			'background': 'url(/images/'+ the_box_id +'.png) repeat 0 0', 
  			'left': $("#" + the_box_id).css("left"), 
  			'top': $("#" + the_box_id).css("top"),
  			'display': 'none'
    	});
    	$("#content_box").fadeIn(600);
    	$("#"+ the_box_id).fadeOut(700);
    	$(".box").animate({ 
    		opacity: 0
  		}, 700, function() {
  			$(".box").css("display","none"); 
  		});
		$("#content_box").animate({ 
    		width: "580px",
    		height: "284px",
    		left: "105px",
    		top: "80px"
    	}, 700);
    	$(".minibox span").css("opacity", "0.3");
    	$("#mini"+the_box_id+" span").css("opacity", "1");
    	$(".minibox").delay(1400).fadeIn(500);
    	$.ajax({
  			url: '/home/ajax_module_content/C'+the_box_id.substr(3,1),
  			success: function(data) {
  				$('#content_box_content').html("");
    			$('#content_box_content').html(data);
  			}
		});
    	$("#content_box_content").delay(1400).fadeIn(500);
    	
}


function colorSwitch(the_box_id) {
		$("#content_box").fadeOut(500, function () {
			$("#content_box").css({
				'background-image': 'url(/images/'+ the_box_id +'.png)',
				'display': 'none'
			});    	
			$.ajax({
  				url: '/home/ajax_module_content/C'+the_box_id.substr(3,1),
  				success: function(data) {
  					$('#content_box_content').html("");
    				$('#content_box_content').html(data);
  				}
			});
			$("#content_box").fadeIn(500);
		});
		
    	$(".minibox span").animate({ 'opacity': '0.3' }, 500);
    	
    	$("#mini"+the_box_id+" span").animate({	'opacity': '1' }, 500);
}


/* Timer für Twitter Klicks */

function set_tweet(content) {
	$('#thetweets').fadeOut(400, function() {
		$(this).html(content);
		$(this).fadeIn(400);
	});
}

function stop_twitter_clicker() { 
	if(timerRunning) {
		clearTimeout(timerID);
		timerRunning = false;
		timerID = null;
	}
}
function start_twitter_clicker() {
    timerRunning = true;
    timerID = self.setTimeout("click_twitter()", delay);
}
function click_twitter() {
	if (cur_twit <= 19) {
		next_twit();
		start_twitter_clicker();
	} else {
		cur_twit = 1;
		if (twit_arr[cur_twit]) {
			set_tweet(twit_arr[cur_twit]);
			stop_twitter_clicker()
			start_twitter_clicker();
		}
	}
}

function next_twit() {
		if (cur_twit < 20) {
			cur_twit++;
			if (twit_arr[cur_twit]) {
				set_tweet(twit_arr[cur_twit]);
			} else {
				cur_twit--;
			}		
		}
		if (timerRunning) {
			stop_twitter_clicker();
		}
		return false;
}

function load_twitter() {
	$.getJSON("/main/get_twitter.php", function(data, textstatus) {
			var i = 1;
			while ((c_twit = eval('data.twita_' + i))) {
				twit_arr[i] = c_twit;
				i++;
			}
			cur_twit = 1;
			set_tweet(twit_arr[cur_twit]);
			stop_twitter_clicker()
			start_twitter_clicker();
	});
}

function initLinks()
{
	
	if (!document.getElementsByTagName){return;}
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("class") == "file")){
            if (anchor.href.indexOf('.pdf') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkpdf';
            }
            if (anchor.href.indexOf('.doc') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkdoc';
            }
            if (anchor.href.indexOf('.xls') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkxls';
            }
            if (anchor.href.indexOf('.ppt') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkppt';
            }
            if (anchor.href.indexOf('.jpg') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkjpg';
            }
            if (anchor.href.indexOf('.png') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkpng';
            }
            if (anchor.href.indexOf('.gif') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkgif';
            }
            if (anchor.href.indexOf('.eps') != -1 || anchor.href.indexOf('.ai') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkeps';
            }
            if (anchor.href.indexOf('.zip') != -1) {
                anchor.setAttribute('target', '_blank');
                anchor.className = 'linkzip';
            }
		}
	}
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}
addLoadEvent(initLinks);

function subnavi(id) {
	var subnaviel = $("#subnavi"+id);
	subnaviel.css({
		'left': $('#'+id).position().left,
		'width' : subnaviel.width()+"5"
   	});
	subnaviel.fadeIn(500);
    var linkel = $('#'+id);
    
    linkel.addClass('unclick');
    $('#bg'+id).addClass('sel');
   	
   	linkel.unbind('mouseleave');
   	linkel.unbind('mouseenter');
   	
   	/*$('.unclick').click( function() {
   		subnaviUnclick(id);
   	});*/
   	
   	
}

function subnaviUnclick(id) {
	
	$("#subnavi"+id).fadeOut(500);
	$('#'+id).removeClass('unclick');
	$('#bg'+id).removeClass('sel');
	
   	$('#'+id).bind('mouseleave');
   	$('#'+id).bind('mouseenter');
   	
   	$('#'+id).click( function() {
   		subnavi(id);
   	});
}

function subnaviPosition() {
	if ($('.linkbox_sel').position() != null) {
		$(".subnavi_sel").css({
			'left': $('.linkbox_sel').position().left 
   		});
   	}
}


$(document).ready(function() {
	
	$("#box1").click(function () { 
		fadebox('box1');   	
    });
	$("#box2").click(function () { 
		fadebox('box2');   	
    });
	$("#box3").click(function () { 
		fadebox('box3');   	
    });
	$("#box4").click(function () { 
		fadebox('box4');   	
    });
	$("#box5").click(function () { 
		fadebox('box5');   	
    });
	$("#box6").click(function () { 
		fadebox('box6');   	
    });
    
	$("#minibox1").click(function () { 
		colorSwitch('box1');   	
    });
	$("#minibox2").click(function () { 
		colorSwitch('box2');   	
    });
	$("#minibox3").click(function () { 
		colorSwitch('box3');   	
    });
	$("#minibox4").click(function () { 
		colorSwitch('box4');   	
    });
	$("#minibox5").click(function () { 
		colorSwitch('box5');   	
    });
	$("#minibox6").click(function () { 
		colorSwitch('box6');   	
    });
    
    $(".box").hover(
    	function () {
        	$(this).animate({ 'opacity': '0.85' }, 150);
      	}, 
      	function () {
        	$(this).animate({ 'opacity': '1' }, 150);
    });
    
    
    
    $(".linkbox").hover(
    	function () {
    	
	    	var id = $(this).attr("id");
	    	$("#a"+id).css('margin-bottom', '30px');
	    	$("#bg"+id).fadeIn(200);
    		
    		$(".subnavi_sel").animate ({
    			opacity: "0.2"
    		}, 200);
    		
      	}, 
      	function () {
	    	var id = $(this).attr("id");
	    	$("#bg"+id).fadeOut(200);
	    	$("#a"+id).css('margin-bottom', '6px');
    		
	    	$(".subnavi_sel").animate ({
	    		opacity: "1"
	    	}, 200);
    });
    
    $("#li2").click(function () { 
		subnavi('li2');
    });
	
	load_twitter();
	subnaviPosition();
});

