/** Voting */

var yourPoints = Array();
yourPoints.id = '';
yourPoints.itemCount = 0;
yourPoints.value = 0;
yourPoints.active = false;

function initYourPoints(yourPointsId, itemCount, value, active) {
	yourPoints.id = yourPointsId;
	yourPoints.itemCount = itemCount;
	yourPoints.value = value;
	yourPoints.active = active;
	
	ypid = document.getElementById(yourPoints.id);
	if(ypid) {
		if(yourPoints.active) {
			ypid.className = 'onmouseout active';
			/*ypid.setAttribute('onmouseout', '"javascript:resetYourPoints();"', 1);*/
		}
		else {
			ypid.className = 'onmouseout';
		}
		
		var innerhtml = '';
		for(i = 0; i < yourPoints.itemCount; i++) {
			var this_id = yourPoints.id + i;
			if(active && i < yourPoints.value) {
				innerhtml = innerhtml + '<img src="img/points_yellow.png" alt="" id="' + this_id + '" onmouseover="javascript:updateYourPoints(' + i + ');" onclick="javascript:setYourPoints();" />';
			}
			else if(active) {
				innerhtml = innerhtml + '<img src="img/points_transparent.png" alt="" id="' + this_id + '" onmouseover="javascript:updateYourPoints(' + i + ');" onclick="javascript:setYourPoints();" />';
			}
			else if(i < yourPoints.value) {
				innerhtml = innerhtml + '<img src="img/points_yellow.png" alt="" />';
			}
			else {
				innerhtml = innerhtml + '<img src="img/points_transparent.png" alt="" />';
			}
		}
		ypid.innerHTML = innerhtml;
	}
}

function updateYourPoints(value) {
	for(i = 0; i < yourPoints.itemCount; i++) {
		var this_id = document.getElementById(yourPoints.id + i);
		if(this_id) {
			if(i <= value) {
				this_id.setAttribute('src', 'img/points_yellow.png', 1);
			}
			else {
				this_id.setAttribute('src', 'img/points_transparent.png', 1);
			}
		}
	}
	
	yourPoints.value = value;
}

function resetYourPoints() {
	for(i = 0; i <= yourPoints.value; i++) {
		var this_id = document.getElementById(yourPoints.id + i);
		if(this_id) {
			this_id.setAttribute('src', 'img/points_transparent.png', 1);
		}
	}
	
	yourPoints.value = 0;
}

function setYourPoints(value) {
	
}

/** slideToggle */

var oldToggleId = "";
var oldToggleLink = "";
var oldToggleTeaser = "";

function myToggle(myId, teaser) {
	
	var newId = document.getElementById(myId);
	var newTeaser = document.getElementById(teaser);
	
	if(newId) {
	
		//alert("myId="+myId+"   oldToggleId="+oldToggleId+"   newId="+newId);
	
		$(newId).slideToggle("normal");
		
		$(newTeaser).slideToggle("normal");
		
		if(myId != oldToggleId) {
			var oldId = document.getElementById(oldToggleId);
			
			var oldTeaser = document.getElementById(oldToggleTeaser);
			
			if(oldId) {
				$(oldId).slideToggle("normal");
				
				$(oldTeaser).slideToggle("normal");
			}
			oldToggleId = myId;
			
			oldToggleTeaser = teaser;
		}
		else {
			oldToggleId = "";
			
			oldToggleTeaser = "";
		}
	}	
}



var oldToggle2Id = "";
var oldToggle2Link = "";
var oldToggle2Teaser = "";

function myToggle2(myId) {
	
	var newId = document.getElementById(myId);
	
	if(newId) 
	{
		//alert("myId="+myId+"   oldToggleId="+oldToggleId+"   newId="+newId);
	
		$(newId).slideToggle("normal");
		
		if(myId != oldToggle2Id) 
		{
			var oldId = document.getElementById(oldToggle2Id);
			
			if(oldId) 
			{
				$(oldId).slideToggle("normal");
			}
			
			oldToggle2Id = myId;
		}
		else 
		{
			oldToggle2Id = "";
		}
	}	
}









