var Configurator = {
	// prices
	stylePrice:    0.00,
	panelPrice:    0.00,
	printingPrice: 0.00,
	framingPrice:  0.00,

    // Shipping and Tax
    shippingPrice: 0.00,
    taxPrice: 0.00,
    shippingTaxPrice: 0.00,
    taxPerc: 0.00, // 0.06 if Florida

	numerOfPhotos: 0,

	// ids
	styleId:    '',
	panelId:    '',
	printingId: '',
	framingId:  '',

	styleObject: null,

	setPrintingSizes: function(sizes) {
		this.printingSizes = sizes;
	},

	calculateFramingPrice: function(basePrice) {
		if (!this.printingId) {
			return basePrice;
		}

		var size = this.printingSizes[this.printingId];
		var perimeter = this.roundNumber((((parseInt(size.w)+parseInt(size.h))*2) / 12));
		var price = this.roundNumber(((perimeter / 10) * basePrice));
		return Math.ceil(price/5)*5;
	},

	roundNumber: function(number) {
		return Math.round(number*Math.pow(10,2))/Math.pow(10,2)
	}
};

Configurator.updateForm = function() {
	$('#ProjectStyleId').val(this.styleId);
	$('#ProjectPanelId').val(this.panelId);
	$('#ProjectPrintingId').val(this.printingId);
	$('#ProjectFramingId').val(this.framingId);
}

Configurator.updateShippingTax = function() {
    $('#ProjectAddForm .shipping-tax-content a.value').text('$' + Configurator.shippingTaxPrice.toFixed(2));
}

Configurator.getProjectPrice = function() {
	var subTotal =
		parseFloat(this.stylePrice) +
		parseFloat(this.panelPrice) +
		parseFloat(this.printingPrice) +
		parseFloat(this.framingPrice);

	if (null != this.styleObject && this.numerOfPhotos > this.styleObject.base_quantity) {
		var extraQty = this.numerOfPhotos - this.styleObject.base_quantity;
		subTotal += extraQty * this.styleObject.price_each;
	}

    return subTotal;
}

Configurator.getSubTotal = function() {
	var subTotal =
		parseFloat(this.stylePrice) +
		parseFloat(this.panelPrice) +
		parseFloat(this.printingPrice) +
		parseFloat(this.framingPrice);

	if (null != this.styleObject && this.numerOfPhotos > this.styleObject.base_quantity) {
		var extraQty = this.numerOfPhotos - this.styleObject.base_quantity;
		subTotal += extraQty * this.styleObject.price_each;
	}

    // calculate shipping and tax too
    Configurator.taxPrice = subTotal * Configurator.taxPerc;
    Configurator.shippingTaxPrice = Configurator.taxPrice + Configurator.shippingPrice;
    var subTotalWithShippingTax = subTotal + Configurator.shippingTaxPrice;

    return subTotalWithShippingTax; //subTotal;
}

Configurator.updateSubTotal = function() {
    var subTotal = Configurator.getSubTotal();

	$('#subtotal-price p.subtotal').show();
	$('#subtotal-price p.subtotal span').text('$' + subTotal.toFixed(2));
};

//
// ---- manipulation ----------------------------------------------
//
Configurator.updatePanels = function(styleId) {
	var url = g_basePath + 'api/panels/' + styleId + '.json';
	var firstPanelId;
	var _scope = this;

	$('#picker-panels .options *').remove();
	$('#picker-panels').addClass('loading');

	$.getJSON(url, function(data) {
		var newList = $('#picker-panels .options');

		$.each(data, function(i, item){
			if (typeof firstPanelId == 'undefined') {
				firstPanelId = item.PanelCategory.id;
				_scope.updatePrinting(item.PanelCategory.id);
			}

			$('<div></div>')
				.addClass('category')
				.text(item.PanelCategory.name)
				.appendTo(newList);

			$.each(item.PanelCategory.Panel, function(i, panel) {
				$('<a></a>')
					.attr('href', '#')
					.text(panel.name)
					.addClass('item')
					.appendTo(newList)
					.click(function() {
						Configurator.setPanel(this, panel.id, panel.price);
					});
			});
		});

		$('#picker-panels').removeClass('loading');
		$('#picker-panels').scrollable({ 
			items:'.options', 
			vertical:true,
			size:15,
			next:'a.down', 
			prev:'a.up' 
		});
	});
};

