/**
 * 物件履歴クラス
 * @author suwahara
 */
Ozein.BukkenHistory = new Class.create();
Ozein.BukkenHistory.prototype = {
    cookie_name: 'c_m7_bukken_history',
    cookie_expires: 5184000000,
    options: {},
    /** 初期化 */
    initialize: function(node,options){
        this.setOptions(options);
        this.ozeinCookie = new Ozein.Cookie(null,{name: this.cookie_name, expires: this.cookie_expires});
        if(this.options.mode != 'history'){
            this.registBukkenHistory(this.options.bukkenId);
        }else{
            var doDeleteUp = $('do_delete_up');
            var doDeleteDown = $('do_delete_down');
            var allCheckIn = $('all_check_in');
            var allCheckOut = $('all_check_out');
            var close = $('close');
            if(doDeleteUp){   Event.observe(doDeleteUp, 'click', this.deleteBukkenHistory.bindAsEventListener(this,doDeleteUp));}
            if(doDeleteDown){ Event.observe(doDeleteDown, 'click', this.deleteBukkenHistory.bindAsEventListener(this,doDeleteDown));}
            if(allCheckIn){   Event.observe(allCheckIn, 'click', this.allCheckIn.bindAsEventListener(this,allCheckIn));}
            if(allCheckOut){  Event.observe(allCheckOut, 'click', this.allCheckOut.bindAsEventListener(this,allCheckOut));}
            Event.observe(close, 'click', function(event,node){Event.stop(event);window.close();}.bindAsEventListener(this,close));
        }
    },
    /** オプション設定 */
    setOptions : function(options) {
        Object.extend(this.options, options || {});
    },
    /** 物件履歴登録 */
    registBukkenHistory: function(bukkenId){
        //既存データ取り出し
        var cookieStr = this.ozeinCookie.getCookie(this.cookie_name);

        var newdata = new Array();
        if(cookieStr != null){
            //セパレータで分解
            var data = cookieStr.split('/');
    
            //新規追加。登録済みの場合は先頭へ持っていく
            for(var i=0; i<data.length; i++){
                if(data[i] != 'dummy' && data[i].indexOf(bukkenId) == -1){
                    newdata.push(data[i]);
                }
            }
        }
        var date = new Date();
        var today = date.getFullYear() + '-' + ('0' + (date.getMonth()+1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
        newdata.unshift(bukkenId + ':' + today);  

        //30件を超えたら削除する
        while(newdata.length > 30){
            newdata.pop();
        }
        //再び結合
        var dataStr = newdata.join('/');
        
        //保存
        this.ozeinCookie.setValue(this.cookie_name, dataStr);
        this.ozeinCookie.setCookie();
    },
    /** 物件履歴削除 */
    deleteBukkenHistory: function(event, node){
        Event.stop(event);
        var newdata = new Array();
        //チェックボックスリスト取り出し
        var checkboxList = document.getElementsByAttribute('ozein_history_bukken', '*', document.body);
        //チェックがされいるかどうか調べる
        var checkFlg = false;
        for(var i=0; i<checkboxList.length; i++){
            if(checkboxList[i].checked){
                checkFlg = true;
                break;
            }
        }
        if(!checkFlg){
            alert('物件にチェックが入っていません');
            return;
        }
        //既存データ取り出し
        var cookieStr = this.ozeinCookie.getCookie(this.cookie_name);
        //セパレータで分解
        var data = cookieStr.split('/');
        for(var i=0; i<data.length; i++){
            var bukkenId = checkboxList[i].getAttribute('ozein_history_bukken');
            if(!checkboxList[i].checked && data[i].indexOf(bukkenId) != -1){
                newdata.push(data[i]);
            }else{
                checkboxList[i].checked = false;
            }
        }
        //再び結合
        var dataStr = newdata.join('/');
        //保存
        if(dataStr){
            this.ozeinCookie.setValue(this.cookie_name, dataStr);
        }else{
            this.ozeinCookie.setValue(this.cookie_name, 'dummy');
        }
        this.ozeinCookie.setCookie();
        
        alert('チェックされた履歴を削除しました。');
        window.location.reload();
    },
    /** チェックボックスを全部onにする */
    allCheckIn: function(event, node){
        Event.stop(event);
        var checkboxList = document.getElementsByAttribute('ozein_history_bukken', '*', document.body);
        for(var i=0; i<checkboxList.length; i++){
            checkboxList[i].checked = true;
        }
    },
    /** チェックボックスを全部offにする */
    allCheckOut: function(event, node){
        Event.stop(event);
        var checkboxList = document.getElementsByAttribute('ozein_history_bukken', '*', document.body);
        for(var i=0; i<checkboxList.length; i++){
            checkboxList[i].checked = false;
        }
    }
}

/**
 * 検索履歴クラス
 * @author suwahara
 */
Ozein.SearchHistory = new Class.create();
Ozein.SearchHistory.prototype = {
    cookie_name: 'c_m7_search_history',
    cookie_expires: 5184000000,
    ozeinCookie: null,
    options: {},
    /** 初期化 */
    initialize: function(node,options){
        this.setOptions(options);
        this.ozeinCookie = new Ozein.Cookie(null,{name: this.cookie_name, expires: this.cookie_expires});
        if(this.options.mode != 'history'){
            this.registSearchHistory(this.options.type,this.options.code);
        }else{
            var doDeleteUp = $('do_delete_up');
            var doDeleteDown = $('do_delete_down');
            var allCheckIn = $('all_check_in');
            var allCheckOut = $('all_check_out');
            var close = $('close');
            if(doDeleteUp){   Event.observe(doDeleteUp, 'click', this.deleteSearchHistory.bindAsEventListener(this,doDeleteUp));}
            if(doDeleteDown){ Event.observe(doDeleteDown, 'click', this.deleteSearchHistory.bindAsEventListener(this,doDeleteDown));}
            if(allCheckIn){   Event.observe(allCheckIn, 'click', this.allCheckIn.bindAsEventListener(this,allCheckIn));}
            if(allCheckOut){  Event.observe(allCheckOut, 'click', this.allCheckOut.bindAsEventListener(this,allCheckOut));}
            Event.observe(close, 'click', function(event,node){Event.stop(event);window.close();}.bindAsEventListener(this,close));
            var deleteBtnList = document.getElementsByAttribute('ozein_history_delete', '*', document.body);
            for(var i=0; i<deleteBtnList.length; i++){
                Event.observe(deleteBtnList[i], 'click', this.deleteOneSearchHistory.bindAsEventListener(this,deleteBtnList[i]));
            }
        }
    },
    /** オプション設定 */
    setOptions : function(options) {
        Object.extend(this.options, options || {});
    },
    /** 検索履歴登録 */
    registSearchHistory: function(type, code){
        //既存データ取り出し
        var cookieStr = this.ozeinCookie.getCookie(this.cookie_name);

        var newdata = new Array();
        if(cookieStr != null){
            //セパレータで分解
            var data = cookieStr.split('/');
    
            //新規追加。登録済みの場合は先頭へ持っていく
            for(var i=0; i<data.length; i++){
                if(data[i] != 'dummy' && (data[i].indexOf(type) == -1 || data[i].indexOf(code) == -1)){
                    newdata.push(data[i]);
                }
            }
        }
        var date = new Date();
        var today = date.getFullYear() + '-' + ('0' + (date.getMonth()+1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
        newdata.unshift(type + ':' + code + ':' + today);  

        //10件を超えたら削除する
        while(newdata.length > 10){
            newdata.pop();
        }
        //再び結合
        var dataStr = newdata.join('/');
        
        //保存
        this.ozeinCookie.setValue(this.cookie_name, dataStr);
        this.ozeinCookie.setCookie();
    },
    /** 検索履歴削除 */
    deleteSearchHistory: function(event, node){
        Event.stop(event);
        var newdata = new Array();
        //チェックボックスリスト取り出し
        var checkboxList = document.getElementsByAttribute('ozein_history_search', '*', document.body);
        //チェックがされいるかどうか調べる
        var checkFlg = false;
        for(var i=0; i<checkboxList.length; i++){
            if(checkboxList[i].checked){
                checkFlg = true;
                break;
            }
        }
        if(!checkFlg){
            alert('履歴にチェックが入っていません');
            return;
        }
        //既存データ取り出し
        var cookieStr = this.ozeinCookie.getCookie(this.cookie_name);
        //セパレータで分解
        var data = cookieStr.split('/');
        for(var i=0; i<data.length; i++){
            var serarchId = checkboxList[i].getAttribute('ozein_history_search');
            if(!checkboxList[i].checked && data[i].indexOf(serarchId) != -1){
                newdata.push(data[i]);
            }else{
                checkboxList[i].checked = false;
            }
        }
        //再び結合
        var dataStr = newdata.join('/');
        //保存
        if(dataStr){
            this.ozeinCookie.setValue(this.cookie_name, dataStr);
        }else{
            this.ozeinCookie.setValue(this.cookie_name, 'dummy');
        }
        this.ozeinCookie.setCookie();
        
        alert('チェックされた履歴を削除しました。');
        window.location.reload();
    },
    /** 検索履歴を１件削除 */
    deleteOneSearchHistory: function(event, node){
        Event.stop(event);
        var newdata = new Array();
        //既存データ取り出し
        var cookieStr = this.ozeinCookie.getCookie(this.cookie_name);
        //セパレータで分解
        var data = cookieStr.split('/');
        for(var i=0; i<data.length; i++){
            var searchId = node.getAttribute('ozein_history_delete');
            if(data[i].indexOf(searchId) == -1){
                newdata.push(data[i]);
            }
        }
        //再び結合
        var dataStr = newdata.join('/');
        //保存
        if(dataStr){
            this.ozeinCookie.setValue(this.cookie_name, dataStr);
        }else{
            this.ozeinCookie.setValue(this.cookie_name, 'dummy');
        }
        this.ozeinCookie.setCookie();
        
        alert('履歴を削除しました。');
        window.location.reload();
    },
    /** チェックボックスを全部onにする */
    allCheckIn: function(event, node){
        Event.stop(event);
        var checkboxList = document.getElementsByAttribute('ozein_history_search', '*', document.body);
        for(var i=0; i<checkboxList.length; i++){
            checkboxList[i].checked = true;
        }
    },
    /** チェックボックスを全部offにする */
    allCheckOut: function(event, node){
        Event.stop(event);
        var checkboxList = document.getElementsByAttribute('ozein_history_search', '*', document.body);
        for(var i=0; i<checkboxList.length; i++){
            checkboxList[i].checked = false;
        }
    }
}
/**
 * 検討中リストクラス
 * @author suwahara
 */
Ozein.ConsiderList = new Class.create();
Ozein.ConsiderList.prototype = {
    cookie_name: 'c_m7_consider_list',
    cookie_expires: 5184000000,
    ozeinCookie: null,
    win: null,
    options: {},
    /** 初期化 */
    initialize: function(node,options){
        this.setOptions(options);
        this.ozeinCookie = new Ozein.Cookie(null,{name: this.cookie_name, expires: this.cookie_expires});
        if(this.options.mode != 'considerList'){
            var considerNodeList = document.getElementsByAttribute('ozein_consider_list', '*', 'body');
            for(var i=0; i < considerNodeList.length; i++){
                Event.observe(considerNodeList[i], 'click', this.registConsiderList.bindAsEventListener(this,considerNodeList[i]));
            }
        }else{
            var doDeleteUp = $('do_delete_up');
            var doDeleteDown = $('do_delete_down');
            var allCheckIn = $('all_check_in');
            var allCheckOut = $('all_check_out');
            var close = $('close');
            if(doDeleteUp){   Event.observe(doDeleteUp, 'click', this.deleteConsiderList.bindAsEventListener(this,doDeleteUp));}
            if(doDeleteDown){ Event.observe(doDeleteDown, 'click', this.deleteConsiderList.bindAsEventListener(this,doDeleteDown));}
            if(allCheckIn){   Event.observe(allCheckIn, 'click', this.allCheckIn.bindAsEventListener(this,allCheckIn));}
            if(allCheckOut){  Event.observe(allCheckOut, 'click', this.allCheckOut.bindAsEventListener(this,allCheckOut));}
            Event.observe(close, 'click', function(event,node){Event.stop(event);window.close();}.bindAsEventListener(this,close));
        }
    },
    /** オプション設定 */
    setOptions : function(options) {
        Object.extend(this.options, options || {});
    },
    /** 検討中リスト登録 */
    registConsiderList: function(event, node){
        Event.stop(event);
        //クリックされた物件id
        var bukkenId = node.getAttribute('ozein_consider_list');

        //既存データ取り出し
        var cookieStr = this.ozeinCookie.getCookie(this.cookie_name);

        var newdata = new Array();
        if(cookieStr != null){
            //セパレータで分解
            var data = cookieStr.split('/');
            
            if(data.length >= 50){
            	this.popup(node,true);
                return;
            }
    
            //登録済みの場合は先頭へ持っていくために一度削除する
            for(var i=0; i<data.length; i++){
                if(data[i] != 'dummy' && data[i].indexOf(bukkenId) == -1){
                    newdata.push(data[i]);
                }
            }
        }
        //新規追加
        newdata.unshift(bukkenId);

        //再び結合
        var dataStr = newdata.join('/');
        
        //保存
        this.ozeinCookie.setValue(this.cookie_name, dataStr);
        this.ozeinCookie.setCookie();

        //結果をポップアップ
        this.popup(node,false);
    },
    /** 検討中リスト削除 */
    deleteConsiderList: function(event, node){
        Event.stop(event);
        var newdata = new Array();
        //チェックボックスリスト取り出し
        var checkboxList = document.getElementsByAttribute('ozein_history_bukken', '*', document.body);
        //チェックがされいるかどうか調べる
        var checkFlg = false;
        for(var i=0; i<checkboxList.length; i++){
            if(checkboxList[i].checked){
                checkFlg = true;
                break;
            }
        }
        if(!checkFlg){
            alert('物件にチェックが入っていません');
            return;
        }
        //既存データ取り出し
        var cookieStr = this.ozeinCookie.getCookie(this.cookie_name);
        //セパレータで分解
        var data = cookieStr.split('/');
        for(var i=0; i<data.length; i++){
            var bukkenId = checkboxList[i].getAttribute('ozein_history_bukken');
            if(!checkboxList[i].checked && data[i].indexOf(bukkenId) != -1){
                newdata.push(data[i]);
            }else{
                checkboxList[i].checked = false;
            }
        }
        //再び結合
        var dataStr = newdata.join('/');
        //保存
        if(dataStr){
            this.ozeinCookie.setValue(this.cookie_name, dataStr);
        }else{
            this.ozeinCookie.setValue(this.cookie_name, 'dummy');
        }
        this.ozeinCookie.setCookie();
        
        alert('チェックされた検討中物件を削除しました。');
        window.location.reload();
    },
    /** チェックボックスを全部onにする */
    allCheckIn: function(event, node){
        Event.stop(event);
        var checkboxList = document.getElementsByAttribute('ozein_history_bukken', '*', document.body);
        for(var i=0; i<checkboxList.length; i++){
            checkboxList[i].checked = true;
        }
    },
    /** チェックボックスを全部offにする */
    allCheckOut: function(event, node){
        Event.stop(event);
        var checkboxList = document.getElementsByAttribute('ozein_history_bukken', '*', document.body);
        for(var i=0; i<checkboxList.length; i++){
            checkboxList[i].checked = false;
        }
    },
    /**
     * ポップアップ表示を行う。
     * Ozein.PopupやOzein.MonoPopupを使用すると
     * jsのcookie保存とphpのcookie保存が衝突するため、
     * 順序を保証するためにスレッド内で処理する。
     * @param {Object} node 「検討中リストに追加」ノード
     */
    popup: function(node,limitflg){
        var options = {
            width: document.body.offsetWidth,
            height: document.body.offsetHeight,
            menubar:'yes',
            toolbar:'yes',
            location:'yes',
            status:'yes',
            scrollbars:'yes',
            resizable:'yes'
        }
        var href =  node.getAttribute('href');
        var name =  node.getAttribute('ozein_consider_list_popup');
        
        //すでに50件以上検討中リストに追加されていた場合の処理
        if(!!limitflg){
        	href = href+"?limit_flg=true";
        }
        
        if(this.win == null || this.win.closed == undefined || this.win.closed) {
            var settings = "";
            var settings = $H(options).toQueryString().replace(/&/g, ',');
            this.win = window.open(href, name, settings);
        } else {
            this.win.location.replace(href);
            this.win.focus();
        }
    }
}
/**
 * 物件履歴クラス(Jump時)
 */
Ozein.BukkenHistorySite = new Class.create();
Ozein.BukkenHistorySite.prototype = {
	options: {},
	/** 初期化 */
    initialize: function(node,options){
    	var bukkenHistoryNodeList = document.getElementsByAttribute('ozein_add_bukken_history_Id', '*', 'body');
        for(var i=0; i < bukkenHistoryNodeList.length; i++){
            Event.observe(bukkenHistoryNodeList[i], 'click', this.addBukkenHistory.bindAsEventListener(this,bukkenHistoryNodeList[i]));
        }
    },
    /** オプション設定 */
    setOptions : function(options) {
        Object.extend(this.options, options || {});
    },
    addBukkenHistory : function(event, node){
        Event.stop(event);
        //クリックされた物件id
        var bukkenId = node.getAttribute('ozein_add_bukken_history_Id');
		this.BukkenHistory = new Ozein.BukkenHistory;
		this.BukkenHistory.registBukkenHistory(bukkenId);
    }
}
