/* 文字コード：UTF-8 */
var clip = new Object();
clip.loaded = false;
clip.item_count = 0;
clip.item_max_count = 15;
clip.cookie_id = 'clip';
clip.cookie_url_id = 'clipUrl';
clip.url_id_prefix = 'http://' + location.host +'/clip/index.html?import=';
clip.mwp_detail_url = '/wedding_plan/detail.html?sh=';
clip.is_error = false;

clip.add = function(type, id){
	if(!clip.loaded){
		clip.load();
	}
	if(clip.item_max_count - clip.item_count <= 0){
		clip.error('クリップ可能な情報は' + clip.item_max_count + '件までです。');
		return void(0);
	}
	if(type == 'ch'){
		clip.ch.set(id);
	}
	else if(type == 'pt'){
		clip.pt.set(id);
	}
	else if(type == 'po'){
		if(typeof(arguments[2]) != 'undefined'){
			clip.po.set(id, arguments[2]);
		}
	}
	else{
		clip.dr.set(id, type);
	}
	clip.save_data();
	
	//計測用
	sc_ItemClip(type + id);
	
	clip.success('クリップに追加しました。');
}
clip.load = function(){
	if(!navigator.cookieEnabled){
		clip.error('クッキーが無効です。');
		return void(0);
	}
	clip.saved_data = cookiejar.getCookie(clip.cookie_id);
	
	/* ';'へのセパレータ変更対応 */
	if(clip.saved_data.indexOf(':') != -1){
		clip.saved_data = clip.saved_data.replace(/:/g,';');
		clip.save_cookie(clip.saved_data);
	}
	
	var items = clip.saved_data.split(',');
	for(var i = 0; i < items.length; i++){
		var item = items[i];
		var info = item.split(';');
		if(info.length >= 2){
			var type = info[0];
			var id = info[1];
			if(type == 'ch'){
				clip.ch.set(id);
			}
			else if(type == 'pt'){
				clip.pt.set(id);
			}
			else if(type == 'po'){
				if(typeof(info[2]) != 'undefined'){
					clip.po.set(id, info[2]);
				}
			}
			else{
				clip.dr.set(id, type);
			}
		}
	}
	clip.update_item_count();
	clip.loaded = true;
}
clip.save_data = function(){
	clip.update_item_count();
	var saved_data = '';
	var i = 0;
	var id = '';
	for(i = 0; i < clip.ch.order.length; i++){
		id = clip.ch.order[i];
		saved_data += (saved_data != '' ? ',' : '') + 'ch;' + id;
	}
	for(i = 0; i < clip.po.order.length; i++){
		id = clip.po.order[i];
		saved_data += (saved_data != '' ? ',' : '') + 'po;' + id + ';' + clip.po.items[id].ch_id;
	}
	for(i = 0; i < clip.dr.order.length; i++){
		id = clip.dr.order[i];
		saved_data += (saved_data != '' ? ',' : '') + clip.dr.items[id].type + ';' + id;
	}
	for(i = 0; i < clip.pt.order.length; i++){
		id = clip.pt.order[i];
		saved_data += (saved_data != '' ? ',' : '') + 'pt;' + id;
	}
	clip.save_cookie(saved_data);
}
clip.save_cookie = function(saved_data){
	clip.saved_data = saved_data;
	cookiejar.setCookie(clip.cookie_id, clip.saved_data);
	cookiejar.setCookie(clip.cookie_url_id, clip.url_id_prefix + clip.saved_data);
}
clip.update_item_count = function(msg){
	clip.item_count = clip.ch.order.length + clip.pt.order.length + clip.po.order.length + clip.dr.order.length;
}
clip.error = function(msg){
	alert(msg);
	clip.is_error = true;
}
clip.success = function(msg){
	if(!clip.is_error){
		alert(msg);
	}
	clip.is_error = false;
}

/* clip.ch */
clip.ch = new Object();
clip.ch.order = new Array();
clip.ch.items = new Array();
clip.ch.set = function(id){
	if(typeof(this.items[id]) != 'undefined'){
		clip.error('この式場は登録済みです。');
		return void(0);
	}
	this.order.push(id);
	this.items[id] = new clip.ch._item(id);
}
clip.ch._item = function(id){
	this.id = id;
}

/* clip.po */
clip.po = new Object();
clip.po.order = new Array();
clip.po.items = new Array();
clip.po.set = function(id, ch_id){
	if(typeof(this.items[id]) != 'undefined'){
		clip.error('このプランオプションは登録済みです。');
		return void(0);
	}
	this.order.push(id);
	this.items[id] = new clip.po._item(id, ch_id);
}
clip.po._item = function(id, ch_id){
	this.id = id;
	this.ch_id = ch_id;
}

/* clip.dr */
clip.dr = new Object();
clip.dr.type_names = {d1:'ウェディングドレス',d2:'タキシード',d3:'カラードレス',d4:'花嫁衣装',d5:'花婿衣装'};
clip.dr.order = new Array();
clip.dr.items = new Array();
clip.dr.set = function(id, type){
	var type_name = this.type_names[type];
	if(typeof(this.items[id]) != 'undefined'){
		clip.error('この' + type_name + 'は登録済みです。');
		return void(0);
	}
	this.order.push(id);
	this.items[id] = new clip.dr._item(id, type);
}
clip.dr._item = function(id, type){
	this.id = id;
	this.type = type;
}

/* clip.pt */
clip.pt = new Object();
clip.pt.order = new Array();
clip.pt.items = new Array();
clip.pt.set = function(id){
	if(typeof(this.items[id]) != 'undefined'){
		clip.error('このパーティー会場は登録済みです。');
		return void(0);
	}
	this.order.push(id);
	this.items[id] = new clip.pt._item(id);
}
clip.pt._item = function(id){
	this.id = id;
}

/* functions */
function addClipCh(id) {
	clip.add('ch', id);
}
function addClipPo(id, ch_id) {
	clip.add('po', id, ch_id);
}
function addClipDr(id) {
	clip.add('d1', id);
}
function addClipTx(id) {
	clip.add('d2', id);
}
function addClipCd(id) {
	clip.add('d3', id);
}
function addClipJw(id) {
	clip.add('d4', id);
}
function addClipJm(id) {
	clip.add('d5', id);
}
function addClipPt(id) {
	clip.add('pt', id);
}

