/**
 * app.js
 */

/**
 * appClass
 */
function appClass() {
	this.initialize.apply(this, arguments);
};

appClass.prototype = {

	/**
	 * initialize
	 */
	initialize : function() {
		this._tmp = {};
		this._params = null;
	},

	/**
	 * init
	 */
	init : function(params) {
		this._params = params;
		this.pulldown();
		this.imageViewer();
		this.myList();
		this.cartList();
	},

	/**
	 * jstrDecode
	 */
	jstrDecode : function(rtxt) {
		if(rtxt.indexOf('%u') < 0 && rtxt.indexOf('%') > -1) return decodeURIComponent(rtxt);
		return rtxt;
	},

	/**
	 * findParentByTagName
	 */
	findParentByTagName : function(elm, name) {
		var reg = new RegExp('^' +name+ '$', 'i');
		if(elm.parentNode.nodeName.match(reg)) return elm.parentNode;
		return this.findParentByTagName(elm.parentNode, name);
	},

	/**
	 * removeTmp
	 */
	removeTmp : function() {
		this._tmp = {};
	},

	/**
	 * imageViewer
	 */
	imageViewer : function() {
		if(!$(this._params.jqviewer.slider).length) return;
		var viewer = new slideViewerClass();
		viewer.add(false, this._params.jqviewer);
	},

	/**
	 * addMyList
	 */
	addMyList : function(id) {
		if(id == 'added') {
			this.msgwin(false, '気になるアイテムに追加しました');
			this.myList();
			return;
		}

		this.msgwin(true, '商品を気になるアイテムに追加します...');
		$.ajax({
			type: 'POST',
			url: this._params.mylist.add,
			cache: false,
			data: 'id=' + id,
			success: $.scope(this, this.addMyList)
		});
	},

	/**
	 * delMyList
	 */
	delMyList : function(id, elm) {
		if(id == 'removed') {
			this.myList();
			$(this._tmp.p).fadeOut('normal', function() { $(this).remove(); });
			this.removeTmp();
			return;
		}

		var p = this.findParentByTagName(elm, 'section');
		this._tmp.p = p;

		$.ajax({
			type: 'POST',
			url: this._params.mylist.del,
			cache: false,
			data: 'id=' + id,
			success: $.scope(this, this.delMyList)
		});
	},

	/**
	 * myList
	 */
	myList : function(r) {
		if(!$(this._params.mylist.v).length) return;
		if(r) {
			$(this._params.mylist.v).replaceWith(r);
			return;
		}

		$.ajax({
			type: 'POST',
			url: this._params.mylist.side,
			cache: false,
			data: '',
			success: $.scope(this, this.myList)
		});
	},

	/**
	 * addCart
	 */
	addCart : function(asin) {
		if(asin == 'added') {
			this.msgwin(false, 'カートに商品を追加しました');
			this.cartList();
			return;
		}
		if(asin.match(/^ERROR/)) {
			this.msgwin(false, asin.replace(/^ERROR\:/, ''));
			this.cartList();
			return;
		}

		this.msgwin(true, 'カート処理中...');
		$.ajax({
			type: 'POST',
			url: this._params.cart.add,
			cache: false,
			data: 'ASIN=' + asin,
			success: $.scope(this, this.addCart)
		});
	},

	/**
	 * cartList
	 */
	cartList : function(r) {
		if(!$(this._params.cart.v).length) return;
		if(r) {
			$(this._params.cart.v).replaceWith(r);
			return;
		}

		$.ajax({
			type: 'POST',
			url: this._params.cart.side,
			cache: false,
			data: '',
			success: $.scope(this, this.cartList)
		});
	},

	/**
	 * cartSheet
	 */
	cartSheet : function(r) {
		if(!$(this._params.cart.sheet).length) return;
		if(r) {
			$(this._params.cart.sheet).replaceWith(r);
			return;
		}

		$.ajax({
			type: 'POST',
			url: this._params.cart.sht,
			cache: false,
			data: '',
			success: $.scope(this, this.cartSheet)
		});
	},

	/**
	 * delCart
	 */
	delCart : function(asin, elm) {
		if(asin == 'removed') {
			$(this._tmp.p).fadeOut('normal', function() { $(this).remove(); });
			this.cartList();
			this.cartSheet();
			this.removeTmp();
			this.msgwin(false, 'カートから商品を削除しました');
			return;
		}

		var p = this.findParentByTagName(elm, 'section');
		this._tmp.p = p;
		this.msgwin(true, 'カート処理中...');

		$.ajax({
			type: 'POST',
			url: this._params.cart.del,
			cache: false,
			data: 'ASIN=' + asin,
			success: $.scope(this, this.delCart)
		});
	},

	/**
	 * msgwin
	 */
	msgwin : function(sw, msg) {
		// add
		if(sw) {
			this.screen();
			$('body').append('<div id="added">' +msg+ '</div>');
			$('#added').css({
				position: 'fixed',
				top: '0px',
				left: '0px',
				padding: '20px 0',
				width: '100%',
				background: '#4c86ed',
				fontSize: '110%',
				fontWeight: 'bold',
				textAlign: 'center',
				color: '#fff',
				display: 'none',
				zIndex: '9990'
			});
			$('#added').slideDown('normal');
			return;
		}

		// remove
		if(!$('#added').length) return;
		$('#added').css({background:'#69b9ec'}).html(msg);
		this.screen(true);
		var itv = setTimeout(
			$.scope(this, function() { $('#added').slideUp('normal', function() { $(this).remove(); }); }), 900
		);
	},

	/**
	 * screen
	 */
	screen : function(hide) {
		$('div.h-screen').fadeOut('fast', function() { $(this).remove() });
		if(hide) return;

		$('body').append('<div class="h-screen"></div>');
		$('div.h-screen').css({
			background:'#fff',
			border: 'none',
			padding: '0',
			margin: '0',
			width: '100%',
			height: $('body').height() + 'px',
			top: '0px',
			left: '0px',
			position: 'fixed',
			display: 'block',
			filter: 'alpha(opacity=65)',
			MozOpacity: '0.65',
			opacity: '0.65',
			zIndex: '9910'
		});
//		$('.h-screen').click($.scope(this, this.reset));
	},

	/**
	 * pulldown
	 */
	pulldown : function() {
		if(!$(this._params.pulldown).length) return;
		$(this._params.pulldown + ' li').css({position: 'relative'});

		// children word break fixed.
		var size = {w: 0};
		$(this._params.pulldown + ' li ul li a').css({position: 'absolute', display: 'block'});
		$(this._params.pulldown + ' li ul li a').each($.scope(size, function(i, elm) {
			if($(elm).outerWidth() > this.w) this.w = $(elm).outerWidth();
		}));
		$(this._params.pulldown + ' li ul li a').css({position: 'static', minWidth: size.w + 'px'});

		$(this._params.pulldown + ' li ul').hide();
		$(this._params.pulldown + ' li[class!="current"]').hover(
			function() {
				$('ul', this).css({
					display: 'block',
					position: 'absolute',
					zIndex: '100',
					top: Math.round($(this).height() / 2) + 'px',
					left: Math.round($(this).width() / 7 * 3) + 'px'
				});
			},
			function() { $('ul', this).css('display', 'none'); }
		);
	}
}


/**
 * instance
 */
var app;

$(function() {
	app = new appClass();
	app.init({
		jqviewer: {
			slider: 'p.ibox',
			viewer: 'p.image',
			thumbW: 80,
			thumbMargin: 5,
			next: 'Next',
			prev: 'Prev'
		},
		itrans: {
			prepare: '#prepare',
			viewer : '#toptitle',
			slider : '#slider'
		},
		mylist: {
			v: '#mylist',
			side: '/mylist/side',
			add: '/mylist/add',
			del: '/mylist/del'
		},
		cart: {
			v: '#cart',
			sheet: '#sheet',
			side: '/cart/side',
			add: '/cart',
			del: '/cart/del',
			sht: '/cart/sheet',
			check: '/cart/check'
		},
		pulldown: '#categories'
	});
});


/**
 * analytics
 */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-27926151-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


