//$(document).ready(function(){

	//alert($("img:first").width());
	
	//var img = $("#ranking img:first");
	/*
	jQuery.each($("#ranking img.thumbnail"), function (idx) {
		
		var img = jQuery(this);
		
	
		if (img.width() > 70) {
			img.attr("width", "70");
		}
		if (img.height() > 70) {
			img.attr("width", "70");
		}
		
	});
	*/
	
//});


function getAgent() {
	var ua = navigator.userAgent;
	var strBrowser;
	
	if       (ua.indexOf("MSIE") >= 0)         strBrowser = "IE";
	else if (ua.indexOf("Netscape") >= 0) strBrowser = "NN";
	else if (ua.indexOf("Safari") >= 0)      strBrowser = "Safari";
	else if (ua.indexOf("Opera") >= 0)      strBrowser = "Opera";
	else if (ua.indexOf("Firefox") >= 0)    strBrowser = "Firefox";
	else if (ua.indexOf("Gecko") >= 0)      strBrowser = "Mozilla";
	else if (ua.indexOf("Mozilla/4") >= 0)   strBrowser = "NN";
	else    strBrowser = "";	
	
	return strBrowser;

}


/**************************
** ajax待ち画像表示
***************************/
function showAjaxWait() {
	
	var left = Math.floor(($(window).outerWidth() - $("#ajax_wait").width()) / 2)+ $(window).scrollLeft();   
 	var top  = Math.floor(($(window).outerHeight() - $("#ajax_wait").height()) / 2) + $(window).scrollTop();  
	
	$('#ajax_wait').css({'top':top, 'left':left});	
	$('#ajax_wait').show();
}

/**************************
** ajax待ち画像非表示
***************************/
function hideAjaxWait() {
//alert('hide');
	$('#ajax_wait').hide();
	//window.setTimeout("$('#ajax_wait').hide()", 100);
}

/**************************
** GETリクエスト用確認ポップアップ
***************************/
function getRequestConfirm(message, url) {

    if(confirm(message)){
        // YES選択
        
	      // ボタンを無効に
	      disableAllButton();
	      
	      // IEのバグ？対策
	      window.setTimeout('window.location.href="'+url+'"', 0);
	       
    }
}

/**************************
** GETリクエスト用確認ポップアップ ajax load版
***************************/
function getRequestConfirmLoad(message, url, id) {

    if(confirm(message)){
			showAjaxWait();
      // YES選択
			$('#'+id).load( url);   
    }
}

function getRequestLoad(url, id, func) {
			$('#'+id).load( url, func);   
}

/**************************
** POSTリクエスト用確認ポップアップ
** inputはフォーム名、もしくはフォームそのもの
***************************/
function postRequestConfirm(message, input) {

		if (input == null) {
		
			alert('エラー：フォームがありません');
			
		} else {
	
			var form = null;
	
			if (input.action == null) {
				// フォーム名と認識してフォームを取得
				form = window.document.forms[input];
				
			} else {
				// 入力をそのままフォームとする
				form = input;
			}
						
	    if(confirm(message)){
	        // YES選択
	        
	        // ボタンを無効に
	        disableAllButton();
	        
	        // ビフォアアンロード無効
	        $(window).unbind('beforeunload');
	        
	        form.submit();
	    }
    }
}

/**************************
** POSTリクエスト用確認ポップアップ ajax load版
***************************/
function postRequestConfirmLoad(message, input, id) {

		if (input == null) {
		
			alert('エラー：フォームがありません');
			
		} else {
							
			var form = null;
	
			if (input.action == null) {
				// フォーム名と認識してフォームを取得
				form = window.document.forms[input];
				
			} else {
				// 入力をそのままフォームとする
				form = input;
			}
			
			form = new jQuery(form);
			
	    if(confirm(message)){
	    
					showAjaxWait();
	        // YES選択	        
					$('#'+id).load( form.attr("action")+'?'+form.serialize()+'&'+ new Date().getTime());
					//$('#'+id).load( form.attr("action") , form.serialize());
	    }
    }
}

