/*Scene - SiteBar auf Home Background tauschen*/
function ggToggleBg(which) {
	if (which.className != "active")
		which.className = "active";
	else which.className = "";
}

function ggCheckTextLength(maxLength, labelElementId, inputElementId) {
	var inputElement = document.getElementById(inputElementId);
    var text = inputElement.value;
	if (text.length > maxLength) {
		text = text.substring(0, maxLength);
		inputElement.value = text;
	}
	document.getElementById(labelElementId).innerHTML = text.length;
}



 /* Upload Media START */

function initializeUploadMediaLayout(toShow)
{
	var add_to_class = "sMenu" + toShow;
	if ($(add_to_class))
	{
		$(add_to_class).addClassName('active');
	}
	
	if ((toShow == 'Video') || (toShow == 'VideoFile'))
	{
		$('sMenuSelecter').show();
	}
	else
	{
		$('sMenuSelecter').hide();
	}
	
	$('contribution_contribution_type_'+toShow.toLowerCase()).checked = true;

	if(toShow == 'Video')
	{
		$('contributionUpload').hide();
		$('contributionVideo').show();
	}
	else
	{
		$('contributionVideo').hide();
		$('contributionUpload').show();
	}

    if (toShow == 'Playlist')
    {
       	$('commentfield').hide();
    }
	else
	{
       	$('commentfield').show();
	}

}

/* EDITED BY JG */
  function uploadMedia (toShow, isError) {

	if ($('contributionForm') != null)
	{
	  	overlayDisp('uploadMedia','overlay_media');
	  	if ($('sMenuVideo')) { $('sMenuVideo').removeClassName('active'); }
	  	if ($('sMenuVideoFile')) { $('sMenuVideoFile').removeClassName('active'); }
		if ($('sMenuPlaylist')) { $('sMenuPlaylist').removeClassName('active'); }
		if ($('sMenuPhotoset')) { $('sMenuPhotoset').removeClassName('active'); }
	
	
		initializeUploadMediaLayout(toShow)
				
		if ( $('uploadHeadline').hasChildNodes() )
		{
			while ( $('uploadHeadline').childNodes.length >= 1 )
			{
				$('uploadHeadline').removeChild( $('uploadHeadline').firstChild );
			} 
    	}
		
		if (toShow == 'Photoset')
		{
        	var headline = new Element('h4').update('Bild hochladen');
            $('uploadHeadline').appendChild(headline);
		}
        else if (toShow == 'Playlist')
        {
            var headline = new Element('h4').update('Sound hochladen');
            $('uploadHeadline').appendChild(headline);
		}
                
                
        if (isError)
        {
            new Effect.Appear('contribution_errors', { queue: 'end'})  
        }
		
	}
	 else
	{
		overlayDisp('fanInteraction','overlay','Um diese Funktion nutzen zu können, musst Du dich erst anmelden!');
	}
  }

