//this is UTF-8 document！

var home = {};

home.ini = {};

home.init = function(){
	
	var h1 = document.getElementsByTagName('h1');
	var brand = document.getElementById('brand').getElementsByTagName('li');
	var bnr = document.getElementById('bnr').getElementsByTagName('li');
	
	this.ini.h1 = h1;
	this.ini.brand = brand;
	this.ini.bnr = bnr;
	
	this.allShow();
	this.brandSet();
	this.bnrSet();
	
}

home.allShow = function(){
	
	var loading = document.createElement('div');
	var loading_img = new Image();
	
	var showList = (function(){
		var a = [];
		var b = [ home.ini.h1, home.ini.brand, home.ini.bnr ];
		for(i=0; i<b.length; i++){
			for(j=0; j<b[i].length; j++){
				a[a.length] = b[i][j];
			}
		}
		return a;
	})();
	
	var imgShow = function(i){
		
		$(showList[i]).animate( { 'opacity':1 }, 400, function(){
			var _this = $(this);
			var thisImageSrc = $(this).find('img').attr('src')
			if( thisImageSrc.match(/index_h1/i) ){
				_this.css('opacity','auto');
				//console.log(_this.parent())
			}
		} );
		
		if(i==showList.length-1) home.brandSet.changeSet();
		
	}
	
	var goVisible = function(){
		
		$('.motionArea')[0].removeChild(loading);
		
		for(i=0; i<showList.length; i++) {
			(function(i){
				common.DELAY( 200*(i+1), imgShow, i-0);
			})(i);
		}
		
	}
	
	loading_img.src = '/freshel/common/images/thickbox_loading_01.gif';
	loading_img.style.marginTop = '256px';
	
	loading.style.width = '960px';
	loading.style.height = '569px';
	loading.style.textAlign = 'center';
	loading.style.zIndex = 200;
	
	loading.appendChild(loading_img);
	
	$('.motionArea')[0].insertBefore(loading, $('.motionArea')[0].firstChild)
	
	common.DELAY( 800, goVisible );
	
}

home.e = {
	
	hover : function(where, size){
		
		var now = where.getAttribute(common.CLASS);
		now = home.ini[size][now];
		
		$('.wrap', where).stop().animate(
			{
				'borderTopWidth' : '5px', 'borderLeftWidth' : '5px',
				'height' : (now.height-10)+'px', 'width' : (now.width-10)+'px'
			},
			150,
			'swing'
		);
		$('div.bg', where).stop().animate( { 'right' : '0px' }, 150);
		
		return false;
		
	},
	
	hoverout : function(where, size){
				
		var now = where.getAttribute(common.CLASS);
		now = home.ini[size][now];

		$('.wrap', where).stop().animate(
			{
				'borderTopWidth' : '0px', 'borderLeftWidth' : '0px',
				'height' : (now.height)+'px', 'width' : (now.width)+'px'
			},
			250,
			'swing'
		);
		$('div.bg', where).stop().animate( { 'right' : '10px' }, 250);
		
		return false;
		
	},
	
	click : function(where){
		
		var flag = where.getAttribute(common.CLASS);
		
		if(flag && -1<flag.indexOf('thickbox')) $(where).trigger('click');
		else window.location = where.href;
		
		return false;
		
	}

}

home.brandSet = function(){
	
	var brand = this.ini.brand;
	
	var moistlift_pre = new Image();
	var moistlift_item = 1;
	
	var hover = function(){
		
		home.e.hover(this, 'brandSize');
	}
	
	var hoverout = function(){
		
		home.e.hoverout(this, 'brandSize');
		
	}
	
	var click = function(){
		
		home.e.click($('.link > a', this.parentNode)[0]);
		
	}
	
	var changeImg = function(){
		
		$('div.bg > img', brand[0]).animate( { 'opacity' : 0 }, 300, 'swing', function(){
			common.SRC_DEFORMATION(this, (moistlift_item==1) ? '_c' : '' , false);
			moistlift_item = -1 * moistlift_item;
			$(this).animate( { 'opacity' : 1 }, 300, 'swing', function(){
			});
		});
		
	}
	
	this.brandSet.changeSet = function(){
		
		setInterval(changeImg, 3000);
		
	}
	
	this.ini.brandSize = {};
	
	for(i=0; i<brand.length; i++) {
		
		var elem = document.createElement('div');
		var brandSize = this.ini.brandSize[brand[i].getAttribute(common.CLASS)] = {};
		
		elem.setAttribute(common.CLASS, 'wrap');
		
		brand[i].appendChild(elem);
		
		brandSize.height = $(brand[i]).height();
		brandSize.width = $(brand[i]).width();
		
		$(brand[i]).hover(hover, hoverout);
		$('div.wrap', brand[i]).bind('click', click);
	}
	
	moistlift_pre.src = common.SRC_DEFORMATION($('div.bg > img', brand[0])[0], '_c', true);
	
}

home.bnrSet = function(){
	
	var bnr = this.ini.bnr;
	
	var hover = function(){
		
		home.e.hover(this, 'bnrSize');
		
	}
	
	var hoverout = function(){
		
		home.e.hoverout(this, 'bnrSize');
		
	}
	
	var click = function(){
		
		home.e.click(this.getElementsByTagName('a')[0]);
		
	}
	
	this.ini.bnrSize = {};
	
	for(i=0; i<bnr.length; i++) {
		
		var elem = document.createElement('div');
		var bnrSize = this.ini.bnrSize[bnr[i].getAttribute(common.CLASS)] = {};
		
		elem.setAttribute(common.CLASS, 'wrap');
		
		bnr[i].appendChild(elem);
		
		bnrSize.height = $(bnr[i]).height();
		bnrSize.width = $(bnr[i]).width();
		
		$(bnr[i]).hover(hover, hoverout).bind('click', click);
		
	}
	
}

var common = {};

common.DELAY = function( time, callback, argument ){
	
	var a = function(){
		clearTimeout(b);
		if(argument!=undefined) callback(argument);
		else callback();
	}
	
	var b = setTimeout(a,time);
	
}

common.SRC_DEFORMATION = function( targetImg, Included, toReturn ){
	
	var a = targetImg.src.split('.');
	var b = a.pop();
	
	if(Included==''){
		c = a[a.length-1].split('_');
		c.pop();
		c = c.join('_');
		a[a.length-1] = c;
	}
	b = a.join('.') + Included + '.' + b;
	if(-1<b.indexOf('http:')) b = '/' + b.split('/').slice(3).join('/');
	
	if(toReturn) return b;
	else targetImg.src = b;
	
}

common.IS_IE = (document.documentElement.getAttribute("style") == document.documentElement.style);

common.CLASS = (common.IS_IE) ? 'className' : 'class';


$(function(){
	home.init();
});




