function initializeWidgets() {
	$('.category .toggle-button').bind('click', function() {
		var li = $(this).closest('.category');
		if (li.is('.collapsed')) {
			li.switchClass('collapsed','expanded',200);
		} else {
			li.switchClass('expanded','collapsed',200);
		}
		return false;
	});
	$('.week-days').buttonset();
	var catlis = $('#adv-search-cat-list .toggle-button');
	if (catlis.length > 0) {
		catlis.bind('click', function() {
			var li = $(this).closest('li');
			if (li.is('.collapsed')) {
				li.switchClass('collapsed','expanded',200);
			} else {
				li.switchClass('expanded','collapsed',200);
			}
			return false;
		});
	}
	if ($('#complex-search').length > 0) {
		var emptyText = $('.search-filter-summary .auto').first().text();
		updateAdvancedSearchFilterFeedback(emptyText);
		$('#complex-search #q').bind('keyup', function() {
			updateAdvancedSearchFilterFeedback(emptyText);
		});
		$('#complex-search select').bind('change', function() {
			updateAdvancedSearchFilterFeedback(emptyText);
		});
		$('#complex-search :checkbox').bind('click', function() {
			updateAdvancedSearchFilterFeedback(emptyText);
		});
	}
}

function updateAdvancedSearchFilterFeedback(emptyText) {
	var autoTextNodes = $('.search-filter-summary .auto');
	autoTextNodes.empty();
	autoTextNodes.append($('#complex-search #q').val());
	// categories are checked separately
	var noDefSelects = $('#complex-search table option:selected').not('.default');
	if (noDefSelects.length) {
		noDefSelects.each(function() {
			if (autoTextNodes.text().length) autoTextNodes.append(", ");
			autoTextNodes.append($.trim($(this).text()));
		});
	}
	var catlis = $('#adv-search-cat-list').children('li');
	if (catlis.length) {
		catlis.each(function() {
			var checkbox = $(this).children('input');
			var mcSelected = checkbox.is(':checked');
			var mcText = checkbox.attr('title');
			var subCatlis = $(this).find('option:selected').not('.default');
			$(this).removeClass('subselects');
			var subtext = "";
			if (subCatlis.length) {
				// toggle the class 'subselects' on the mc li
				$(this).addClass('subselects');
				subCatlis.each(function() {
					if (subtext.length) subtext = subtext + ", ";
					subtext = subtext + $.trim($(this).text());
				});
			}
			if (mcSelected | subtext.length) {
				if (autoTextNodes.text().length) autoTextNodes.append(", ");
				autoTextNodes.append(mcText);
				if (subtext.length) autoTextNodes.append(" (" + subtext + ")");
			}
		});
	}
	var checked = $('#complex-search input:checked').not('.input-mc');
	if (checked.length) {
		checked.each(function() {
			if (autoTextNodes.text().length) autoTextNodes.append(", ");
			autoTextNodes.append($(this).attr('title'));
		});
	}
	if (!autoTextNodes.text().length) {
		autoTextNodes.append(emptyText);
		disableSubmit($('.search-submit'), true);
		$('.search-filter-summary input:hidden').val('');
	} else {
		disableSubmit($('.search-submit'), false);
		$('.search-filter-summary input:hidden').val(autoTextNodes.first().text());
	}
}

