$j = jQuery.noConflict();

$j(document).ready(function(){
	$j("#my_sample").o({mode:"instance"});
	$j.extend($o,{
		init: function(){
			var e = $j().oURLParam();

			$o.tabNo=0;
			$o.searchMode=['me','p','f','ex'];
			var tabs = $j('#my_tab').tabs({
				selected:0,
				select: function(event,ui){
					$o.tabNo = ui.index;
					var isForm = (ui.index==3);
					$j(this).css({
						height: $o.tabHeight + (isForm ? 142 : 0)
					}).find("form").ifShow(isForm);
					$o.initSearch();
					switch (ui.index) {
						case 0:
						case 1:
							//$anD.refreshFn=$anD.list.anD_allList.refreshFn;
							$o.search.refresh = true;
							$o.newOrder("n");
							break;
					}
					$o.receiveSearch($o.search);
				}
			});
			$o.tabHeight=tabs.height();
			$o.checkReadOnly("noDialog");


			$o.jStmp = $j.createTemplateURL($o.baseDir+"template/mypage.tpl");
			$j("#searchBody").find("#result").setTemplate($o.jStmp).processTemplate({mode: e.md});
			$o.jR = $j("#searchResult");
			$j("#header_btnReturn").click(function(){
				location.href = "/ipex/";
			});
			$j("#my_btnEdit").click(function(){
				location.href = "/ipex/account";
			});
			$j("#footer_btnTop").click(function(){
				$j.onPageTop(0);
			}).oDecoButton();
			$j("#search_more,#search_more2").click(function(){
				$o.onSearchMore();
			});
			$j("#search_back,#search_back2").click(function(){
				onSearchBack();
			});
			$j("#header_btnSignOut,#my_btnSignOut").click(function(){
				location.href=$o.baseDir+'signin/signout';
			});
			$j("#header_btnSignIn,#my_btnSignIn").click(function(){
				location.href=$o.baseDir+'signin';
			});

			$j("#my_btnNewCoin").click(function(){
				$j.IE6form(false);
				$o.checkReadOnly(null,function(){
				$o.wizEntry("new",function(){$j.IE6form(true);});
				});
			});
			$j("#my_btnSearch").click(function(){
				$o.onSearch();
			});
			$j("#search_btnCreate").click(function(){
				$o.newOrder("n");
				$o.receiveSearch($o.search)
			}).css("border-color", "red");
			$j("#search_btnUpdate").click(function(){
				$o.newOrder("u");
				$o.receiveSearch($o.search)
			}).css("border-color", "black");
			$o.newOrder(e.md)
			$o.more = 0;
			$o.search.refresh = true;
			CoinApi.getCategories(function(c){
				$o.categories = {};
				$j.each(c,function(k,v){
					$o.categories[v.categoryId]=v.category;
				});
				$o.receiveSearch($o.search);
			});
		},
		onSearchMore: function(){
			if ($o.search.moving) {
				return
			}
			window.scroll(0, 0);
			$o.search.moving = true;
			var h = $o.search.offset + $o.search.limit;
			if (h < $o.search.count) {
				$o.search.offset = h;
				$o.receiveSearch($o.search, function(){
					err.html("Not Found!").show()
				})
			}
		},
		onSearchBack: function(){
			if ($o.search.moving) {
				return
			}
			window.scroll(0, 0);
			$o.search.moving = true;
			var h = (Math.floor($o.search.offset / $o.search.limit) - 1) * $o.search.limit;
			if (h >= 0) {
				$o.search.offset = h;
				$o.receiveSearch($o.search, function(){
					err.html("Not Found!").show()
				})
			}
		},
		onSearch:function(){
			$j("#category_err").empty().hide();
			$o.setSearch();
			$o.receiveSearch($o.search, function(){
				$j("#search_err").html("Not Found!").show()
			});
		},
		dateFormat: function(d){
			if (d==null) return '';
			var mon = ['Jan.','Feb.','Mar.','Apr.','May','June','July','Aug.','Sep.','Oct.','Nov.','Dec.'];
			return d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()+' '+mon[d.getMonth()]+' '+(d.getDate()+1)+'.'+(d.getYear()+1900);
		},
		initSearch: function(d){
			var fm = document.indexActionForm;
			$o.search = $j.extend($o.search, {
				mode:$o.searchMode[$o.tabNo],
				lastcount: 0,
				offset: 0,
				limit:10,
				cache: [],
				myName: ($o.searchMode[$o.tabNo]=="me")?fm.myName.value:"",
				keyword: "",
				categoryId: 0,
				themeCd: 0,
				themeMask: 0,
				author:"",
				moving: true,
				refresh: true
			}, d)
			fm.author.value="";
			fm.keyword.value="";
			fm.category.value="";
			fm.tension1.value="";
			fm.tension2.value="";
			fm.doing.value="";
			if (typeof(document.frmCount)=="undefined") $o.jR.html('');
		},
		setSearch: function(d){
			var fm = document.indexActionForm,t=$o.themeCd(fm,true);
			$o.search = $j.extend($o.search, {
				mode:$o.searchMode[$o.tabNo],
				lastcount: 0,
				offset: 0,
				limit:10,
				cache: [],
				myName: ($o.searchMode[$o.tabNo]=="me")?fm.myName.value:"",
				author: fm.author.value,
				keyword: fm.keyword.value,
				categoryId: (($j.isEmpty(fm.category.value))?0:Number(fm.category.value)),
				themeCd: t.themeCd,
				themeMask: t.themeMask,
				moving: true,
				refresh: true
			}, d)
			$o.jR.html('');
		},
		newOrder: function(d){
			if (d == "u") {
				$j("#search_btnUpdate").css("border-color", "red");
				$j("#search_btnCreate").css("border-color", "black");
				$o.initSearch({
					order: "u"
				})
			}
			else {
				$j("#search_btnCreate").css("border-color", "red");
				$j("#search_btnUpdate").css("border-color", "black");
				$o.initSearch({
					order: "n"
				})
			}
		},
		receiveSearch: function(e, d){
			$j("#server_access").show();
			CoinApi.searchResult(e, function(f){
				$j("#server_access").hide();
				$o.search.count = f.count;
				$o.updateInfoList(f, e, d);
				$o.jR.show();
			})
		},
		updateInfoList: function(d, g, fn){
			var isOpe=(typeof(d.init)=="undefined");
			$j("#search_count").html(d.count + " Hits!");
			if (d.count == 0) {
				$j("#search_board").hide();
				$o.jR.html('');
				$o.search.moving = false;
				if ($j.isFunction(fn)) fn();
				return;
			}
			if (g.refresh == true) {
				$j("#search_slider").slider("destroy").slider({
					min: 0,
					max: (Math.ceil(d.count / $o.search.limit) * $o.search.limit - 1),
					value: 0,
					slide: function(h, i){
						$j("#search_slider").parent().find("span").html(f(e(i.value)))
					},
					change: function(h, j){
						$o.search.offset = e(j.value);
						var i = f($o.search.offset);
						$j(this).parent().find("span").html(i);
						$j("#search_slider").slider("disable").slider("option","value", j.value, null, true).parent().find("span").html(i).end().end().slider("enable");
						$o.search.refresh = false;
						$o.receiveSearch($o.search, function(){
							self.sliderReset();
							err.html("Not Found!").show()
						});
						window.scroll(0, 0)
					}
				}).parent().ifShow((d.count > g.limit)).find("span").html(f(0));
				$o.search.refresh = g.refresh = false
			}
			$j("#search_more,#search_more2").ifShow((d.count - $o.search.offset) > $o.search.limit);
			$j("#search_back,#search_back2").ifShow($o.search.offset > 0);
			if (isOpe){
			if (!$o.search.cache[$o.search.offset]) {
				if ($o.search.lastCount != d.count) {
					$o.search.cache = []
				}
				$o.search.cache[$o.search.offset] = d.results
			}
				$o.jR.html($o.jStmp._templates.search_result.get({
					mode: g.mode,
					infotd: $o.search.cache[$o.search.offset],
					no: 0
				}, {
					no: $o.search.offset + 1
				}, document.body));
			}
			$o.imgCourt();
			$j(".infoText").each(function(){
				var h = $j(this).height() + 8, i = $j(this).get(0).scrollHeight;
				if (h < i) {
					$j(this).parent().find(".infoScrCover").hide()
				}
			});
			$o.search.moving = false;
			$o.search.lastCount = d.count;
			function f(h){
				return Number(h + 1) + "-" + Number(h + $o.search.limit)
			}
			function e(i){
				var h = Math.floor(i / $o.search.limit) * $o.search.limit;
				return (h >= d.count) ? h-- : h
			}
		},
		tag: function(i, g, f){
			var h = $o.aspect(i, g), j = (160 - i * h) >> 1, d = (120 - g * h) >> 1;
			if (f > 0) {
				var e = "left:" + j + "px;top:" + d + "px;"
			}
			else {
				var e = "margin-top:" + d + "px;"
			}
			return "width:" + Math.ceil(i * h) + "px;height:" + Math.ceil(g * h) + "px;" + e
		},
		aspect: function(f, d){
			var e = Math.min((160 / f), (120 / d));
			e = Math.min(e, 1);
			return e;
		},
		dF:function(c){
			$o.favorite("delete",c,function(){
				$o.initSearch();
				$o.receiveSearch($o.search);
			});
		}
	});
});
