/**
 * WDCA - Sweet Tooth
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the WDCA SWEET TOOTH POINTS AND REWARDS 
 * License, which extends the Open Software License (OSL 3.0).
 * The Sweet Tooth License is available at this URL: 
 * http://www.wdca.ca/solutions_page_sweettooth/Sweet_Tooth_License.php
 * The Open Software License is available at this URL: 
 * http://opensource.org/licenses/osl-3.0.php
 * 
 * DISCLAIMER
 * 
 * By adding to, editing, or in any way modifying this code, WDCA is 
 * not held liable for any inconsistencies or abnormalities in the 
 * behaviour of this code. 
 * By adding to, editing, or in any way modifying this code, the Licensee
 * terminates any agreement of support offered by WDCA, outlined in the 
 * provided Sweet Tooth License. 
 * Upon discovery of modified code in the process of support, the Licensee 
 * is still held accountable for any and all billable time WDCA spent 
 * during the support process.
 * WDCA does not guarantee compatibility with any other framework extension. 
 * WDCA is not responsbile for any inconsistencies or abnormalities in the
 * behaviour of this code if caused by other framework extension.
 * If you did not receive a copy of the license, please send an email to 
 * contact@wdca.ca or call 1-888-699-WDCA(9322), so we can send you a copy 
 * immediately.
 * 
 * @category [TBT]
 * @package [TBT_Rewards]
 * @copyright Copyright (c) 2009 Web Development Canada (http://www.wdca.ca)
 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
 * General Sweet Tooth (rewards) utility functions.
 *
 * @category TBT
 * @package TBT_Rewards
 * @author WDCA Sweet Tooth Team <contact@wdca.ca>
 */


/**
 * <b>Requires rewards/js/captions.phtml template </b>
 * @param amount
 * @param currency_id
 * @return
 */
function getPointsString(amount, currency_id) {
 s = amount + ' ';
 if(currency_map[currency_id] != '' && currency_map[currency_id] != ' ') {
 s = s + currency_map[currency_id] + " ";
 }
 if(amount > 1) {
 s += CAPTION_POINTS;
 } else {
 s += CAPTION_POINT;
 }
 return s;
}




/**
 * WDCA - Sweet Tooth
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the WDCA SWEET TOOTH POINTS AND REWARDS 
 * License, which extends the Open Software License (OSL 3.0).
 * The Sweet Tooth License is available at this URL: 
 * http://www.wdca.ca/solutions_page_sweettooth/Sweet_Tooth_License.php
 * The Open Software License is available at this URL: 
 * http://opensource.org/licenses/osl-3.0.php
 * 
 * DISCLAIMER
 * 
 * By adding to, editing, or in any way modifying this code, WDCA is 
 * not held liable for any inconsistencies or abnormalities in the 
 * behaviour of this code. 
 * By adding to, editing, or in any way modifying this code, the Licensee
 * terminates any agreement of support offered by WDCA, outlined in the 
 * provided Sweet Tooth License. 
 * Upon discovery of modified code in the process of support, the Licensee 
 * is still held accountable for any and all billable time WDCA spent 
 * during the support process.
 * WDCA does not guarantee compatibility with any other framework extension. 
 * WDCA is not responsbile for any inconsistencies or abnormalities in the
 * behaviour of this code if caused by other framework extension.
 * If you did not receive a copy of the license, please send an email to 
 * contact@wdca.ca or call 1-888-699-WDCA(9322), so we can send you a copy 
 * immediately.
 * 
 * @category [TBT]
 * @package [TBT_Rewards]
 * @copyright Copyright (c) 2009 Web Development Canada (http://www.wdca.ca)
 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
 * Redemption drop down methods
 *
 * @category TBT
 * @package TBT_Rewards
 * @author WDCA Sweet Tooth Team <contact@wdca.ca>
 */

var rSlider;
var usesSelect;
var usesCaption;
var usesContainer;


/**
 * 
 */
function getProductPriceBeforeRedemptions() {
 var priceBeforeRedemptions = 0;
 if(optionsPrice.optionPrices.config != undefined) {
 priceBeforeRedemptions = optionsPrice.optionPrices.config + optionsPrice.productPriceBeforeRedemptions;
 } else {
 priceBeforeRedemptions = optionsPrice.productPriceBeforeRedemptions;
 }

 return priceBeforeRedemptions;
 
}

/**
 * 
 * @param rule_id
 * @return
 */
