/*
*                Script for  "ZERO EMISSION top"
*                var 1a.16.NOV.2011  
*/
/*-------------------------------------------------------------------
/*
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 */
 
 jQuery.timer = function (interval, callback)
 {
 /**
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  */
	var interval = interval || 100;
	if (!callback)
		return false;
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		this.internalCallback = function () {
			callback(self);
		};
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		var self = this;
	};
	return new _timer(interval, callback);
 };

/*-------------------------------------------------------------------
		Propaties
-------------------------------------------------------------------*/

// News Item Max Length
var news_count_max = 5;

// Slide Image Propaties
var now_selected = 1;
var gTime = 0;
var dTime = 8;
var onMo = false;
var slideTh = true;
var mv_ary = new Array();
var thm_ary = new Array();
var nbBtn_ary = new Array();

//var counterNum = 0;

/*-------------------------------------------------------------------
		PFA Area
-------------------------------------------------------------------*/

function slideImage(num){
	slideTh = false;
	
	var aTime = 900;
	var aEase = "easeInOutQuart";
	gTime = 0;
	
	setThmbs(false);
	setNextBack(false);
	
	var mImg = $('#mainVisual');
	$(mImg).stop();
	$(mImg).animate({left:-964*num}, aTime, aEase, function(){
		if(num == mv_ary.length-1){
			$(this).css({'left':-964});
			now_selected = 1;
		}else if(num == 0){
			$(this).css({'left':-964*(mv_ary.length-2)});
			now_selected = mv_ary.length-2;
		}else{
			now_selected = num;
		}
		slideTh = true;
		setNextBack(true);
		setThmbs(true);
	});
}

function setThmbs(_bool){
	
	if(_bool){
		for(var i=0; i<thm_ary.length; i++){
			if(i == now_selected-1){
				$(thm_ary[i]).removeClass('btnOn').css({'background-position':'bottom'});
			}else{
				$(thm_ary[i]).addClass('btnOn').css({'background-position':'top'});
			}
		}
	}else{
		for(var i=0; i<thm_ary.length; i++){
			$(thm_ary[i]).removeClass('btnOn').css({'background-position':'top'}).unbind();
		}
	}
	
	$('#mv_thms .btnOn').hover(function(){
		//$(this).addClass('btnOv');
		$(this).css({'background-position':'bottom'});
	}, function(){
		$(this).css({'background-position':'top'});
		//$(this).removeClass('btnOv');
	}).click(function(){
		var a = Number($(this).attr('num'));
		slideImage(a);
	});	
}

function setNextBack(_bool) {
nbBtn_ary[0].stop();
nbBtn_ary[1].stop();

if(_bool){
		nbBtn_ary[0].animate({'right':'0px'}, 600, "easeOutQuart", function(){});
		nbBtn_ary[1].animate({'left':'0px'}, 600, "easeOutQuart", function(){});
	}else{
		nbBtn_ary[0].animate({'right':'-30px'}, 600, "easeOutQuart", function(){});
		nbBtn_ary[1].animate({'left':'-30px'}, 600, "easeOutQuart", function(){});
	}
}


/*-------------------------------------------------------------------
		What's New  
-------------------------------------------------------------------*/

function parseNewsXml(news_xml){
	var count = 1;
	$('#newsArea').append('<ul id="newsItems"></ul>');	
	var newsBlk = $('#newsItems');
	$(news_xml).find("msg").each(function(){
	
		if(count <= news_count_max){
			var str = $(this).text();
			str_date = str.substring(0, 10);
			str_text = str.substring(12, str.length);
			str_link = $(this).attr('urls');
			str_trgt = $(this).attr('target');
			$(newsBlk).append('<li class="clearfix"><span class="date">'+str_date+'</span><span class="text"><a href="'+str_link+'" target="'+str_trgt+'">'+str_text+'</a></span></li>');
			count++;
		}else{
			return false;
		}
	});
}

/*-------------------------------------------------------------------
		Reduced CO2 Emission
-------------------------------------------------------------------*/

function setCounter(){
	var str = String(counterNum);
	for(var i=str.length-1; i>=0; i--){
		var n = str.charAt(i);
		var node = $('#counter ul li').eq((str.length-1)-i);
		node.css({'background-position': '0px '+(-39*Number(n)-39)+'px'}).html('<span>'+n+'</span>');
	}
	
	$('#counterDate').text($('#counterDate').text() + counterDate);
}

/*-------------------------------------------------------------------
    Timer action
-------------------------------------------------------------------*/

