
// JQuery仕様

(function($){

//* アニメーション速度設定（1000=1秒）  *//	
var animSpeed = 800;

//* Wipe効果（無し：off、横切り替え：horizontal、縦切り替え：vertical）*//	
var wipe = 'off';

//* イージング設定  *//	
var animEasing = 'easeInOutCubic';

//* ディレクトリ名 *//
var dir = 'top_visual';



	
var kf = {};
var visual = {};

var j2=0;
var prevpos = 0;
var xmldataSet;

	/* XML読み込み */
	$(function(){
	
		$.ajax( {
		type : "GET",
		url : dir + "/xml/visual.xml?" + new Date().getTime(),
		dataType : "xml",
		timeout: 1000,
		success : function(data){
		visual.init(data);
		}
	
	});


visual ={
	init : function(data){
	visual.autoFlg = true;
	var imp = $('#kf_impression');
	var mmi = $('#kf_main-images');
	var btns = [];
	var btn = $('<div id="kf_main-btn"><ul></ul></div>').css('display','none');
	
	var baseImg = $('#kf_fstImg');
	visual.imgW = baseImg.width();
	visual.imgH = baseImg.height();
	
	mmi.css({'width':visual.imgW,'height':visual.imgH});
	imp.css({'width':visual.imgW,'height':visual.imgH});
	
	visual.width2_3 = Math.floor(visual.imgW/3)*2;
	
	/* 各XML要素取得 */
	
	var imgset = $(data).find('kf_impression').get();
	
	visual.farstdelay = parseInt(imgset[0].getAttribute('farstDelay'),10)*1000;
    
	visual.loop = imgset[0].getAttribute('loopFlag');
	visual.randomFlg  = imgset[0].getAttribute('random');
	visual.menubtn  = imgset[0].getAttribute('menuBtn').toUpperCase();
	visual.menuType  = imgset[0].getAttribute('menuType');
	visual.defclip = 'rect(0px '+ visual.imgW + 'px ' + visual.imgH  + 'px 0px)';
	

	visual.maxImg = 4;
	
	if(visual.maxImg > $(data).find('img').length){
		var imgcount = $(data).find('img').length;
	}else{
		var imgcount = visual.maxImg;
	}

	var startNum = 0;
	
	var bn = $(data).find('firstImages');
	var pimg = $('<div id="kf_impression_fstImg"><img src="' + dir + '/images/' + bn.attr('src') +'" alt="'+ bn.attr('alt') +'" /></div>').css({'opacity':0});

	imp.append(pimg);
	
	/**/
	visual.count = 0;
	visual.delayArray = [];

	$(data).find('img').each(function(index){
	
		if(index < visual.maxImg) {
		visual.count += 1;

		if(this.getAttribute('href')){
		
		if(this.getAttribute('target')) var targetnum = this.getAttribute('target');
		else var targetnum = '_self';
			
		var pimg = $('<div class="kf_impression_viewImg"><a href="'+this.getAttribute('href')+'" target="'+targetnum+'"><img src="' + dir + '/images/' + this.getAttribute('src') +'" alt="'+ this.getAttribute('alt') +'" /></a></div>');
		
		}else{
		var pimg = $('<div class="kf_impression_viewImg"><img src="' + dir + '/images/' + this.getAttribute('src') +'" alt="'+ this.getAttribute('alt') +'" /></div>');
		}
        
			
		if(this.getAttribute('delay')){
			visual.delayArray.push(parseInt(this.getAttribute('delay'),10)*1000);
		}else{
			visual.delayArray.push(visual.farstdelay);
		}
			
	
		if(index == 0) {
			//pimg.attr('id','kf_fstLoadImg');
			pimg.css({'zIndex':100,'display':'none'});
		}else{
			pimg.css({'zIndex':80,'visibility':'hidden','display':'none'});
		}		
		
		mmi.append(pimg);
		
		if(visual.menubtn != 'OFF'){
		
		if((index+1) < 10) ii = '0' + (index+1);
		else ii = index+1;
		
		if(visual.menuType != 1 && visual.menuType != 2){
		
			if(index==startNum) var litag = $('<li class="kf_stay"><span class="kf_over">&nbsp;</span></li>');
			else var litag = $('<li><span>&nbsp;</span></li>');
		
		}else if(visual.menuType == 1){
		
			if(index==startNum) var litag = $('<li class="kf_stay"><span class="kf_over"><img src = "' + dir + '/images/' + this.getAttribute('src') + '" /></span></li>');
			else var litag = $('<li><span><img src = "' + dir + '/images/' + this.getAttribute('src') + '" /></span></li>');
		
		}else if(visual.menuType == 2){
            
            var mm = (this.getAttribute('src')).substring(0,(this.getAttribute('src')).lastIndexOf('.'));
            
			if(index==startNum) var litag = $('<li class="kf_stay"><span class="kf_over"><img src = "' + dir + '/images/btn_' + mm + '_o.png" /></span></li>');
			else var litag = $('<li><span><img src = "' + dir + '/images/btn_' + mm + '.png" /></span></li>');
		
		}
		
		
		litag.css('visibility','hidden');
		litag.click(visual.elmcl);
		litag.hover(function(){
         $(this).children().addClass('kf_over');
         this.getElementsByTagName('img')[0].src = imgsrc.add(this.getElementsByTagName('img')[0].src,'_o');
        },function(){
         if(this.className != 'kf_stay') {
         $(this).children().removeClass('kf_over');
        this.getElementsByTagName('img')[0].src = imgsrc.del(this.getElementsByTagName('img')[0].src,'_o');
        }
        });
		
		btn.children().append(litag);
		}
		
		}

	});
	
	mmi.after(btn);

	$('#kf_fstImg').remove();

	visual.elm = mmi.find('div.kf_impression_viewImg').get();
	visual.elmbtn = btn.find('span').get();
	
	j2 = startNum;
	
	if(wipe == "horizontal") visual.clip = 'rect(0px '+visual.imgW+'px '+visual.imgH+'px '+visual.imgW+'px)';
	else if(wipe == "vertical") visual.clip = 'rect('+visual.imgH/2+'px '+visual.imgW+'px '+visual.imgH/2+'px '+0+'px)';
	else visual.clip = visual.defclip;
	
	
	
	$('#kf_main-images img').load(function(){

		if(visual.count-- == 1) {
          
			$('#kf_impression_fstImg').css('display','block');
            			$('#kf_impression_fstImg').animate({'opacity':1},800,'linear',function(){
			setTimeout(function(){
            btn.css('display','block');
if(window.attachEvent && !window.opera && typeof document.documentElement.style.msInterpolationMode == 'undefined'){
	btn.find("img").fixPng();
	$("#kf_main-btn").fixPng();  
	}
            visual.elm[0].style.display = 'block';
			$('#kf_impression_fstImg').animate({'opacity':0},800,'linear',function(){
			this.style.display ='none';
			visual.elmbtn2();
			});
			},6000);
			});	
		}
	});
},

elmbtn2 : function(){

var i=0;
var loopfunc = function(){
	if(i>=visual.elmbtn.length) {
	clearInterval(settime);
    
	visual.time = setTimeout(visual.elmani2,visual.farstdelay);
	return;
	}
	$(visual.elmbtn[i]).css({'visibility':'visible'});
//	$(visual.elmbtn[i]).animate({"opacity":1},500,'linear',function(){   });
	
	i++;
}

var settime = setInterval(loopfunc,100);
},


elmani2 : function(){
	
	if(visual.endFlg || visual.maxImg <= 1) return;

	visual.endFlg = true;	
	
	var vElm_st = visual.elm[j2].style;
	vElm_st.display = 'block';
	vElm_st.visibility = 'visible';
	vElm_st.zIndex = 100;
	
	if(j2 == (visual.elm.length-1)){
		var pos = 0;
		var npos = visual.elmbtn.length-1;
	}else{
		var pos = j2+1;
		var npos = j2;
	}
	
	visual.elm[pos].style.display = 'block';
	visual.elm[pos].style.visibility = 'visible';
	
	$(visual.elm[pos]).css({'opacity':'','clip':visual.defclip,'left':0});	
	$(visual.elmbtn[npos]).removeClass('kf_over').parent().removeClass('kf_stay');
    var src= $(visual.elmbtn[npos]).find('img').attr('src');
    $(visual.elmbtn[npos]).find('img').attr('src',imgsrc.del(src,'_o'));
    
	$(visual.elmbtn[pos]).addClass('kf_over').parent().addClass('kf_stay');
	var src= $(visual.elmbtn[pos]).find('img').attr('src');
    $(visual.elmbtn[pos]).find('img').attr('src',imgsrc.add(src,'_o'));
    
 	$(visual.elm[j2]).stop().animate({"opacity":0,'clip':visual.clip}, animSpeed, animEasing,
	function(){
		vElm_st.zIndex = 90;
		vElm_st.visibility = 'hidden';
		j2++
		if(j2 == (visual.elm.length)){
			j2 = 0;
			if(visual.loop == '0') visual.loopfix = true;
		}
	
		prevpos = j2;
		
		visual.endFlg = false;
		
		if(visual.autoFlg) {
			clearTimeout(visual.time);
			if(!visual.loopfix) visual.time = setTimeout(visual.elmani2,visual.delayArray[j2]);
		}
	});

},

elmcl : function(){
	
	if(visual.endFlg) return;
	if(this.className == 'kf_stay') return;
	clearTimeout(visual.time);
	visual.endFlg = true;
    
    var p_elm = $(this).parents('ul').find('.kf_stay .kf_over');    
	p_elm.removeClass('kf_over');
    
    var src= p_elm.find('img').attr('src');
    p_elm.find('img').attr('src',imgsrc.del(src,'_o'));
    
    $(this).parents('ul').find('.kf_stay').removeClass('kf_stay');
	
    var src= $(this).find('img').attr('src');
    $(this).find('img').attr('src',imgsrc.add(src,'_o'));
    
	this.className = 'kf_stay'
	
	j2 = $('#kf_main-btn li').index(this);
	
	visual.autoFlg = false;
	var visp_st = visual.elm[prevpos].style;
	var visj_st = visual.elm[j2].style;
	
	visj_st.display = 'block';
	visj_st.visibility = 'visible';

	visp_st.zIndex = 100;
	visj_st.zIndex = 90;
	$(visual.elm[j2]).css({'clip':visual.defclip,'left':0,'opacity':''});
	$(visual.elm[prevpos]).css({'clip':visual.defclip,'left':0});
	
	
	$(visual.elm[prevpos]).stop().animate({"opacity":0,'clip':visual.clip}, animSpeed, animEasing,function(){
	visp_st.zIndex = 80;
	visj_st.zIndex = 100;
	visp_st.visibility = 'hidden';
	prevpos = j2;
	visual.endFlg = false;
	});
	visual.autoFlg = true;
	if(!visual.loopfix) visual.time = setTimeout(visual.elmani2,visual.delayArray[j2]);
}

}

});
var imgsrc = {
	add : function (objsrc,plusnm){
		if(objsrc.indexOf(plusnm) != -1) return objsrc;
		var ftype = objsrc.substring(objsrc.lastIndexOf('.'), objsrc.length);
		var presrc = objsrc.replace(ftype, plusnm + ftype);
		return presrc;
	},
	del : function (objsrc,plusnm){
		if(objsrc.indexOf(plusnm) == -1) return objsrc;
		var ftype = objsrc.substring(objsrc.lastIndexOf('.'), objsrc.length);
		var presrc = objsrc.replace(plusnm + ftype, ftype);
		return presrc;
	}
}
})($);



