var XMLData = function() {}
XMLData.prototype = {
	
	startItem : 0,
	endItem : 0,
	
	/**
	 *  表示item範囲設定
	 */
	setStartAndEndItem : function ( _row , _page ){
		startItem = _row*(_page - 1);
		endItem = startItem + (_row - 1);
	},
	
	/**
	 *  XMLデータから表示要素取得（クリエイティブ情報）
	 */
	getCreativesElements : function ( _data, _obj, _pattern ){
		
		var elements = '';
		
		$(_data).find('item').each(function(i){
			if(( i >= startItem )&&( i <= endItem )){
			
				var item = {
					title			 : $(this).children('title')								.text(),	//クリエイティブのタイトル
					description		 : $(this).children('description')							.text(),	//クリエイティブのコメント
					vLink			 : $(this).children('link')									.text(),	//クリエイティブ詳細ページURL
					pubDate			 : $(this).children('pubDate')								.text(),	//クリエイティブ登録日
					eid 			 : $(this).children('event')	.children('eid')			.text(),	//暗号化企画ID
					eventTitle		 : $(this).children('event')	.children('title')			.text(),	//企画名
					eventComments	 : $(this).children('event')	.children('comments')		.text(),	//企画説明文
					contentType		 : $(this).children('content_type')							.text(),	//クリエイティブタイプ
					mid				 : $(this).children('md5_id')								.text(),	//クリエイティブMD5ID
					uid				 : $(this).children('user')		.children('user_md5_id')	.text(),	//ユーザMD5ID
					handleName		 : $(this).children('user')		.children('handle_name')	.text(),	//ハンドルネーム
					uLink			 : $(this).children('user')		.children('mypage')			.text(),	//ユーザマイページURL
					thumb1Path		 : $(this).children('thumb1')								.text(),	//サムネイル1へのパス
					thumb2Path		 : $(this).children('thumb2')								.text(),	//サムネイル2へのパス
					thumb3Path		 : $(this).children('thumb3')								.text(),	//サムネイル3へのパス
					//genreの配列対応後ほど・・・
					//genreId			 : $(this).children('genre') 	.attr('id')					.text(),	//カテゴリID 
					//genreName		 : $(this).children('genre') 	.attr('text')				.text(),	//カテゴリ名 
					createDate		 : $(this).children('create_date')							.text(),	//クリエイティブ投稿日時
					updateDate		 : $(this).children('update_date')							.text(),	//クリエイティブ更新日時
					play			 : $(this).children('counts')	.children('play')			.text(),	//クリエイティブ視聴回数
					favorite		 : $(this).children('counts')	.children('favorite')		.text()		//クリエイティブ被お気に入り数
				}
				elements = elements + eval(_obj).getView( item, _pattern );
			}
		});
		return elements;
	},
	
	/**
	 *  XMLデータから表示要素取得（ユーザ情報）
	 */
	getUsersElements : function ( _data, _obj, _pattern ){
		
		var elements = '';
		
		$(_data).find('item').each(function(i){
			if(( i >= startItem )&&( i <= endItem )){
			
				var item = {
					title			 : $(this).children('title')							.text(),	//ハンドルネーム
					description		 : $(this).children('description')						.text(),	//プロフィール
					uLink			 : $(this).children('link')								.text(),	//マイページURL
					uid				 : $(this).children('user_md5_id')						.text(),	//ユーザMD5ID
					profPath		 : $(this).children('prof')								.text(),	//プロフィール画像パス
					createDate		 : $(this).children('create_data')						.text(),	//ユーザ登録日時
					updateDate		 : $(this).children('update_date')						.text(),	//ユーザ情報更新日時
					play			 : $(this).children('counts')	.children('play')		.text(),	//ユーザ視聴回数
					favorite		 : $(this).children('counts')	.children('favorite')	.text(),	//ユーザ被お気に入り数
					pickupComments	 : $(this).children('pickupComments')					.text()		//お勧めコメント
				}
				elements = elements + eval(_obj).getView( item, _pattern );
			}
		});
		return elements;
	},
	
	/**
	 *  XMLデータから表示要素取得（関連クリエイティブ情報）
	 */
	getRelatedCreativesElements : function ( _data, _obj, _pattern ){
		
		var elements = '';
		
		$(_data).find('item').each(function(i){
			if(( i >= startItem )&&( i <= endItem )){
			
				var item = {
					
				}
				elements = elements + eval(_obj).getView( item, _pattern );
			}
		});
		return elements;
	},
	
	/**
	 *  XMLデータから表示要素取得（関連クリエイティブ情報）
	 */
	getAuthorCreativesElements : function ( _data, _obj, _pattern ){
		
		var elements = '';
		
		$(_data).find('item').each(function(i){
			if(( i >= startItem )&&( i <= endItem )){
			
				var item = {
					
				}
				elements = elements + eval(_obj).getView( item, _pattern );
			}
		});
		return elements;
	},
	
	/**
	 *  XMLデータから表示要素取得（新着情報）
	 */
	getInfosElements : function ( _data, _obj, _pattern ){
		
		var elements = '';
		
		$(_data).find('news').each(function(i){
			if(( i >= startItem )&&( i <= endItem )){
			
				var item = {
					id				 : $(this).attr('id'),
					date			 : $(this).attr('date'),
					url				 : $(this).attr('url'),
					title			 : $(this).text()
				}
				elements = elements + eval(_obj).getView( item, _pattern );
			}
		});
		return elements;
	}
	
}