function myLinkToggleJump(target) {
	// jump to anchor if new target is not the old one
	if(target != "" && target != oldToggleLink) {
		
		// delete old anchor
		var myLocation = document.location.toString();
		var myLocationSplit = myLocation.split('#');
		
		// jump to anchor
		document.location.href = myLocationSplit[0] + '#' + target;
		
		// set this link as old link
		oldToggleLink = target;
	}
}

function myLinkToggle(myId, teaser, target) {

	// get new news item
	var newId = document.getElementById(myId);
	
	// get old news item
	var oldId = document.getElementById(oldToggleId);
	
	// get new teaser item
	var newTeaser = document.getElementById(teaser);
	
	// get old teaser item
	var oldTeaser = document.getElementById(oldToggleTeaser);
	
	// a news item is opened and the old one is the new one -> just close it
	// myId will never be empty
	if(myId == oldToggleId) {
		
		// close old news item and call callback function when finished
		$(oldTeaser).slideToggle("normal", function(){
			$(oldId).slideToggle("normal", function(){
				
				// set old news item empty
				oldToggleId = "";
				
				// set old link target empty
				oldToggleLink = "";
				
				// set old link target empty
				oldToggleLink = "";
			});
		});
	}
	
	// a news item is opened -> close this before opening a new one
	else if(myId != oldToggleId && oldToggleId != "") {
		
		// old news item found
		if(oldId) {
		
			// close old news item and call callback function when finished
			$(oldTeaser).slideToggle("normal", function(){
				$(oldId).slideToggle("normal", function(){
					
					// new news item found
					if(newId) {
						
						// open new news item and call callback function when finished
						$(newId).slideToggle("normal", function() {
							// set new news item as old news item
							oldToggleId = myId;
							
							// jump to new news item
							myLinkToggleJump(target);
							
							// show new teaser and set is as the old one
							$(newTeaser).slideToggle("normal");
							oldToggleTeaser = teaser;
						});
					}
				});
			});
		}
	}
	
	// no news item is opened -> just open the new one
	else if(oldToggleId == "") {
		
		// new news item found
		if(newId) {
			
			// open new news item and call callback function when finished
			$(newId).slideToggle("normal", function() {
				// set new news item as old news item
				oldToggleId = myId;
				
				// jump to new news item
				myLinkToggleJump(target);
				
				// show new teaser and set is as the old one
				$(newTeaser).slideToggle("normal");
				oldToggleTeaser = teaser;
			});
		}
	}
}

/** avatar selector */

function initMyAvatar() {
	var avatar_hidden = document.getElementById('inputfield_tx_ltbuser_avatar');
	if(avatar_hidden) {
		var myAvatarId = avatar_hidden.value;
	}
}

function setMyAvatar(myId) {

}

/** word counter */

function initKeyCounter(myObj, myKeyLimit, myDisplayAct, myDisplayMax) {
	var myObjElem = document.getElementById(myObj);
	var myDisplayActElem = document.getElementById(myDisplayAct);
	var myDisplayMaxElem = document.getElementById(myDisplayMax);
	
	if(myObjElem) {
		if(myObjElem.addEventListener) {
			myObjElem.addEventListener('keyup', function(event) {
				updateKeyCounter(event, myObjElem, myKeyLimit, myDisplayActElem, myDisplayMaxElem);
			}, false);
		}
		if(myObjElem.attachEvent) {
			myObjElem.attachEvent('onkeyup', function(event) {
				updateKeyCounter(event, myObjElem, myKeyLimit, myDisplayActElem, myDisplayMaxElem);
			});
		}
		if(myDisplayActElem && myDisplayMaxElem) {
			initKeyLimit(myObjElem, myKeyLimit, myDisplayActElem, myDisplayMaxElem);
		}
	}
}

