/*****************************************************************************/
var auto_move_time = 10000
	,timer = null
	,banner_index = 0;

function upateBannerContent(){
	$('#content-words').css('left','-481px')
}

function moveBannerContent(){
	$('#content-words').animate({
		left : '21'
	},700
	,'easeOutBack')
}

function moveBanner(){
	var width = $('#index_banner').width();
	banner_moving = true;
	
	//for slider montion
	/*$('#index_banner div').animate({
		left : '-='+width
	}, 700, function() {
		banner_moving = false;
	});*/
	
	//for fade in/out montion
	$('#index_banner div.a:last').fadeOut(800,function(){
	//$('#index_banner div').fadeOut(800,function(){
		banner_moving = false;
		$(this).remove();
	});
}

function addBanner(val){
	//update banner content, and reset motion
	upateBannerContent()
	
	if($('#index_banner div').length < 1){
		//var html = '<div style="background: url('+banner_list[val]['img01']+') no-repeat center top; display:none;"></div>';
		
		//var html = '<div class="a" style="background: url('+banner_list[val]['img01']+') no-repeat center top; display:none;"><div class="move" style="background: url('+banner_list[val]['img02']+') no-repeat center top; "></div></div>';
		
		var html = '<div class="a" style="background: url('+banner_list[val]['img01']+') no-repeat center top; left:0"><div class="move" style="background: url('+banner_list[val]['img02']+') no-repeat center top; "></div><div class="move2" style="background: url('+banner_list[val]['img03']+') no-repeat center top; "></div><div class="move3" style="background: url('+banner_list[val]['img04']+') no-repeat center top; "></div></div>';
		
		
		$('#index_banner').append(html);
		
		//show banner
		$('#index_banner div').fadeIn(800,function(){
			banner_moving = false;
		});
	} else {
		if(!banner_moving){
			var width = $('#index_banner').width();
			
			//for slider show
			//var html = '<div style="background: url('+banner_list[val]+') no-repeat center top; left:'+width+'px"></div>';
			
			//add banner
			//$('#index_banner').append(html);
			
			//for fade in/out
			//var html = '<div style="background: url('+banner_list[val]['img01']+') no-repeat center top; left:0"></div>';
			
			var html = '<div class="a" style="background: url('+banner_list[val]['img01']+') no-repeat center top; left:0"><div class="move" style="background: url('+banner_list[val]['img02']+') no-repeat center top; "></div><div class="move2" style="background: url('+banner_list[val]['img03']+') no-repeat center top; "></div><div class="move3" style="background: url('+banner_list[val]['img04']+') no-repeat center top; "></div></div>';
			
			if(!empty(banner_list[val]['link'])){
				$('a#other_link').attr('href',banner_list[val]['link']);
				
				href = banner_list[val]['link'];
				
				patt=/youtube/g;
				result=patt.test(href);
				
				if(result){
					$("a#other_link").fancybox({
						'transitionIn'		: 'elastic'
						,'transitionOut'	: 'fade'
						,'padding'			: 0
						,'overlayColor'		: '#000'
						,'centerOnScroll'	: true
						,'width'		    : 680
						,'height'		    : 495
						,'href'			    : href.replace(new RegExp("watch\\?v=", "i"), 'v/')
						,'type'			    : 'swf'
						,'swf'			    : {
							 'wmode'		: 'transparent',
							'allowfullscreen'	: 'true'
						}
						,'titleShow'		: false
					});
				} else {
					$('#other_link').unbind();
				}
			} else {
				$('#other_link').unbind();
				$('#other_link').removeAttr('href')
			}
			
			//add banner
			$('#index_banner').prepend(html);
			
			//move banner image
			moveBanner();
		}
	}
	updateBannerPOS(535,288);
	
	if(banner_list[val]['nodesc'] != 'yes'){
		//show content
		moveBannerContent();
	}
}

//get banner image path
function getBanner(){
	$('#content-dots a').each(function(index, ele){
		//save banner list
		banner_list[index] = new Array();
		banner_list[index]['img01'] = $(ele).attr('href');
		banner_list[index]['img02'] = $(ele).attr('rel01');
		banner_list[index]['img03'] = $(ele).attr('rel02');
		banner_list[index]['img04'] = $(ele).attr('rel03');
		banner_list[index]['nodesc'] = $(ele).attr('nodesc');
		banner_list[index]['link'] = $(ele).attr('link');
	})	
}
/*****************************************************************************/
//defind var
var banner_list = new Array(), banner_moving = false;