/* Upload Media END */

  function overlayDisp (show,device,messageForm, overlayFormId) {
    
    if (overlayFormId == null)
    {
      overlayFormId = "overlayForms"    
    }
    
    var scrollTop = function () { return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop || 0;};
  	if ($(device) == null && device != '') { device = 'overlay'; }
  	
    if ($(show) != null && show != 0) {
    
      if ($('radio_widget')) {
	   $('radio_widget').style.visibility = 'hidden';
  	  }
  	
      $(device).style.display = 'none';
      
      myF = $(overlayFormId);
      for (s=0;s<myF.childNodes.length;s++) {
        if(myF.childNodes[s].className == "overlayDev") {
          myF.childNodes[s].style.display = "none";
        }
      }
      if ($('message_form')) {
	      myF = $('message_form');
    	  for (s=0;s<myF.childNodes.length;s++) {
	        if(myF.childNodes[s].className == "showOnce" && s != 0) {
        	  myF.removeChild(myF.childNodes[s]);
    	    }
	      }      
      }
      if ($(show)) { $(show).style.display = 'block'; }
    
      var setTop = scrollTop();
      // absTop( $('overlay').parentNode;
      
      $(device).style.top = parseInt(setTop) + 'px';
      $(device).style.display = 'block';
      
      if ($('contribution_errors')) {
	      $('contribution_errors').hide();
      }
    } else {
      if ($(device)) {
      	if (show == 'fanInteraction') {
	      	$('message_form').innerHTML = '';
      	}
      	$(device).style.top = '0px';
      	$(device).style.display = 'none';
      }
      if ($('message_form') && ($('message_form').innerHTML != '')) { $('message_form').innerHTML = ''; }
	  	if ($('radio_widget')) {
		   $('radio_widget').style.visibility = 'visible';
	  	}
    }
    if (messageForm && $('message_form')) {
	  $('message_form').innerHTML = '';
      var message_formText = new Element('div', { id: "message_formText", 'class':'showOnce'} ).update(messageForm);
      $('message_form').appendChild(message_formText);      	
    }
    
    function absTop(el) { return (el.offsetParent)?(el.offsetTop+absTop(el.offsetParent)):(el.offsetTop); }
  }

  function lighterDispById (openid) {
	if ($(openid)) {
	  	var section = $(openid).parentNode.id;
	  	if (section != '') {
	  		var element = $(openid).parentNode;
	  			element.cleanWhitespace();
	  			element = element.firstChild;
	  		var i = 0;
	  		while (element != null) {
	  			if (element.id != '' && Element.hasClassName(element, "items") ) {
	  				if (element.id == openid) {
	  					lighterDisp(section,i);
	  				}
	  				i++;
	  			}
	  			element = element.nextSibling;
	  		}
	  	}
	}
  }

  function lighterDisp(section,num) {
  
  	if ($('radio_widget')) {
	   $('radio_widget').style.visibility = 'hidden';
  	}
  	
  	var element = $(section).select('.items')[num];
  	
    
    if (($('lighterBlack_bg') == null) || ($('lighterValue_bg') == null)) {
      var black_bg = new Element('div', { id: "lighterBlack_bg", 'onclick':'lighterDispHide();return false;' } );
      var value_bg = new Element('div', { id: "lighterValue_bg"} );
      
      $('lighterDisp').appendChild(black_bg);
      $('lighterDisp').appendChild(value_bg);
      
      var lighterBox = new Element('div', { id: "lighterBox"} );
      $('lighterValue_bg').appendChild(lighterBox);
    }
    
    
    if ( $('lighterBox').hasChildNodes() )  {
        while ( $('lighterBox').childNodes.length >= 1 ) {
            $('lighterBox').removeChild( $('lighterBox').firstChild );       
        } 
    }
    
    $('lighterBlack_bg').setOpacity(0.8);
    
    lighterDispResizeBG();

	// ADDING THE CLOSE BUTTON
    var closer = new Element('div', { 'class': 'closer'});
    var a = new Element('a', { href: 'javascript:lighterDispHide(\'\');', 'onclick':'lighterDispHide(\'\');return false;' }).update("schließen");
    closer.appendChild(a);
    $('lighterBox').appendChild(closer);
    
    // CREATE DISPLAY CONTENT
    if (element.select('a.thumb')[0].rel == 'image') {
    	var img = lighterDispImage(section,num);
    	ImageLoader(section,num,img);
        $('lighterBlack_bg').setStyle('display:block');
        $('lighterValue_bg').style.display = 'block';
        $('lighterDisp').style.display = 'block';
 
    } else {
        $('lighterBlack_bg').setStyle('display:block');
        $('lighterValue_bg').style.display = 'block';
        $('lighterDisp').style.display = 'block';
 
    	if (element.select('a.thumb')[0].rel == 'youtube') {
    		lighterDispYoutube(section,num);
    	} else if (element.select('a.thumb')[0].rel == 'video') {
			lighterDispVideo(section,num);
	    } else if (element.select('a.thumb')[0].rel == 'audio') {
			lighterDispVideo(section,num);
	    }
	    lighterDispInfo(section,num);
	}
  }
  
  
  function lighterDispSpinner (message) {
    $('lighterDisp').style.display = 'block';
    
    if (($('lighterBlack_bg') == null) || ($('lighterValue_bg') == null)) {
      var black_bg = new Element('div', { id: "lighterBlack_bg"} );
      var value_bg = new Element('div', { id: "lighterValue_bg"} );
      
      $('lighterDisp').appendChild(black_bg);
      $('lighterDisp').appendChild(value_bg);
      
      var lighterBox = new Element('div', { id: "lighterBox"} );
      $('lighterValue_bg').appendChild(lighterBox);
    }
    
    $('lighterValue_bg').style.display = 'block';
    
    if ( $('lighterBox').hasChildNodes() )  {
        while ( $('lighterBox').childNodes.length >= 1 ) {
            $('lighterBox').removeChild( $('lighterBox').firstChild );       
        } 
    }
    
    $('lighterBlack_bg').setStyle('display:block');
    $('lighterBlack_bg').setOpacity(0.8);
    
      var swfData = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="20" height="20" id="spinner" align="middle">'
                  + '<param name="allowScriptAccess" value="sameDomain" />'
                  + '<param name="allowFullScreen" value="false" />'
                  + '<param name="FlashVars" value="" />'
                  + '<param name="movie" value="/swf/spinner.swf" />'
                  + '<param name="quality" value="high" />'
                  + '<param name="bgcolor" value="#ffffff" />'
                  + '<embed src="/swf/spinner.swf" FlashVars="" quality="high" bgcolor="#ffffff" width="20" height="20" name="spinner" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
                  + '</object>'
   
   
      var dispSpinner = new Element('p', {'class':'spinnericon'}).update(swfData);
      var dispMessage = new Element('p',{'class':'spinnermsg'}).update(message);
      var cancelLink = new Element('a', {'href':document.location, 'class':'spinnerLink'}).update('Abbrechen');
      
      var spinnerBox = new Element('div', {'class':'spinnerBox'});
	      spinnerBox.appendChild(dispSpinner);
    	  spinnerBox.appendChild(dispMessage);
	      spinnerBox.appendChild(cancelLink);

      $('lighterBox').appendChild(spinnerBox);

    lighterDispResizeBG();
    lighterDispPos();
  }
  
  function ImageLoader(section,num,img) {
	if(!img.complete){
		var callback = function() { ImageLoader(section,num,img); };
		setTimeout(callback,25);
	} else {
		lighterDispInfo(section,num);
	}
  }

  function lighterDispInfo(section,num) {
  	var element = $(section).select('.items')[num];

	if (element.select('.title')[0] != null) {
	    var headline = new Element('h2').update(element.select('.title')[0].innerHTML);
			$('lighterBox').appendChild(headline);
	}
    
    var prev = $(section).select('.items')[num-1];
    var next = $(section).select('.items')[num+1];
    var navigation = new Element('div',{'class':'lighterDispNav'});

    if (prev != null || next != null) {
	    if (prev != null) {
    	  var prev_node = parseInt(num) - 1;
	      var nav_prev = new Element('a',{'href':'javascript:lighterDisp(\''+section+'\','+prev_node+');','onclick':'lighterDisp(\''+section+'\','+prev_node+');return false;', 'class':'prev'}).update('zurück');
    	  navigation.appendChild(nav_prev);
	    } else {
    	  var nav_prev = new Element('span',{'class':'noprev'}).update('zurück');
	      navigation.appendChild(nav_prev);
    	}
	    if (next != null) {
    	  var next_node = parseInt(num) + 1;
	      var nav_next = new Element('a',{'href':'javascript:lighterDisp(\''+section+'\','+next_node+');','onclick':'lighterDisp(\''+section+'\','+next_node+');return false;', 'class':'next'}).update('weiter');
    	  navigation.appendChild(nav_next);
	    } else {
    	  var nav_next = new Element('span',{'class':'nonext'}).update('weiter');
	      navigation.appendChild(nav_next);
    	}
    }

    $('lighterBox').appendChild(navigation);

    if ((element.select('.further')[0] != null) &&
    	(element.select('.further')[0].select('span.user')[0] != null) &&
    	(element.select('.further')[0].select('span.date')[0] != null)
    	) {

	    var user = new Element('span',{'class':'user'});
    	var user_name = element.select('.further')[0].select('span.user')[0].select('a')[0].innerHTML;
	    var user_link = element.select('.further')[0].select('span.user')[0].select('a')[0].href;
    	var user_href =  new Element('a',{'href':user_link,'title':user_name}).update(user_name);
			user.appendChild(user_href);
    
	    var date = new Element('span',{'class':'date'}).update(element.select('.further')[0].select('span.date')[0].innerHTML);
    
	    var information = new Element('div',{'class':'lighterDispInfo'});
    		information.appendChild(user);
			information.appendChild(date);
			$('lighterBox').appendChild(information);
			
	} else if (element.select('.user')[0] != null && element.select('.user')[0].innerHTML != '') {
	
	    var information = new Element('div',{'class':'lighterDispInfo'});
    		information.appendChild(element.select('.user')[0].cloneNode('true'));
			$('lighterBox').appendChild(information);
			
	}
   
    lighterDispPos();
    
    if (element.select('.description')[0] && element.select('.description')[0].innerHTML != '') {
	    var copComment = new Element('p',{'class':'description'}).update(element.select('.description')[0].innerHTML);
		$('lighterBox').appendChild(copComment);
	}
  }
  
  function lighterDispResizeBG () {
  
    var docWidth = function() { return document.body.offsetWidth || window.innerWidth || document.documentElement.clientWidth || 0; };
    var docHeight = function() { return document.body.offsetHeight || window.innerHeight || document.documentElement.clientHeight || 0; }
    var winWidth = function() { return document.viewport.getWidth() || 0; };
    var winHeight = function() { return document.viewport.getHeight() || 0; }
	
	if ($('lighterDisp')) {
	    $('lighterDisp').setStyle('height:'+(parseInt(docHeight())+50)+'px');
    	$('lighterDisp').setStyle('width:'+parseInt(winWidth())+'px');
    }
    
    if ($('lighterBlack_bg')) {
	    $('lighterBlack_bg').setStyle('height:'+(parseInt(docHeight())+50)+'px');
    	$('lighterBlack_bg').setStyle('width:'+parseInt(winWidth())+'px');
	}
  }
  
  function lighterDispPos() {
  	if ($('objDisp').getWidth() > 0) {
		$('lighterBox').style.width = $('objDisp').getWidth()+'px';
		
	    var docWidth = function() { return document.body.offsetWidth || window.innerWidth || document.documentElement.clientWidth || 0; };
    	var docHeight = function() { return document.body.offsetHeight || window.innerHeight || document.documentElement.clientHeight || 0; }
	    var winWidth = function() { return document.viewport.getWidth() || 0; };
    	var winHeight = function() { return window.innerHeight || document.viewport.getHeight() || 0; }
	    var scrollTop = function () { return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop || 0;};
    	var scrollLeft = function () { return window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft || 0;};
    
		if ($('lighterValue_bg')) {
			var setT = parseInt(scrollTop() + (winHeight() / 2) - ($('lighterValue_bg').getHeight() / 2));
			var setL = parseInt(scrollLeft() + (winWidth() / 2) - ($('lighterValue_bg').getWidth() / 2));
//			var setL = parseInt(scrollLeft() + (950 / 2) - ($('lighterValue_bg').getWidth() / 2));
	
			$('lighterValue_bg').style.left = setL+'px';
			$('lighterValue_bg').style.top  = setT+'px';
		}
	} else {
		setTimeout("lighterDispPos();",50);
	}
  }
  
  function lighterDispHide() {
  	var lighterKill = function () {
	    if ( $('lighterDisp').hasChildNodes() ) {
    	    while ( $('lighterDisp').childNodes.length >= 1 ) {
        	    $('lighterDisp').removeChild( $('lighterDisp').firstChild );       
	        } 
    	}
	    $('lighterDisp').hide();
    	$('lighterDisp').style.height = '0px';
    	
	  	if ($('radio_widget')) {
		   $('radio_widget').style.visibility = 'visible';
	  	}
    }
	lighterKill();
  }
  
  function lighterDispImage(section,num) {
  	var element = $(section).select('.items')[num];
	var img = new Image();
		img.src = element.select('a.thumb')[0].href;
		img_insert = new Element('div');
		img_insert.setStyle('width:514px;height:290px;background-color:#000000;background-image:url(\''+element.select('a.thumb')[0].href+'\');background-position: center;background-repeat: no-repeat');
		img_insert.id = "objDisp";
		$('lighterBox').appendChild(img_insert);
	return img;
  }
  
  function lighterDispVideo(section,num) {
  	var element = $(section).select('.items')[num];
  	var objDisp = new Element('div',{id:'objDisp'});
  		objDisp.setStyle('width:384px;height:318px');
  		$('lighterBox').appendChild(objDisp);

	var so = new SWFObject("/swf/VideoPlayer.swf", "VideoPlayer", "384", "318", "9", "#000000");
		so.addVariable("videoUrl",element.select('a.thumb')[0].href);
		so.addParam("allowScriptAccess", "always");
		so.addParam("pluginspage", "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash");
		so.useExpressInstall("/swf/expressInstall.swf");
     	so.write(objDisp);
  }
  
  function lighterDispYoutube(section,num) {
  	var element = $(section).select('.items')[num];
  	var link_href = element.select('a.thumb')[0].href;
  		link_href = link_href.replace(/http:\/\/www.youtube.com\/watch\?v=/g,'');
  	var youtubeUrl = 'http://www.youtube.com/v/'+link_href+'&amp;border=0&amp;enablejsapi=1&amp;playerapiid=objDispUTController';
  	var objDisp   = new Element('div',{id:'objDisp'});
  	var objDispUT = new Element('div',{id:'objDispUT'});
		objDisp.appendChild(objDispUT);
  		objDisp.setStyle('width:384px;height:318px');	
		$('lighterBox').appendChild(objDisp);

    var params = { allowScriptAccess: "always" };
    var atts = { id: "mytplayer" };

    swfobject.embedSWF(youtubeUrl,"objDispUT", "384", "318", "8", null, null, params, atts);    
  }
  
  window.onresize = reloadStaff;
  
  function reloadStaff () {
  	lighterDispResizeBG();
  	lighterDispPos();
  }

