articleId = '';
voted = new Array();

var artEngine = {
	init: function() {
    artEngine.renderTaggedStories();
    artEngine.output_comments();
    var commentsRefresher = new PeriodicalExecuter(artEngine.refresh_comments, 300);
    setTimeout('artEngine.output_fillers();',500);
    artEngine.renderVoteObjects();
    /* var voteUpdater = new PeriodicalExecuter(artEngine.renderVoteObjects, 30); */ // FUNKER IKKE I IE
	},
	
	refresh_comments: function() {
  	var myAjax = new Ajax.Request('/include/forumFeed_js.php', {
  		method: 'get',
  	  requestHeaders: ['Cache-Control', 'no-cache, must-revalidate, no-store', 'Expires', '0', 'pragma', 'no-cache'],
  	  parameters: 'art=' + articleId,
  		onSuccess: function(req){
  				  eval(req.responseText);
          	artEngine.output_comments();
            }   		  
    });
  },
	
  output_comments: function() {
	  forumEngine.output_comments('storyComments');
    if ($('requestedPost')) {
      Position.prepare();
      $('storyComments').scrollTop = Position.cumulativeOffset($('requestedPost'))[1] - Position.cumulativeOffset($('storyComments'))[1];
      $('storyComments').scrollTo();
      }
  },
  
  renderTaggedStories: function() {
    if(typeof(taggedStories) != 'undefined'){ //making sure that taggedStories exists and contains stories
    $$('.taggedStory').each(function(taggedStory){
      for (i = 0; i<= taggedStories.length-1; i++) {
        if (!in_array(taggedStoriesIds[i], omitTaggedArticles)) {
          $(taggedStory).innerHTML = taggedStories[i];
          omitTaggedArticles[omitTaggedArticles.length] = taggedStoriesIds[i];
          break;
        } 
      }
    });
    }
  },

  renderVoteObjects: function() {
      $$('.voteObject').each(function(voteObject){
            objectId = $(voteObject).id.replace('vote','');
          	var myAjax = new Ajax.Request('/include/art/voteObject.php', {
          		method: 'get',
          	  requestHeaders: ['Cache-Control', 'no-cache, must-revalidate, no-store', 'Expires', '0', 'pragma', 'no-cache'],
          	  parameters: 'objectId=' + objectId,
          		onSuccess: function(req){
                $(voteObject).innerHTML = req.responseText;    
                if (in_array(objectId, voted)) $('vote' + objectId + 'info').innerHTML = '&nbsp;&nbsp;&nbsp;Din stemme er registerert!'; 
              }   		  
            });
      });
  },
  
  doVote: function(altId, voteId) {
    var myAjax = new Ajax.Request('/art_action.php', {
      		method: 'post',
      	  requestHeaders: ['Cache-Control', 'no-cache, must-revalidate, no-store', 'Expires', '0', 'pragma', 'no-cache'],
      	  parameters: 'task=doVote&voteId=' + voteId + '&altId=' + altId + '&ajax=' + new Date() * 1,
      		onSuccess: function(req){
      				if (req.responseText) alert(req.responseText);
      				else {
                $('vote' + voteId + 'info').innerHTML = '&nbsp;&nbsp;&nbsp;Din stemme er registerert!'; 
      				  voted[voted.length] = voteId;
              }
					    setTimeout(artEngine.renderVoteObjects, 500);
          }   		  
     });
  },
  
  registerComment: function() {
    $('storyComments').scrollTop = $('storyComments').scrollHeight;	
    if (($F('commentInputText') === 'Skriv her...') || !$F('commentInputText')) {
      alert("Du må skrive noe i kommentarfeltet først!");
      $('commentInputText').clear();
      $('commentInputText').focus();
    }
    else if (my.id < 1) {
      loginMsg = "Du må være pålogget som bruker av ITavisen.no for å skrive kommentarer.";
      doAfterLogin = "forumEngine.submitPost($F('commentInputText'), 'articleComment');";
      fpEngine.showLoginScreen(loginMsg);
    } else {
      forumEngine.submitPost($F('commentInputText'), 'articleComment');
    }
  },
  
  output_fillers: function(){
    if ($('storyCenter')) {
      //storyCenterHeight = $('storyCenter').offsetHeight;
      //storyTitleHeight = $('storyTitleArea').offsetHeight;
      //for qxl
      //qxlHeight	= $('qxlAds').getHeight();
      //$('storyRight').style.height = ( ( storyCenterHeight + storyTitleHeight) - (qxlHeight + 11) ) + "px";
      //$('storyLeft').style.height = storyCenterHeight - 10 + "px";
      //$('storyRight').style.top	= ( qxlHeight + 20 ) + "px";
      //numStories = Math.floor(($('storyRight').offsetHeight-135) / 112);
      //artEngine.outputNewsList(numStories+1);
      //leftFillingHeight = $('storyLeft').offsetHeight - ($('storyLeftAdFilling').offsetHeight + $('storyLeftRelated').offsetHeight );
      //$('storyLeftFilling').style.height = leftFillingHeight -20 + "px";
      //numTopics = Math.floor((leftFillingHeight-90) / 37);
      //numTopics = (numTopics > 100) ? 100 : numTopics;
      numTopics = 8;
      $('forumBox').innerHTML = '<div id="artForumList">' + fpEngine.output_forumList(numTopics, '<span class="forumTitle">ITavisens</span> FORUM', true) + '</div>';
    }
  },
  
  outputNewsList: function(numStories) {
  	var myAjax = new Ajax.Request('/include/newsList_js.php', {
    method: 'get',
		requestHeaders: ['Cache-Control', 'must-revalidate, no-store', 'Expires', '300'],
		onSuccess: function(req){
			eval(req.responseText);
		  $('storyRight').innerHTML = fpEngine.outputNewsList(numStories, articleId);
		}   		  
	});
  },
  
  showTipBox: function() {
    output = '<div id="tipBox"><div style="padding: 4px;">';
    output += '<div><b>Send tips om denne saken til noen du kjenner:</b></div>';
    output += '<div>Epost-adresse: <input type="text" name="tipMail" id="tipMail" value="" /></div>';
    var tipName = ((my.username != 'gjest') && (typeof my.username != 'undefined')) ? my.username : '';
    output += '<div>Ditt navn: <input type="text" name="tipName" id="tipName" value="' + tipName + '" /></div>';
    output += '<div><br /><a href="javascript: artEngine.submitTip();" title="Send tips" class="button">Klikk her for å sende!</a> &nbsp;&nbsp;&nbsp;&nbsp; <a href="javascript: artEngine.hideTipBox();" title="Glem det"><u>Glem det</u></a></div>';
    output += '</div></div>';
    //$('tipBoxArea').innerHTML = output;
    $('storyCenter').scrollTo();
    $('tipMail').focus();
  },
  
  submitTip: function() {
    if($('tipBox')) {
      var tipMail = $F('tipMail');
      var tipName = $F('tipName');
      if (tipMail && tipName) {
        var myAjax = new Ajax.Request('/art_action.php', {
      		method: 'post',
      	  requestHeaders: ['Cache-Control', 'no-cache, must-revalidate, no-store', 'Expires', '0', 'pragma', 'no-cache'],
      	  parameters: 'task=sendTip&articleId=' + articleId + '&tipMail=' + encodeURIComponent(tipMail) + '&tipName=' + encodeURIComponent(tipName) + '&ajax=' + new Date() * 1,
      		onSuccess: function(req){
      				  if (req.responseText) alert(req.responseText);
      				  if (req.responseText == 'Tips sendt til ' + tipMail) artEngine.hideTipBox();
                }   		  
        });
      } else {
        alert("Du må fylle ut alle feltene først!");
      }
    }
  },
  
  hideTipBox: function() {
    if($('tipBox')) $('tipBox').hide();
  }
};
