/*
	onload events trigger
***************************************************/

	$j(document).ready(function(){
		setPerPageInit();
		fakeInputChkInit();
		collectionItemArrowLinkInit();
		sidebarChangeButtonInit();
		togglePartsVisInit();
		chgColTypeInit();
		chgItemAmountInit();
		getSocialBookmarkInfo();
		itemDeleteConfirmInit();
	});
	
/*
	item delete confirm init
***************************************************/
	
	/*
		init
	***************************************************/
	
		function itemDeleteConfirmInit(){
			$j("#mainArea-body div.collectionItems td.item dd.editMenu li.delete a").click(function(){
				if(window.confirm("本当に削除しますか？")){
					var url=$j(this).attr("href");
					window.open(url,"deletePop","width=300,height=130,location=no,menubar=no,status=no,toolbar=no");
				}
				return false;
			});
		}
	
	
/*
	searchSubmitCheckInit
***************************************************/

	/*
		init
	***************************************************/
	
		function chgItemAmountInit(){
			var cookieStatus_itemPerPage = $j.cookie("itemPP");
			var itemPerPage = parseInt(cookieStatus_itemPerPage);
			if(itemPerPage==48){
				$j("td#itemAmountStatus48").attr("class","current");
				chgItemAmountButtonBind24();
				chgItemAmountButtonBind12()
			}else if(itemPerPage==24){
				$j("td#itemAmountStatus24").attr("class","current");
				chgItemAmountButtonBind48();
				chgItemAmountButtonBind12()
			}else{
				$j("td#itemAmountStatus12").attr("class","current");
				chgItemAmountButtonBind48();
				chgItemAmountButtonBind24();
			}
		}
	/*
		bind button click
	***************************************************/
		
		function chgItemAmountButtonBind12(){
			$j("td#itemAmountStatus12 span").click(function(){
				$j.cookie("itemPP","12",{expires:60});
				chgLocationWithItemPPChange(12);
			});
		}
		function chgItemAmountButtonBind24(){
			$j("td#itemAmountStatus24 span").click(function(){
				$j.cookie("itemPP","24",{expires:60});
				chgLocationWithItemPPChange(24);
			});
		}
		function chgItemAmountButtonBind48(){
			$j("td#itemAmountStatus48 span").click(function(){
				$j.cookie("itemPP","48",{expires:60});
				chgLocationWithItemPPChange(48);
			});
		}
		
	/*
		change location with itemPP change
	***************************************************/
	
		function chgLocationWithItemPPChange(newAmount){
			var currentLocation = location.href;
			currentLocation = currentLocation.replace(/#[0-9a-zA-Z-]*$/,"");
			var result = currentLocation.search(/itemPP=[0-9]+/);
			if(result>0){
				var newLocation = currentLocation.replace(/itemPP=[0-9]+/,"itemPP="+newAmount);
			}else if(currentLocation.search(/\?$/)>0){
				var newLocation = currentLocation+"itemPP="+newAmount;
			}else if(currentLocation.search(/\?[0-9a-zA-Z-]+=[0-9a-zA-Z-]*$/)>0){
				var newLocation = currentLocation+"&itemPP="+newAmount;
			}else{
				var newLocation = currentLocation+"?itemPP="+newAmount;
			}
			location.replace(newLocation);
		}
	
	
/*
	set search results value
***************************************************/

	/*
		init
	***************************************************/
	
		function setPerPageInit(){
			var cookieStatus_itemPerPage = $j.cookie("itemPP");
			var itemPerPage = parseInt(cookieStatus_itemPerPage);
			if(itemPerPage>0){
				var options = $j("#sItemPerPage option");
				var optionAmount = options.length;
				for(i=0;i<optionAmount;i++){
					if($j(options[i]).attr("value")==itemPerPage){
						$j(options[i]).attr("selected","selected");
						for(j=0;j<optionAmount;j++){
								if(j!=i){$j(options[j]).attr("selected","");
							}
						}
						break;
					}
				}
			}
		}
	
/*
	get social bookmark user info
***************************************************/

	/*
		init
		- load following bookmark info
		   hatena/livedoor clip
	***************************************************/

		function getSocialBookmarkInfo(){
			getSocialBookmarkInfo_hatena();
			getSocialBookmarkInfo_livedoor();
		}
	
	/*
		hatena
		- get hatena bookmark's count image and put it on server by php
	***************************************************/
	
		function getSocialBookmarkInfo_hatena(){
			var siteLogoImgSrc = userRootDir+"/common/imgs/icons/socialbookmark/hatena.gif";
			$j("#mainArea-body dd.list-social li.hatena").each(function(){
				var entryUrl = $j(this).find("a").eq(0).attr("href").replace(/^http:\/\/b.hatena.ne.jp\/entry\//,"");
				var entryId = $j(this).parents("td.item").eq(0).attr("id").replace("item","");
				var phpUrlToGetImg = userRootDir+"/common_modules/socialbookmarkimgloader/hatena.php?id="+entryId+"&url="+entryUrl;
				var imgSrcOnServer = userRootDir+"/socialbookmarkimgs/hatena/"+entryId+".png";
				
				$j.ajax({
					url:phpUrlToGetImg,
					success: function(msg){
						
						var returnedValue = msg.split(",");
						var imgW = parseInt(returnedValue[0]);
						var currentId = returnedValue[1];
						
						$socialBookmarkContainer = $j("#"+currentId+" dd.list-social li.hatena");
						
						if(imgW>1){
							
							// if the result was not NULL
								
								$socialBookmarkContainer.find("img.siteLogo").eq(0).attr("src",siteLogoImgSrc);
								$socialBookmarkContainer.find("img.regNumberImg").eq(0).attr("src",imgSrcOnServer);
							
						}else if(imgW==1){
							
							// if the result was NULL
							
								$socialBookmarkContainer.css("visibility","hidden");
							
						}else{
							
							// if php got error
							
								$socialBookmarkContainer.html("php got error");
								
						}
						
					}
				});
			});
		}
		
	
	/*
		livedoor
		- get livedoor bookmark's comnt image and put it on server by php
	***************************************************/
	
		function getSocialBookmarkInfo_livedoor(){
			var siteLogoImgSrc = userRootDir+"/common/imgs/icons/socialbookmark/livedoor.gif";
			$j("#mainArea-body dd.list-social li.livedoor").each(function(){
				var entryUrl = $j(this).find("a").eq(0).attr("href").replace(/^http:\/\/clip.livedoor.com\/page\//,"");
				var entryId = $j(this).parents("td.item").eq(0).attr("id").replace("item","");
				var phpUrlToGetImg = userRootDir+"/common_modules/socialbookmarkimgloader/livedoor.php?id="+entryId+"&url="+entryUrl;
				var imgSrcOnServer = userRootDir+"/socialbookmarkimgs/livedoor/"+entryId+".png";
				
				$j.ajax({
					url:phpUrlToGetImg,
					success: function(msg){
						
						var returnedValue = msg.split(",");
						var imgW = parseInt(returnedValue[0]);
						var currentId = returnedValue[1];
						
						$socialBookmarkContainer = $j("#"+currentId+" dd.list-social li.livedoor");
						
						if(imgW>1){
							
							// if the result was not NULL
								
								$socialBookmarkContainer.find("img.siteLogo").eq(0).attr("src",siteLogoImgSrc);
								$socialBookmarkContainer.find("img.regNumberImg").eq(0).attr("src",imgSrcOnServer);
							
						}else if(imgW==1){
							
							// if the result was NULL
							
								$socialBookmarkContainer.css("visibility","hidden");
							
						}else{
							
							// if php got error
							
								$socialBookmarkContainer.html("php got error");
								
						}
						
					}
				});
			});
		}


/*
	toggle column type init
	- control column type of collectionItems
***************************************************/

	/*
		load cookie and set visibility
		- executed before onload
		- column type change will be executed in PHP
	***************************************************/
	
		function setColTypeBeforeLoad(){
			
			var cookieStatus_colType = $j.cookie("colType");

			if(cookieStatus_colType==2){
				$j(function(){
					chgColTypeButton($j("span#selectColType-2"));
				})
			}else if(cookieStatus_colType==3){
				$j(function(){
					chgColTypeButton($j("span#selectColType-3"));
				})
			}else if(cookieStatus_colType==4){
				$j(function(){
					chgColTypeButton($j("span#selectColType-4"));
				})
			}else{
				$j(function(){
					chgColTypeButton($j("span#selectColType-2"));
				});
			}
		}
		setColTypeBeforeLoad();

	/*
		init
		- bind button click
		- should be executed when onload
	***************************************************/
		
		function chgColTypeInit(){
		
			$j("span#selectColType-2").click(function(){
				chgColTypeButton($j(this));
				setCookie_colType("2");
				location.reload();
			});
			$j("span#selectColType-3").click(function(){
				chgColTypeButton($j(this));
				setCookie_colType("3");
				location.reload();
			});
			$j("span#selectColType-4").click(function(){
				chgColTypeButton($j(this));
				setCookie_colType("4");
				location.reload();
			});
		}
		
	/*
		switch image
		- switch image path "on" "off"
		- executed when clicked
	***************************************************/
		
		function chgColTypeButton($elem){
			$j("td#selectColumnType ul li span").each(function(){
				if($j(this).attr("id")!=$elem.attr("id")){
					var imgSrc = $j(this).find("img").eq(0).attr("src");
					imgSrc = imgSrc.replace("/on/","/off/");
					$j(this).find("img").eq(0).attr("src",imgSrc);
				}
			});
			
			var imgSrc = $elem.find("img").eq(0).attr("src");
			imgSrc = imgSrc.replace("/off/","/on/");
			$elem.find("img").eq(0).attr("src",imgSrc);
		}
		
	/*
		set cookie
		- set the colType status as cookie
		- executed when clicked
	***************************************************/
	
		function setCookie_colType(colType){
			$j.cookie("colType",colType,{expires:60});
		}
	


/*
	toggle parts visiblity init
	- control visibility of collectionItems
***************************************************/

	/*
		load cookie and set visibility
		- load cookie and set visibility
		- this writes style decralation directly
		- executed before onload
	***************************************************/
	
		function setPartsVisBeforeLoad(){
			document.write("<style type=\"text/css\">");
			
			var cookieStatus_partsVisComment = $j.cookie("partsVisComment");
			var cookieStatus_partsVisTags = $j.cookie("partsVisTags");
			var cookieStatus_partsVisSocial = $j.cookie("partsVisSocial");

			if(cookieStatus_partsVisComment==0){
				document.write("div.collectionItems td.item dd.comment{display:none;}");
				$j(function(){
					togglePartsVisImgSwith($j("span#togglePartsVis-comment"));
					togglePartsVisClassSwith($j("span#togglePartsVis-comment"));
				});
			}
			if(cookieStatus_partsVisTags==0){
				document.write("div.collectionItems td.item dd.list-tags{display:none;}");
				$j(function(){
					togglePartsVisImgSwith($j("span#togglePartsVis-tags"));
					togglePartsVisClassSwith($j("span#togglePartsVis-tags"));
				});
			}
			if(cookieStatus_partsVisSocial==0){
				document.write("div.collectionItems td.item dd.list-social{display:none;}");
				$j(function(){
					togglePartsVisImgSwith($j("span#togglePartsVis-social"));
					togglePartsVisClassSwith($j("span#togglePartsVis-social"));
				});
			}
			document.write("</style>");
		}
		setPartsVisBeforeLoad();

	/*
		init
		- bind button click
		- load cookie and set status
		- should be executed when onload
	***************************************************/
		
		function togglePartsVisInit(){
		
			/* comment */
		
				$j("span#togglePartsVis-comment").click(function(){
					$j("div#mainArea-body div.collectionItems td.item dd.comment").slideToggle("fast");
					togglePartsVisImgSwith($j(this));
					var status = togglePartsVisClassSwith($j(this));
					setCookie_partsVis_comment(status);
				});
			
			/* tags */
			
				$j("span#togglePartsVis-tags").click(function(){
					$j("div#mainArea-body div.collectionItems td.item dd.list-tags").slideToggle("fast");
					togglePartsVisImgSwith($j(this));
					var status = togglePartsVisClassSwith($j(this));
					setCookie_partsVis_tags(status);
				});
			
			/* social bookmark */
			
				$j("span#togglePartsVis-social").click(function(){
					$j("div#mainArea-body div.collectionItems td.item dd.list-social").toggle();
					togglePartsVisImgSwith($j(this));
					var status = togglePartsVisClassSwith($j(this));
					setCookie_partsVis_social(status);
				});
		}
		
		
	/*
		switch class
		- switch class name "on" "off"
		- return class status "on"/"off"
		- executed when clicked
	***************************************************/
	
		function togglePartsVisClassSwith($elem){
			if($elem.attr("class") == "on"){
				$elem.attr("class","off");
				var status = "off";
			}else{
				$elem.attr("class","on");
				var status = "on";
			}
			return status;
		}
		
	/*
		switch image
		- switch image path "on" "off"
		- executed when clicked
	***************************************************/
		
		function togglePartsVisImgSwith($elem){
			var imgSrc = $elem.find("img").eq(0).attr("src");
			if(imgSrc.search("/off/")!=-1){
				imgSrc = imgSrc.replace("/off/","/on/");
			}else{
				imgSrc = imgSrc.replace("/on/","/off/");
			}
			$elem.find("img").eq(0).attr("src",imgSrc);
		}
		
	/*
		set cookie
		- set the visibility status as cookie
		- executed when clicked
	***************************************************/
	
		function setCookie_partsVis_comment(status){
			if(status == "on"){
				$j.cookie("partsVisComment","1",{expires:60});
			}else{
				$j.cookie("partsVisComment","0",{expires:60});
			}
		}
	
		function setCookie_partsVis_tags(status){
			if(status == "on"){
				$j.cookie("partsVisTags","1",{expires:60});
			}else{
				$j.cookie("partsVisTags","0",{expires:60});
			}
		}
	
		function setCookie_partsVis_social(status){
			if(status == "on"){
				$j.cookie("partsVisSocial","1",{expires:60});
			}else{
				$j.cookie("partsVisSocial","0",{expires:60});
			}
		}
	

/*
	side bar width change
***************************************************/

	/*
		load cookie and set width
		- load cookie and set width
		- this writes style decralation directly
		- executed before onload
	***************************************************/
	
		function sidebarExpandBeforeLoad(){
			var cookieStatus_sidebarExpand = $j.cookie("sidebarExpand");
			document.write("<style type=\"text/css\">");
			if(cookieStatus_sidebarExpand==1){
				document.write("#sidenavAreaWidthKeeper {width:240px}");
				document.write("td#sidenavArea dl dd ul li {float:left;}");
				sidebarCookieExpanded = true;
				isFirstExpandWasDone = false;
			}else{
				sidebarCookieExpanded = false;
				isFirstExpandWasDone = true;
			}
			document.write("</style>");
		}
		sidebarExpandBeforeLoad();

	/*
		init
		- should be executed when onload and when button was changed
	***************************************************/
	
		function sidebarChangeButtonInit(){
			
			// if sidebar would be expanded by cookie
			// this will be executed only once
			if(sidebarCookieExpanded && !isFirstExpandWasDone){
				sidebarButtonSwithToShrink();
				isFirstExpandWasDone = true;
			}
			
			$j("#sidebarWidthExpand").unbind("click");
			$j("#sidebarWidthExpand").click(function(){
				$j("#sidenavAreaWidthKeeper").width($j('#sidenavAreaWidthKeeper').width());
				$j("td#sidenavArea dl dd ul li").css("float","left");
				$j("#sidenavAreaWidthKeeper").animate(
					{width:"240px"},"fast",function(){
						sidebarButtonSwith();
						$j.cookie("sidebarExpand","1",{expires:60});
					}
				);
			});
			$j("#sidebarWidthShrink").unbind("click");
			$j("#sidebarWidthShrink").click(function(){
				$j("td#sidenavArea dl dd ul li").css("float","none");
				setTimeout(function(){$j("#sidenavAreaWidthKeeper").animate(
					{width:"1px"},"fast",function(){
						sidebarButtonSwith();
						$j.cookie("sidebarExpand","0",{expires:60});
					}
				)},100);
			});
		}

	/*
		button swith
		- should be executed when any button was clicked
		- called in sidebarChangeButtonInit()
	***************************************************/
	
		function sidebarButtonSwith(){
			if(document.getElementById("sidebarWidthExpand")){
				sidebarButtonSwithToShrink();
			}else if(document.getElementById("sidebarWidthShrink")){
				sidebarButtonSwithToExpand();
			}
			sidebarChangeButtonInit();
		}
		
		function sidebarButtonSwithToShrink(){
			$j("#sidebarWidthExpand img")
				.attr("src",userRootDir+"/common/imgs/module_parts/parts_sidebarchgfunc/shrink.gif")
				.attr("alt","サイドバーを縮める");
			$j("#sidebarWidthExpand").attr("id","sidebarWidthShrink");
		}
		function sidebarButtonSwithToExpand(){
			$j("#sidebarWidthShrink img")
				.attr("src",userRootDir+"/common/imgs/module_parts/parts_sidebarchgfunc/expand.gif")
				.attr("alt","サイドバーを広げる");
			$j("#sidebarWidthShrink").attr("id","sidebarWidthExpand");
		}

/*
	bind arrow click as title link
***************************************************/

	/*
		init
		- should be executed when onload
	***************************************************/
	
	function collectionItemArrowLinkInit(){
		$j("div#mainArea-body div.collectionItems td.item dt.title span").each(function(){
			var linkURL = $j(this).siblings("a").eq(0).attr("href");
			$j(this).html("<a href=\""+linkURL+"\">&nbsp;</a>")
		});
	}

/*
	fake input check
***************************************************/

	/*
		init
		- should be executed when onload
	***************************************************/

		function fakeInputChkInit(){
		
			// load cookie and set last status
				
				var lastSearchType = $j.cookie("lastSearchType");
				if(lastSearchType=="all"){
					$j("#sInAllDatav").attr("checked","checked");
				}else if(lastSearchType=="tagName"){
					$j("#sInTag").attr("checked","checked");
				}else if(lastSearchType=="TandC"){
					$j("#sInTitleComment").attr("checked","checked");
				}else if(lastSearchType=="URL"){
					$j("#sInUrl").attr("checked","checked");
				}
				
				var lastSearchType = $j.cookie("lastSearchByOnlyStar");
				if(lastSearchType=="1"){
					$j("#starOnly").attr("checked","checked");
				}else{
					$j("#starOnly").attr("checked","");
				}
		
			// bind click event
			
			$j("tr#searchFunctionOptions span").click(function(){
				switchCheck($j(this).find("input").eq(0));
			});
			
			// check inputs' satus and set class for bg
			
			$j("tr#searchFunctionOptions span input").each(function(){
				var inputType = $j(this).attr("type");
				if($j(this).attr("checked")==true){
					if(inputType=="radio"){
						$j(this).parents("span").eq(0).attr("class","radioOn");
					}else if(inputType=="checkbox"){
						$j(this).parents("span").eq(0).attr("class","checkOn");
					}
				}else{
					if(inputType=="radio"){
						$j(this).parents("span").eq(0).attr("class","radioOff");
					}else if(inputType=="checkbox"){
						$j(this).parents("span").eq(0).attr("class","checkOff");
					}
				}
			});
		}
	
	/*
		switch check
		- this function will switch the input status
		- called in fakeInputChkInit()
	***************************************************/
	
		function switchCheck($elem){
			var inputType = $elem.attr("type");
			if(inputType=="radio"){
				var inputName = $elem.attr("name");
				var selector = "input[name="+inputName+"]";
				$j(selector).attr("checked","");
				$j(selector).each(function(){
					$j(this).parents("span").eq(0).attr("class","radioOff");
				});
				$elem.attr("checked","checked");
				$elem.parents("span").eq(0).attr("class","radioOn");
			}else if(inputType=="checkbox"){
				if($elem.attr("checked")==true){
					$elem.attr("checked","");
					$elem.parents("span").eq(0).attr("class","checkOff");
				}else{
					$elem.attr("checked","checked");
					$elem.parents("span").eq(0).attr("class","checkOn");
				}
			}
		}
	