// UPLOAD FORM CHECK 
function validateForm(element) {
  errors = new Array()
  var uploadWhat = '';
  if ($F('contribution_title') == "") {
    errors.push("Titel darf nicht leer sein")
  }
  if( $('contribution_contribution_type_video').checked ) {
    if ($F('contribution_video_video_url') == "") {
      errors.push("Video-URL darf nicht leer sein")
    } else {
      if (!$F('contribution_video_video_url').match(/youtube.com\/(v\/|watch\?v=)/)) {
        errors.push("Video-URL muss von YouTube sein!")
      } else {
        uploadWhat = 'YouTube-URL wird gespeichert ...';
      }
    }
  } else {
    if ($F('contributionUploadInputFile') == "") {
      errors.push("Keine Datei ausgewählt!")
    } else {
      uploadWhat = $('contributionUploadInputFile').value+'<br />wird gerade hochgeladen ...';
    }
  }
  
  if(errors.size() > 0) {
    $('contribution_error_list').update("")
    errors.each(function(e) {
      $('contribution_error_list').insert("<li>" + e + "</li>")
    })

	new Effect.Appear('contribution_errors', { queue: 'end'})  
	return false;
	
  } else {
	lighterDispSpinner('Bitte warten.<br /> '+uploadWhat,true);
	return true;
  }
  
}

function openInNewWindow(link){
	window.open(link)
}
