﻿//////////////////////
// Rss Functions /////
// GD            /////
//////////////////////

function GetSelectAjaxInfo(selectedIndex)
{
    // by what id get the correct ajax
    var url = appPathScript + "/Pages/Application/Feeds/CustomRssService.aspx";
	var params = "CATG="+selectedIndex;
	
	AJAX.syncWithCallback(url, params, "CallBackFromAjax", true, true);	
}

function CallBackFromAjax(response)
{
    //build new options // or input for autocomplete
    if (!isNullOrUndefined(response))
    {
        var str = trim(response);
        if (str.length > 0)
        { 
            var col_array=str.split(',');
            HandleResponseString( col_array );       
        }
    }
}
function HandleResponseString(strArray)
{
    var optionSelect = document.getElementById(CustomRss.TypeSelectId);
    optionSelect.options.length = 0;
   
    optionSelect.options[0] = new Option('All','-1');
    var part_num=0;
    var index = 1;
    while (part_num < strArray.length)
    {
      var singleValue= strArray[part_num].split('#');
      if ( singleValue.length == 2)
      {  
        optionSelect.options[index] = new Option(singleValue[0],singleValue[1]);
        index++;
      }
      part_num+=1;
    }

}

function GetSelectAjaxAutoComplete()
{
      // not implemnted
}

//First drop down
function GetFeedType()
{
    makeLinkLikeLoading();
    var FeedSourceID = document.getElementById(CustomRss.FeedSourceID);
   
    var selectedIndex = FeedSourceID.selectedIndex;
    var isLatestes = (selectedIndex == 0 ? 1 : 0 );
   
    UpdateStage(1, FeedSourceID.options[selectedIndex].value);
    
    if ( selectedIndex > -1 )
    {
        if (  CustomRss.ArrayInput.indexOf( FeedSourceID.options[selectedIndex].value ) > -1)
        {
            // Input    
            document.getElementById(CustomRss.TypeSelectIdTd).className = "RssHideMe"; 
            document.getElementById(CustomRss.TypeInputId).className = "RssShowMe";           
            UpdateLink(0,1);
            HandleTypeOfSortShow(FeedSourceID.options[selectedIndex].value,1);
        }
        else
        {
            //drop  
            document.getElementById(CustomRss.TypeSelectIdTd).className = "RssShowMe";
            document.getElementById(CustomRss.TypeInputId).className = "RssHideMe";           
            if (selectedIndex == 0)
            {
                    UpdateLink(2,0);
            }
            else
            {
                UpdateLink(1,0);
            }
            
            HandleTypeOfSortShow(0,0);
        }
        
        if (isLatestes == 0) 
        {
            GetSelectAjaxInfo(FeedSourceID.options[selectedIndex].value);
            WriteTypeSearch(FeedSourceID.options[selectedIndex].text);
        }
        else
        {
            document.getElementById(CustomRss.TypeNameSelectId).innerHTML = '';
        }
        
    } 
    
 
    MakeSelectSelected(isLatestes , CustomRss.TypeSelectId);
    MakeSelectSelected(isLatestes , CustomRss.SortID);
    MakeSelectSelected(isLatestes , CustomRss.TimeId);     
    
   
    makeLinkNotLoading();   
    
    var TypeSelectSelect = document.getElementById(CustomRss.TypeSelectId);  
    var selectedIndexType = TypeSelectSelect.selectedIndex; 
    UpdateStage(2, TypeSelectSelect.options[selectedIndexType].value);
}

//selectedindex is value !
function HandleTypeOfSortShow(selectedindex , showDropDown)
{
     if (showDropDown == 0)
     {
        //need to show drop downs
        document.getElementById(CustomRss.FirstRad).className = "RssHideMe";
        document.getElementById(CustomRss.SecondRad).className = "RssHideMe";
        document.getElementById(CustomRss.ThirdRad).className = "RssHideMe";
     }
     else
     {
        if ( selectedindex == parseInt(CustomRss.ArrayInput[0]))
        { // fav
            document.getElementById(CustomRss.FirstRad).className = "RssHideMe";
            document.getElementById(CustomRss.SecondRad).className = "RssHideMe";
            document.getElementById(CustomRss.ThirdRad).className = "RssShowMe";
        }
        if ( selectedindex == parseInt(CustomRss.ArrayInput[1]))
        { //users
            document.getElementById(CustomRss.FirstRad).className = "RssHideMe";
            document.getElementById(CustomRss.SecondRad).className = "RssShowMe";
            document.getElementById(CustomRss.ThirdRad).className = "RssHideMe";
        }
        if ( selectedindex == parseInt(CustomRss.ArrayInput[2]))
        { // tag
            document.getElementById(CustomRss.FirstRad).className = "RssShowMe";
            document.getElementById(CustomRss.SecondRad).className = "RssHideMe";
            document.getElementById(CustomRss.ThirdRad).className = "RssHideMe";
        }
     }
}
//Region stage 2
function UpdateDropDownTypeSelect()
{
    var selected = document.getElementById(CustomRss.TypeSelectId);
    UpdateStage(2,selected.options[selected.selectedIndex].value);
    UpdateLink(1,0);       
}

