pimpedPCRatings = new Array();
myRating = 0;
pimpedPCId = 0;
pimpUsername = '';
numPCs = 0;

var pimpmypcEngine = {
  init: function(id) {
    pimpedPCId = id;
    pimpmypcEngine.renderDice();
    
	},

  showImg: function(img) {
      var newMainImageSrc = $('image' + img).src.substr(0, $('image' + img).src.indexOf('&w')) + '&w=430';
      $('image' + img).src = $('mainImage').src.substr(0, $('mainImage').src.indexOf('&w')) + '&w=60&h=60';
      $('mainImage').src = newMainImageSrc;
      var newMainImageCaption = $('caption' + img).value;
      $('caption' + img).value = $('mainImageCaption').innerHTML;
      $('mainImageCaption').innerHTML = newMainImageCaption;
  },
  
	rateIt: function(rating) {
      
    	document.body.style.cursor = "wait";
      $('die_' + rating).style.cursor = "wait";
      var myAjax = new Ajax.Request('/pimpmypc/pimpmypc_action.php', {
    		method: 'post',
    	  requestHeaders: ['Cache-Control', 'no-cache, must-revalidate, no-store', 'Expires', '0', 'pragma', 'no-cache'],
    	  parameters: 'task=ratePC&pimpedPCId=' + pimpedPCId + '&rating=' + rating + '&ajax=' + new Date() * 1,
    		onSuccess: function(req){
          if (req.responseText && req.responseText.match(/avgRatingDie/i)) {
            $('ratingSummary').update(req.responseText);
            myRating = rating;
            pimpmypcEngine.renderDice();          		 
          } else  {
            MODAL.Action.Show('<span class="close"><span class="sprite close-icon"></span></span><div class="inner span-10"><h2>Feil</h2><p>' + req.responseText + '</p></div>');
          } 
          document.body.style.cursor = "default";
          $('die_' + rating).style.cursor = "pointer";
        }   		  
      });

  },
  
  renderDice: function() {
    if (myRating == 0){ myRating = pimpmypcEngine.get_myRating(); }
    
    $$('.die').each(function(el){    	
      currentDie = el.id.replace('die_', '');
      if ((currentDie != myRating) && (myRating > 0)) Element.addClassName(el, "highlight");
      else if (Element.hasClassName(el, "highlight")) Element.removeClassName(el, "highlight"); 
    })
  },
  
  get_myRating: function(){
    myRating = 0;
    /*pimpedPCRatings.each(function(rating){
      // rating[0] = 'readerUsername', rating[1] = rating
      if (rating[0] == my.username) myRating = rating[1];
    })*/
    
    var myAjax = new Ajax.Request('/pimpmypc/pimpmypc_action.php', {
		method: 'post',
		requestHeaders: ['Cache-Control', 'no-cache, must-revalidate, no-store', 'Expires', '0', 'pragma', 'no-cache'],
		parameters: 'task=getMyRating&pimpedPCId=' + pimpedPCId + '&ajax=' + new Date() * 1,
		onSuccess: function(req){			
			if(req.responseText>0){				
				myRating = req.responseText;
				
				$$('.die').each(function(el){    	
			      currentDie = el.id.replace('die_', '');
			      if ((currentDie != myRating) && (myRating > 0)) Element.addClassName(el, "highlight");
			      else if (Element.hasClassName(el, "highlight")) Element.removeClassName(el, "highlight"); 
			    })
			}
		}   		  
	});
	
    return myRating;
  },
    
  
  submitPimpSearch: function() {
    searchQuery = $F('searchQuery');
    skip = $F('skip');
      top.location.href = '/pimpmypc/finn/' + encodeURIComponent(searchQuery);
  },
  
  goToEnterPCForm: function() {

    top.location.href = "/pimpmypc/ny/"; 
  },
  
  getRandomPC: function() {
    top.location.href = "/pimpmypc/tilfeldig/" + Math.round(numPCs * Math.random());
  },
  showReportPimpedPC : function(pimpedPCId){
        if (!pimpedPCId) return;
    MODAL.Action.Show('<span class="close"><span class="sprite close-icon"></span></span><div class="inner span-10"><h2>Rapport&eacute;r maskin</h2><form action="/pimpmypc/pimpmypc_action.php" name="ReportPimpedPCForm" id="ReportPimpedPCForm"><p>Hvis du mener det er noe galt med dette PimpMyPC-innlegget, og du &oslash;nsker at moderatorene skal ta en n&aelig;rmere titt p&aring; den (evt. fjerne den), vennligst oppgi begrunnelsen din for dette &oslash;nsket her:</p><input type="hidden" value="' + pimpedPCId + '" id="pimpedPCId" /><textarea style="width:90%;" id="REPORT_PC_REASON" name="REPORT_PC_REASON"></textarea><button type="button" id="ReportPimpedPCButton">Rapport&eacute;r</button></form></div>');
      Event.observe('ReportPimpedPCButton', 'click', function(event){
        pimpmypcEngine.reportPimpedPC(pimpedPCId);        
        MODAL.Action.Unload();
      });
  },
  reportPimpedPC: function(pimpedPCId) {
      new Ajax.Request( $('ReportPimpedPCForm').action, {
          method: 'post',
          requestHeaders: ['Cache-Control', 'no-cache, must-revalidate, no-store', 'Expires', '0', 'pragma', 'no-cache'],
          parameters: 'task=reportPC&pimpedPCId=' + pimpedPCId + '&reason=' + encodeURIComponent($('REPORT_PC_REASON').value) + '&ajax=' + new Date() * 1,
          onSuccess: function(req){
            if (req.responseText) {
                MODAL.Action.Show('<span class="close"><span class="sprite close-icon"></span></span><div class="inner span-10"><h2>Feil</h2><p>' + req.responseText + '</p></div>');
            } else {
                MODAL.Action.Show('<span class="close"><span class="sprite close-icon"></span></span><div class="inner span-10">Takk! Din kommentar er registrert, og ITavisens moderatorer vil bli varslet. </div>');

              $('pimpVarsling').style.display = 'none';
            }
          }
      });
  


      
  }
  
}

