$(function(){
	imex.initialize.bootstrap();
});


	/*
	 *========================================================================================
	 * オブジェクト
	 * imex.initialize
	 *========================================================================================
	 * メソッド
	 * bootstrap()       => ページ起動
	 * listener()        => イベントリスナー初期化
	 * menu(name:string) => メニュー内容の初期化
	 * registvar()       => 共通変数初期化
	 * setting()         => ページ設定
	 *========================================================================================
	 */
	imex.initialize = {

		bootstrap : function(){
			imex.initialize.registvar();
			imex.initialize.listener();
			imex.timer.start();
			if(imex.storage.services_id != 0){
				imex.initialize.setting();
			}
		},

		listener: function(){
			$('#tab_color')
				.click(function(){
					imex.operation.menuselected('color');
				})
				.mouseover(function(){
					$('#navi').html('TIPS : ボディカラーの変更ができます');
				});
			$('#tab_upload')
				.click(function(){
					imex.operation.menuselected('upload');
				})
				.mouseover(function(){
					$('#navi').html('TIPS : プリントするデザインの追加・レイアウトができます');
				});
			$('#tab_size').click(function(){
				if($("#tab_size").css("top") == "0px"){
					imex.load.size();
				}
				imex.operation.menuselected('size');
			});
			$('#selected_image').click(function(){
				imex.operation.dataselected('image');
			});
			$('#selected_text').click(function(){
				imex.operation.dataselected('text')
			});
			$('#data_upload').click(function(){
				if($('#doui:checked').val() != 'on'){
					alert('注意事項既読のチェックが入っていません');
				} else {
					$('#upload').submit();
				}
			});
			$('#use_color').change(function(){
				$('#use_color option:selected').each(function(){
					imex.storage.vector.color = $(this).val();
					imex.operation.reloadpreview();
				});
			});
			$('#reset_image').click(function(){
				imex.storage.layer.data.pop();
				$('#upload_form').show();
				$('#data_info').hide();
			});
			$('#set_image').click(function(){
				imex.operation.imageset();
				imex.operation.menuselected('upload');
			});
			$('#textForm').submit(function(){
				return false;
			});
			$('#text_remove_bottn').click(function(){
				imex.operation.remove('text');
			});
			$('#text_add_bottn').click(function(){
				imex.operation.textset();
			});
//			$('#string')
//				.focus(function(){
//					imex.operation.inputwatch('string', 'start');
//				})
//				.blur(function(){
//					imex.operation.inputwatch('string', 'stop');
//				});
			$('#font_type').change(function(){
				imex.create.fontlist();
			});
//			$('#font_id').change(function(){
//				imex.operation.textset('font_id');
//			});
			$('#font_color').change(function(){
				if(imex.storage.image_method == 'vector'){
					$('#font_color option:selected').each(function(){
						$color = $(this).val();
					});
					if((imex.storage.layer.data.length != 0) && (imex.storage.vector.color != $color)){
						alert('すでに設定されているカラーと違うものが選択されています');
					}
				}
//				imex.operation.textset('font_color');
			});
			$('#size_slider').slider({
				value : 10,
				min : 10,
				max : 200,
				step : 1,
				change : function(event, ui){
//					if (imex.storage.layer.active) {
						$('#font_size').val(ui.value);
//						imex.operation.textset('size');
//					} else {
//						imex.storage.layer.active = true;
//					}
				}
			});
			$('#synthesis').click(function(){
				imex.operation.synthesis();
			});
			$('#cart').click(function(){
				imex.operation.cartsend();
			});
			$('#layout').click(function(){
				$('#preview').hide();
				$('#btn_cart').hide();
				$('#synthesis').show();
			});
			$('#coupon_code').click(function(){
				$('#coupon_code')
					.val('')
					.css({
						'color' : 'black'
					});
			});
			$('#set_coupon_code').click(function(){
				if($('#coupon_code').val() == ''){
					alert('クーポンコードを入力してください');
				} else {
					imex.load.coupon();
				}
			});
		},

		menu : function(name){
			if (name == 'upload') {
				if($('#data_info').css('display') != 'none'){
					imex.storage.layer.data.pop();
				}
				$('#data_select').show();
				$('#upload_form').hide();
				$('#data_info').hide();
				$('#image_tool').hide();
				$('#text_tool').hide();
				$('#menu_' + name).css({
					'height': '130px',
					'top': '-131px'
				});
				$('#string').val('');
				$('#font_type').val('');
				$('#font_id').val('');
				$('#size_slider').slider({
					value : 10,
					min : 10,
					max : 200,
					step : 1
				});
				imex.storage.layer.selected = null;
			}
		},

		coupon : function(price){
			var total = 0;
			$('#coupon_price').html(imex.common.numberformat(price));
			total = (parseInt($('#sub_total').val()) - parseInt(price)) + parseInt($('#carriage').val());
			$('#total').html(imex.common.numberformat(total));
			$('#coupon_form').hide();
			$('#coupon_list').show();
		},

		registvar : function(){
//			imex.storage.root = '/speedgift';
			imex.storage.root = '';
			imex.storage.debug = false;
			imex.storage.preview = false;
			imex.storage.previewtype = 0;
			imex.storage.history = {
				'string': null
			};
			imex.storage.price = {
				'service' : 0,
				'delivery' : 0,
				'item' : 0,
				'color' : 0,
				'print' : 0,
				'option' : 0
			};
			imex.storage.select = {
					'color' : null,
					'size' : null,
					'option': {
						'id' : null,
						'no' : null
					}
			};
			imex.storage.vector = {
					'color' : 'black'
			};
			imex.storage.layer = {
				'active' : true,
				'serial' : 0,
				'data' : [],
				'selected' : null
			};
		},

		setting : function (){
			imex.create.alpha();
			imex.load.item();
			imex.load.option();
			imex.load.font();
			imex.load.delivery();
		}

	}