/**************************
** 削除チェック時にファイルボタンを無効にする
***************************/
function checkFormFiles(formName, index) {

	if (window.document.forms[formName].elements['deleteFlags['+index+']'].checked==true) {
	
		window.document.forms[formName].elements['image'+index].disabled = true;
		
	} else {
	
		window.document.forms[formName].elements['image'+index].disabled = false;
	}

}

function addList(formName, mainListName, otherListName ,textName) {

	var form = window.document.forms[formName];
	var item = replaceTag(form.elements[textName].value);
	var mainList = form.elements[mainListName];
	var otherList = form.elements[otherListName];

	if (item != "") {
	
		if (mainList.length + otherList.length >= 10 ) {
			alert('タグは合計10までです');
		} else {
	
			var exist = false;
		
			for (var i=0;i<mainList.length;i++) {
				if (mainList.options[i].text == item) {
					exist = true;
					break;
				}
			}
			
			for (var i=0;i<otherList.length;i++) {
				if (otherList.options[i].text == item) {
					exist = true;
					break;
				}
			}
			
			if (!exist) {
				mainList.length++;
				mainList.options[mainList.length-1].text = item;
				
				// 隠れ要素に値を設定
				setHiddenTags();

				form.elements[textName].value = "";

			} else {
				alert('そのタグは既にあります');
			}
		}
	}
}

function removeList(formName, listName) {

	var form = window.document.forms[formName];
	var list = form.elements[listName];
	
	var selectedIndex = list.options.selectedIndex;
	
	if (selectedIndex >= 0) {
		list.options[selectedIndex] = null;
		
		// 隠れ要素に値を設定
		setHiddenTags();
		
	}

}

function setHiddenTags() {

	var form = window.document.forms['imageEntryForm'];
	var mainList = form.elements['ownerTags'];
	var otherList = form.elements['publicTags'];
	
//alert('['+form.elements['resultOwnerTags'].value+']');

	// 文字列作成
	var ownerTags = "";
	for (var i=0;i<mainList.length;i++) {
		ownerTags = ownerTags + mainList.options[i].text + " ";
	}
	ownerTags = ownerTags.trim();
	
	// alert('['+ownerTags+']');
	
	var publicTags = "";
	for (var i=0;i<otherList.length;i++) {
		publicTags = publicTags + otherList.options[i].text + " ";
	}
	publicTags = publicTags.trim();
	
	// alert('['+publicTags+']');
	
	// 隠れ要素に値を設定
	form.elements['resultOwnerTags'].value = ownerTags;
	form.elements['resultPublicTags'].value = publicTags;
}

/**************************
** disable all button
***************************/
function disableAllButton() {
	
  for (i=0;i<window.document.forms.length;i++) {
    for (j=0;j<window.document.forms[i].elements.length;j++) {
      var element = window.document.forms[i].elements[j];
      if (element.type == "button" || element.type == "submit") {
        element.disabled = true;
      }
    }
  }
}

/**************************
** String extend method trim
***************************/
String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$|^[　]+|[　]+$/g, '');
}