function feignPriceChange(rule_id) {
 var newPrice = $(new_price_dom_id);
 var oldPrice = $(old_price_dom_id);
 var numUses = getRedemptionUses();
 numUses = (numUses == "") ? 1 : parseInt(numUses);
 if(rule_id == null) {
 rule_id = $('redemption_rule').value;
 }

 if(!show_lowest_price) { return; }
 
 if(oldPrice == null) {
 oldPrice = newPrice.cloneNode(true);
 newPrice.up().insertBefore(oldPrice, newPrice);
 oldPrice.id = old_price_dom_id;
 oldPrice.removeClassName('price');
 oldPrice.addClassName('old-price');
 do_hide_old_price = true;
 }

 var finalPrice;
 if(optionsPrice.optionPrices.config != undefined) {
 if(optionsPrice.optionPrices.config > 0) {
 finalPrice = optionsPrice.productPriceBeforeRedemptions;
 } else {
 finalPrice= getProductPriceBeforeRedemptions();
 }
 } else {
 finalPrice = getProductPriceBeforeRedemptions();
 }
 if(rule_id == "") {
 if(do_hide_old_price) oldPrice.hide();
 optionsPrice.productPrice = finalPrice;
 optionsPrice.reload();
 } else {
 if(do_hide_old_price) oldPrice.show();
 var price_disposition = rule_options[rule_id]['price_disposition'];
 //Edited 2/24/2010 7:41:46 AM : prices that set "to_fixed" discount should also 
 if(rule_options[rule_id]['discount_action'] == 'by_fixed' && rule_options[rule_id]['new_price_flt'] <= 0.0000) {
 optionsPrice.productPrice = 0;
 optionsPrice.minusDisposition = 9999999999; // hack to make sure the price stays at 0;
 } else {
 optionsPrice.productPrice = finalPrice - price_disposition*numUses;
 }
 //Edited
 //points_caption = rule_options[rule_id]['points_caption'];
 points_amount = rule_options[rule_id]['amount'];
 points_currency_id = rule_options[rule_id]['currency_id'];
 points_caption = getPointsString(points_amount*numUses, points_currency_id);
 
 optionsPrice.reload();
 
 var points_with = " " + CAPTION_WITH + " " + points_caption ;
 if(newPrice.down() != null) {
 newPrice.down().innerHTML = newPrice.down().innerHTML + points_with ;
 } else if(newPrice.down() == null && newPrice != null) {
 newPrice.innerHTML = newPrice.innerHTML + points_with;
 } else {
 // don't know where it is so just don't show the points thing.
 }
 }
 //newPrice.toggle();
}

/**
 * 
 * @param rule_id
 * @param retain_value
 * @return
 */
function updateRemptionUsesSelector(rule_id, retain_value) {
 
 var init_value = retain_value ? retain_value : usesSelect.value ;
 
 if(rule_id == '') { 
 usesContainer.hide(); 
 } else { 
 usesSelect.innerHTML = '';
 var uses = 1;
 var amt = rule_options[rule_id]['amount'];
 var curr = rule_options[rule_id]['currency_id'];
 var max_uses = rule_options[rule_id]['max_uses'];
 var relevant_customer_points = customer_points ? customer_points[curr] : default_guest_points;
 var price_disposition = rule_options[rule_id]['price_disposition'];
 var nextPrice = getProductPriceBeforeRedemptions() - price_disposition;
 
 usesSelect.hide();
 usesCaption.hide();
 if(max_uses == 1 || max_uses == null) {
 usesCaption.innerHTML = CAPTION_YOU_WILL_SPEND + " " + getPointsString(uses*amt, curr);
 usesCaption.show();
 } else {
 while(relevant_customer_points >= amt*uses) {
 var oOption = document.createElement("option");
 oOption.text = getPointsString(uses*amt, curr);
 if(show_discount_in_uses_selector) {
 oOption.text = oOption.text + " ( - "+ optionsPrice.formatPrice(price_disposition*uses) +")";
 }
 oOption.value = uses;
 usesSelect.appendChild(oOption);
 uses++;
 if (nextPrice <= 0 || (uses > max_uses && max_uses != 0)) break;
 nextPrice = getProductPriceBeforeRedemptions() - price_disposition*uses;
 }
 if(retain_value) {
 if(init_value > uses) {
 init_value = max_uses;
 }
 usesSelect.setValue(init_value);
 } else {
 usesSelect.setValue(1);
 }
 usesSelect.show();
 }
 usesContainer.show();
 }
}
/**
 * WDCA - Sweet Tooth
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the WDCA SWEET TOOTH POINTS AND REWARDS 
 * License, which extends the Open Software License (OSL 3.0).
 * The Sweet Tooth License is available at this URL: 
 * http://www.wdca.ca/solutions_page_sweettooth/Sweet_Tooth_License.php
 * The Open Software License is available at this URL: 
 * http://opensource.org/licenses/osl-3.0.php
 * 
 * DISCLAIMER
 * 
 * By adding to, editing, or in any way modifying this code, WDCA is 
 * not held liable for any inconsistencies or abnormalities in the 
 * behaviour of this code. 
 * By adding to, editing, or in any way modifying this code, the Licensee
 * terminates any agreement of support offered by WDCA, outlined in the 
 * provided Sweet Tooth License. 
 * Upon discovery of modified code in the process of support, the Licensee 
 * is still held accountable for any and all billable time WDCA spent 
 * during the support process.
 * WDCA does not guarantee compatibility with any other framework extension. 
 * WDCA is not responsbile for any inconsistencies or abnormalities in the
 * behaviour of this code if caused by other framework extension.
 * If you did not receive a copy of the license, please send an email to 
 * contact@wdca.ca or call 1-888-699-WDCA(9322), so we can send you a copy 
 * immediately.
 * 
 * @category [TBT]
 * @package [TBT_Rewards]
 * @copyright Copyright (c) 2009 Web Development Canada (http://www.wdca.ca)
 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
 * This is a mod to the Script.aculo.us Control.Slider utility to make it smooth slide.
 * <b>Depends on:</b> Control.Slider, Effect.Move 
 *
 * @category TBT
 * @package TBT_Rewards
 * @author WDCA Sweet Tooth Team <contact@wdca.ca>
 */

