/**
 * Product manipulation
 * 
 */
var Product = {

  /**
   * ...
   * 
   */
  addToBasketUrl: null,

  /**
   * Adds comment for product
   * 
   */
  addComment: function ()
  {
	var fields = {
		content: jQuery('#sf_fuero_commerce_comment_content').val(), 
		rate:    jQuery('#sf_fuero_commerce_comment_rate').val() 
	};
	jQuery.post(Product.commentUrl, {sf_fuero_commerce_comment: fields}, function () {
		AjaxPagination.getInstance('comments').setPage(1);
		jQuery('#sf_fuero_commerce_comment_content').val('');
		jQuery('#sf_fuero_commerce_comment_rate').val(0);
	});
  },
  
  /**
   * Adds question for product
   * 
   */
  addQuestion: function ()
  {
	var fields = {
		question: jQuery('#sf_fuero_commerce_product_question_question').val()
	};
	jQuery.post(Product.askQuestionUrl, {sf_fuero_commerce_product_question: fields}, function () {
		AjaxPagination.getInstance('questions').setPage(1);
		jQuery('#sf_fuero_commerce_product_question_question').val('');
	});	  
  },
  
  /**
   * Adds to basket
   * 
   */
  addToBasket: function (optionId)
  {
	var options = jQuery('[name^=sf_fuero_commerce_basket_item_option]:checked');
    
	var href = Product.addToBasketUrl + '?productId=' + Product.productId;
  href += '&optionIds[]=' + optionId;
  
//	for (var o = 0; o < options.length; o++)
//	{
//	  href += '&optionIds[]=' + options[o].value;
//	}
		
	document.location.href = href;
  },  
  
  /**
   * ...
   * 
   * @param int questionId
   */
  answerQuestion: function (questionId)
  {
	var fields = {
		answer: jQuery('#sf_fuero_commerce_product_question_' + questionId + '_question').val()
	};
	jQuery.post(Product.answerQuestionUrl + '?questionId=' + questionId, {sf_fuero_commerce_product_question: fields}, function () {
		AjaxPagination.getInstance('questions').setPage(1);
	});
  },
    
  /**
   * ...
   * 
   */
  answerQuestionUrl: null,
  
  /**
   * Alias for Product.addQuestion
   * 
   */
  askQuestion: function ()
  {
	Product.addQuestion();  
  },
  
  /**
   * ...
   * 
   */
  askQuestionUrl: null,		

  /**
   * ...
   * 
   */
  basePrice: null,
  
  /**
   * ...
   * 
   * @param Array optionsArray
   */
  checkPrice: function (optionsArray)
  {
    var ids = optionsArray.join(',');
    jQuery('#price').load(Product.estimateUrl + '?productId=' + Product.productId + '&optionIds=' + ids);
  },  
  
  /**
   * Url to be used in addComment call
   * 
   * @var string
   */
  commentUrl: null,	
  
  /**
   * Inits Product singleton
   * 
   * @param string url Url to be used in AJAX calls 
   */
  init: function (commentUrl, askQuestionUrl, answerQuestionUrl, addToBasketUrl, estimateUrl, productId)
  {
	Product.commentUrl = commentUrl;
	Product.askQuestionUrl = askQuestionUrl;
	Product.answerQuestionUrl = answerQuestionUrl;
	Product.addToBasketUrl = addToBasketUrl;
	Product.estimateUrl = estimateUrl;
	Product.productId = productId;
	
	jQuery('[name^=sf_fuero_commerce_basket_item_option]').bind('change', function () {
	  var optionsSelected = [];
	  jQuery('[name^=sf_fuero_commerce_basket_item_option]:checked').each(function () {
		optionsSelected.push(jQuery(this).val());
	  });
	  Product.checkPrice(optionsSelected);
	});
	
	jQuery('#productPrintButton').css('cursor', 'pointer').click(function () { window.print() });
  }
		
};
