$(function(){
		if (location.search){
			var qry = location.search;
			qry = qry.replace("?","");
			var searchtext = decodeURI(qry);
			$("#searchtext").attr("value",searchtext);
		}else{
			var qry = "iphone";
			$("#searchtext").attr("value","AppStore Search");
		}
			offset = 0 ;
			view = 20;
		search(qry,offset);
		window.onload = function(){setTimeout(hideURLbar,100);}
		window.onorientationchange = hideURLbar;
		
		$("#searchtext").change(function(){
			qry = $("#searchtext").attr("value");
			url = location.href.replace(location.search,"");
			location.href = url +"?"+ qry;
		});
		
		$("#searchtext").focus(function(){
			$("#searchtext").attr("value","");
		});
		$("#header h1").click(function(){
			location.href = location.href.replace(location.search,"");
		});
		$("#rss").click(function(){
			window.open("http://pipes.yahoo.com/pipes/pipe.run?_id=c3e25ef25e321099e1411c0e95e09b0c&_render=rss&limit=100&search="+qry);
		});
	});

	function search(qry,offset){
		$.getJSON(
		"http://pipes.yahoo.com/pipes/pipe.run?_id=c3e25ef25e321099e1411c0e95e09b0c&_render=json&search="+qry+"&offset="+ offset +"&_callback=?",
		function(data){ 
			$.each(data.value.items, function(i,item){
				link = decodeURIComponent(item.trackViewUrl);
				releaseDate = item.releaseDate.substr(0,10);
				if(item.price == 0 ){var price = "Free";}else{var price = "¥" + item.price;}
				i += offset;

				relatedblog = "";
				if(item.blog != ""){
					relatedblog = '<div class="popupbottom">関連ブログかも<ul id="item_bloglist'+ i +'">';
					$.each(item.blog,function(k,blogitem){
						relatedblog +=	'<li><a href="'+ blogitem.link +'" target="_blank">'+ blogitem.title +'</a></li>';
					});
					relatedblog += '</ul></div>';
				}
				
				$("<span/>").html('<a href="#item_block'+ i +'" class="facebox"><img src="'+ item.artworkUrl60 +'" title="'+ item.trackName +'"/></a><br><div id="item_block'+ i +'" class="itemblock" style="display:none;"><div class="popuptop"><h3>'+item.trackCensoredName+' - Ver.'+item.version+'</h3><span class="popup'+offset+'" style=""><a href="'+ link +'" target="_blank"><img src="'+ item.artworkUrl60 +'" title="'+ item.trackName +'" /></a></span><a href="?'+item.artistName+'">'+item.artistName+'</a><br />Price : '+price+'<br />Category : <a href="?'+ item.primaryGenreName +'">'+ item.primaryGenreName+'</a><br />Released : '+releaseDate+'<br /><a href="'+ link +'" target="_blank">Go AppStore</a><br /><a href="'+ item.sellerUrl +'" target="_blank">Go WebSite</a></div>'+ relatedblog +'</div>').attr("class","page"+offset).appendTo("#images");

				if ( i >= data.count + offset ) return false;
			});
			$(".page"+offset).corner("cc:#000");
			$(".popup"+offset).corner("cc:#fff");
			$(".facebox").facebox();
			$(".searchnext").hide();
			if(data.count == view){
				$("<span/>").attr("id","searchnext"+offset).attr("class","searchnext").html("Next").appendTo("#footer");
				$("#searchnext"+offset).click(function(){
				$(this).animate({opacity: 0},4000);
				//$(this).html("Loading").animate({opacity: 100},2000);
				search(qry,offset);
			});
			}

		});
		offset += view;

	}

	function hideURLbar() { window.scrollTo(0,1);}