Configurator.updatePrinting = function(panelId) {
	var url = g_basePath + 'api/printings/' + panelId + '.json';
	var firstPrintingId;
	var _scope = this;

	$('#picker-printing .options *').remove();
	$('#picker-printing').addClass('loading');

	$.getJSON(url, function(data) {
		var newList = $('#picker-printing .options');

		$.each(data, function(i, item){
			if (typeof firstPrintingId == 'undefined') {
				firstPrintingId = item.PrintingCategory.id;
				_scope.updateFraming(item.PrintingCategory.id);
			}

			$('<div></div>')
				.addClass('category')
				.text(item.PrintingCategory.name)
				.appendTo(newList);

			$.each(item.PrintingCategory.Printing, function(i, printing) {
				$('<a></a>')
					.attr('href', '#')
					.text(printing.name)
					.addClass('item')
					.appendTo(newList)
					.click(function(){
						Configurator.setPrinting(this, printing.id, printing.price);
						return false;
					});
			});
		});

		$('#picker-printing').removeClass('loading');
		$('#picker-printing').scrollable({ 
			items:'.options', 
			vertical:true,
			size:15,
			next:'a.down', 
			prev:'a.up' 
		});
	});
};

Configurator.updateFraming = function(printingId) {
	var url = g_basePath + 'api/framings/' + printingId + '.json';

	$('#picker-framing .options *').remove();
	$('#picker-framing').addClass('loading');

	$.getJSON(url, function(data) {
		var newList = $('#picker-framing .options');

		$.each(data, function(i, item){
			var li = $('<a></a>')
				.attr('href', '#')
				.text(item.name)
				.addClass('item')
				.appendTo(newList)
				.click(function() {
					Configurator.setFraming(this, item.id, item.price);

                    // added by Fahad
                    var currentSubtotal = $('#ProjectAddForm p.subtotal span').text();
                    var estimateHref = $('#get-estimate').attr('href');
                    $('#subtotal-price span.shipping-tax-label').show();
                    $('#subtotal-price span.shipping-tax-content').attr('rel', currentSubtotal).show();
                    $('#get-estimate').attr('href', estimateHref+'subtotal:'+currentSubtotal+'/');

					return false;
				});
		});

		$('#picker-framing').removeClass('loading');
		$('#picker-framing').scrollable({ 
			items:'.options', 
			vertical:true,
			size:15,
			next:'a.down', 
			prev:'a.up' 
		});
	});
};

//
// ---- events ----------------------------------------------
//
Configurator.setNumberOfPhotos = function(photos) {
	this.numerOfPhotos = photos;
	this.updateSubTotal();
}

Configurator.setStyle = function(obj, style) {
	$('#style-price span').text('$' + style.price);

	this.styleId    = style.id;
	this.stylePrice = style.price;
	this.styleObject = style;

	this.updatePanels(style.id);
	this.updateSubTotal();

	this.panelId    = '';
	this.printingId = '';
	this.framingId  = '';

    // Added by Fahad
    this.panelPrice = 0.00;
    this.printingPrice = 0.00;
    this.framingPrice = 0.00;
    $('#panel-price span').text('');
    $('#printing-price span').text('');
    $('#framing-price span').text('');
    this.updateShippingTax();

	$('#design-quantity-selector select').val('');

	switch (style.product_sold_by_type) {
		case 'PHOTO': {
			$('#design-quantity-selector label').text('Photos');
		}
		break;

		case 'SUBJECT': {
			$('#design-quantity-selector label').text('Subjects');
		}
		break;
	}

	if (style.price_each == 0) {
		$('#design-quantity-selector .inner').hide();
	} else {
		$('#design-quantity-selector .inner').show();
	}

	Configurator.setNumberOfPhotos(0);

	ConfiguratorPreview.setStyle(style.id, style.name);

	this.updateForm();
    //this.hideShippingTax();
	return false;
};

