// JavaScript Document

//共通初期設定
$(function () {
	//「とりあいずストックした求人」
	$(".stock div.inside ul li:last").addClass('last');
	$(".stock div.inside ul li:odd").addClass('odd');
	
	/* 角丸 */
	//サイド
	$('#Side .stock').corner('top 5px');
	$('#Side .mobile').corner('top 5px');
	$('#Side .useful').corner('top 5px');
	$('#Side .week-new').corner('top 5px');
	$('#Side .pickup').corner('top 5px');
	
	//「ストックした求人」
	if($('#Side .stock ul').hasClass('list')){
		$('#Side .stock').pager('ul.list',{
			navClass: 'pager',
			prevText: 'images/common/ico_prev.gif',
			nextText: 'images/common/ico_next.gif'
		});
	}
	
	//カウンタ
	if($('.update p').hasClass('count')){
		$('.update p.count').flightboard({
			messages: [$('.update p.count').text()],
			afterFlip: endedFlip
		});
	}
	
	function endedFlip(){
		$('.update p.count').flightboard('stop');
	}
	
	//ポップアップ
	if($("a").hasClass("openwindow")){
		$(".openwindow").popupwindow();
	}
});

//情報提供のマウスオーバー
function offer_hover(Target,AddTag){
	var Add = '<'+AddTag+' style="display:none;"></'+AddTag+'>';
	
	$(Target).find('img').after(Add);
	$(Target).hover(function(){
		$(this).find(AddTag).fadeIn().fadeTo("fast",0.7);
	},function(){
		$(this).find(AddTag).fadeTo("fast",0);
	});
}


//scrollable用のスピード設定
$.easing.custom = function (x, t, b, c, d) {
var s = 1.70158; 
	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}

//タブの画像入れ替え
function TabsChenge(TAB,TARGET){
	$(TAB).css("cursor","pointer");
	$(TAB).click(function(){
		//初期状態に戻す
		$(TAB).each(function(){
			$(this).removeClass("active");
			if($(this).find("img").is(".rover")){
				$(this).find("img").attr("src",$(this).find("img").attr("src").replace("_on",""));
			}
			$(TARGET+'#'+this.className).hide();
		});
		
		//タブの切り替え
		$(this).each(function(){
			$(this).addClass('active');
			if($(this).find("img").is(".rover")){
				$(this).find("img").attr("src",$(this).find("img").attr("src").replace(".gif","_on.gif"));
			}
		});
		
		//TARGETを表示
		var CLASS = this.className.split(" ");
		$(TARGET+'#'+CLASS[0]).show();
	});
	//$(TAB+":first").click();
}

//ナビ検索の検索内容取得
var navi_str = new Array();
function NaviSearch(TAG, reset){
	if( reset ){
		$(TAG+' input:checkbox').attr('checked',false);
	}
	$(TAG+' input:checkbox').click(function(){
		var CLASS = this.className;
		var n = $('#'+CLASS+' input:checked').length;
		if($(this).is(':checked')){
			if(n == '1'){
				navi_str[CLASS] = '';
				navi_str[CLASS] += '、'+this.title;
			}else{
				navi_str[CLASS] += '、'+this.title;
			}
		}else{
			if(n == '0'){
				navi_str[CLASS] = '、指定なし';
			}else{
				navi_str[CLASS] = navi_str[CLASS].replace('、'+this.title,'');
			}
		}
		$('.'+CLASS+'-selection').text(navi_str[CLASS].replace(/^、/,''));

		// 雇用形態・職種が変更されたら勤務地の件数を更新する
		if(this.className == 'step01' || this.className == 'step02'){
			$.get('/jobs/count_each_area?' + $('#JobIndexForm').serialize(), function(data){
				var job_counts = eval(data);
				$.each(job_counts, function(){
					var contents = '';
					$.each(this.areas, function(){
						$('#step03-'+ this.id +'-count').html(this.job_count +'件');
						if( this.job_count == 0 ){
							$('#step03-'+ this.id +'-li').toggleClass('disabled', true);
							$('#step03-'+ this.id +'-checkbox').attr('disabled', 'disabled');
						}else{
							$('#step03-'+ this.id +'-li').toggleClass('disabled', false);
							$('#step03-'+ this.id +'-checkbox').removeAttr('disabled');
						}
					});
				});
			});
		}
	});
}

function setNaviStr(key, str){
	navi_str[key] = str;
}

//GoogleMap
// グローバル変数の宣言
var geocoder;
var map;

function initialize(ADDRESS,ID){
	var myOptions = {
		zoom: 18,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var marker = null;
	var geocoder = new google.maps.Geocoder();
	var map = new google.maps.Map(document.getElementById(ID), myOptions);
	
	if (geocoder) {
		geocoder.geocode({'address': ADDRESS}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
				marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location
				});
			} else {
				alert("Geocode was not successfull for the following reason: " + status);
			}
		});
	}
}

this.imagePreview = function(){	
		xOffset = -10;
		yOffset = 10;
		
	$(".preview").hover(function(e){
		this.t = $(this).attr("alt");
		var c = (this.t != "") ? "<br/>" + this.t : "";
		
		var dot = this.src.lastIndexOf('.');
//		var THISHREF = this.src.substr(0, dot) + '_b' + this.src.substr(dot, 4);
		var THISHREF = this.src;
		
		var image_width = 0;
		if( this.className.match(/iw(\d*)/) ){
			image_width = RegExp.$1;
		}
		var image_height = 0;
		if( this.className.match(/ih(\d*)/) ){
			image_height = RegExp.$1;
		}
		var add_attr = '';
		if( image_width > 400 || image_height > 400 ){
			if( image_width > image_height ){
				add_attr = 'width="400"';
			}else{
				add_attr = 'height="400"';
			}
		}
		
		$("body").append("<p id='preview'><img src='"+ THISHREF +"' alt='Image preview' "+ add_attr +" />"+ c +"</p>");
		
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });	
	$(".preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};


$(document).ready(function(){imagePreview()});

