/*
 * Created on 30.10.2008
 *
 * yui_functions.js
 * 
 * author: rwelbers <rwelbers@brainpool.de>
 * video & image list functions added by tobi fl�ter at 2008-12-10
 */

/** Logger **/
/*
window.onload = function(){
	var myLogReader = new YAHOO.widget.LogReader("yui_logger", {newestOnTop:true, verboseOutput:false}); 
	myLogReader.setTitle("Debug Logger");
}
*/


/** Ajax **/
function ajax(method, url, action, div, formId){
	if(action != '') action='?action='+action;
	if(typeof div == 'undefined'){
		YAHOO.log('AJAX Target div nicht gesetzt!', 'error');
		return;
	}
	if(url == ''){
		YAHOO.log('AJAX URL nicht gesetzt!', 'error');
		return;
	}
	
	//2010-05-30 beim redirect gehen hier die POST-Daten verloren
	if(url == 'ajax.php') url = '/tvtotal/includes/php/ajax.php';	
	//if(url == 'ajax.php') url = '/includes/php/ajax.php';

	if(typeof method == 'undefined' || method == '') method = 'GET';
	
	if(typeof formId !== 'undefined' && formId != '') {
		formObj = document.getElementById(formId);
		YAHOO.util.Connect.setForm(formObj);
	} 
	var callback = {
		success: function(o) {
			YAHOO.log('AJAX Request Success '+url+action, 'info'); 
			YAHOO.log('AJAX Request Targetdiv: '+div, 'info');
			//alert(document.getElementById(div));
			if(document.getElementById(div))
				document.getElementById(div).innerHTML = o.responseText;
			else
				YAHOO.log('AJAX Request Targetdiv: '+div+' existiert nicht', 'error');
		},
		failure: function(o) {
			YAHOO.log('AJAX Request Failed: '+url, 'error'); 
		}
	}
	YAHOO.util.Connect.asyncRequest(method, url+action, callback);
	//return false;
}

function ajax_modal(method, url, action){
	if(action != '') action='?action='+action;

	if(url == ''){
		YAHOO.log('AJAX URL nicht gesetzt!', 'error');
		return false;
	}
	
	//2010-05-30 beim redirect gehen hier die POST-Daten verloren
	if(url == 'ajax.php') url = '/tvtotal/includes/php/ajax.php';	

	if(typeof method == 'undefined' || method == '') method = 'GET';
	
	var callback = {
		success: function(o) {
			YAHOO.log('AJAX Request Success '+url+action, 'info'); 
				document.getElementById('ajax_waiting').style.display = 'none';
				bacs_modal_show('Hinweis!', o.responseText, true);
		},
		failure: function(o) {
			YAHOO.log('AJAX Request Failed: '+url, 'error'); 
		}
	}
	YAHOO.util.Connect.asyncRequest(method, url+action, callback);
	//return false;
}

/** Video & Imagelist functions: fadeIn / fadeOut **/

var last_div_id	=	"";
var counter		=	0;

function fadeSubnavi(targetid, countSubElem){
	var fader = document.getElementById(targetid);
	if(counter > 0 && last_div_id != targetid){
		hideSubnavi(last_div_id);
	}
	else{
		counter = 1;
	}
	showSubnavi(targetid, countSubElem);

		/*
	if(fader.style.display == 'none'){
		if(counter > 0 && last_div_id != targetid){
			hideSubnavi(last_div_id);
		}
		else{
			counter = 1;
		}
		showSubnavi(targetid, countSubElem);
	}
	else if(fader.style.display == 'block' || fader.style.display == ''){
		hideSubnavi(targetid);
	}
	*/
}


function showSubnavi(targetid, countSubElem){
	var elem = document.getElementById(targetid);
	//elem.style.display = 'block';
	var new_height = 22 * countSubElem;
	var elem_options = {
		opacity: { to: 1 },
		height: { to: new_height }
	};
	elem_animation = new YAHOO.util.Anim(targetid, elem_options, 0.5, YAHOO.util.Easing.easeOut);
	elem_animation.animate();
	last_div_id = targetid;
}



function hideSubnavi(targetid){
	var elem = document.getElementById(targetid);
	var elem_options = {
		opacity: { to: 0 },
		height: { to: 0 }
	};
	/*
	function hide_this_div(){
		elem.style.display = 'none';
	}
	elem_animation = new YAHOO.util.Anim(targetid, elem_options, 0.25, YAHOO.util.Easing.easeOut);
	*/
	elem_animation = new YAHOO.util.Anim(targetid, elem_options, 0.25, YAHOO.util.Easing.easeOut);
	//elem_animation.onComplete.subscribe(hide_this_div);
	elem_animation.animate();	
} 


function initSubnavi(targetid){
	var elem = document.getElementById(targetid);
	var elem_options = {
		opacity: { to: 0 },
		height: { to: 0 }
	};
	elem_animation = new YAHOO.util.Anim(targetid, elem_options, 0.01, YAHOO.util.Easing.easeOut);
	elem_animation.animate();	
} 




function showElemWithTimeout(id){
	setTimeout('lilHelper("'+id+'")',250);

}

function lilHelper(id){
 	showElem(id);
 }
 