//loaded DOM
$(document).ready(function(){
	//resize banner div
	resizeBg();
	
	//get banner image path
	getBanner();
	
	//add first banner
	//addBanner(0);
	
	$('#container_bottom').delay(500).fadeIn(1500,'easeInElastic')
	
	nextBanner(0)
});

function updateBannerPOS(pageX, pageY){

	var msg = ""
	, width = $(window).width()
	, height = $(window).height()
	, core_x = width / 2
	, core_y = height / 2
	, ext_x = ($(window).width() - 1000) / 2
	,x
	,y;
	msg += (pageX * 1 - core_x) + ", " + (pageY * 1 - core_y);
  
	if($.browser.ie){
		x = intval((pageX * 1 - core_x) / 7 + ext_x + 50);
		y = intval((pageY * 1 - core_y) / 7 + 50);
		$('div.move').css({
			"background-position-x":x,
			"background-position-Y":y
		})
		
		x = intval((pageX * 1 - core_x) / 15 + ext_x + 50);
		y = intval((pageY * 1 - core_y) / 15 + 50);
		$('div.move2').css({
			"background-position-x":x,
			"background-position-Y":y
		})

		/*x = intval((event.pageX * 1 - core_x) / 30 + ext_x + 50);
		y = intval((event.pageY * 1 - core_y) / 30 + 50);
		$('div.move3').css({
			"background-position-x":x,
			"background-position-Y":y
		})*/
	} else {
		x = (pageX * 1 - core_x) / 7 + ext_x + 50;
		y = (pageY * 1 - core_y) / 7 + 50;
			$('div.move').animate({
			'backgroundPosition' : x+' '+y
		},0)	
		x = (pageX * 1 - core_x) / 15 + ext_x + 50;
		y = (pageY * 1 - core_y) / 15 + 50;
			$('div.move2').animate({
			'backgroundPosition' : x+' '+y
		},0)	

		/*x = (event.pageX * 1 - core_x) / 30 + ext_x + 50;
		y = (event.pageY * 1 - core_y) / 30 + 50;
			$('div.move3').animate({
			'backgroundPosition' : x+' '+y
		},0)*/	
	}
}

$(document).mousemove(function(event){
	updateBannerPOS(event.pageX, event.pageY);
});




//for banner dots function
$('#content-dots a').each(function(index, ele){
	$(ele).click(function(){
		//check dots selected or not
		var bool = $(ele).hasClass('selected').toString();
	
	
	
		//when not select dots, move banner
		if(bool == 'false'){
		
			clearTimeout(timer);
		
			//add banner
			addBanner(index)
			
			banner_index = index;
			
			$('#content-dots a').removeClass('selected');
			
			$(ele).addClass('selected');	
			
			timer = setTimeout(function(){
				nextBanner(banner_index + 1)
			},auto_move_time);
		}

		return !1;
	})
})

function nextBanner(index){
	var total_num = $('#content-dots a').length;
	
	if(index >= total_num){
		index = 0;
	}
	
	var ele = $('#content-dots a').eq(index);	
	
	//add banner
	addBanner(index)
	
	banner_index = index;
	
	$('#content-dots a').removeClass('selected');
	
	$(ele).addClass('selected');		
	
	clearTimeout(timer);
	
	timer = setTimeout(function(){
		nextBanner(banner_index + 1)
	},auto_move_time);
}

var href = $("a#other_link").attr('href');
$("a#other_link").fancybox({
	'transitionIn'		: 'elastic'
	,'transitionOut'	: 'fade'
	,'padding'			: 0
	,'overlayColor'		: '#000'
	,'centerOnScroll'	: true
	,'width'		    : 680
	,'height'		    : 495
	,'href'			    : href.replace(new RegExp("watch\\?v=", "i"), 'v/')
	,'type'			    : 'swf'
	,'swf'			    : {
		 'wmode'		: 'transparent',
		'allowfullscreen'	: 'true'
	}
	,'titleShow'		: false
});
