/**
 * Common Script
 * @encodehint 共通スクリプト
 */
(function(window, undefined){
var Hnet = window.Hnet = {
	init: function(){
		$('a.rewind').click(function(){
			window.scrollTo(0,0);
		});
		
		//init favorite
		Hnet.Shop.Favorite.init();
		
	},
	Index: {
		init: function(){
			
		}
	},
	Girl: {
		Details: {
			init: function(id_girl) {
				$('.vote a').click(function(){
					var $this = $(this);
					$.ajax({
						'url': './girl/vote/',
						'data': {
							'id': id_girl
						},
						'success': function(res) {
							if ('true' !== res) {
								return;
							}
							$('<span>投票しました</span>').insertAfter($this);
							$this.remove();
						}
					});
				});
				
				$('#gravures a').mouseover(function() {
					$gravure = $('#gravure img');
					$gravure.attr('src', $gravure.attr('src').replace(/[^\/]+$/g, '') + $(this).children('img').attr('src').replace(/^.+[\/]/g, ''));
				});
			}
		},
		Index: {
			init: function() {
				$('#togglecondition a').click(function(){
					var $p = $(this).parent();
					if ($p.hasClass('open')) {
						$p.removeClass('open').siblings('#hiddencondition').hide();
						window.scrollTo(0, $('#girlindex').top);
					} else {
						$p.addClass('open').siblings('#hiddencondition').show();
					}
				});
				$('#reset').click(function(){
					$('div.conditions a').removeClass('checked');
					Hnet.Girl.Index.submit(null);
				});
				$('div.conditions a').click(function(){
					var $this = $(this);
					if ($this.hasClass('checked')) {
						$this.removeClass('checked')
					} else {
						$this.addClass('checked');
						if ($this.parent('p').hasClass('radio')) {
							$this.siblings('a').removeClass('checked');
						}
					}
					Hnet.Girl.Index.submit(null);
				});
				
				$('.pager a').live('click', function(){
					Hnet.Girl.Index.submit($(this).attr('id').split('_').pop());
				});
			},
			submit: function(offset) {
				if (null !== offset) {
					var scroll = true;
				}
				var cond = [];
				offset = offset || 0;
				$('div.conditions a.checked').each(function(){
					var $this = $(this);
					cond.push($this.attr('name')+'='+$this.attr('id').split('_').pop());
				});
				$.ajax({
					'url': './girl/',
					'data': cond.join('&')+'&offset='+offset,
					'success': function(res) {
						var $res = $(res);
						$('#results').empty().append($res.find('#results').children());
						if (scroll) {
							window.scrollTo(0, $('h3.results').offset().top);
						}
					}
				});
			},
			addResult: function(data) {
				var $box = $('.results div.girls');
				
				for (var k in data) {
					var girl = data[k];
					var $girl = $('<div class="data"></div>')
						.append(
							$('<a class="thumbnail" href="./girl/'+girl.id+'"><img src="./img/thumbs/'+girl.thumbnail+'" /></a>')
						)
						.append(
							$('<a class="name" href="./girl/'+girl.id+'"></a>').text(girl.name)
						)
					;
					$('<div class="girl"></div>')
					.append($girl)
					.appendTo($box);
				}
			}
		}
	},
	Mypage: {
		init: function(){
			$('.addmylist').click(function(){
				$shop = $(this).parents('.shop');
				Hnet.Shop.Favorite.remove($shop.attr('id').split('_').pop());
				location.reload();
			}).each(function(){
				$(this).empty().append('<span class="listed"><img src="./img/system/addmylist_remove.png" alt="マイリスト解除" /></span>');
			}).css('cursor', 'pointer');
		}
	},
	Shop: {
		_id: 0,
		Favorite: {
			init: function(){
				var mylists = Hnet.Shop.Favorite.get();
				function listed ($this) {
					$this.after('<span class="listed"><img src="./img/system/addmylist_listed.png" alt="マイリスト登録済" /></span>').remove();
				}
				$('.addmylist a').each(function(){
					var $this = $(this);
					var id = $this.parents('.shop').attr('id').split('_').pop();
					if (-1 !== $.inArray(id, mylists)) {
						listed($this);
						return;
					}
				}).click(function(){
					var $this = $(this);
					var id = $this.parents('.shop').attr('id').split('_').pop();
					Hnet.Shop.Favorite.add(id);
					listed($this);
				});
			},
			add: function(id) {
				if (Hnet.Shop.Favorite.isset(id)) {
					return;
				}
				var cookie = Hnet.Shop.Favorite.get();
				cookie.push(id);
				Red.cookie('favorites', cookie.join(','), {expires: 86400*365*10, path: '/'});
			},
			remove: function(id) {
				var cookie = Hnet.Shop.Favorite.get();
				cookie.push(id);
				var removed = [];
				for(var k in cookie) {
					if (cookie[k] == id) {
						continue;
					}
					removed.push(cookie[k]);
				}
				Red.cookie('favorites', removed.join(','), {expires: 86400*365*10, path: '/'});
			},
			get: function() {
				var cookie = Red.cookie('favorites')
				return cookie ? cookie.split(',') : [];
			},
			isset: function(id) {
				var reg = new RegExp('(^|,)'+id+'($|,)');
				return (Red.cookie('favorites') || '').match(reg);
			}
		},
		Index: {
			_area: 0,
			_type: 0,
			init: function(area, type){
				Hnet.Shop.Index._area = area;
				Hnet.Shop.Index._type = type;
				
				$('#reset').click(function(){
					$('div.conditions div.area a,div.conditions div.type a').removeClass('checked');
					Hnet.Shop.Index._area = 0;
					Hnet.Shop.Index._type = 0;
					Hnet.Shop.Index._search();
				});
				
				$('div.conditions div.area a').live('click', function(){
					var $this = $(this)
					  , id = $this.attr('id').split('_').pop()
					;
					if (Hnet.Shop.Index._area == id) {
						Hnet.Shop.Index._area = 0;
						$this.removeClass('checked');
					} else {
						Hnet.Shop.Index._area = id;
						$('div.conditions div.area a').removeClass('checked');
						$this.addClass('checked');
					}
					Hnet.Shop.Index._search();
				});
				$('div.conditions div.type a').live('click', function(){
					var $this = $(this)
					  , id = $this.attr('id').split('_').pop()
					;
					if (Hnet.Shop.Index._type == id) {
						Hnet.Shop.Index._type = 0;
						$this.removeClass('checked');
					} else {
						Hnet.Shop.Index._type = id;
						$('div.conditions div.type a').removeClass('checked');
						$this.addClass('checked');
					}
					Hnet.Shop.Index._search();
				});
			},
			_search: function(bool) {
				var request = {
					'area': parseInt(Hnet.Shop.Index._area),
					'type': parseInt(Hnet.Shop.Index._type)
				};
				
				$.ajax({
					'url': './shop/',
					'data': request,
					'success': function(res) {
						var $res = $(res);
						if (!bool) {
							$('div.conditions div.area').empty().append($res.find('div.conditions div.area').children());
							$('div.conditions div.type').empty().append($res.find('div.conditions div.type').children());
						}
						$('#results').empty().append($res.find('#results').children());
						Hnet.Shop.Favorite.init();
					}
				});
			}
		},
		Details: {
			init: function(id) {
				$('div.news div.pager a').live('click', function(){
					var $this = $(this);
					$.ajax({
						'url': $this.attr('href'),
						'success': function(res){
							var $res = $(res);
							$('div.news div.informations').empty().append($res.find('div.news div.informations').children()).animate({
								'scrollTop': 0
							});
							$('div.news div.pager').empty().append($res.find('div.news div.pager').children());
						}
					});
					return false;
				});
			}
		},
		Discount: {
			init: function(){
				$('div.print a').click(function(){
					$(this).parent().parent().removeClass('noprint').siblings('div.discount').addClass('noprint');
					$('body').addClass('printMode');
					print();
				});
			}
		}
	},
	Apply: {
		init: function(){
			$('input[name="plan"]').click(function(){
				var $this = $(this);
				if (2 <= $this.val()) {
					$('#sidebanner').attr('disabled', false);
				} else {
					$('#sidebanner').attr('disabled', true);
				}
			});
			
			$('#appform').submit(function(){
				if (!$(this).validate()) {
					window.scrollTo(0, $(this).offset().top);
					return false;
				}
			});
		}
	}
};
$.fn.extend({
	'validate': function(){
		var bool = true;
		var $form = $(this);
		var $inputs = $form.find('input,select,textarea');
		$form.find('p.error').remove();
		
		function setError($input, msg) {
			$parent = $input.parent();
			if ($parent.find('p.error').length) {
				return;
			}
			$('<p class="error"></p>').text(msg).prependTo($parent);
		}
		
		$inputs.each(function(){
			$this = $(this);
			if ($this.hasClass('required')) {
				if ('INPUT' === $this.attr('tagName') && 'radio' === $this.attr('type')) {
					if (0 == $form.find('input[name="'+$this.attr('name')+'"]:checked').length) {
						setError($this, '入力は必須です。');
						bool = false;
					}
					return;
				}
				if ('' === $this.val()) {
					setError($this, '入力は必須です。');
					bool = false;
					return;
				}
			}
		});
		return bool;
	}
});
$(Hnet.init);
})(window);
