﻿//
// handle clicks on the start time radio buttons (options)
//
function setGridStart(nValue)
{
	var obj = document.getElementById("selStartTime");
	if(obj)
	{
	    switch(nValue)
	    {
		    case 0:
		    {
			    optionGridStart = "-1";
			    if(obj) { obj.disabled = true; }	
			    obj = document.getElementById("defGridStartFixed");
			    if(obj){obj.checked = false;} 
			    break;
		    }
		    case 1:
	        {
			    if(obj) 
			    { 
				    obj.disabled = false; 
				    optionGridStart = obj[obj.selectedIndex].value;
    			    obj = document.getElementById("defGridStartLocal");
	    		    if(obj){obj.checked = false;} 
			    }	
			    break;
		    }
		    default: break;
	    }
        setOptionsChanged();
	}
}

function addfav(favImage)
{  
    var nChan = favImage.id.replace(/^cc/,"");
    var n, selList;
    
    if(favImage.src.indexOf('heart-off')>0)    
	{
        // add this channel to favorites
        favImage.src = favImage.src.replace(/off/i,"on");
		selList = availChannels;
		if(selList)
		{
			for(n=0; n<selList.length; n++)
			{
				if(nChan == selList[n].value)
				{
					selList.selectedIndex = n;
					moveOneChannel(1);
				}
			}
            if(selectChannels.options.length==1)
            { tabHiLite(iCurrentTab); }            
		}        
	}
	else
	{
        // remove this channel from favorites
        favImage.src = favImage.src.replace(/on/i,"off");
		selList = selectChannels;
		if(selList)
		{
			for(n=0; n<selList.length; n++)
			{
				if(nChan == selList[n].value)
				{
					selList.selectedIndex = n;
					moveOneChannel(0);
				}
			}
            if(selectChannels.options.length<=0)
            { tabHiLite(iCurrentTab); }
		}
	}
	setChannelsCookie(selectChannels);
}

function addch(theCheckbox)
{  
    var nChan = theCheckbox.id.replace(/^cc/,"");
    var n, selList;

	if(theCheckbox.checked)
	{
		selList = availChannels;
		if(selList)
		{
			for(n=0; n<selList.length; n++)
			{
				if(nChan == selList[n].value)
				{
					selList.selectedIndex = n;
					moveOneChannel(1);
				}
			}
		}
	}
	else
	{
		selList = selectChannels;
		if(selList)
		{
			for(n=0; n<selList.length; n++)
			{
				if(nChan == selList[n].value)
				{
					selList.selectedIndex = n;
					moveOneChannel(0);
				}
			}
		}
	}
	setChannelsCookie(selectChannels);
}


function closeOptions(nOpt)
{
	objGenreSelect.value = iLastGenreValue;

    doOptionsTab(nOpt);
	
	if(bOptionsChanged)
	{
		var oldMusic = optionShowMusic;
		optionShowMusic = isOptionChecked("chkShowMusic", optionShowMusic);				
		if(optionShowMusic){paramSuppressMusic=0;}else{paramSuppressMusic=1;}

		var oldPPV = optionShowPPV;
		optionShowPPV = isOptionChecked("chkShowPPV", optionShowPPV);				
		if(optionShowPPV){paramSuppressPPV=0;}else{paramSuppressPPV=1;}
		
		var old24Hour = optionShow24Hour;
		optionShow24Hour = isOptionChecked("chkShow24Hour", optionShow24Hour);				
		if(optionShow24Hour){paramSuppress24Hour=0;}else{paramSuppress24Hour=1;}
				
		optionEnableGridSelect = isOptionChecked("chkEnableGridSelect", optionEnableGridSelect);	
		optionTruncateText 	  = isOptionChecked("chkTruncateText", optionTruncateText);	
		optionHideIcons 	  = isOptionChecked("chkHideIcons", optionHideIcons);	
		optionTimebarRepeat   = getSelectListValue("selTimebarRepeat", optionTimebarRepeat);
	    optionDisableFlyovers = isOptionChecked("chkDisableFlyovers", optionDisableFlyovers);
	    optionPopNewWindows   = isOptionChecked("chkPopNewWindows", optionPopNewWindows);
	    optionDontUseGoButton = isOptionChecked("chkDontUseGoButton", optionDontUseGoButton);
		
		toggleGoButton(optionDontUseGoButton);
		
		if(isOptionChecked("gridExpand", !optionScrollGrid))
		{
			optionScrollGrid = false;
            iGridScrollTop = -1;
		}
		else
		{
			optionScrollGrid = true;
			var gridHeightPx = document.getElementById("selGridHeightPx");		
			if(gridHeightPx)
			{
				optionGridHeightPx = gridHeightPx.value;
			}
		}

		/* Over-rides for iPad and iPhone */
		if(isAppleMobile) { setAppleMobile(); }		
		
		var selChans = objSelectedChannelsCombo;

		if(selChans.length>0)
		{
			if(bFilterChanged)
			{
			    // if we added or removed any channels 
			    // switch to favorites view automatically
			    optionFavChannels = true;
			    bFilterChanged = false;
                optionGenreFilter = 0;
			    iLastGenreValue = 1;
			}
			setChannelsCookie(selChans);
		}
		else
		{
			optionFavChannels = false;
			deleteCookie(cookieSelectedChannels + iServiceID);
			setChannelFavorites(iServiceID, "");
		}
		
		showLoadingScreen();
		
		optionGridMinutes = getSelectListValue("timespanSelect", iGridSpanMinutes);	
			
		if((optionGridMinutes!=iGridSpanMinutes)||(oldMusic!=optionShowMusic)||(oldPPV!=optionShowPPV)||(old24Hour!=optionShow24Hour))
		{
			// if gridspan changes we need to get new data
			paramGridMins = optionGridMinutes;
			saveCookieOptions();
			if(iCurrentTab!=7)
            {
				reloadGrid();
			}
		}
		else
		{
			saveCookieOptions();
			if(iCurrentTab!=7)
            {
				setTimeout("drawListings()", 1);
			}
		}
	}
}

