var tgt;
var currentUrl;
var currentThumb;
var loadedImgs = new Array();

function popImg(theUrl, w, h, thumbID){
	// find the target image
	if(tgt == undefined){
		var div = document.getElementById('popupTarget');
		var img = div.getElementsByTagName('img');
		for (i = 0; i < img.length; i++) {
	  		if(img[i].src.indexOf("/clear.gif") > -1){
	  			continue;
	  		} else {
	  			tgt = img[i];
	  			break;
	  		}
		}
	}
	
	// not yet loaded?
	if (!loadedImgs[thumbID] == 1){
		currentUrl = theUrl;
		currentThumb = thumbID;
		
		// find the thumb and show
		var tmpA = document.getElementById(thumbID);
		var tmpImgs  = tmpA.getElementsByTagName('img');
		var tmp = tmpImgs[0];
	  	tgt.width  = w;
	  	tgt.height = h;
	  	tgt.src = tmp.src;
	  	
	  	// load the new image
	  	var newImg = new Image();
  			newImg.onload = function(){
  			loadedImgs[currentThumb] = 1;
			tgt.src = this.src;
		}
  		newImg.src = currentUrl;
  	// already loaded
	} else {
		tgt.width  = w;
	  	tgt.height = h;
		tgt.src = theUrl;
	}
  	
}

/*------------- TEACHERS --------------*/
/* fŸr popup wieder freischalten !!!

var BMS = {loaddiv: null, clicked: null, clickedMargin: 30};
$(document).ready(function(){
  $('.contentblock.bms_teachers_pi1').each(function(indx, elem){
    var id = $('.bms-teacher', $(this)).attr('id').substr(12);
    $(this).click(function(){
      BMS.clicked = $(this);
      BMS.loadElem(id);
    })
    .css('cursor', 'pointer');
  });
});

BMS.loadElem = function(id){
  if(BMS.loaddiv == null){
    $('body').prepend('<div id="BMSload" style="display:none;"></div>');
    BMS.loaddiv = $('#BMSload');
  }
  BMS.loaddiv.load(
    'index.php?eID=teacher&tuid=' + id,
     null,
     function(data, status, xhr) {
      var p = $('#content_main').offset();
      var w = $('#content_main').width();
      var l = p.left + (w/2) - (BMS.loaddiv.width() / 2);
      var c = BMS.clicked.offset();
      
      var t = p.top;
      BMS.loaddiv.css({left: l, top: c.top + BMS.clickedMargin});
      BMS.loaddiv.fadeIn('fast');
      BMS.loaddiv.intoViewport();
      $('.BMSload-close', BMS.loaddiv).click(function(){
        BMS.loaddiv.fadeOut('fast'); 
      });
  });
}
*/




/*------------- NEWS ROTATION --------------*/
$(document).ready(function(){
  var $allnews = $('#BMSrotate .contentblock');
  if ($allnews.length < 2) return;
  
  var $BMSrotate = $('#BMSrotate');
  
  $BMSrotate.parent().prepend('<div id="bms-news-nav"></div>');
  var $newsnav = $('#bms-news-nav');
  var w = 0;
  
  $('.contentblock', $BMSrotate).each(function(indx, elem){
    var $news = $(this);
    w += $news.outerWidth(true);
    var $navitem = $('<div class="bms-news-navitem">' + (indx + 1) + '</div>')
      .click(function(){
        if($BMSrotate.is(':animated')) return;
        
        var $c = $($('.contentblock', $BMSrotate).get(0));
        var $g = $($allnews.get(indx));
        
        if($c.find('.csc-default').attr('id') == $g.find('.csc-default').attr('id')){
          return;
        }

        $('.bms-news-navitem', $newsnav).removeClass('bms-news-nav-current');
        $g.insertAfter($c);
        var cw = $c.outerWidth(true);
        
        $BMSrotate.animate(
          {left: '-' + cw + 'px'}, 
          600, 
          'swing', 
          function(){
            $BMSrotate.append($c)
              .css({left: '0px'});
            $($('.bms-news-navitem', $newsnav).get(indx))
              .addClass('bms-news-nav-current');
        });
      });
    if(indx == 0){
      $navitem.addClass('bms-news-nav-current');  
    }
    $newsnav.append($navitem);
  });
  
  var style = {width: w + 'px'};
  $BMSrotate.css(style);
  
});

