/* =====================================================================
*
*   GENKI_GOLF | FRONT.js
*
* =================================================================== */

/*----------------------------------------------------------------------
	jQuery PLUGINS
----------------------------------------------------------------------*/

(function(){
	
	$.fn.GGFrontTab = function(i_options) {
		
		/*----------------------------------------------------------------------
			Option
		----------------------------------------------------------------------*/
		var opt = $.extend({
			parentBoxesSelector : null,
			displaySelector : null,
			clearContent : "",
			useHashChange : false
		}, i_options);
		
		/*----------------------------------------------------------------------
			Common Elems
		----------------------------------------------------------------------*/
		var $handles = this;
		var $parentBoxes = $(opt.parentBoxSelector);
		var $display = $(opt.displaySelector);
		
		/*----------------------------------------------------------------------
			Common Functions
		----------------------------------------------------------------------*/
		function contentClear() {
			$parentBoxes.removeClass("selected");
			$display.children(opt.clearContent).hide();
		}
		
		function exec(i_targetHash) { 
			var $currentSelf = $("a[href='" + i_targetHash + "']");
			var $target = $(i_targetHash);
			var $parentBox = $currentSelf.parents("li").eq(0);
		
			contentClear();
			$parentBox.addClass("selected");
			$target.show();
		}
		
		/*----------------------------------------------------------------------
			HashFlag Driven Only
		----------------------------------------------------------------------*/
		if(opt.useHashChange) {
			$(window).bind("hashchange", function(){
				var targetHash = location.hash;
				exec(targetHash);
			});
		}
		
		if(opt.useHashChange) {
			var hash = location.hash;
			if(hash == "") hash = "#jContactIdea";
			exec(hash);
		}
		
		/*----------------------------------------------------------------------
			Setup
		----------------------------------------------------------------------*/
		return this.each(function(){
			
			var targetId = $(this).attr("href");
			
			$(this).bind("click", function(){
				
				//王冠変更用
				$(window).trigger("front-tab-click", [this]);
			
				if(opt.useHashChange) location.hash = targetId;
				else exec(targetId);
				
				return false;
			});
			
		});
		
		
	}
	
})();


/*----------------------------------------------------------------------
	MODULES
----------------------------------------------------------------------*/

GGOLF.FlowTopics = (function(){
	
	/*----------------------------------------------------------------------
		VARS
	----------------------------------------------------------------------*/
	var ctr;
	var interval = 3000;
	var stack = [];
	var request = "/api/t/news/";
	var index = 0;
	var lastn = 5;
	
	/*----------------------------------------------------------------------
		JQUERY OBJECTS
	----------------------------------------------------------------------*/
	var $dispatcher = $({});
	var $target;
	var $display;
	
	/*----------------------------------------------------------------------
		UTILITY
	----------------------------------------------------------------------*/
	function convertDateFormat(i_dataStr) {
	
		i_str = String( i_dataStr );
		var numStr;
		if( i_str.match( /^\/Date\((.+)\)\// ) ) {
			numStr = i_str.replace(/^\/Date\((.+)\)\//, "$1");
		}else {
			numStr = i_str;
		}
		
		var date = new Date(parseInt(numStr));
		var month = date.getMonth() + 1;
		if( month < 10 ) month = "0" + month;

		var day = date.getDate();
		if( day < 10 ) day = "0" + day;
		
		return date.getFullYear() + "." + month + "." + day;
		
	}
	
	return {
		
		/*----------------------------------------------------------------------
			INIT
		----------------------------------------------------------------------*/
		setup : function(i_targetId) {
			
			$target = $(i_targetId);
			$display = $target.find(".inner");
			
			ctr = GGOLF.FlowTopics;
			ctr._loadData(request);
			
		},
		
		/*----------------------------------------------------------------------
			DATA LOAD
		----------------------------------------------------------------------*/
		_loadData : function(i_request) {
			
			$.ajax({
			
				type: "POST",
				url: request,
				success : function(i_data) {
					var tmp = i_data["Data"]["T_NEWS_List"];
					
					for(var i = 0; i < tmp.length; i++) {
						
						var tmpl = [
							'<a href="' + "/news/" + tmp[i]["NEWS_ID"] + '/">',
							'<span class="date">' + convertDateFormat(tmp[i]["POST_DATE"]) + '</span>',
							'<span class="infobody">',
							'<img src="/content/images/common/parts/icon/news' + tmp[i]["NEWS_CATEGORY_TYPE"] + '.png" width="115" height="22" alt="" />',
							tmp[i]["TITLE"],
							'</span>',
							'</a>'
						].join("")
						
						stack.push(tmpl);
					}
				
					ctr._set(null, stack[index++]);
				}, 
				error : function(){
				
				}
			
			});
			
		},
		
		/*----------------------------------------------------------------------
			CONTENT SET
		----------------------------------------------------------------------*/
		_set : function(i_remove, i_add) {
			if(index >= stack.length) index = 0;
			if(i_remove != null) i_remove.remove();
			
			var $current = $(i_add);
			$current.hide();
			$display.append($current)
			ctr._newsIn($current);
		},
		
		/*----------------------------------------------------------------------
			MOTION FUNCS
		----------------------------------------------------------------------*/
		_newsIn : function(i_target) {
			i_target.fadeIn(function(){
				setTimeout(function(){
					ctr._newsOut(i_target);
				}, interval);
			});
		},
		
		_newsOut : function(i_target) {
			i_target.fadeOut(function(){
				ctr._set(i_target, stack[index++]);
			});
		}
		
	};
	
})();





/*----------------------------------------------------------------------
	FRONT ENTRY
----------------------------------------------------------------------*/
$(function(){

	if($("#FlowTopics").length) GGOLF.FlowTopics.setup("#FlowTopics");
		
	$(".topRoundTab").GGFrontTab({
		parentBoxSelector : ".jPicupRoundBox",
		displaySelector : ".topRoundTabDisplay",
		clearContent : "div[id^='jPickupRoundBody']"
	});
	
	$(".contactTab").GGFrontTab({
		parentBoxSelector : ".contactChoiceNav li",
		displaySelector : "#jContactTabDisplay",
		useHashChange : true
	});
	
	var crownImages = {
		on : "/content/images/common/parts/icon/crown-on.png",
		off: "/content/images/common/parts/icon/crown-off.png"
	}
	
	$(window).bind("front-tab-click", function(i_event, i_this){
		
		var $crowns = $(".jPicupRoundBox .hImg img");
		var $currentCrown = $(i_this).parent().siblings(".hImg").find("img");
		
		$crowns.each(function(){
			$(this).attr("src", crownImages.off);
		});
		
		$currentCrown.attr("src", crownImages.on);
		
	});
	
});