var SmoothSlider = Class.create();
SmoothSlider.prototype = Control.Slider.prototype;
SmoothSlider.prototype.setValue = function(sliderValue, handleIdx){
 if(!this.active) {
 this.activeHandleIdx = handleIdx || 0;
 this.activeHandle = this.handles[this.activeHandleIdx];
 this.updateStyles();
 }
 handleIdx = handleIdx || this.activeHandleIdx || 0;
 if(this.initialized && this.restricted) {
 if((handleIdx>0) && (sliderValue<this.values[handleIdx-1]))
 sliderValue = this.values[handleIdx-1];
 if((handleIdx < (this.handles.length-1)) && (sliderValue>this.values[handleIdx+1]))
 sliderValue = this.values[handleIdx+1];
 }
 sliderValue = this.getNearestValue(sliderValue);
 this.values[handleIdx] = sliderValue;
 this.value = this.values[0]; // assure backwards compat
 
 // WDCA CODE BEGIN -->>
 if(this.slideFxBusy == true) {
 if(this.slideFx) {
 this.slideFx.cancel();
 this.slideFxBusy = false;
 this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] = this.translateToPx(sliderValue);
 }
 } else {
 this.slideFxBusy = true;
 //Edited 10/03/2010 4:37:21 AM : to fix IE7-8 bug
 var translated_value = this.translateToPx(sliderValue);
 if(translated_value != "NaNpx") { 
 var move_x = this.isVertical() ? 0 : parseInt(translated_value);
 var move_y = this.isVertical() ? parseInt(translated_value) : 0;
 this.slideFx = new Effect.Move(this.handles[handleIdx], {
 x: move_x, 
 y: move_y,
 mode:'absolute',
 duration: 0.5,
 afterFinish: function() {
 this.slideFxBusy = false;
 }.bindAsEventListener(this)
 });
 }
 }
 // <<-- WDCA CODE END

 this.isMoving = false;
// this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] = this.translateToPx(sliderValue); // WDCA COMMENTED THIS

 this.drawSpans();
 if(!this.dragging || !this.event) this.updateFinished();
 };


