MAX_ITEMS = 100;

function selectPhoto(ele, translatedViewLarger, frame_number) {
  // The image
  $("#photo_preview img.preview").replaceWith($("<img src=\""+$(ele).next().html()+"\" class=\"preview\" id=\""+$(ele).attr('id')+"_selected\"/>"));

  // The "view larger" link
  $("#photo_preview a").replaceWith('<a target="_blank" href="' + $(ele).next().next().html() + '">'+translatedViewLarger+'</a>');
  // The "file name" link
  $('#frame_number').html(frame_number);

}

function buyPhoto() {
  $("#buy_photo_bg").show();
  showSpinner();
  
  $.get($("#buy_photo_path").html(), {
      's': $("#photo_preview img").attr('id').split('_')[1]
    }, function(resp) {
      $("#buy_photo_wrapper").replaceWith(resp['overlay']);
      hideSpinner();
      //registerTranslatableItems();
      $("#buy_photo_wrapper").show();
    }, 'json'
  );
}

function buyThisPhoto(ele) {
  $("#buy_photo_bg").show();
  showSpinner();

  $.get($(ele).next().html(), {
    s: $(ele).attr('id').split('_')[2]
  }, function(resp) {
    $("#buy_photo_wrapper").replaceWith(resp['overlay']);
    hideSpinner();
    $("#buy_photo_wrapper").show();
  }, 'json');
}

function showSpinner() {
  $("#buy_photo_spinner_wrapper").show();
  $("#buy_photo_spinner").show();    
}

function hideSpinner() {
  $("#buy_photo_spinner_wrapper").hide();
  $("#buy_photo_spinner").hide();
}

function showTab(tabName, translatedTabName) {
  $("#buy_photo_tabs li").each(function() { $(this).removeClass('active'); })
  $("#bp_purchasables table").each(function() { $(this).hide(); })
  
	$("#" + tabName + "_tab").addClass('active');
	$("#product_category").html(translatedTabName)
	$("#bp_" + tabName).show();
}

function backToPhotos() {
  $("#buy_photo_wrapper").hide();
  $("#buy_photo_bg").hide();
}

function increaseQuantity(ele) {
	$('.bp-error').hide();
  var qty = parseInt($(ele).prev().html()) + 1;
	priceWithCurrency = $(ele).parent().parent().find(".price").html();
  var price = stripCurrency(priceWithCurrency);
  if(qty < MAX_ITEMS ) {
    $(ele).prev().html(qty);
		priceTotal = putCurrency(price*qty);
    updateTotalPrice(ele, priceTotal);
  }
	else
		$('.bp-error#max_quantity').show();
}

function decreaseQuantity(ele) {
	$('.bp-error').hide();
	var qty = parseInt($(ele).next().html()) - 1;
	priceWithCurrency = $(ele).parent().parent().find(".price").html();
	price = stripCurrency(priceWithCurrency);
  if(qty > 0) {
    $(ele).next().html(qty);
		priceTotal = putCurrency(price*qty);
    updateTotalPrice(ele, priceTotal);
  }
	else
		$('.bp-error#zero_quantity').show();
}

function stripCurrency(priceWithCurrency) {
	currencyLayout = CURRENCY_FORMAT.replace('%u', CURRENCY_UNIT);
	stringsToRemove = currencyLayout.split('%n');
	priceStr = (priceWithCurrency+'%end').replace(stringsToRemove[0], '').replace(stringsToRemove[1]+'%end', '');
	priceInt = parseInt(priceStr.split(CURRENCY_DELIMITER).join('').replace(CURRENCY_SEPARATOR,''), 10);
	return priceInt;
}

function putCurrency(price) {
	priceStr = price.toString();
	digitsBeforeSeparator = priceStr.length - CURRENCY_PRECISION;
	if (digitsBeforeSeparator > 0) {
		priceStr = priceStr.substr(0, digitsBeforeSeparator) + CURRENCY_SEPARATOR + priceStr.substr(digitsBeforeSeparator);
		digitsBeforeSeparator = digitsBeforeSeparator - 3;
		while (digitsBeforeSeparator > 0) {
			priceStr = priceStr.substr(0, digitsBeforeSeparator) + CURRENCY_DELIMITER + priceStr.substr(digitsBeforeSeparator);
			digitsBeforeSeparator = digitsBeforeSeparator - 3;
		}
	}
	else {
		if (digitsBeforeSeparator == 0) {
			priceStr = '0' + CURRENCY_SEPARATOR + priceStr;
		}
		else {
			zeroStr = '';
			for (i=0; i<Math.abs(digitsBeforeSeparator); i++) {
				zeroStr = zeroStr + '0';
			}
			priceStr = '0' + CURRENCY_SEPARATOR + zeroStr + priceStr;
		}
	}
	priceWithCurrency = CURRENCY_FORMAT.replace('%n', priceStr).replace('%u', CURRENCY_UNIT);
	return priceWithCurrency;
}

function updateTotalPrice(ele, price) {
  $(ele).parent().parent().find(".total-price").html(price);
}

function addToCart(ele) {
	$('.bp-error').hide();
  $(ele).hide();
  $(ele).next().show();
  
  $.post('/cart/add_to_cart', {
    job_id: $("#bp_job_id").html(),
    subject_id: $("#bp_subject_id").html(),
    purchasable_id: $(ele).parent().parent().attr('id').split('_')[1],
    photo_id: $("#bp_photo_id").html(),
    quantity: $(ele).parent().parent().find(".quantity span").html()
  }, function(resp) {
    if(resp['error']) {
      jAlert(resp['error']);
      $(ele).next().hide();
      $(ele).show();
    } else {
      $(ele).next().hide();
      $(ele).show();
      $("#bp_cart_summary").replaceWith(resp['cart_summary']);
    }
  }, 'json');
}

function emptyCart(ele) {
  $(ele).hide();
  $(ele).next().show();
  
  $.post('/cart/empty', {}, 
  function(resp) {
    if(resp['error']) {
      jAlert(resp['error']);
      $(ele).next().hide();
      $(ele).show();
    } else {
      $(ele).next().hide();
      $(ele).show();
      $("#bp_cart_summary").replaceWith(resp['cart_summary']);
    }
  }, 'json');
}