var utl = {
	
	/**
	 *  Ajaxを用いてデータ取得
	 */
	getDataWithAjax : function( _arg ){
		
		var ajaxUrl 		= '';
		var ajaxAsync 		= false;
		var ajaxType		= 'get';
		var ajaxDataType	= 'xml';
		var ajaxProcessData	= false;
		
		var myData 			= '';
		
		if ( typeof( _arg ) == 'undefined') {
			//_arg = {}
			return false;
		}
		
		if (typeof(_arg.url) == 'undefined') {
			return false;
		} else {
			ajaxUrl = _arg.url;
		}
		
		if (typeof(_arg.async) != 'undefined') {
			ajaxAsync = _arg.async;
		}
		if (typeof(_arg.type) != 'undefined') {
			ajaxType = _arg.type;
		}
		if (typeof(_arg.dataType) != 'undefined') {
			ajaxDataType = _arg.dataType;
		}
		if (typeof(_arg.processData) != 'undefined') {
			ajaxProcessData = _arg.processData;
		}
		
		$.ajax({
			url			 : ajaxUrl,
			async		 : ajaxAsync,
			type		 : ajaxType,
			dataType	 : ajaxDataType,
			processData	 : ajaxProcessData,
			success		 : function (_data) { myData = _data;},
			error		 : function (_data) { myData = null;}
		});
		return myData;
	},
	
	/**
	 *  現在ページURLのサーチ情報取得
	 */
	getUrlSearchArgs : function(){
		
		var data = new Array();
		var str = location.search.substring(1).split('&');
		
		for(var i = 0; i != str.length; i++) {
			data[utl.ckIdentifier( str[i].split('=')[0] )] = utl.getEscapeStr( unescape( str[i].split('=')[1] ));
		}
		return data;
	},
	
	/**
	 *  識別子チェック
	 */
	ckIdentifier : function( _str ){
		
		initialID = _str.substring(0,1);//an initial letter of identifier
		if( initialID.match(/[a-zA-Z_$]/g ) == null ){
			//alert( _str + "の引数が不正です。（一文字目は ASCII、_、$、のみ）" );
			return null;
		}else if( initialID.match(/[^a-zA-Z0-9_$]/g ) != null ){
			//alert( _str + "の引数が不正です。（命名文字は ASCII、数字、_、$、のみ）" );
			return null;
		}
		return _str;
		
	},
	
	/**
	 *  文字参照変換
	 */
	getEscapeStr : function( _str ){
		
		_str = _str.replace( /\&/g, '&amp;' );
		_str = _str.replace( /</g, '&lt;' ); 
		_str = _str.replace( />/g, '&gt;' ); 
		_str = _str.replace( /\"/g, '&quot;' );
		_str = _str.replace( /\'/g, '&#39;' );
		
		return _str;
		
	},
	
	/**
	 *  文字列のTRIMを行う(先頭から_len文字数を返す)
	 * @param	int	_len		trim数
	 * @param	str	_str		対象文字列
	 * @return	str str			変換された文字列（trim対象の場合語尾に「...」が付与）
	 */
	getTrimStr : function( _len, _str ){
		
		if( typeof(_len) != 'number' ){
			alert(typeof(_len));
			return '';
		}
		var str = _str.substr( 0 , _len );
		if( _str.length > _len ){
			str = str + '...';
		}
		
		return str;
	
	},
	
	/**
	 * 改行を<br />に置換する
	 * @param	str	_str		対象文字列
	 * @return	str _str		変換された文字列
	 */
	getConvertLFStr : function( _str ){
	
		_str = _str.replace(/\r\n/g, '<br />');
		_str = _str.replace(/(\n|\r)/g, '<br />');
		
		return _str;
		
	},
	
	/**
	 * DBフォーマットの時間を表示用に変換する(例　2000-01-01 59:59:00 -> 2000/01/01 59:59:00)
	 * @param	str	_str		対象文字列
	 * @return	str _str		変換された文字列
	 */
	getDataFormatStr : function( _str ){
	
		_str = _str.replace(/-/g, '/');
		
		return _str;
		
	}
}