/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright (C) 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/
(function(jQuery){
// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) 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;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});
		  })(jQuery);

/*
 *
 * TERMS OF USE - EASING EQUATIONS
 * 
 * Open source under the BSD License. 
 * 
 * Copyright (C) 2001 Robert Penner
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
 */

 /*
 * jQuery css clip animation support -- Jim Palmer
 * version 0.1.2
 * idea spawned from jquery.color.js by John Resig
 * Released under the MIT license.
 */
(function(jQuery){
	jQuery.fx.step.clip = function(fx){
if (fx.state == 0) {
/* handle three valid possibilities
* val val val val
* val, val, val, val
* val,val,val,val
*/
var cRE = /rect\(([0-9]{1,})(px|em)[,\s]+([0-9]{1,})(px|em)[,\s]+([0-9]{1,})(px|em)[,\s]+([0-9]{1,})(px|em)\)/;
// no longer replace commas - they may not exist and the regex compensates for them anyway
//grab the curent clip region of the element
jQueryelem = jQuery(fx.elem);
var clip = cRE.test(fx.elem.style.clip) ? fx.elem.style.clip : 'rect(0px ' + jQueryelem.width() + 'px ' + jQueryelem.height() + 'px 0px)';
fx.start = cRE.exec(clip.replace(/,/g, " "));
// handle the fx.end error
try {
fx.end = cRE.exec(fx.end.replace(/,/g, ''));
} catch (e) {
return false;
}
}
var sarr = new Array(), earr = new Array(), spos = fx.start.length, epos = fx.end.length,
emOffset = fx.start[ss + 1] == 'em' ? (parseInt($(fx.elem).css('fontSize')) * 1.333 * parseInt(fx.start[ss])) : 1;
for (var ss = 1; ss < spos; ss += 2) { sarr.push(parseInt(emOffset * fx.start[ss])); }
for (var es = 1; es < epos; es += 2) { earr.push(parseInt(emOffset * fx.end[es])); }
fx.elem.style.clip = 'rect(' +
parseInt((fx.pos * (earr[0] - sarr[0])) + sarr[0]) + 'px ' +
parseInt((fx.pos * (earr[1] - sarr[1])) + sarr[1]) + 'px ' +
parseInt((fx.pos * (earr[2] - sarr[2])) + sarr[2]) + 'px ' +
parseInt((fx.pos * (earr[3] - sarr[3])) + sarr[3]) + 'px)';
} })($);
