function setCookie( name, value, expires, path, domain, secure )
{
  // set time, in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );
  
  /*
  if the expires variable is set, make the correct
  expires time, the current script below will set
  it for x number of days, to make it for hours,
  delete * 24, for minutes, delete * 60 * 24
  */
  if ( expires )
  {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  
  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
  ( ( path ) ? ";path=" + path : "" ) +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
  
}


function getCookie(c_name){
  if (request.header.cookie)
  {
    c_start=request.header.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    {
      c_start=c_start + c_name.length+1;
      c_end=request.header.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=request.header.cookie.length;
         return unescape(request.header.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

function  setValues(s1sec,s1count,s1simple,s1detail,s2sec,s2count,s2simple,s2detail,s3sec,s3count,s3simple,s3detail,s4sec,s4count,s4simple,s4detail){
    setCookie( 's1sec', s1sec, 1000, '/', '', '' );
    setCookie( 's1count', s1count, 1000, '/', '', '' );
    if(s1simple == true){
      setCookie( 's1show', 'simpleSection', 1000, '/', '', '' );
    }
    else{
      setCookie( 's1show', 'pictureSection', 1000, '/', '', '' );
    }

    setCookie( 's2sec', s2sec, 1000, '/', '', '' );
    setCookie( 's2count', s2count, 1000, '/', '', '' );
    if(s2simple == true){
      setCookie( 's2show', 'simpleSection', 1000, '/', '', '' );
    }
    else{
      setCookie( 's2show', 'pictureSection', 1000, '/', '', '' );
    }
    
    setCookie( 's3sec', s3sec, 1000, '/', '', '' );
    setCookie( 's3count', s3count, 1000, '/', '', '' );
    if(s3simple == true){
      setCookie( 's3show', 'simpleSection', 1000, '/', '', '' );
    }
    else{
      setCookie( 's3show', 'pictureSection', 1000, '/', '', '' );
    }
    
    setCookie( 's4sec', s4sec, 1000, '/', '', '' );
    setCookie( 's4count', s4count, 1000, '/', '', '' );
    if(s4simple == true){
      setCookie( 's4show', 'simpleSection', 1000, '/', '', '' );
    }
    else{
      setCookie( 's4show', 'pictureSection', 1000, '/', '', '' );
    }
  }
/*
[3,[['s1','http://www.business.dk/unwire/latest/news_article/2945/',1],
                            ['s2','http://www.business.dk/unwire/latest/news_article/2949/',5],
                            ['s4','http://www.business.dk/unwire/latest/news_article/2955/',5],
                            ['s5','http://www.business.dk/unwire/latest/news_article/2956/',5],
                            ['s6','http://www.business.dk/unwire/latest/news_article/2967/',5]]],
*/
function getFeedFromCookieVal(portalId,cookieVal,sectionId){
    var feedMapping = [[1,[['s1','http://www.b.dk/unwire/latest/news_article/2/',1],
                            /*['s2','http://www.berlingske.dk/unwire/nodequeue/218/',5],*/
                            ['s2','http://www.b.dk/unwire/latest/news_article/1363/',5],
                            /*['s4','http://www.berlingske.dk/unwire/nodequeue/216/',5],*/
                            ['s4','http://www.b.dk/unwire/latest/news_article/1364/',5],
                            /*['s5','http://www.berlingske.dk/unwire/nodequeue/226/',5],*/
                            ['s5','http://www.b.dk/unwire/latest/news_article/1368/',5],
                            ['s6','http://www.b.dk/unwire/nodequeue/277/',5],
                            ['s7','http://www.b.dk/unwire/nodequeue/276/',5],
                            ['s8','http://www.business.dk/unwire/latest/news_article/2943/',5]]],
                    [2,[['s1','http://www.erhvervsbladet.dk/unwire/latest/news_article/11/',1],
                            ['s2','http://www.erhvervsbladet.dk/unwire/latest/news_article/12/',5],
                            ['s4','http://www.erhvervsbladet.dk/unwire/latest/news_article/13/',5],
                            ['s5','http://www.erhvervsbladet.dk/unwire/latest/news_article/15/',5],
                            ['s6','http://www.erhvervsbladet.dk/unwire/latest/news_article/16/',5]]],
                    [3,[['s1','http://www.business.dk/unwire/latest/news_article/2945/',1],
                            ['s2','http://www.business.dk/unwire/latest/news_article/2949/',5],
                            ['s4','http://www.business.dk/unwire/latest/news_article/2955/',5],
                            ['s5','http://www.business.dk/unwire/latest/news_article/2956/',5],
                            ['s6','http://www.business.dk/unwire/latest/news_article/2967/',5]]],

                    [4,[['s1','http://www.sporten.dk/unwire/latest/news_article/3412/',1],
                            ['s2','http://www.sporten.dk/unwire/latest/news_article/9432/',5],
                            ['s4','http://www.sporten.dk/unwire/latest/news_article/9434/',5],
                            ['s5','http://www.sporten.dk/unwire/latest/news_article/9426/',5],
                            ['s6','http://www.sporten.dk/unwire/latest/news_article/9415/',5]]],
                    [5,[['s1','http://www.bt.dk/unwire/latest/news_article/1/',1],
                            ['s2','http://www.bt.dk/unwire/term/news_article/5/',5],
                            ['s4','http://www.bt.dk/unwire/term/news_article/9/',5],
                            ['s5','http://www.bt.dk/unwire/term/news_article/56/',5],
                            ['s6','http://www.bt.dk/unwire/term/news_article/57/',5],
                            ['s7','http://www.bt.dk/unwire/term/news_article/419/',5]]]]
                            
    var defaultMapping = [[1,['http://www.b.dk/unwire/nodequeue/276/','http://www.business.dk/unwire/latest/news_article/2943/']],
                          [2,['http://www.erhvervsbladet.dk/unwire/latest/news_article/11/','http://www.erhvervsbladet.dk/unwire/latest/news_article/12/']],
                          /*[3,['http://www.business.dk/unwire/latest/news_article/2945/','http://www.business.dk/unwire/latest/news_article/2949/']],*/
                          [3,['http://www.business.dk/unwire/nodequeue/403/4/','http://www.b.dk/unwire/nodequeue/210/4/']],
                          [4,['http://www.sporten.dk/unwire/latest/news_article/3412/','http://www.sporten.dk/unwire/latest/news_article/9432/']],
                          [5,['http://www.bt.dk/unwire/latest/news_article/1/','http://www.bt.dk/unwire/term/news_article/5/','','http://www.bt.dk/unwire/term/news_article/419/']]];
    
    var output = '';                        
    
    if(cookieVal != ''){                        
        for(var i=0; i < feedMapping.length; i++){      
            var portalFeeds = feedMapping[i];
            if(portalFeeds[0] == portalId){
                var sectionFeeds = portalFeeds[1];
                for(var j = 0; j < sectionFeeds.length; j++){
                    var currentSection = sectionFeeds[j];
                    if(currentSection[0] == cookieVal){
                        output = currentSection[1];
                        break;
                    }
                }
                break;
            }
        }
    }else{
        for(var i=0; i < defaultMapping.length; i++){
            var defaultFeeds = defaultMapping[i];
            if(defaultFeeds[0] == portalId){
                var sectionFeeds = defaultFeeds[1];
                if(sectionId < sectionFeeds.length){
                    output = sectionFeeds[sectionId];
                }
                break;
            }
        }
    }
    
    return output;
    
}

function getVideoJobId(portalId){
     var videoMapping = [[1,'253'],
                            [2,'255'],
                            [3,'255'],
                            [4,'256'],
                            [5,'254']]
     var output = '';
     for(var i = 0; i < videoMapping.length; i++){
        if(videoMapping[i][0] == portalId){
            output = videoMapping[i][1];
            break;
        }
     }
    return output;
}

function showSection(portalId, sectionId){      
    
    var sectionMapping = [['s1count','s1sec','s1show',['s1','s2','s3','s4','s5','s7','s8','s6']],
                          ['s2count','s2sec','s2show',['s1','s2','s3','s4','s5','s7','s8','s6']],
                          ['s3count','s3sec','s3show',['s1','s2','s3','s4','s5','s7','s8','s6']],
                          ['s4count','s4sec','s4show',['s0','s1','s2','s3','s4','s5','s7','s8','s6']]]
    


                        
    var countName = sectionMapping[sectionId][0];
    var cookieName = sectionMapping[sectionId][1];
    var tagrootName = sectionMapping[sectionId][2];
    var count = getCookie(countName);
    var cookieVal = getCookie(cookieName);
    var tagroot = getCookie(tagrootName);
    var src = getFeedFromCookieVal(portalId,cookieVal,sectionId);
    var cache = 5;
    var jobId = getVideoJobId(portalId);
    
    if(count == ''){
      if(portalId == 1) {
        if(sectionId == 0 ){
              count = '4';
          }else{
              count = '4';
          }
      }
      else if(portalId == 3) {
        if(sectionId == 0 ){
              count = '4';
          }else{
              count = '4';
          }
      }
      else {
      
        if(portalId != 5){
            count = '5';
        }else{
            if(sectionId == 0 || sectionId == 3){
                count = '10';
            }else{
                count = '5';
            }
        }
     }
    }
    
    
    
    if(tagroot == ''){
        switch(tagrootName){
            case 's1show':
              if(portalId == 1 ||portalId == 3){
                    tagroot = 'pictureSectionNoEdit';
                }
            case 's2show':
                if(portalId == 1 || portalId==3){
                    tagroot = 'pictureSectionNoEdit';
                }else if (portalId == 5){
                    tagroot = 'simpleSection';
                } 
                else {
                      tagroot = 'pictureSection';
                  }
                  
                  /*if(portalId != 5){
                      tagroot = 'pictureSection';
                  }
                  else{
                      tagroot = 'simpleSection';
                  }*/
                
                break;
            case 's3show':
                /*if(portalId==1){
                    tagroot = 'simpleSection';
                }*/
                /*tagroot = 'pictureSection';*/
                break;
            case 's4show':
                if(portalId == 5){
                    tagroot = 'pictureSection';
                }
                break;
        }
    }
    
    if(cookieVal == ''){
        switch(cookieName){
            case 's1sec':
                if(portalId != 5){
                    cookieVal = 's6';
                }else{
                    cookieVal = 's7';
                } 
                /*cookieVal = 's7'; */
                break;               
            case 's2sec':
                if(portalId != 5){
                    cookieVal = 's7';
                }else{
                    cookieVal = 's8';
                } 
                /*cookieVal = 's8';*/
                break;
            case 's3sec':               
                cookieVal = 's3';
                break;
            case 's4sec':
                 if(portalId != 5){
                    cookieVal = 's0';
                }else{
                    cookieVal = 's7';
                } 
                /*cookieVal = 's0';*/
                break;
        } 
    }

    
    var returnString = '';
    
    if(cookieVal =='s3'){
        returnString = '<videosection hidden="false"><mvs:listclips itemsPerPage="'+count+'" jobId="'+jobId+'" showCategories="false" showLatestCategoriesOnly="false" sortAscending="false" sortByTimestamp="true"/></videosection>';
    }
    else if(src != '' && src != 'mobiltv'){
        returnString = '<ump:importXml timeout="3000" cacheMinutes="'+cache+'" src="'+src+count+'" docpath="//channel" tagroot="'+tagroot+'"/>';
    }
    
    return returnString;
    
}