Configurator.setPanel = function(obj, id, price) {
	$('#panel-price span').text('$' + price);

	ConfiguratorPreview.setPanel(id);

	this.panelId    = id;
	this.panelPrice = price;

	this.updatePrinting(id);
	this.updateSubTotal();

	this.printingId = '';
	this.framingId  = '';

    // Added by Fahad
    this.printingPrice = 0.00;
    this.framingPrice = 0.00;
    $('#printing-price span').text('');
    $('#framing-price span').text('');
    this.updateShippingTax();

	this.updateForm();
    //this.hideShippingTax();
	return false;
};

Configurator.setPrinting = function(obj, id, price) {
	$('#printing-price span').text('$' + price);

	ConfiguratorPreview.setPrinting(id);

	this.printingId    = id;
	this.printingPrice = price;

	this.updateFraming(id);
	this.updateSubTotal();
    this.updateShippingTax();

	this.framingId  = '';

    // Added by Fahad
    this.framingPrice = 0.00;
    $('#framing-price span').text('');
    this.updateShippingTax();

	this.updateForm();
    //this.hideShippingTax();
	return false;
};

Configurator.setFraming = function(obj, id, price) {
	price = parseInt(price);

	var newPrice = this.calculateFramingPrice(price);
	$('#framing-price span').text('$' + newPrice.toFixed(2));

	ConfiguratorPreview.setFraming(id);

	this.framingId    = id;
	this.framingPrice = newPrice;

	this.updateSubTotal();
    this.updateShippingTax();

	this.updateForm();
    //this.hideShippingTax();
    var deleteShipSession = $.getJSON(g_basePath + 'api/framings/1.json');
	return false;
};

Configurator.onSubmit = function() {
	if (this.styleId == '') {
		alert('Please select a style');
		return false;
	}

	if (this.panelId == '') {
		alert('Please select the panels');
		return false;
	}

	if (this.printingId == '') {
		alert('Please select the printing');
		return false;
	}

	if (this.framingId == '') {
		alert('Please select the framing');
		return false;
	}

	if (this.styleObject.base_quantity != 0 && this.numerOfPhotos < 1) {
		var text = '';
		if (this.styleObject.product_sold_by_type == 'PHOTO') {
			text = 'Please select the numer of photos';
		} else {
			text = 'Please select the numer of subjects';
		}

		alert(text);
		return false;
	}

	return true;
}

/**
 * hide Shipping/Tax container
 */
Configurator.hideShippingTax = function() {
    $('.shipping-tax-label').hide();
    $('.shipping-tax-content').hide();
    $('.shipping-tax-content a.value').hide();
    $('#get-estimate').text('get estimate');
}

$(document).ready(function() {
	$("#configurator .picker").scrollable({ 
		size:10,
		items:'.options', 
		vertical:true,
		next:'.down', 
		prev:'.up' 
	});

	$("#design-quantity-selector select").change(function() {
		var photos = $(this).val();
		if (photos == '') {
			photos = 0;		
		}

		photos = parseInt(photos, 10);
		Configurator.setNumberOfPhotos(photos);

        // Added by Fahad
        Configurator.updateShippingTax();
	});

	try {
		if (window.location.hash.length > 0) {
			setTimeout(function() {
				$('#conf-' + window.location.hash.substring(1)).click();
			}, 1000);
		}
	} catch (Exception) {
	}


    // added by Fahad
    $('#subtotal-price p.photos a').click(function() {
        return false;
    });
    /*$('#subtotal-price p.shipping-tax').click(function() {
        new Boxy.load(g_basePath+'buy/shipping_and_tax', {
                modal: true,
                center: true,
                title: 'Shipping and Tax',
                width: 350,
                height: 259
        });
        
        return false;
    });*/
});