String.prototype.trimTag = function() {
   //return this.replace(/[　 ]/g, '');
   return this.replace(/[　 !"#|$%&'\(\)\*\+,\-\.\/:;<=>\?@\[\\\]\^_`\{\|\}~]/g, '');
}


String.prototype.whiteSpaceCollapse = function () {
  return this.replace(/ +/g, " ").trim();
}



//文字定義
var half = "0123456789";
half += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
half += "abcdefghijklmnopqrstuvwxyz";
//half += "-+_@., ";
half += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
half += "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァィゥェォッャュョー";
halfArr = new Array("ヴ","ガ","ギ","グ","ゲ","ゴ","ザ","ジ","ズ","ゼ","ゾ","ダ","ヂ","ヅ","デ","ド","バ","ビ","ブ","ベ","ボ","パ","ピ","プ","ペ","ポ");
 
full = "０１２３４５６７８９";
full += "ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺ";
full += "ａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ";
//full += "－＋＿＠．，　";
full += "！”＃＄％＆＇（）＊＋，－．／：；＜＝＞？＠［￥］＾＿｀｛｜｝～";
full += "ｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜｦﾝｧｨｩｪｫｯｬｭｮｰ";
fullArr = new Array("ｳﾞ","ｶﾞ","ｷﾞ","ｸﾞ","ｹﾞ","ｺﾞ","ｻﾞ","ｼﾞ","ｽﾞ","ｾﾞ","ｿﾞ","ﾀﾞ","ﾁﾞ","ﾂﾞ","ﾃﾞ","ﾄﾞ","ﾊﾞ","ﾋﾞ","ﾌﾞ","ﾍﾞ","ﾎﾞ","ﾊﾟ","ﾋﾟ","ﾌﾟ","ﾍﾟ","ﾎﾟ");
  
function changeTag(formName, textName){

  var form = window.document.forms[formName];
  var text = form.elements[textName];
 
  var messIn = text.value;
  
  text.value = replaceTag(text.value);
  
}

function replaceTag(messIn){
 
  var messOut = "";
 
  //半角カナ用
  for(i=0; i<halfArr.length; i++){
    reg = new RegExp(fullArr[i],"g"); 
    messIn = messIn.replace(reg, halfArr[i]);
  }
 
  for(i=0; i<messIn.length; i++){
    oneStr = messIn.charAt(i);
    num = full.indexOf(oneStr,0);
    oneStr = num >= 0 ? half.charAt(num) : oneStr;
    messOut += oneStr;
  }
  
  return messOut.toUpperCase().trimTag();
  
}


function changeDisplay(id) {

	if ($('#'+id).css("display") == "none") {
		$('#'+id).css("display","block");
	} else {
		$('#'+id).css("display","none");
	}

}


function requestHitokoto(targetId, formName) {

	showAjaxWait();
	$('#'+targetId).load( $('#'+formName).attr("action")+'?'+$('#'+formName).serialize());
	
	changeDisplay('hitokoto-tooltip');
	
}

function requestSwapAngle(url) {

	showAjaxWait();
		
	$('#angles').load( url);
	//window.setTimeout("$('#angles').load( '"+url+"', hideAjaxWait())", 0);
		
}


function onTagEdit(before, after) {

	jQuery.each($('#tags .tag_edit_'+after), function (idx, val) {
		new jQuery(val).css('display','inline');
	});
	
	jQuery.each($('#tags .tag_edit_'+before), function (idx, val) {
		new jQuery(val).css('display','none');
	});
	
}

function trimText(id) {
//alert(('|'+'   aa bb   '+'|').replace(/^\s+|\s+$/g, ''));
//alert('|'+jQuery.trim($('#'+id).attr('value')).trim()+'|');
	var value = $('#'+id).attr('value');

	//alert('|'+value.trim()+'|');
	$('#'+id).attr('value', value.trim());
}

/*
function FadeTimer(speed, interval) {
	this.speed = speed;
	this.interval = interval;
	this.count = 0;
}

FadeTimer.prototype.start = function () {

	var speed = this.speed;
	var count = this.count;

	var func = function () {
		$(".photo_frame .photo").each(function (idx ,val){ 
			var div = new jQuery(val);
			if (idx == int) {
				var width = ($(".photo_frame .photo img").get(idx)['width']);
				div.fadeIn(speed);
			} else {
				div.fadeOut(speed);
			}
		 });
		count++;
		
		if ($(".photo_frame .photo").length == count) {
			count=0;
		}
		//alert(count);
	}
	
	func();
	setInterval(func, this.interval);
}

FadeTimer.prototype.execute = function () {
	
	$(".photo_frame .photo").each(function (idx ,val){ 
		var div = new jQuery(val);
		if (idx == int) {
			var width = ($(".photo_frame .photo img").get(idx)['width']);
			div.fadeIn(this.speed);
		} else {
			div.fadeOut(this.speed);
		}
	 });
	this.count++;
	
	if ($(".photo_frame .photo").length == this.count) {
		this.count=0;
	}
}
*/