(function($,window,undefined){
	$(function(){
		var mouseOverFlg = null;
		var url = $("#webroot").text();
		
		$(window).bind("resize", function() {
			var posLeft = $(document).width() / 2 + (450 - 440);
			$(".searchRignts").css('left', posLeft);
		});
		
		$(".topSearch a").live('click', function() {
			var posLeft = $(document).width() / 2 + (450 - 440);
			$(".searchRignts").css('left', posLeft);
			$(".searchRignts").animate( { height: 'toggle' }, 'fast' );
			return false;
		});
		
		$(".searchRignts .close").live('click', function() {
			$(".searchRignts").animate( { height: 'hide' }, 'fast' );
		});
		
		$("#large_cd").change(function (){
			var large = $("#large_cd"),
			middle = $("#middle_cd"),
			small = $("#small_cd"),
			sel = large.children('option:selected').val();
			$.ajax({
				async: true,
				type: 'GET',
				url: url+'tags/get_tag_list_js/list/2/'+sel,
				dataType: "json",
				success: function(data,dataType){
					if(dataType == 'success'){
						middle.children('option').remove();
						if(small.children('option:selected').val() != "0"){
							small.children('option').remove();
						}
						
						if(small.children('option').size() == 0){
							small.append($('<option value="0">選択してください</option>'));
						}
						middle.append($('<option value="0">選択してください</option>'));
						for(var i=0,len=data.length; i < len;i++){
							middle.append($('<option value="'+data[i].tag_cd+'">'+data[i].tag_name+'</option>'));
						}
						small.css('background-color','#666');
						middle.css('background-color','white');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
				},
				complete: function(XMLHttpRequest, textStatus){
				}
			});
		});
		
		$("#middle_cd").change(function (){
			var middle = $("#middle_cd"),
			small = $("#small_cd"),
			sel = middle.children('option:selected').val();
			$.ajax({
				async: true,
				type: 'GET',
				url: url+'tags/get_tag_list_js/list/3/'+sel,
				dataType: "json",
				success: function(data,dataType){
					if(dataType == 'success'){
						small.children('option').remove();
						small.append($('<option value="0">選択してください</option>'));
						for(var i=0,len=data.length; i < len;i++){
							small.append($('<option value="'+data[i].tag_cd+'">'+data[i].tag_name+'</option>'));
						}
						small.css('background-color','white');
						middle.css('background-color','white');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
				},
				complete: function(XMLHttpRequest, textStatus){
				}
			});
		});
		
		$('#brand').keyup(function(e){
			var sel = $('#brand').val();
			var brand = $('#brand_list');
			if(sel.length > 0){
				$.ajax({
					async: true,
					type: 'GET',
					url: url+'tags/get_brand_list_js/list/2000/'+sel,
					dataType: "json",
					success: function(data,dataType){
						if(dataType == 'success'){
							brand.children('option').remove();
							for(var i=0,len=data.length; i < len;i++){
								brand.append($('<option value="'+data[i].tag_cd+'">'+data[i].tag_name+'</option>'));
							}
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					},
					complete: function(XMLHttpRequest, textStatus){
					}
					
				});
			}else{
				brand.children('option').remove();
				brand.append($('<option value="0">ここから選んでください</option>'));
			}
		});
		$('#brand').children('li').bind('click',function(e){
			var brand = $('#brand_list');
			$.ajax({
					async: true,
					type: 'GET',
					url: url+'tags/get_brand_list_js/list/2000/'+$(this).text(),
					dataType: "json",
					success: function(data,dataType){
						if(dataType == 'success'){
							brand.children('option').remove();
							for(var i=0,len=data.length; i < len;i++){
								brand.append($('<option value="'+data[i].tag_cd+'">'+data[i].tag_name+'</option>'));
							}
						}
						return false;
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					},
					complete: function(XMLHttpRequest, textStatus){
					}
				});
		});
		$('#brand_list').click(function(){
			var brand = $('#brand_check_list');
			var li = $('#brand_check_list').children('li');
			var list = new Array();
			var list2 = new Array();
			if($('#brand_list').children('option:selected').val() == 0 || $('#brand_list').children('option:selected').val() == undefined){
				return false;
			}
			for(var i=0,len=li.length;i<len;i++){
				if($('#brand_check_id_list'+(i+1)).val() == $('#brand_list').children('option:selected').val()){
					return false;
				}
			}
			
			
			if($('#brand_list').children('option:selected').val() != 0){
				for(var i=0,len=li.length;i<len;i++){
					list[i] = $('#brand_check_list'+(i+1)).val();
					list2[i] = $('#brand_check_id_list'+(i+1)).val();
				}
				
				if(list.length <3){
					list.push($('#brand_list').children('option:selected').text());
					list2.push($('#brand_list').children('option:selected').val());
				}
				
				$('#brand_check_list').children('li').remove();
				for(var i=0,len=list.length;i<len;i++){
					brand.append($('<li>'+list[i]+'<span class="close"><img src="'+url+'img/close.png" id="'+i+'" /></span></li>'));
					$('#brand_check_list'+(i+1)).val(list[i]);
					$('#brand_check_id_list'+(i+1)).val(list2[i]);
				}
			}
		});
		
		$('#genre > button').bind('mouseover',function(e){
			var id = e.target.value;
			var genre = $('#genre_list');
			var list = {
			'feeling':['6000','feeling','-208'],
			'situation':['8000','situation','-252'],
			'season':['7000','season','-42'],
			'color':['5000','color','-336'],
			'char':['4000','char','-126'],
			'life':['3000','life','0'],
			'size':['1000','size','0'],
			'use':['10000','use','-168'],
			'condition':['9000','condition','-294'],
			'shoes':['1000','shoes','-378'],
			'clothe':['1000','clothe','-420'],
			'items_tag':['11000','items_tag','-84']
			};
			if(id!=mouseOverFlg){
				$('#'+list[e.target.id][1]).css('background-position',list[e.target.id][2]+'px -393px');
			}
		});
		
		$('#genre > button').bind('mouseout',function(e){
			var id = e.target.value;
			var genre = $('#genre_list');
			var list = {
			'feeling':['6000','feeling','-210'],
			'situation':['8000','situation','-252'],
			'season':['7000','season','-42'],
			'color':['5000','color','-336'],
			'char':['4000','char','-126'],
			'life':['3000','life','0'],
			'size':['1000','size','0'],
			'use':['10000','use','-168'],
			'condition':['9000','condition','-294'],
			'shoes':['1000','shoes','-378'],
			'clothe':['1000','clothe','-420'],
			'items_tag':['11000','items_tag','-84']
			};
			if(id!=mouseOverFlg){
				$('#'+list[e.target.id][1]).css('background-position',list[e.target.id][2]+'px -351px');
			}
		});
		
		$('#genre > button').bind('click',function(e){
			mouseOverFlg = e.target.value;
			var id = e.target.value;
			var genre = $('#genre_list');
			var list = {
			'feeling':['6000','feeling','-210'],
			'situation':['8000','situation','-252'],
			'season':['7000','season','-42'],
			'color':['5000','color','-336'],
			'char':['4000','char','-126'],
			'life':['3000','life','0'],
			'size':['1000','size','0'],
			'use':['10000','use','-168'],
			'condition':['9000','condition','-294'],
			'shoes':['1000','shoes','-378'],
			'clothe':['1000','clothe','-420'],
			'items_tag':['11000','items_tag','-84']
			};
			for(i in list){
				$('#'+list[i][1]).css('background-position',list[i][2]+'px -351px');
			}
			$('#'+list[e.target.id][1]).css('background-position',list[e.target.id][2]+'px -435px');
			$.ajax({
				async: true,
				type: 'GET',
				url: url+'tags/get_genre_list_js/list/'+list[e.target.id][0]+'/'+list[e.target.id][1],
				dataType: "json",
				success: function(data,dataType){
					if(dataType == 'success'){
						genre.children('option').remove();
						for(var i=0,len=data.length; i < len;i++){
							genre.append($('<option value="'+data[i].tag_cd+'">'+data[i].tag_name+'</option>'));
						}
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
				},
				complete: function(XMLHttpRequest, textStatus){
				}
			});
		});
		
		$('#genre_list').bind('click',function(e){
			var genre = $('#genre_check_list');
			var li = $('#genre_check_list').children('li');
			var list = new Array();
			var list2 = new Array();
			if($('#genre_list').children('option:selected').val() == undefined){
				return false;
			}
			if($('#genre_list').children('option:selected').val() != 0){
				for(var i=0,len=li.length;i<len;i++){
					if($('#genre_check_id_list'+(i+1)).val() == $('#genre_list').children('option:selected').val()){
						return false;
					}
				}
			
				for(var i=0,len=li.length;i<len;i++){
					list[i] = $('#genre_check_list'+(i+1)).val();
					list2[i] = $('#genre_check_id_list'+(i+1)).val();
				}
				
				if(list.length <3){
					list.push($('#genre_list').children('option:selected').text());
					list2.push($('#genre_list').children('option:selected').val());
				}
				//alert(list);
				$('#genre_check_list').children('li').remove();
				for(var i=0,len=list.length;i<len;i++){
					genre.append($('<li>'+list[i]+'<span class="close"><img src="'+url+'img/close.png" id="'+i+'"></span></li>'));
					$('#genre_check_list'+(i+1)).val(list[i]);
					$('#genre_check_id_list'+(i+1)).val(list2[i]);
					/*$('#genre_check_list > li > .close').bind('click',function(e){
						alert(list);
						alert(list2);
						$('#genre_check_list').children('li').remove();
						list[e.target.id-0] = null;
						list2[e.target.id-0] = null;
						var work = new Array();
						var work2 = new Array();
						for(var i=0,len=list.length;i<len;i++){
							if(list[i] != '' && list[i] != null){
								work.push(list[i]);
								work2.push(list2[i]);
							}
						}
						list = work;
						list2 = work2;
						for(var i=0,len=list.length;i<len;i++){
							$('#genre_check_list').append($('<li>'+list[i]+'<span id="'+i+'" class="close">[×]</span></li>'));
						}
						alert(list);
						alert(list2);
					});
					*/
				}
			}
		});
		
		$('#genre_check_list > li > .close').live('click',function(e){
			var list = new Array();
			var list2 = new Array();
			var li = $('#genre_check_list').children('li');
			
			for(var i=0,len=li.length;i<len;i++){
				list[i] = $('#genre_check_list'+(i+1)).val();
				list2[i] = $('#genre_check_id_list'+(i+1)).val();
				$('#genre_check_list'+(i+1)).val('');
				$('#genre_check_id_list'+(i+1)).val('');
			}

			$('#genre_check_list').children('li').remove();
			list[e.target.id-0] = null;
			list2[e.target.id-0] = null;
			var work = new Array();
			var work2 = new Array();
			for(var i=0,len=list.length;i<len;i++){
				if(list[i] != '' && list[i] != null){
					work.push(list[i]);
					work2.push(list2[i]);
				}
			}
			list = work;
			list2 = work2;
			for(var i=0,len=list.length;i<len;i++){
				$('#genre_check_list').append($('<li>'+list[i]+'<span class="close"><img src="'+url+'img/close.png" id="'+i+'" /></span></li>'));
				$('#genre_check_list'+(i+1)).val(list[i]);
				$('#genre_check_id_list'+(i+1)).val(list2[i]);
			}
		});
		
		$('#brand_check_list > li > .close').live('click',function(e){
			var list = new Array();
			var list2 = new Array();
			var li = $('#brand_check_list').children('li');

			for(var i=0,len=li.length;i<len;i++){
				list[i] = $('#brand_check_list'+(i+1)).val();
				list2[i] = $('#brand_check_id_list'+(i+1)).val();
				$('#brand_check_list'+(i+1)).val('');
				$('#brand_check_id_list'+(i+1)).val('');
			}
			
			$('#brand_check_list').children('li').remove();
			list[e.target.id-0] = null;
			list2[e.target.id-0] = null;
			var work = new Array();
			var work2 = new Array();
			for(var i=0,len=list.length;i<len;i++){
				if(list[i] != '' && list[i] != null){
					work.push(list[i]);
					work2.push(list2[i]);
				}
			}
			list = work;
			list2 = work2;
			for(var i=0,len=list.length;i<len;i++){
				$('#brand_check_list').append($('<li>'+list[i]+'<span class="close"><img src="'+url+'img/close.png" id="'+i+'" /></span></li>'));
				$('#brand_check_list'+(i+1)).val(list[i]);
				$('#brand_check_id_list'+(i+1)).val(list2[i]);
			}
		});
		
		$('#clear').bind('click',function(e){
			$('#free_word').val('');
			$('#large_cd').val('0');
			$('#middle_cd').children('option').remove();
			$('#small_cd').children('option').remove();
			$('#middle_cd').append($('<option value="0">選択してください</option>'));
			$('#small_cd').append($('<option value="0">選択してください</option>'));
			for(var i=0;i<3;i++){
				$('#genre_check_list'+(i+1)).val('');
				$('#genre_check_id_list'+(i+1)).val('');
				$('#brand_check_list'+(i+1)).val('');
				$('#brand_check_id_list'+(i+1)).val('');
			}
			$('#genre_check_list').children('li').remove();
			$('#brand_check_list').children('li').remove();
			$('#store_cd').val('0');
			$('#middle_cd').css('background-color','#666');
			$('#small_cd').css('background-color','#666');
			clear_fn('all');
		});
		
		$('#clear2').bind('click',function(e){
			$('#free_word').val('');
			$('#large_cd').val('0');
			$('#middle_cd').children('option').remove();
			$('#small_cd').children('option').remove();
			$('#middle_cd').append($('<option value="0">選択してください</option>'));
			$('#small_cd').append($('<option value="0">選択してください</option>'));
			for(var i=0;i<3;i++){
				$('#genre_check_list'+(i+1)).val('');
				$('#genre_check_id_list'+(i+1)).val('');
				$('#brand_check_list'+(i+1)).val('');
				$('#brand_check_id_list'+(i+1)).val('');
			}
			$('#genre_check_list').children('li').remove();
			$('#brand_check_list').children('li').remove();
			$('#store_cd').val('0');
			$('#middle_cd').css('background-color','#666');
			$('#small_cd').css('background-color','#666');
			clear_fn('all');
		});
		
		$('#free_word_clear').live('click',function(e){
			$('#free_word').val('');
			clear_fn('free');
		});
		
		$('#cd_clear').live('click',function(e){
			$('#large_cd').val('0');
			$('#middle_cd').children('option').remove();
			$('#small_cd').children('option').remove();
			$('#middle_cd').append($('<option value="0">選択してください</option>'));
			$('#small_cd').append($('<option value="0">選択してください</option>'));
			$('#middle_cd').css('background-color','#666');
			$('#small_cd').css('background-color','#666');
			clear_fn('cd');
		});
		
		$('#genre_clear').live('click',function(e){
			for(var i=0;i<3;i++){
				$('#genre_check_list'+(i+1)).val('');
				$('#genre_check_id_list'+(i+1)).val('');
			}
			$('#genre_check_list').children('li').remove();
			clear_fn('genre');
		});
		
		$('#brand_clear').live('click',function(e){
			for(var i=0;i<3;i++){
				$('#brand_check_list'+(i+1)).val('');
				$('#brand_check_id_list'+(i+1)).val('');
			}
			$('#brand_check_list').children('li').remove();
			clear_fn('brand');
		});
		
		$('#store_clear').live('click',function(e){
			$('#store_cd').val('0');
			clear_fn('store');
		});
		
		$('#alert,#alert2').live('click',function(e){
			var num = 0;
			if($('#large_cd').val() != 0){
				num +=1;
			}
			if($('#middle_cd').val() != 0){
				num +=1;
			}
			if($('#small_cd').val() != 0){
				num +=1;
			}
			for(var i=0;i<3;i++){
				if($('#genre_check_id_list'+(i+1)).val() != ''){
					num +=1;
				}
			}
			for(var i=0;i<3;i++){
				if($('#brand_check_id_list'+(i+1)).val() != ''){
					num +=1;
				}
			}
			if($('.alerts > .alert5 > .view > .tag').text().substring(0,3) != 'タグ：'){
				alert('アラートの数が5つ設定されています。');
				return false;
			}
			if(num > 0 && num <4){
				$.ajax({
					async: true,
					type: 'POST',
					data:$("#right_search").serialize(),
					url: url+'my_page/keywordalert_js',
					dataType: "json",
					success: function(data,dataType){
						
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					},
					complete: function(XMLHttpRequest, textStatus){
						location.href = XMLHttpRequest.responseText;
					}
				});
			} else if(num < 1) {
				alert('タグが１つも指定されていません。');
			} else if(num > 3) {
				alert('タグは最大３つまでです。');
			}
		});
		
		$('#store_clear').live('click',function(e){
			$('#store_cd').val('0');
			clear_fn('store');
		});
		
		function clear_fn(str){
			$.ajax({
				async: true,
				type: 'GET',
				url: url+'search/right_search_clear/'+str,
				success: function(data,dataType){
					if(dataType == 'success'){
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
				},
				complete: function(XMLHttpRequest, textStatus){
				}
			});
		}
	});
})(jQuery,window);