function initKeyLimit(myObjElem, myKeyLimit, myDisplayActElem, myDisplayMaxElem) {
	if(myObjElem && myDisplayActElem && myDisplayMaxElem) {
		myDisplayActElem.innerHTML = myKeyLimit - myObjElem.value.length + ' Zeichen';
		myDisplayMaxElem.innerHTML = myKeyLimit + ' Zeichen';
	}
}

function updateKeyCounter(e, myObjElem, myKeyLimit, myDisplayActElem, myDisplayMaxElem) {
	if(!e) e = window.event;
	var key = e.keyCode || e.which;
	
	if(myObjElem) {
		if(myDisplayActElem && myDisplayMaxElem) {
			myDisplayActElem.innerHTML = myKeyLimit - myObjElem.value.length + ' Zeichen';
			//myDisplayMaxElem.innerHTML = myKeyLimit + ' Zeichen';
		}
		
		if(myObjElem.value.length >= myKeyLimit) {
			if(myObjElem.value.length >= myKeyLimit+(key==13?2:0) && key!=8 && key!=46 && key!=37 && key!=38 && key!=39 && key!=40 && key!=35 && key!=36){
				myObjElem.value = myObjElem.value.substr(0, myKeyLimit);
				
				if(myDisplayActElem && myDisplayMaxElem) {
					myDisplayActElem.innerHTML = myKeyLimit - myObjElem.value.length + ' Zeichen';
					//myDisplayMaxElem.innerHTML = myKeyLimit + ' Zeichen';
				}
				
				alert('Textlimit erreicht.');	
			}
		}
	}
}

/** open lightbox from flash movie */

function showFlashLightbox(myImg, lightboxImgWidth, lightboxImgHeight, myImgText, myImgAltText) {
	var lightboxKey = 'flash_lightbox';
	var lightboxContainerKey = 'flash_lightbox_container';
	var lightboxImgKey = 'flash_lightbox_img';
	
	var lightboxContainer = document.getElementById(lightboxContainerKey);
	
	if(!lightboxContainer) {
		$('body').append('<div id=' + lightboxContainerKey + ' style="display:block;"></div>');
		lightboxContainer = document.getElementById(lightboxContainerKey);
	}
	
	lightboxContainer.innerHTML = '<a href="typo3conf/ext/galerie/template.php?img=' + myImg + '&amp;width=' + lightboxImgWidth + '&amp;height=' + lightboxImgHeight + '&amp;text=' + myImgText + '&amp;alttext=' + myImgAltText + '" rel="' + lightboxKey + '" style="display:none;">showFlashLightbox</a>';
	/** */
	lightboxImgWidth = parseInt(lightboxImgWidth) + 20;
	lightboxImgHeight = parseInt(lightboxImgHeight) + 35;
	
	$("a[rel^='" + lightboxKey + "']").prettyPopin({
		width : lightboxImgWidth,
		height: lightboxImgHeight,
        opacity: 0.75,
        followScroll: true,
        loader_path: 'fileadmin/templates/img/loader.gif',
		callback: function() {
			lightboxContainer.innerHTML = '';
		}
    });
	/** */
	/**
	$("a[rel^='" + lightboxKey + "']").lightBox({
		fixedNavigation:true,
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: 'fileadmin/templates/javascript/jquery.plugins/jquery-lightbox/images/lightbox-ico-loading.gif',
		imageBtnClose: 'fileadmin/templates/javascript/jquery.plugins/jquery-lightbox/images/lightbox-btn-close.gif',
		imageBtnPrev: 'fileadmin/templates/javascript/jquery.plugins/jquery-lightbox/images/lightbox-btn-prev.gif',
		imageBtnNext: 'fileadmin/templates/javascript/jquery.plugins/jquery-lightbox/images/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Bild',
		txtOf: 'von'
	});
	*/
	
	$('#' + lightboxContainerKey + ' a:first').trigger('click');
}


/** open lightbox for User Gallery from flash movie */