function setOptionsChanged()
{
	bOptionsChanged = true;
}

function moveOneChannel(iDirection)
{
	var source = null;
	var target = null;
	
	switch(iDirection)
	{
		case 0: /* delete */
		source = selectChannels;
		target = availChannels;
		break;
		
		case 1: /* add */
		source = availChannels;
		target = selectChannels;
		break;
		default: break;
	}
		
	if(!((source)&&(target)))
	{
	    alert("Error: Source or Target is NULL");
	}
	
	var index = source.selectedIndex;

	if(index<0) 
	{
		alert('nothing selected!');
		return false;
	}
	var newIndex = index;
	for(index; index<source.length; index++)
	{
	    if(source.options[index].selected)
	    {	
	        var added		= false;
	        var iLastIndex 	= target.length;
        	
	        var newElem    	= document.createElement("OPTION");
        	
	        newElem.text 	= source.options[index].text;
	        newElem.value   = source.options[index].value;
        	
	        var n = 0;
        	
        	// walk the list to see if we need to insert
	        for(n=0; n<target.length; n++)
	        {		
		        if(parseInt(newElem.value,10) < parseInt(target[n].value,10))
		        {
			        if(isIE)
			        {
			            target.options.add(newElem, n);
			        }
			        else
			        {
				        target.add(newElem, target.options[n]);
			        }
        				
			        n = target.length + 1;
			        added = true;
		        }
		    }
		    
	        // if not inserted above, add here
	        if(!added)
	        {
		        if(isIE)
		        {
	                target.options.add(newElem);
		        }
		        else
		        {
    		        target.add(newElem, null);
    	        }
    	    }
	        
	        source.remove(index);
	        newIndex = index;	
	        if(index>=0){index--;}
	    }
	}
		
    if(newIndex<source.length)
    {
        source.selectedIndex = newIndex;
    }
    else
    {
        source.selectedIndex = source.length-1;
    }

	setOptionsChanged();
	bFilterChanged = true;
	return false;
}

function moveAllChannels(iDirection)
{
	var source = null;
	
	switch(iDirection)
	{		    
		case 0: /* delete */
		source = selectChannels;
		break;
		
		case 1: /* add */
		source = availChannels;
		break;
		default: break;
	}
	if(source.length>0)
	{   
	    if(iDirection===0)
	    { 
	        if(!confirm("Are you sure you want to clear all your selected channels? This action is not reversible! If you choose OK, you will need to re-select your preferred channels again."))
            {
                return false;
            }
        }    
	    source.selectedIndex = source.length-1;
	    while(source.length>0)
	    {
		    moveOneChannel(iDirection);
		    source.selectedIndex = source.length-1;
	    }
	}
	return false;
}

function xmlPrefsDataEvent()
{
	/*
	if (xmlPrefsData.readyState==4 || xmlPrefsData.readyState=="complete")
	{ alert(xmlPrefsData.responseText); }
	*/
}