function disableSubmit(jqObj,disable){
	if (disable) {
		jqObj.find('input:submit').attr('disabled','disabled');
	} else { 
		jqObj.find('input:submit').removeAttr('disabled');
	}
}
  	/**---------------------------------------------------------------- Pop Ups ---------------------------------------*/       

	/**
	 * This flag is set to true, if the joblisttoggle.js file is included into the page.
	 * If it is true, functions from that javascript can be called.
	 */
	var joblisttoggle_exists = false;
	
    function popup(link)
	{
        win = window.open(link,"tvtv_details","menubar=no,scrollbars=no,resizable=yes,status=no,width=500,height=500,dependent=yes");
        win.focus();
		// return false will prevent the href from which this function was called to be processed
		return false;
    }
    
    //Open the content popup window with a specific root node to start with
    function contentPopup(document,rootid)
    {
        win = window.open("web/content_popup.vm?document=" + document + "&popuproot=" + rootid,"tvtv_popup","width=519,height=600,scrollbars=yes,resizable=yes");
		win.focus();
		// return false will prevent the href from which this function was called to be processed
		return false;
    }
	    
  	/**---------------------------------------------------------------- Hide and Show ---------------------------------------*/       
    
   	/**
   	 * This function opens or closes the category menu identified by key. Then,
   	 * the link is called.
   	 */
	function toggleCategoryAndLink(key, link)	
	{
		toggleCategory(key);
		window.location.href=link;
	}
	
	/**
	 * This function opens or closes the category menu identified by key. Then,
	 * the function linkCategoryWithParams is called with the link.
	 */
	function toggleCategoryAndLink2(key, link)
	{
		toggleCategory(key);
		linkCategoryWithParams(link);
	}
	

	/**
	 * This function reads all hidden attributes from the category result page
	 * in order to generate the complete link with all selected days and 
	 * channelGroups.
	 */
	function linkCategoryWithAllParams()
	{
		var parameters = "index.vm?mainTemplate="+maintemplate;//$!{request_mainTemplate}";
		
		var element = document.getElementById("mcid");
		
		if(element != null && element != ""){
			parameters = parameters + "&mcid=" + element.value;
		}

		element = document.getElementById("scid");

		if(element != null && element != ""){
			parameters = parameters + "&scid=" + element.value;
		}
		linkCategoryWithParams(parameters);
	}

	
	
	/**
	 * This function reads all hidden attributes from the category result page
	 * in order to generate the complete link with all selected days and 
	 * channelGroups.
	 */
	function linkCategoryWithParams(link)
	{
		var parameters = "";

		for(var i = 0; i <= 20; i++)
		{
			aValue = document.DayArray.elements[i].value;
			aName = document.DayArray.elements[i].name;
			parameters = parameters + "&" + aName + "=" + aValue;
		}
		
		var elements = document.getElementsByName("cgid");
		
		for(var i = 0; i < elements.length; i++)
		{
			if(elements[i].checked)
			{
				parameters = parameters + "&cgid=" + elements[i].value;
			}
		}

		var completeLink = link + parameters;
		
		window.location.href=completeLink;
	}

    function toggleDiv(key) 
    {
    	obj1=document.getElementById(key);
    	if (obj1)
		{
    		visible = (obj1.style.display != "none");
			if (visible) 
			{
			  obj1.style.display="none";
			  setCookie(key, "no");
			} 
			else 
			{
			  obj1.style.display="";         
			  setCookie(key, "yes");
			}
		}
    }   
		
	function toggleOnOff(key)
	{
	   toggleDiv(key + "_on");
	   toggleDiv(key + "_off");		
    }
            
    function toggleCategory(key) 
    {
       toggleOnOff("cat_" + key);
        
       var catMaxValue;
       catMaxValue = getCookie("catMaxValue");
       if(catMaxValue == "" | catMaxValue < key)
       {
		 setCookie("catMaxValue", key);
       }
    }

    function toggleBlock(key)
    {
	    toggleOnOff("timeTab" + key);
        toggleDiv(key + "1");
        toggleDiv(key + "2");
        toggleDiv(key + "3");
    }

    
    function toggleTipOfDay()
	{
	   toggleOnOff("TipOfDay0");		
	   toggleDiv("TipOfDay1");
	   toggleDiv("TipOfDay2");
    }
        
    function toggleMorning()	{ toggleBlock("Morn"); }
    function toggleEve()		{ toggleBlock("Eve"); }
    function toggleDay()		{ toggleBlock("Day"); }
    function toggleNight()		{ toggleBlock("Night"); }

	function checkOnOff(key)
	{
	   checkDiv(key + "_on");
	   checkDiv(key + "_off");		
    }
	

	function checkDiv(key) 
    {
    	obj1=document.getElementById(key);
    	visible = getCookie(key);
		
		if (obj1)
		{
			if (visible == "no") 
			{
			  obj1.style.display="none";
			} 
			else if (visible == "yes") 
			{
			  obj1.style.display="";          
			}
		}
    }  

    function checkBlock(key)
    {
	    checkOnOff("timeTab" + key);
        checkDiv(key + "1");
        checkDiv(key + "2");
        checkDiv(key + "3");

        var value;
        value = getCookie("timeTab" + key + "_on");

        if (! value)
        {
            // No cookie defined
            
            var jetzt = new Date();
			var Std = jetzt.getHours();
			var nowKey = "";
			if (Std >= 5 && Std < 12) {
			  nowKey = "Morn";
			} else if (Std >= 12 && Std < 18) {
			  nowKey = "Day";
			} else if (Std >= 18 && Std <= 23) {
			  nowKey = "Eve";
			} else if (Std >= 0 && Std < 5) {
			  nowKey = "Night";
			}
            
            if (key == nowKey) {
                toggleBlock(key);
            }
        }
    }

    function checkBlockTipOfDay()
    {
		checkOnOff("TipOfDay0");	
		checkDiv("TipOfDay1");
		checkDiv("TipOfDay2");
    }
	
	function checkCookie()
    {
        checkBlockTipOfDay();
        checkBlock("Morn");
        checkBlock("Day");
        checkBlock("Eve");
        checkBlock("Night");
     }
	
	function checkCategory(){
        var catMaxValue;
        
        catMaxValue = getCookie("catMaxValue");
        if(catMaxValue != "")
        {
	        for(var i = 0; i <= catMaxValue; i++)
	        {
				checkOnOff("cat_" + i);
			}
	    }
	    
	    checkOnOff("mytvtv");	
    }
	
	/**---------------------------------------------------------------- Cookies ---------------------------------------*/

    function setCookie(key,value)
    {
    	////if ((${consumer} && $consumer.isUseCookies()) || !${consumer})
		if(useCookies)	{
	    	document.cookie= key + "=" +escape(value); 
		}
	    ////end
    }

     function getCookie(key)
    {
        var value = "";

		////if ((${consumer} && $consumer.isUseCookies()) || !${consumer})
		if(useCookies)	{
	        if (document.cookie.length>0)
	        {
	          c_start = document.cookie.indexOf(key + "=")
	
	          if (c_start != -1)
	          { 
	            c_start = c_start + key.length+1 
	            c_end = document.cookie.indexOf(";",c_start)
	            if (c_end==-1) 
	            {
	                c_end=document.cookie.length
	            }
	            value = unescape(document.cookie.substring(c_start,c_end))
	          } 
	        }
		}
        ////end
        
        return value;
    }

	/**---------------------------------------------------------------- Scrollbars and other style stuff ---------------------------------------*/

	
	
	function getFooterHeight()
	{
		var height;
        var footer;
        footer = document.getElementById("footer");
		height = getHeight(footer);
		return height;
	}

	function getHeaderHeight()
	{
		var height;
        var header;
        header = document.getElementById("header");
		height = getHeight(header);
		return height;
	}
	

	
    function adjustLeft(h) 
    {
        var addHeight;            
        //var mainleft;
		//var mainleft_height=0;
		
      	//mainleft = document.getElementById("main_left"); 		
		//mainleft_height = h - getHeaderHeight();
		//mainleft.style.height = mainleft_height;
				
		var catNavBar;
     	catNavBar = document.getElementById("catNavBar");    
        var a = getAutoAttribute("catNavBar", "top"); 
		var b = getAutoAttribute("search_form", "height");
		var c = getAutoAttribute("search_form", "bottom");
		addHeight = a+b+c+20;
		catNavBar.style.height = h - addHeight;
    }
	
	function getAutoAttribute(element_name, attrName)	{
		
		var hs;
        hs = 0;
		var element = document.getElementById(element_name);
		
        if (element)
        {
            if (document.getComputedStyle)
            {
				prop = window.getComputedStyle(element,'null').getPropertyValue(attrName);
            }
			else 	{
				var curr = eval("element.currentStyle."+attrName);
				if("auto" != curr)	{
					prop = curr;
				}
			}
            //velocityComment#* the actual height is reported as e.g. 90px, so 
            //*/ we have to strip off the px at the end
		
			if(isNaN(prop))	{
				hs = prop.slice(0, prop.length - 2);
			}
			else	{
				hs = prop;
			}
			
			return parseInt(hs);
        }
		return 0;
	}

    function adjustMain(h, w) 
    {
        var listView;		
		var heightDelta = 0;
		
		heightDelta = getAutoAttribute("main_right", "top");
		heightDelta += getAutoAttribute("listView", "top");
		heightDelta += getFooterHeight();
				
        listView = document.getElementById("listView");

        if (listView)
        {
            listView.style.height = h - heightDelta;
            listView.style.width = w - getAutoAttribute("main_left", "width"); 
        }
        
        var checkBoxTable = document.getElementById("checkBoxTable");
        if(checkBoxTable)
        {
        	checkBoxTable.style.top="-3px";
        }
    }

    function adjustAll() 
    {
    	var w,h;
        if (self.innerHeight)	{	// all except Explorer
            w = self.innerWidth;
            h = self.innerHeight;
        }	else if (document.documentElement && document.documentElement.clientHeight)	{
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
        }	else if (document.body)	{ // other Explorers
            w = document.body.clientWidth;
            h = document.body.clientHeight;
        }	else	{
			return;
		}
		adjustMain(h, w);
    	adjustLeft(h);
	}

    function getHeight(element)
    {
        var h;
        h = 0;
        if (element)
        {
            if (document.getComputedStyle)
            {
				prop = window.getComputedStyle(element,'null').getPropertyValue('height');
            }
            else
            {
                prop = element.currentStyle.height;
            }
            //velocityComment#* the actual height is reported as e.g. 90px, so 
            //*/ we have to strip off the px at the end
            h = prop.slice(0, prop.length - 2);
        }
        return parseInt(h);
    }

    function init()
    {    	   	
        if (navigator.appName == "Microsoft Internet Explorer") 
        {
            adjustAll(); 
        }
		
		checkCategory();
		
        if (document)
        {
            var lv;
            lv = document.getElementById("listView");
            if (lv)
            {
                checkCookie();
            }
        }
    }

    if (navigator.appName == "Microsoft Internet Explorer") 
    {
        window.onresize=adjustAll; 
    }
    
    
    //#* if a cookie with selected channels exists, these will be opened in the listview. */
    function openAdditionalChannels(link){
    	var selectedIds = getCookie("selAddChnls");    	
   		link = link + "&mainTemplate=web/additionalChannelsSelection.vm&view=addChnls";
    	window.location.href=link;
    }
    
    //#* If the day is changed, and the additionalChannelView is shown, a different link must be called than when the day is changed in the listview. */
    function openListviewOrAddChnls(link){
    	var selectedIds = getCookie("selAddChnls");
    	if(currentGroup == "additionalChannels")	{
    		link = link + "&chnl=" + selectedIds;
		}
		window.location.href=link;
    }