function showFlashLightbox_ug(myImg, lightboxImgWidth, lightboxImgHeight, myImgText, myImgAltText) {
	var lightboxKey = 'flash_lightbox';
	var lightboxContainerKey = 'flash_lightbox_container';
	var lightboxImgKey = 'flash_lightbox_img';

 
	myImgAltText = " ";
	
	var aspectratio = lightboxImgWidth / lightboxImgHeight;
	var surfframe_height  = (typeof window.innerHeight == 'undefined') 
							? document.documentElement.clientHeight 
							: window.innerHeight;
		surfframe_height-= 200;
	
		
	
	
	
	var surfframe_width  = (typeof window.innerWidth == 'undefined') 
							? document.documentElement.clientWidth 
							: window.innerWidth;
		surfframe_width -= 35;
	
	

		
	
	
	if (lightboxImgWidth > surfframe_width)
	{
		lightboxImgWidth = surfframe_width;
		lightboxImgHeight = parseInt(lightboxImgWidth / aspectratio);
	}
	
	if (lightboxImgHeight > surfframe_height)
	{
		lightboxImgHeight = surfframe_height;
		lightboxImgWidth = parseInt(lightboxImgHeight * aspectratio);
	}
	

	// hier auch die Länge des Textes berücksichtigen
	var charsperline = 1;
	if (lightboxImgWidth >= 270)
	{
		charsperline = parseInt(lightboxImgWidth/12); 
	}
	else
	{
		charsperline = parseInt(270/12);
	}
	
	var textlines = parseInt(myImgText.length/charsperline); 
	
	//alert (charsperline);	
	//alert (textlines);

	lightboxImgHeight -= (textlines * 10);
	lightboxImgWidth = parseInt(lightboxImgHeight * aspectratio);

	
	
	//alert (surfframe_height+"-"+lightboxImgHeight+" -/- "+surfframe_width+"-"+lightboxImgWidth);
	
	
	
	var lightboxContainer = document.getElementById(lightboxContainerKey);
	
	if(!lightboxContainer) {
		$('body').append('<div id=' + lightboxContainerKey + ' style="display:block; margin-top:-10px; padding-bottom:10px;"></div>');
		lightboxContainer = document.getElementById(lightboxContainerKey);
	}
	
	lightboxContainer.innerHTML = '<a href="typo3conf/ext/galerie/template.php?img=' + myImg + '&amp;width=' + lightboxImgWidth + '&amp;height=' + lightboxImgHeight + '&amp;text=' + myImgText + '&amp;alttext=' + myImgAltText + '" rel="' + lightboxKey + '" style="display:none;">showFlashLightbox</a>';
	/** */
	lightboxImgWidth = parseInt(lightboxImgWidth) + 20;
	lightboxImgHeight = parseInt(lightboxImgHeight) + 35;
	
	
	
	
	$("a[rel^='" + lightboxKey + "']").prettyPopin({
		width : lightboxImgWidth,
		height: lightboxImgHeight+10+(textlines * 10),
        opacity: 0.75,
        followScroll: true,
        loader_path: 'fileadmin/templates/img/loader.gif',
		callback: function() {
			lightboxContainer.innerHTML = '';
		}
    });
	/** */
	/**
	$("a[rel^='" + lightboxKey + "']").lightBox({
		fixedNavigation:true,
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: 'fileadmin/templates/javascript/jquery.plugins/jquery-lightbox/images/lightbox-ico-loading.gif',
		imageBtnClose: 'fileadmin/templates/javascript/jquery.plugins/jquery-lightbox/images/lightbox-btn-close.gif',
		imageBtnPrev: 'fileadmin/templates/javascript/jquery.plugins/jquery-lightbox/images/lightbox-btn-prev.gif',
		imageBtnNext: 'fileadmin/templates/javascript/jquery.plugins/jquery-lightbox/images/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Bild',
		txtOf: 'von'
	});
	*/
	
	$('#' + lightboxContainerKey + ' a:first').trigger('click');
}



/** tool tip */

var isToolTip = false;

