/**
 * コンテンツクラス
 * @return
 */
var Content = function(contentId){
	this.intialize(contentId);
}

/** トップ画面のお知らせキー */
Content.TOP_INFORMASION = 'cp_top_information';

/** お知らせキー */
Content.INFORMASION = 'cp_information';

/** 採用情報キー */
Content.NEWS = 'cp_news';

/** トップ画像キー */
Content.TOPICS = 'cp_topics';

/** リクエストURL */
Content.REQUEST_URL = location.protocol+'//'+location.hostname+'/cgi-bin/contents/contents.cgi'


/**
 * コンテンツのプロトタイプ定義
 */
Content.prototype = {

	/** コンテンツID */
	id : null,

	/** XmlHttpRequestインスタンス */
	request : null,

	// コンストラクタ
	intialize: function(contentId){
		this.id = contentId;
		if(window.XMLHttpRequest){
			this.request = new XMLHttpRequest();
		}else if(typeof ActiveXObject != 'undefined'){
			try{
				this.request = new ActiveXObject('Microsft.XMLHTTP');
			}catch(e){
				this.request = new ActiveXObject('Msxml2.XMLHTTP');
			}
		}
	},

	// コンテンツ取得
	get : function(func) {

		// データ準備
		var contentId = this.id;
		var request = this.request;
		var requestUrl = Content.REQUEST_URL+"?type="+contentId;

		// リクエスト準備
		this.request.open('GET',requestUrl,true);
		this.request.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT");
		this.request.onreadystatechange = function(){
			if(request.readyState == 4){
				func(contentId,request);
			}
		}

		// リクエスト発行
		this.request.send(null);
	}

};

/**
 * コンテンツ出力
 * @param responseText
 * @return
 */
function printContent(contentId,request) {
	var contentDiv = document.getElementById(contentId);
	if(contentDiv){
		contentDiv.innerHTML = request.responseText;
	}
};

/**
 * コンテンツ取得
 * @return
 */
function getContents(){

	// お知らせ取得(トップ)
	if(document.getElementById(Content.TOP_INFORMASION)){
		var contentTopInformation = new Content(Content.TOP_INFORMASION);
		contentTopInformation.get(printContent);
	}

	// お知らせ取得
	if(document.getElementById(Content.INFORMASION)){
		var contentInformation = new Content(Content.INFORMASION);
		contentInformation.get(printContent);
	}

	// 採用情報取得
	if(document.getElementById(Content.NEWS)){
		var contentNews = new Content(Content.NEWS);
		contentNews.get(printContent);
	}

	// トップ画像取得
	if(document.getElementById(Content.TOPICS)){
		var contentTopics = new Content(Content.TOPICS);
		contentTopics.get(printContent);
	}

}

/**
 * イベントリスナー追加
 * @param elem
 * @param eventType
 * @param func
 * @param cap
 * @return
 */
function addListener(elem, eventType, func, cap){
	if(elem.addEventListener){
		elem.addEventListener(eventType, func, cap);
	}else if(elem.attachEvent){
		elem.attachEvent('on' + eventType, func);
	}
}

// windowへイベントリスナー設定
addListener(window, 'load', getContents ,false);