/**
 * WDCA - Sweet Tooth
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the WDCA SWEET TOOTH POINTS AND REWARDS 
 * License, which extends the Open Software License (OSL 3.0).
 * The Sweet Tooth License is available at this URL: 
 * http://www.wdca.ca/solutions_page_sweettooth/Sweet_Tooth_License.php
 * The Open Software License is available at this URL: 
 * http://opensource.org/licenses/osl-3.0.php
 * 
 * DISCLAIMER
 * 
 * By adding to, editing, or in any way modifying this code, WDCA is 
 * not held liable for any inconsistencies or abnormalities in the 
 * behaviour of this code. 
 * By adding to, editing, or in any way modifying this code, the Licensee
 * terminates any agreement of support offered by WDCA, outlined in the 
 * provided Sweet Tooth License. 
 * Upon discovery of modified code in the process of support, the Licensee 
 * is still held accountable for any and all billable time WDCA spent 
 * during the support process.
 * WDCA does not guarantee compatibility with any other framework extension. 
 * WDCA is not responsbile for any inconsistencies or abnormalities in the
 * behaviour of this code if caused by other framework extension.
 * If you did not receive a copy of the license, please send an email to 
 * contact@wdca.ca or call 1-888-699-WDCA(9322), so we can send you a copy 
 * immediately.
 * 
 * @category [TBT]
 * @package [TBT_Rewards]
 * @copyright Copyright (c) 2009 Web Development Canada (http://www.wdca.ca)
 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
 * Sweet Tooth RedemptionSlider
 * @requires js/tbt/scriptaculous/SmoothSlider.js
 *
 * @category TBT
 * @package TBT_Rewards
 * @author WDCA Sweet Tooth Team <contact@wdca.ca>
 */

var RedemptionSlider = Class.create({
 /**
 * Constructor
 * @param sliderHandleId
 * @param sliderRailId
 * @param sliderCaptionId
 * @param sliderValueboxId
 * @return
 */
 initialize: function(sliderHandleId, sliderRailId, sliderCaptionId, sliderValueboxId) {
 this.sliderHandleId = sliderHandleId;
 this.sliderRailId = sliderRailId;
 this.sliderCaptionId = sliderCaptionId;
 this.sliderValuebox = $(sliderValueboxId);
 this.sliderData = {
 minimum: 1,
 maximum: 1,
 sliderValue: 1,
 step: 1, 
 range: $R(0, 100),
 values: $R(0, 100),
 onSlide: this.slideListener.bind(this),
 onChange: this.changeListener.bind(this)
 }; 
 this.regenerateSlider(1, 1, 1, 1);
 this.sliderCaption = $(sliderCaptionId);
 this.points_per_use = 1;
 this.points_currency = -1;
 this.oldRuleId = -1;
 this.oldProductPrice = -1;
 },

 /**
 * 
 */
 changeListener: function(val) {
 this.setExternalValue(val);
 feignPriceChange();
 },
 
 /**
 * 
 * @param val
 * @return
 */
 slideListener: function(val) {
 this.changeListener(val);
 },
 
 /**
 * 
 * @return
 */
 getValue: function () {
 return this.slider.value;
 },
 
 /**
 * 
 * @return
 */
 getUses: function () {
 return this.getValue();
 },
 

 /**
 * Calculates the maximum uses based on the final product price
 * and how many points the customer has.
 * @param max_uses : max uses from the rule model
 * @param points_per_use : how many points are required for each use of the rule?
 * @param cp : how many points in the currency of the rule does the customer have
 * @param pp : what is the price of the product?
 * @param pp_disp : what is the disposition in product price after the rule has been applied? 
 * @return integer
 */
 getRealMaxUses: function(max_uses, points_per_use, cp, pp, pp_disp) {
 var lowest_max_uses = max_uses;
 
 // Check max points that can be spent on product price
 // befor the price goes to 0;
 var max_pp_uses = pp/pp_disp;
 max_pp_uses = parseInt(max_pp_uses) + ( (max_pp_uses - parseInt(max_pp_uses) > 0) ? 1 : 0 ); // add remainder
 if(max_pp_uses < lowest_max_uses) {
 lowest_max_uses = max_pp_uses;
 }
 
 // Check customer points balance
 var max_cp_uses = parseInt(cp/points_per_use);
 if(max_cp_uses < lowest_max_uses) {
 lowest_max_uses = max_cp_uses;
 }
 
 return lowest_max_uses;
 },
 
 /**
 * Regenerates the slider JS model
 * @param min
 * @param max
 * @param step
 * @param initial_value
 * @return
 */
 regenerateSlider: function(min, max, step, initial_value) {
 if(this.slider != null) { 
 this.slider.dispose();
 }
 this.sliderData.minimum = min;
 this.sliderData.maximum = max;
 this.sliderData.step = step;
 this.sliderData.range = $R(min, max);
 if(step == 1) {
 this.sliderData.values = $R(min, max);
 } else {
 var vals=new Array(); vals.push(min);
 $R(min, max-1).each( function(v){ if(v%step==0){ vals.push(v+step); } } );
 this.sliderData.values = vals;
 }
 this.sliderData.sliderValue = initial_value;
 this.slider = new SmoothSlider(this.sliderHandleId, this.sliderRailId, this.sliderData);
 },
 
 
 /**
 * Sets the value in the input box and what is displayed to the user.
 * @param val
 * @return
 */
 setExternalValue: function(val) {
 this.sliderCaption.innerHTML = getPointsString(val*this.points_per_use, this.points_currency);
 this.sliderValuebox.value = this.getUses();
 },
 
 incr: function() {
 this.slider.setValue(this.slider.value+this.sliderData.step);
 },
 
 decr: function() {
 this.slider.setValue(this.slider.value-this.sliderData.step);
 },
 
 maximize: function() {
 this.slider.setValue(this.sliderData.maximum);
 },
 
 isMaxed: function() {
 return (this.getValue() == this.sliderData.maximum);
 }

});
/**
 * WDCA - Sweet Tooth
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the WDCA SWEET TOOTH POINTS AND REWARDS 
 * License, which extends the Open Software License (OSL 3.0).
 * The Sweet Tooth License is available at this URL: 
 * http://www.wdca.ca/solutions_page_sweettooth/Sweet_Tooth_License.php
 * The Open Software License is available at this URL: 
 * http://opensource.org/licenses/osl-3.0.php
 * 
 * DISCLAIMER
 * 
 * By adding to, editing, or in any way modifying this code, WDCA is 
 * not held liable for any inconsistencies or abnormalities in the 
 * behaviour of this code. 
 * By adding to, editing, or in any way modifying this code, the Licensee
 * terminates any agreement of support offered by WDCA, outlined in the 
 * provided Sweet Tooth License. 
 * Upon discovery of modified code in the process of support, the Licensee 
 * is still held accountable for any and all billable time WDCA spent 
 * during the support process.
 * WDCA does not guarantee compatibility with any other framework extension. 
 * WDCA is not responsbile for any inconsistencies or abnormalities in the
 * behaviour of this code if caused by other framework extension.
 * If you did not receive a copy of the license, please send an email to 
 * contact@wdca.ca or call 1-888-699-WDCA(9322), so we can send you a copy 
 * immediately.
 * 
 * @category [TBT]
 * @package [TBT_Rewards]
 * @copyright Copyright (c) 2009 Web Development Canada (http://www.wdca.ca)
 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
 * Sweet Tooth PointsSlider
 * @requires js/tbt/rewards/points/RedemptionSlider.js
 *
 * @category TBT
 * @package TBT_Rewards
 * @author WDCA Sweet Tooth Team <contact@wdca.ca>
 */