function initToolTip() {
	var qTop_voting_text_id = document.getElementById('voting_text');
    var qTop_voting_text_content;
   
	if(qTop_voting_text_id) {
		qTop_voting_text_content = qTop_voting_text_id.firstChild.nodeValue;
	}
	
	if(isToolTip) {
		var api = $('#voting_func').qtip('api');
		if(api) {
			try{
				api.updateContent(qTop_voting_text_content);
			}
			catch(e) {
				//alert(e);
			}
		}
	}
	else {
		$('#voting_func').qtip({
		   content: qTop_voting_text_content,
		   show: 'mouseover',
		   hide: 'mouseout',
		   style: { 
			  name: 'light',
			  corner: 'bottomLeft',
			  tip: 'topMiddle',
			  width: 200,
			  border: {
				width: 3,
				radius: 5
			  }
		   },
		   position: {
			  corner: {
				 target: 'bottomLeft',
				 tooltip: 'topLeft'
			  }
		   }
		});
		
		isToolTip = true;
	}
}

/** user image gallery */

var oldImgId = 0;

// myImgId ... database primary key for current image; must be set in xml file, too; so right image can be detected in xml file
function showFlashUserGallery(myImgId, myImgUploader, myImgPath, myImgBeschr, mytitle /** zusätzliche Parameter */) {
	// if an url is set -> only forward
	if(rbeCoverFlowJumpLocation && rbeCoverFlowJumpLocation != "") {
		window.location.href = rbeCoverFlowJumpLocation + myImgId;
		
	}

	//alert("hallo");
	
	// only allow clicks if requested img id is not the old one that is already displayed
	if(myImgId != oldImgId) {
		// save ImgId
		oldImgId = myImgId;
		
		// update voting container --> do it first if img to load is too big
		updateVotingContainerUserGallery(myImgId);

		// create new flash movie
		var flashPath = 'fileadmin/templates/flash/usergallery/UsergalleryMain.swf';
		var flashBase = 'fileadmin/templates/flash/usergallery/';
		var flashWidth = '505';
		var flashHeight = '505';
		
		var flashUserGalleryFlashKey = 'flashUserGalleryFlashContainer';
		
		var so = new SWFObject(flashPath, "flash", flashWidth, flashHeight, "9.0.45", "#FFFFFF");
		so.addParam("wmode", "transparent");
		so.addParam("base", flashBase);
		so.addVariable("galleryID", 521);
		so.addVariable("imgID", myImgId);
		/** zusätzliche Parameter */
		so.addVariable("path", myImgPath);
		so.addVariable("picturetext", myImgBeschr);
		so.addVariable("username", myImgUploader);
		so.addVariable("title", mytitle);
		
		so.write(flashUserGalleryFlashKey);
		
		//alert("hallo2"+myImgPath+myImgId);
	}
}

		

// create new voting for current image
function updateVotingContainerUserGallery(myImgId) {	
	$.ajax({
		type:'GET',
		url:'http://www.lustiges-taschenbuch.de/index.php',
		data:'eID=tx_rbeugpg_ajax&uid_ausgabe=' + myImgId + '&action=getvoting',
		dataType:'json',
		success:function(data) {
			// testing
			/**
			alert('voting_text ' + data.votingtext);
			alert('has_voted ' + data.has_voted);
			alert('voting_avg ' + data.average);
			alert('voting_func ' + data.user_rating);			
			*/
			// end testing
		
			// set tooltip text
			$('#voting_text').html(data.votingtext);
			initToolTip();
			
			// average votes
			$('#voting_avg').html(data.average);
			
			// user is allowed to vote
			if(data.has_voted == false) {
				// reset user value to zero
				$('#voting_func').html(data.user_rating);
				
				// activate rater functionality
				var ajaxUrl = 'http://www.lustiges-taschenbuch.de/index.php?eID=tx_rbeugpg_ajax&uid_ausgabe=' + myImgId;
				$('#voting_func').rater({ postHref: ajaxUrl });
			}
			// user vote already exists
			else {
				$('#voting_func').html(data.user_rating);
			}
		}
	});
}

// user gallery click on big image
function showFlashUserGalleryImage(myImgPath) {
	window.open(myImgPath);
}


function blurLink (dummy)
{

}



















