/**
 * Updates the archive display according to given values
 */
function updateArchiveDisplay(year, subtopicId, viewStyle, pageNr, countBroadcasts) {	
	//
	//update archive navigation
	//
	navMarkTopic(year, subtopicId);		
	
	//
	//update archive display
	//
  //define configuration of archive
  var rangeLengthImageView = 12;
	var rangeLengthListView = 20;
  
  //Create archive display view-style-part
  //create correct switch link
  if (viewStyle == 'imageView') {
    viewStyleSwitch = 'listView';
  }
  else {
    viewStyleSwitch = 'imageView';
  }
  viewStyleSwitchLink = 'javascript:updateArchiveDisplay('+year+','+subtopicId+',\''+viewStyleSwitch+'\',1,'+countBroadcasts+');';
  //create switch table
  //preamble of table
  var viewStylePart = '<table width="106" border="0" cellspacing="0" cellpadding="0" id="ViewSwitcher"><tr><td width="52">'+TXT_VIEW+'</td>';
  //body of table according to selected view style
  if (viewStyle == 'imageView') {
    var rangeLength = rangeLengthImageView;
    viewStylePart += '<td width="36"><img src="'+PATH_BASE_IMAGES+'btn_imageview_on.gif" alt="'+TXT_SHOW_AS_IMAGES+'" width="19" height="18" border="0" name="btn_imageview"/></td>';
    viewStylePart += '<td width="18"><a href="'+viewStyleSwitchLink+'"><img src="'+PATH_BASE_IMAGES+'btn_listview_off.gif" alt="'+TXT_SHOW_AS_LIST+'" width="17" height="18" border="0" name="btn_listview"/></a></td>';
  }
  else {
    var rangeLength = rangeLengthListView;
    viewStylePart += '<td width="36"><a href="'+viewStyleSwitchLink+'"><img src="'+PATH_BASE_IMAGES+'btn_imageview_off.gif" alt="'+TXT_SHOW_AS_IMAGES+'" width="19" height="18" border="0" name="btn_imageview"/></a></td>';
    viewStylePart += '<td width="18"><img src="'+PATH_BASE_IMAGES+'btn_listview_on.gif" alt="'+TXT_SHOW_AS_LIST+'" width="17" height="18" border="0" name="btn_listview"/></td>';
  }
  //close table
  viewStylePart += '</tr></table>';
  
  //Create archive display page-switcher-part
  //preamble of part
  var pageSwitcherPart = '<p id="PageSwitcher">';
  var countPages = Math.ceil(countBroadcasts / rangeLength);
  if (countPages > 1) {
    //show page switcher only if more than one page is available
    for (i = 1; i <= countPages; i++) {
      //add delimeter
      if (i != 1) {
        pageSwitcherPart += ' | ';
      }
      //show identifier of page or link to page
      if (i == pageNr) {
        pageSwitcherPart += '<span class="activePage">'+i+'</span>';
      }
      else {      
        pageSwitcherPart += '<a href="javascript:updateArchiveDisplay('+year+','+subtopicId+',\''+viewStyle+'\','+i+','+countBroadcasts+');">'+i+'</a>'; 
      }
    }
  }
  //close part
  pageSwitcherPart += '</p>';
  
  //Create archive display image-view-part
  //prepare variables
  var rangeStart = (pageNr-1)*rangeLength;
  var parameters = 'functionId=1&station='+STATION+'&year='+year+'&subtopicId='+subtopicId+'&rangeStart='+rangeStart+'&rangeLength='+rangeLength;
  //perform ajax request
	new Ajax.Request(URL_CONTROLLER, {
	  method: 'get',
	  parameters: parameters,
	  onSuccess: function(transport, json) {
  	  //create table or list according to results in json and view style	    
	    //preamble of part
	    if (viewStyle == 'imageView') {
  	    //define max number of cells in a row	      
	      var cells = 3;
  	    var i = 0;
	      var broadcastsPart = '<table width="468" border="0" cellspacing="0" cellpadding="0" id="ImageView"><tr>';
      }
      else {
        var broadcastsPart = '<ul id="ListView">';
      }
	    //iterate through each broadcast
	    json.each(function(broadcast) {
	      //create link
	      var broadcastLink = 'index.php?id='+ID_MOVIE_PAGE+'&station='+STATION+'&broadcastId='+broadcast.BROADCAST_ID+'&no_cache=1';
	      //append link and increase counter
	      if (viewStyle == 'imageView') {
	        broadcastsPart += '<td><a href="'+broadcastLink+'" title="'+broadcast.TITLE+'"><img src="'+broadcast.MAINIMAGE+'" alt="'+broadcast.TITLE+'" /><br/>'+broadcast.TITLE+'</a></td>';	        
  	      i++;
  	      //check if row has reached max cells
  	      if (i == cells) {
  	        broadcastsPart += '</tr><tr>';
  	        i = 0;	      
  	      }  	      
	      }
	      else {
	        broadcastsPart += '<li><a href="'+broadcastLink+'">'+broadcast.TITLE+'</a></li>'
	      }
	    });
	    //close part
	    if (viewStyle == 'imageView') {
	      //append empty cells to close table properly (if necessary)
	      for (; i<cells && i!=0; i++) {
	        broadcastsPart += '<td>&nbsp;</td>'
	      }
	      broadcastsPart += '</tr></table>';
      }
      else {
        broadcastsPart += '</ul>';
      }
      
	    //Append created parts to html element
	    $('ArchiveDisplay').update(viewStylePart+broadcastsPart+pageSwitcherPart);
	  }
	});
	
}

/**
 * Marks the selected topic as active
 */
function navMarkTopic(year, subtopicId) {
	//the preamble to the archive tree
	var preamble = 'div#ArchiveCategories ul';
	
	//create selectors according  to shown archive type (chronological or thematic)
	var activeTopicsSelector = '';
	var newTopicSelector = '';
	if (year != -1) {
		activeTopicsSelector = preamble+' li a#active';
		newTopicSelector = preamble+' li#year_'+year+' a';
	}
	else {
		activeTopicsSelector = preamble+ ' li ul li a#active';
		newTopicSelector = preamble+ ' li ul li#subtopic_'+subtopicId+' a';
	}	
	
	//firsly get all active topics (if any, normally just one), then remove active id from it
	var tmpNodes = $$(activeTopicsSelector);
	tmpNodes.each(function(t) {
		t.id = '';
	});	
	//finally set the selected one as active		
	tmpNodes = $$(newTopicSelector);
	tmpNodes.each(function(t) {
		t.id = 'active';
	});
	
}

/**
 * Opens the selected main topic
 */
function navOpenMaintopic(maintopicId) {
	//the preamble to the archive tree
	var preamble = 'div#ArchiveCategories ul';
	
	//firsly get all opened maintopics (if any, normally just one), then close it
	var tmpNodes = $$(preamble+' li ul.open');
	tmpNodes.each(function(t) {
		t.removeClassName('open');
	});
	//finally open desired one according to given maintopicId
	tmpNodes = $$(preamble+' li#maintopic_'+maintopicId+' ul');
	tmpNodes.each(function(t) {
		t.addClassName('open');
	});
}