function setTimer(){
	/*/Debagger
	$('#frame').prepend('<div id="dbgr" style="padding:24px; position:absolute; z-index:150000; color:#c00;"><p id="dT"></p></div>');
	/*/
	$.timer(1000, function (timer) {
		if(!onMo && slideTh && !$('#nyroModalFull').length){
			gTime += 1;
		}
		/*/Debugger
		$("#dT").html("<p>image = "+(now_selected)+"<br />gTime = "+gTime+"<br />onMo = "+onMo+"<br />modal = "+$('#nyroModalFull').length+"</p>");
		/*/
		if(gTime == dTime){		
			//setCounter();
			
			var n = now_selected+1;
			if(slideTh){
				slideImage(n);
			}
		}
		
	});
}


/*-------------------------------------------------------------------
    jQuery Document Ready
-------------------------------------------------------------------*/

$(function(){

var is_ipad = navigator.userAgent.indexOf( "iPad", 0);
	var is_iphone = navigator.userAgent.indexOf( "iPhone", 0);

	if(is_ipad > 0 || is_iphone > 0){
		$('#modalMovie').attr('href', 'movie01.html');
	}
	
	$.nyroModalSettings({
		processHandler: function(settings) {
		var from = settings.from;
		var str_movie01=String(from).indexOf('@videoPlayer=');
		var str_movie02=String(from).length;
		if(str_movie01!=-1){
			var str_movie03=String(from).substring(str_movie01,str_movie02)
			$.nyroModalSettings({
				type:'swf',
				height:428,/*412*/
				width:760,/*486*/
				url:"http://c.brightcove.co.jp/services/viewer/federated_f9?isVid=1",
				swf:{ 
					bgcolor:"#fff",
					flashVars:str_movie03+"&autoStart=true",
					base:"http://admin.brightcove.co.jp",
					seamlesstabbing:"false",
					name:"flashObj",
					allowFullScreen:"true",
					swLiveConnect:"true",
					allowScriptAccess:"always"}
				});
			}
		}
	});

	/*============  PFA Area  ============*/
	
	//Main Visual Setting
	var mv = $('#mainVisual');
	$(mv).prepend($(mv).find('li:last').clone(true));
	$(mv).append($(mv).find('li:eq(1)').clone(true));

	$(mv).find('li').each(function(){
		mv_ary.push($(this));
	});
	
	for(var i=0; i<mv_ary.length; i++){
		$(mv_ary[i]).css({
			'position':'absolute',
			'top':'0',
			'left':964*i
		});
	}
	
	$(mv).css({
		'width':964*mv_ary.length,
		'left': '-964px'
	});

	$('#mainVisualBlock').mouseover(function(){
		onMo = true;		
	}).mouseleave(function(){
		onMo = false;		
	}).css({'overflow':'hidden'});
	
	//thms
	
	
	$('#mainVisualBlock').append('<ul id="mv_thms"></ul>')
	$('#mv_thms').css({
		'width': (24*(mv_ary.length-2)+5)+'px',
		'height': '14px',
		'top':'304px',
		'left':	Math.floor(482-(24*(mv_ary.length-2)+5)/2)+2+'px'
	});
	
	for(var i=0; i<mv_ary.length-2; i++){
		$('#mv_thms').append('<li class="png_bg"></li>');
	}
	
	var count = 1;
	$('#mv_thms li').each(function(){
		thm_ary.push($(this));
		$(this).attr('num', count);
		count++;
	});
	
	setThmbs(true);
	
	//next Back Buttons
	
	$('#mainVisualBlock').append('<div id="nextBtn" class="png_bg"></div>');
	nbBtn_ary[0] = $('#mainVisualBlock #nextBtn');
	$(nbBtn_ary[0]).hover(function(){
		$(this).css({'background-position':'bottom'});
	}, function(){
		$(this).css({'background-position':'top'});
	}).click(function(){
		var a = now_selected +1;
		slideImage(a);
	});
	
	$('#mainVisualBlock').append('<div id="backBtn" class="png_bg"></div>');
	nbBtn_ary[1] = $('#mainVisualBlock #backBtn');
	$(nbBtn_ary[1]).hover(function(){
		$(this).css({'background-position':'bottom'});
	}, function(){
		$(this).css({'background-position':'top'});
	}).click(function(){
		var a = now_selected -1;
		slideImage(a);
	});
	
	setNextBack(true);
	setTimer();
	
	/*============  Reduced CO2 Emission  ============*/
	
	setCounter();
	
	/*============  What's New  ============*/

	$.ajax({
		type     : "GET",
		url      : "/EN/XML/new.xml",
		dataType : "xml",
		cache    : false,
		success  : function(news_xml) { parseNewsXml(news_xml); },
		error    : function () {  }
	});

});