var PointsSlider = Class.create(RedemptionSlider, {
 /**
 * Changes the redemption rule for this points slider
 * @param rule_id
 * @return
 */
 changeRule: function(rule_id) {
 var init_value = this.getValue() ;
 if(init_value == null) init_value = 1;
 
 var uses = 1;
 if(rule_id == '') { //no rule selected
 usesContainer.hide(); 
 } else { 
 var amt = parseInt(rule_options[rule_id]['amount']);
 var curr = parseInt(rule_options[rule_id]['currency_id']);
 var max_uses = parseInt(rule_options[rule_id]['max_uses']);
 this.points_per_use = amt;
 this.points_currency = curr;
 if(max_uses == 0) {
 max_uses = parseInt(getProductPriceBeforeRedemptions())*1000 + 1;
 }
 var relevant_customer_points = customer_points ? customer_points[curr] : default_guest_points;
 var price_disposition = rule_options[rule_id]['price_disposition'];
 var product_price = getProductPriceBeforeRedemptions();
 max_uses = this.getRealMaxUses(max_uses, this.points_per_use, relevant_customer_points, product_price, price_disposition);
 if(max_uses > 1) {
 if(init_value > max_uses) {
 init_value = max_uses;
 }
 //Edited 15/01/2010 7:08:59 PM : if the next (commented) line is false, it means that there
 // is a posibility that mutliple point usages will yield the same discount:
 // For Example if points are worth $0.0017 USD, using 1-5 points will yield a $0.01 USD 
 // discount after discounts.
 //Math.round(price_disposition*Math.pow(10, optionsPrice.priceFormat.precision)) > 0

 this.regenerateSlider(1, max_uses, 1, init_value);
 this.slider.setValue(init_value);
 usesContainer.show();
 
 } else {
 this.regenerateSlider(1, 1, 1, 1);
 usesContainer.hide(); 
 }
 }

 // Reset the slider to 1 if the rule has changed.
 if(this.oldRuleId != rule_id) {
 this.slider.setValue(1);
 this.oldRuleId = rule_id;
 }
 
 }

});
