
initSubmiter = function() {
	var putSummaryBase = function() {
        var content = $(".optionList").parent();
        //var content = $(".tabs .content");

        var html = '<div class="summaryList" style="display: none;">\
                        <input id="summary_input" type="hidden" value="" name="options">\
                        <div class="specs selectedOptions">\
                            <table width="100%" cellspacing="0" cellpadding="0" border="0">\
                                <tbody>\
                                    <tr>\
                                        <th>' + obSubmiterLang.headers.Categorie + '</th>\
                                        <th>' + obSubmiterLang.headers.Désignation + '</th>\
                                        <th class="info">' + obSubmiterLang.headers.Info + '</th>\
                                        <th class="code">' + obSubmiterLang.headers.Code + '</th>\
                                        <th class="clientPrice">' + obSubmiterLang.headers.PrixClient + '</th>\
                                    </tr>\
                                </tbody>\
                            </table>\
                        </div>\
                        <div class="optionsSelectBtn">\
                            <a class="submit" onclick="">' + obSubmiterLang.applyOptions + '</a>\
                        </div>\
                    </div>';

        $(".summaryList").remove();

        content.append(html);


        $('.summaryList .submit').click(function() {
            $(".optionList").show();
            $(".summaryList").remove();

            return false;
        });
    };

	
    var imgButton = $('div.submit img');
	
    imgButton.removeAttr("onclick");
    imgButton.click(function() {
        $(".optionList").hide();

		putSummaryBase();

        $(".summaryList").show();
		
		updateClientPrice();		

        $('.summaryList .priceFormat').priceFormat(obSubmiterLang.priceFormat);
		
        return false;
    });
	
	$(".optionList input").click(function() {
		updateClientPrice();
	});

    putSummaryBase();


    $("form#main").submit(function() {
        if(!$('.optionList:visible').length)
            $('.optionList input').remove();
		
        return true;
    });

};


function addSummaryLine(rule) {
	var html = '<tr>\
					<td class="label">' + rule.Category + '</td>\
					<td>' + rule.getText() + '\
						<div style="display: none;" class="equip">' + rule.getTextInfo() + '</div>\
					</td>\
					<td class="info">\
						<img onclick="toggleDetail(this);" onmouseout="showDetail(this);" onmouseover="showDetail(this,\'true\');" src="http://img.delta-car.ch/vconfig/gfx/infoOff.gif">\
					</td>\
					<td class="code">['+ rule.code +']</td>\
					<td class="clientPrice priceFormat">'+rule.ClientPrice+'</td>\
				</tr>';

	$(".summaryList table tbody").append(html);


}

function addTotalLine(price) {
	var html = '<tr class="total">\
					<td class="selectedOptionTotalLabel" colspan="3">' + obSubmiterLang.totalDescription + '</td>\
					<td>&nbsp;</td>\
					<td class="clientPrice priceFormat">'+price+'\
					</td>\
				</tr>';

	$(".summaryList table tbody").append(html);
}

updateClientPrice = function() {
	var totalPrice = 0;
	var options = [];
	$("#summary_input").val("");		
	$(".optionList input:checked").each(function(i,el) {
		var code = el.value;
		var rule = optionBuilder.rulesRepository.get(code);
		totalPrice += parseFloat(rule.ClientPrice);
		options.push(code);
		
		if ($(".summaryList").is(":visible")) {
			addSummaryLine(rule);
		}
	});
	$("#summary_input").val(options.join(","));

	var clientPrice = parseInt($('table tr#clientPriceValue td.clientPrice').html());
	if (clientPrice > 0) {
		var tdClientPrice = $('table tr#totalClientPrice td.clientPrice');			
		$('table tr#totalClientPrice').show();
		tdClientPrice.html(clientPrice + totalPrice);
		tdClientPrice.priceFormat(obSubmiterLang.priceFormat);
	}
	if ($(".summaryList").is(":visible")) {
		addTotalLine(totalPrice);
	}
};

$(document).ready(initSubmiter);


(function($) {

	$.fn.priceFormat = function(options) {

		var defaults = {
			prefix: '$ ',
			postfix: '',
			centsSeparator: '',
			thousandsSeparator: ',',
			limit: false,
			centsLimit: 0
		};

		var options = $.extend(defaults, options);

		return this.each(function() {

			var obj = $(this);
			var is_number = /[0-9]/;

			var prefix = options.prefix;
			var postfix = options.postfix;
			var centsSeparator = options.centsSeparator;
			var thousandsSeparator = options.thousandsSeparator;
			var limit = options.limit;
			var centsLimit = options.centsLimit;

			function to_numbers (str) {
				var formatted = '';
				for (var i=0;i<(str.length);i++) {
					char = str.charAt(i);
					if (formatted.length==0 && char==0) char = false;
					if (char && char.match(is_number)) {
						if (limit) {
							if (formatted.length < limit) formatted = formatted+char;
						}else{
							formatted = formatted+char;
						}
					}
				}
				return formatted;
			}

			function fill_with_zeroes (str) {
				while (str.length<(centsLimit+1)) str = '0'+str;
				return str;
			}

			function price_format (str) {
        		var formatted = fill_with_zeroes(to_numbers(str));
				var thousandsFormatted = '';
				var thousandsCount = 0;

				var centsVal = formatted.substr(formatted.length-centsLimit,centsLimit);
				var integerVal = formatted.substr(0,formatted.length-centsLimit);

				formatted = integerVal+centsSeparator+centsVal;

				if (thousandsSeparator) {
					for (var j=integerVal.length;j>0;j--) {
						char = integerVal.substr(j-1,1);
						thousandsCount++;
						if (thousandsCount%3==0) char = thousandsSeparator+char;
						thousandsFormatted = char+thousandsFormatted;
					}
					if (thousandsFormatted.substr(0,1)==thousandsSeparator) thousandsFormatted = thousandsFormatted.substring(1,thousandsFormatted.length);
					formatted = thousandsFormatted+centsSeparator+centsVal;
				}

				if (prefix) formatted = prefix+formatted;
				if (postfix) formatted = formatted+postfix;

				return formatted;

			}

			function price_it () {
				var str = obj.html();
				var price = price_format(str);
				if (str != price) obj.html(price);
			}

			if ($(this).html().length>0) price_it();

		});

	};

})(jQuery);