function suggestSearch(input, div)
{
	YAHOO.BasicRemote = function() {
    // Use an XHRDataSource
    var oDS = new YAHOO.util.XHRDataSource("/tvtotal/includes/php/ajax.php?");
    
    // Set the responseType
    oDS.responseType = YAHOO.util.XHRDataSource.TYPE_JSARRAY;
    
    // Define the schema of the delimited results
    oDS.responseSchema = {
        fields: ["SearchWord", "CountResults"]
    };
    oDS.scriptCallbackParam = "callback";
    
    // Enable caching
    oDS.maxCacheEntries = 10;

    // Instantiate AutoCompletes 
    var oConfigs = { 
        useShadow: false, 
        queryDelay: 0, 
        minQueryLength: 1, 
        animVert: true,
        animHoriz: false,
        animSpeed: 0.2,
        autoHighlight: false,
        typeAhead: true,
        allowBrowserAutocomplete: true,
        maxResultsDisplayed: 10,
        
        prehighlightClassName: "highlight_suggest", 
        highlightClass: "highlight_suggest"
        
    } 
    var oAC = new YAHOO.widget.AutoComplete(input, div, oDS, oConfigs);
   	
    oAC.generateRequest = function(sQuery) { 
    	return "action=getSearchwords&results=100&query=" + sQuery ; 
    }; 
    
    // Result data passed as object for easy access from custom formatter. 
    oAC.resultTypeList = false; 
    
    // Customize formatter to show thumbnail images 
    oAC.formatResult = function(oResultData, sQuery, sResultMatch) { 
    	var str = "<div class=\"result float_left\">"+oResultData.SearchWord+"</div>";
    		//str = str + "<div class=\"small\">"+oResultData.CountResults+" Ergebnisse</div>";
        return str;
    }; 
    oAC.containerCollapseEvent.subscribe(function(type, args){
   		document.getElementById(div).style.display = 'none';
   		return true;
   	});
   	oAC.doBeforeExpandContainer = function(oTextbox, oContainer, sQuery, aResults){
   		document.getElementById(div).style.display = 'block';
   		return true;
   	};
    return {
        oDS: oDS,
        oAC: oAC 
    };
}();
}


function anim_ShowElem(targetId, duration)
{
	
	document.getElementById(targetId).style.opacity = 0;
	document.getElementById(targetId).style.display = 'block';
	var data = { opacity: { to: 1 }};
	
	var anim = new YAHOO.util.Anim(targetId, data, duration, YAHOO.util.Easing.easeIn);
	anim.onComplete.subscribe(function () {
		document.getElementById(targetId).style.opacity = 1;
	});
	anim.animate();
}

function anim_HideElem(targetId, duration)
{
	var data = { opacity: { to: 0 }};
	var anim = new YAHOO.util.Anim(targetId, data, duration, YAHOO.util.Easing.easeIn);
	anim.onComplete.subscribe(function () {
		document.getElementById(targetId).style.opacity = 0;
		document.getElementById(targetId).style.display = 'none';
	});
	anim.animate();
}





function animTopRightBottomLeftWidthHeight(targetID, top, right, bottom, left, width, height, opacity)
{
	if(typeof targetID == "undefined")
	{
		return false;
	}
	if(typeof top == "undefined")
	{
		top = 0;
	}
	if(typeof right == "undefined")
	{
		right = 0;
	}	
	if(typeof bottom == "undefined")
	{
		bottom = 0;
	}	
	if(typeof left == "undefined")
	{
		left = 0;
	}
	if(typeof width == "undefined")
	{
		width = 0;
	}	
	if(typeof height == "undefined")
	{
		height = 0;
	}
	if(typeof opacity == "undefined")
	{
		opacity = 1;
	}	
	var attributes = {
		width: { to: width },
		height: { to: height },
		top: { to: top },
		right: { to: right },
		bottom: { to: bottom },
		left: { to: left },		
		opacity: { to: opacity }
	};
	
	var myAnim = new YAHOO.util.Anim(targetID, attributes, 0.3);
	myAnim.animate();
	return myAnim;
}


function resizeImageWithAnimation(targetID, widthFrom, widthTo, heightFrom, heightTo)
{
	var top 	= 0;
	var bottom 	= 0;
	var opacity = 1;
	var right 	= 0;
	
	var targetIDBig 	= targetID + "__big"; 
	var targetIDClose 	= targetID + "__close";
	
	var currentHeight 	= document.getElementById(targetIDBig).offsetHeight;
	var currentWidth 	= document.getElementById(targetIDBig).offsetWidth;
	
	
	if((currentHeight <= widthTo) && (currentWidth <= widthFrom))
	{
		if(document.getElementById(targetIDClose))
		{
			document.getElementById(targetIDClose).style.display = 'block';
		}
		var height 	= heightTo;
		var width 	= widthTo;
		var left 	= -400;
	}
	else
	{
		if(document.getElementById(targetIDClose))
		{
			document.getElementById(targetIDClose).style.display = 'none';
		}		
		var height 	= heightFrom;
		var width 	= widthFrom;
		var left 	= 40;
	}
	
	animTopRightBottomLeftWidthHeight(targetIDBig, top, right, bottom, left, width, height, opacity);
}



    
/**
 * tooltip
 */
YAHOO.namespace("tooltip.container");

var tooltipDuration = 0.3;
var currentOpendedTooltip = "";

function openTooltip(targetId){
    
    if(currentOpendedTooltip!= "" && document.getElementById(currentOpendedTooltip))
    {
        animateTooltip(currentOpendedTooltip, false);   
    }
    animateTooltip(targetId, true);
}

function closeTooltip(targetId){
    animateTooltip(targetId, false);
}

function animateTooltip(targetId, status) {
    
    	YAHOO.tooltip.container.tooltip = 
	new YAHOO.widget.Overlay(targetId, 
	{
		visible : status,
		width : 275,
		height: 207,
		zIndex : 9999,
		effect:{
			effect : YAHOO.widget.ContainerEffect.FADE,
			duration : tooltipDuration
		} 
	} );
        
        currentOpendedTooltip = targetId;
	YAHOO.tooltip.container.tooltip.render("tooltip");
        
}




