/*
* setting
*/

$(function(){
	
	initRollOverAlpha();
	initRollOverImages();
	
	
	if($('#top-banner').html()){
		$('#top-banner').Horinaja({
		capture:'top-banner',delai:0.5,
		duree:4,pagination:true});
	}
	$('.ord-all-btn').click(function(ev){
		req = $('#ord_all').val();
		add_order(req,'all',ev);
		$('.ord_btn').parent().html('<a href="'+setting.base_url+'order"><img src="'+setting.base_url+'images/buttons/add_order_fin.gif" /></a>');

		$(this).attr('class','ord-all-btn_fin');
		$('.all-fin').html('追加済');
		obj=$('.ord-all-btn_fin').html();
		$('.ord-all-btn_fin').html('<a href="'+setting.base_url+'order">'+obj+'</a>');
	});

	$('.ord_btn').click(function(ev){
		req = $(this).attr('id');
		add_order(req,'c_id',ev);
		if($(this).attr('class')=='ord_btn'){
			var imgsrc = this.src;
			var dot = this.src.lastIndexOf('.');
			var imgsrc_on = this.src.substr(0, dot) + '_fin' + this.src.substr(dot, 4); // finのボタンに置き換え
			$(this).parent().html('<a href="'+setting.base_url+'order"><img src="'+imgsrc_on+'" /></a>');
		}
	});


	$('.ord_btn_detail').click(function(ev){
		req = $(this).attr('id');
		add_order(req,'c_id',ev);
		if($(this).attr('class')=='ord_btn_detail'){
			var imgsrc = this.src;
			var dot = this.src.lastIndexOf('.');
			var imgsrc_on = this.src.substr(0, dot) + '_fin' + this.src.substr(dot, 4); // finのボタンに置き換え
		$('.ord_btn_detail').parent().html('<a href="'+setting.base_url+'order"><img src="'+imgsrc_on+'" /></a>');
		}
	});

if ($.isFunction($.fn.lazyload)){
	if($(".col-s img").attr('src')){$(".col-s img").lazyload({placeholder:"images/gr.gif"});}
	if($(".col-xl img").attr('src')){$(".col-xl img").lazyload({placeholder:"images/gr.gif"});}
	if($(".col-xs img").attr('src')){$(".col-xs img").lazyload({placeholder:"images/gr.gif"});}
	if($("#sbox img").attr('src')){$("#sbox img").lazyload({placeholder:"images/gr.gif"});}
	if($("#top-special img").attr('src')){$("#top-special img").lazyload({placeholder:"images/gr.gif"});}
	if($("#order_checklist img").attr('src')){$("#order_checklist img").lazyload({placeholder:"images/gr.gif"});}
}

	$('.ord_btn_s').click(function(ev){
		req = $(this).attr('id');
		add_order(req,'c_id',ev);
		if($(this).attr('class')=='ord_btn_s'){
			$(this).attr('src',setting.base_url+'images/buttons/add_order_s_fin.gif');
			$(this).wrap('<a href="'+setting.base_url+'order">');
//			$(this).parent().append('<br /><span class="ord-mess">資料請求リストに追加しました。</span>');
			myTimter=setTimeout("messLoss(400)",600);
			$(this).attr('class','');
		}
	});

	$('.order-cancell').click(function(ev){
		var cancell_id=$(this).parent().attr('id');
		$(this).parent().css('background-color','#ddd');
		$.ajax({
				type:'POST',
				url:setting.base_url+'ajax/order_cancell/'+cancell_id,
				data:'cancell_id='+cancell_id,
				success:function(data){
					$('#'+cancell_id).remove();
					$(".ord-cnt").html(data);
				}
		});
	});

/*
	$('.order-cancell').click(function(ev){
		$(this).parent().css('background-color','#AAA');
		var cancell_id=$(this).parent().attr('id');
		alert(cancell_id);
		if($(".ord-cnt").load(setting.base_url+'ajax/order_cancell/'+cancell_id)){
			$('#'+cancell_id).remove();
		}
	});
*/

	$.get(setting.base_url+'ajax/order_count/',function(data){
		$(".ord_cnt").html(data);
	});

	$('#login-btn').click(function(){
		var email = $('#login_email').val();
		var password = $('#login_pass').val();
		$.ajax(
			{
				type:'POST',
				url:setting.base_url+'ajax/login',
				data: {"email":email,"password":password},
				success:function(user){
					if(user!=0){
						location.replace(user);
					}else{
						$('#login_email, #login_pass').css('background-color','#FFCFCF');
						$(".login-box").prepend('<span id="login-error">入力情報が正しくありません。</span>');
						setTimeout(function(){
							$("#login-error").fadeOut("slow",
								function(){$('#login_email, #login_pass').css('background-color','#FFFFFF');}
							)
						},1000);
					}
				}
			}
		);
	});


	$('.sn-more-trigger').click(function(){
		var more = $(this).prev();
		more.toggle();
	});
});




// XY座標取得
function clickhandler(event) {

        if (!event) { event = window.event; }
        var hx, hy ,wx,wy;
     	hx = event.pageX;
		hy = event.pageY;
		wx = $(window).width();
		wy = $(window).height();
		
//        alert("x:"+hx+",y:"+hy);
        return hx+':'+hy+'-'+wx+':'+wy;
}




function messLoss(t){
	$(".ord-mess").fadeOut(t);
}

/* 未使用？*/
function ajax_cal(link){
	$.ajax({dataType:'text',
		url:setting.base_url+link,
		success:function(data){
			$("#ajax_cal").html(data);
		}
	});
}



function add_order(req,type,ev){
	var xy;
	if(ev){
		xy=clickhandler(ev);
	}
	if(type=='all'){
		$.ajax({
			type:"POST",
			url:setting.base_url+'ajax/add_all/',
			data:"req="+req+'&xy='+xy,
			success:function(cnt){
				$(".ord-cnt").html(cnt);
			}
		});
	}else{
		$(".ord-cnt").load(setting.base_url+'ajax/add_chain/'+req+'/'+xy);
	}
}


function initRollOverImages() {
	var image_cache = new Object();
	$(".rollover img").not("[src*='_on.']").each(function(i) {
		var imgsrc = this.src;
		var dot = this.src.lastIndexOf('.');
		var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
		image_cache[this.src] = new Image();
		image_cache[this.src].src = imgsrc_on;
		$(this).hover(
			function() { this.src = imgsrc_on; },
			function() { this.src = imgsrc; }
		);
	});
}

function initRollOverAlpha(){
	$(".rollover2 img").hover(
		function(){
			$(this).css("opacity","0.85");
		},
		function(){
			$(this).css("opacity","1");
		}
	);
}

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

//子WINDOWから呼び出す請求カウント更新

function ajcancell(data){
	$('.ord-cnt').html(data);
}

