﻿function Results()
{
	Spif.DOMEvents.attach(window, "load", this.doLoad, this);
}
Results.prototype =
{
	doLoad : function()
	{      
    var ratingButton = document.getElementById('show-customer-ratings');
    if (ratingButton == null)
    {
      results.showInformation();

		  if (document.getElementById('show-information') != null)
		  {
			  Spif.DOMEvents.attach(document.getElementById('show-information'), "click", results.showInformation, this);
			  Spif.DOMEvents.attach(document.getElementById('show-information'), "focus", function(){this.blur()});
		  }
    }
    else
    {
	    // Toggle viewing information or customerrating of ALL results
	    // based on the view-parameter in the querystring
	    if (/&view=rating$/.test(window.location.href))
	      results.showCustomerRatings();
  	    
	    if (/&view=info$/.test(window.location.href))
	      results.showInformation();

		  // Attach events to the ' information ' and ' customerrating ' links on top of the results
		  if (document.getElementById('show-customer-ratings') != null)
		  {
			  Spif.DOMEvents.attach(document.getElementById('show-customer-ratings'), "click", results.showCustomerRatings, this);
			  Spif.DOMEvents.attach(document.getElementById('show-customer-ratings'), "focus", function(){this.blur()});
	    }
  		
		  if (document.getElementById('show-information') != null)
		  {
			  Spif.DOMEvents.attach(document.getElementById('show-information'), "click", results.showInformation, this);
			  Spif.DOMEvents.attach(document.getElementById('show-information'), "focus", function(){this.blur()});
		  }
  		
		  if (Cookie.read('show-customer-ratings') == "true") this.showCustomerRatings();
	  } 
	  
	  var collapser = document.getElementById('sitemap_collapser');
	  if (collapser != null)
	  {
			Spif.DOMEvents.attach(collapser, "click", results.handleCollapser, this);
	  }
	  
	  var search_text = '';
	  if (document.getElementById('search_parameters') != null)
	  {
			search_text += resources.messages.clicktracker.ct_result;
			search_text += document.getElementById('search_parameters').value;
	  }
	  if (document.getElementById('map_parameters') != null)
	  {
			search_text += resources.messages.clicktracker.ct_resultmap;
			search_text += document.getElementById('map_parameters').value;
	  }
	  
		ClickTracker.trackClick(new Click(search_text));
	},
	
	/*
	 * Activate all transitions when we hit the button at the top of the page.
	 * We limit transitions to the top 3 elements for performance reasons for now,
	 * since the top 3 are the only elements visible when we're able to click the button.
	 * If the result contains the text "no-rating" (the classname of the tab when there are no ratings),
	 * skip it.
	*/
	showCustomerRatings: function(evt)
	{
      Cookie.write('show-customer-ratings', 'true');
	  var results = document.getElementById('results').getElementsByTagName('div');
	  var j = 0;
	  for (i = 0; i < results.length; i++)
	    if (Spif.ClassNameAbstraction.contains(results[i], 'show-info'))
	    {
	      if (/\bno-rating\b/.test(results[i].innerHTML))
	        continue;
	      Spif.ClassNameAbstraction.replace(results[i], 'show-info', 'show-rating', (j>3));
	      j++;
	    }
	      
	  var showRatingsButton = document.getElementById('show-customer-ratings').parentNode;
	  var showInfoButton = document.getElementById('show-information').parentNode;
	  if (!/\bselected\b/.test(showRatingsButton.className))
	  {
	    showRatingsButton.className = showRatingsButton.className += " selected";
	    showInfoButton.className = showInfoButton.className.replace(/\bselected\b/, '');
	  }
	  
	  this.updateSortLinksHrefs("rating");
	  
	  if (evt)
	  {
	    evt.subject.blur();
	    if (evt.preventDefault)
	      evt.preventDefault();
	    return false;
	  }
	},

	showInformation: function(evt)
	{
      Cookie.write('show-customer-ratings', 'false');
    if (!document.getElementById('results'))  
      return;
      
	  var results = document.getElementById('results').getElementsByTagName('div');
	  var j = 0;
	  for (i = 0; i < results.length; i++)
	    if (Spif.ClassNameAbstraction.contains(results[i], 'show-rating'))
	    {
	      if (/\bno-rating\b/.test(results[i].innerHTML))
	        continue;
	      Spif.ClassNameAbstraction.replace(results[i], 'show-rating', 'show-info', (j>3));
	      j++;
	    }
	  
	  var ratingsButton = document.getElementById('show-customer-ratings');
	  if (ratingsButton != null)
	  {
	    var showRatingsButton = document.getElementById('show-customer-ratings').parentNode;
	    var showInfoButton = document.getElementById('show-information').parentNode;
	    if (!/\bselected\b/.test(showInfoButton.className))
	    {
	      showInfoButton.className = showInfoButton.className += " selected";
	      showRatingsButton.className = showRatingsButton.className.replace(/\bselected\b/, '');
	    }
	  }
	  
	  this.updateSortLinksHrefs("info");
	  
	  if (evt)
	  {
	    evt.subject.blur();
	    if (evt.preventDefault)
	      evt.preventDefault();
	    return false;
	  }
	},
	
	updateSortLinksHrefs: function(view)
	{
	  var bestResultEl = document.getElementById("ctl00_PrimaryContentPlaceHolder_linkSortBestResult");
	  var priceEl = document.getElementById("ctl00_PrimaryContentPlaceHolder_linkSortPrice");

    //Check if control is present	  
	  if (bestResultEl != null)
	  {
	    if (!bestResultEl.href.match("&view"))
	      bestResultEl.href += "&view=" + view;
	    else
	      bestResultEl.href = bestResultEl.href.replace(/&view=(.+)$/, "&view=" + view);
	  }
	  
	  //Check if control is present	  
	  if (priceEl != null)
	  {
	    if (!priceEl.href.match("&view"))
	      priceEl.href += "&view=" + view;
	    else
	      priceEl.href = priceEl.href.replace(/&view=(.+)$/, "&view=" + view);
	   }
	},
	
	handleCollapser: function(evt)
	{
		  var collapser = document.getElementById('sitemap_collapser');
			if (collapser != null)
			{
				var collapserText = collapser.getElementsByTagName('label')[0];
				if (Spif.ClassNameAbstraction.contains(collapser, 'iscollapsed'))
				{
					Spif.ClassNameAbstraction.replace(collapser, 'iscollapsed', 'isexpanded');
					collapserText.innerText = resources.messages.less_info;
				}	
				else
				{
					Spif.ClassNameAbstraction.replace(collapser, 'isexpanded', 'iscollapsed');
					collapserText.innerText = resources.messages.more_info;
				}
			}
	}
}
var results = new Results();