function WriteTypeSearch(text)
{
   document.getElementById(CustomRss.TypeNameSelectId).innerHTML = 'Select a ' + text; 
}
function UpdateInputTypeInput(item)
{
    //    
    
    //value 
    UpdateStage(2,item.Value);
  
    //text 
    CustomRssSelectionValues.TypeName = item.Text;
    
    UpdateLink(1,1);       
}
//Region stagte 3 -6 - need value only
function UpdateDropDown3to6(index,id)
{
    var selected = document.getElementById(id);
    var FeedSourceID = document.getElementById(CustomRss.FeedSourceID);
    UpdateStage(index,selected.options[selected.selectedIndex].value);
    if (  CustomRss.ArrayInput.indexOf( FeedSourceID.options[FeedSourceID.selectedIndex].value ) > -1)
    {
         UpdateLink(1,1);  
    }
    else
    {
        UpdateLink(1,0);  
    }
}


// update the link that was selected
// tag / username send 0 all other send 1
function UpdateLink(type,isInput)
{   
     //get link -> change aspx of all params if 1
     //set aspx innerhtml with selected values
     var textoflink = '';
    
     if (type == 1)
     {
        //do here alot
        var basicHref = CustomRss.AniboomAddress + "/" + 
        CustomRssSelectionValues.Source + "/" +
        CustomRssSelectionValues.Type + "/" +
        CustomRssSelectionValues.Sort + "/" +
        CustomRssSelectionValues.Time  + "/" +
        CustomRssSelectionValues.Adult + "/" +
        CustomRssSelectionValues.No  + "/";
        
        document.getElementById(CustomRss.RssLinkAspxId).href = basicHref;
        document.getElementById(CustomRss.RssLinkIconAspxId).href = basicHref;
        document.getElementById(CustomRss.RssLinkGoogleId).href = CustomRss.RssLinkGoogleHtml +  basicHref;
        document.getElementById(CustomRss.RssLinkYahooId ).href = CustomRss.RssLinkYahooHtml + basicHref;
        
        // text 
       
        textoflink = document.getElementById(CustomRss.FeedSourceID).options[document.getElementById(CustomRss.FeedSourceID).selectedIndex].text;
        if ( isInput == 1)
        {
           textoflink += ' ' +  CustomRssSelectionValues.TypeName;
        }
        else
        {
            if (document.getElementById(CustomRss.TypeSelectId).selectedIndex > -1) 
            {
                textoflink += ' ' +  document.getElementById(CustomRss.TypeSelectId).options[document.getElementById(CustomRss.TypeSelectId).selectedIndex].text;  
            }
        }
      
        textoflink += ' ' + document.getElementById(CustomRss.SortID).options[document.getElementById(CustomRss.SortID).selectedIndex].text;
        textoflink += ' ' + document.getElementById(CustomRss.TimeId).options[document.getElementById(CustomRss.TimeId).selectedIndex].text;       
        document.getElementById(CustomRss.RssLinkAspxId).innerHTML =  textoflink;
     }
     else
     {
        //make it unavalivle
        if (type == 2)
        {
            //latests hits
             var basicHref = CustomRss.AniboomAddress + "/" + 
             CustomRssSelectionValues.Source + "/" +
             CustomRssSelectionValues.Type + "/" +
             CustomRssSelectionValues.Sort + "/" +
             CustomRssSelectionValues.Time  + "/" +
             CustomRssSelectionValues.Adult + "/" +
             CustomRssSelectionValues.No  + "/";     
             
             document.getElementById(CustomRss.RssLinkAspxId).href = basicHref;
             document.getElementById(CustomRss.RssLinkIconAspxId).href = basicHref;
             document.getElementById(CustomRss.RssLinkGoogleId).href = CustomRss.RssLinkGoogleHtml +  basicHref;
             document.getElementById(CustomRss.RssLinkYahooId ).href = CustomRss.RssLinkYahooHtml + basicHref;          
               
             textoflink = document.getElementById(CustomRss.FeedSourceID).options[0].text;
             document.getElementById(CustomRss.RssLinkAspxId).innerHTML =  textoflink;
        }
        else
        {
             document.getElementById(CustomRss.RssLinkAspxId).innerHTML = '';             
        }
        
     }
     if (type == 0)
     {
        document.getElementById(CustomRss.RssFeedLinks).className = "RssHideMe";   
        document.getElementById(CustomRss.RssFeedLinksTd).className = "RssHideMe"; 
     }
     else
     {
        document.getElementById(CustomRss.RssFeedLinks).className = "RssShowMe"; 
        document.getElementById(CustomRss.RssFeedLinksTd).className = "RssShowMe";        
     }
     
     document.getElementById(CustomRss.RssFeedNameId).innerHTML = textoflink;
     
}

function makeLinkLikeLoading()
{
}

function makeLinkNotLoading()
{
}

function makeLinkNotSelected()
{
}

// set if select is unavalivle or not
// 1 is disabled
function MakeSelectSelected(type , id)
{
    var select = document.getElementById(id);
    type == 1 ?  select.disabled = true : select.disabled = false;    
}

// get which stage was update ( 1 - 6 ) and sets the value
function UpdateStage(selectedIndex, value)
{
    switch (selectedIndex){
        case 1: 
        CustomRssSelectionValues.Source = value;
        break;
        case 2: 
        CustomRssSelectionValues.Type = value;
        break;
        case 3: 
        CustomRssSelectionValues.Sort = value;
        break;
        case 4: 
        CustomRssSelectionValues.Time = value;
        break;
        case 5: 
        CustomRssSelectionValues.Adult = value;
        break;
        case 6: 
        CustomRssSelectionValues.No = value;
        break;       
        default :
        //do nothing
        index = index;
    }
}
