var ckm = {};
ckm.FAQ = Class.create();
ckm.FAQ.prototype = {
    initialize: function() {
        
    },
    
    dummy: function() {
        
    },
    
    init: function() {
        //history
//        History.address.setDefault('', this.dummy);
//        History.address.setAddress('q', this.historyQuestion.bind(this));

        History.address.setAddress('q', this, "historyQuestion");
        History.address.init();
        
        // permalink
//        var hash = location.hash.replace('#', '');
//        if (hash) {
//            this.openAnswer(hash);
//        }
        
        // click event question
        var questions = $A($$('a.question'));
        for (var i=0; i<questions.length; i++) {
            Event.observe(questions[i].id, 'click', ckm.Event.openAnswer);
//            Event.observe(questions[i], 'click', this.openAnswerSlide.bind(this, questions[i].href.replace(/^[^#]*#/, '')))
        }

        // click event close button
        var close = $A($$('a.close'));
        for (var i=0; i<close.length; i++) {
//            Event.observe(questions[i].id, 'click', ckm.Event.closeAnswer);
            Event.observe(close[i], 'click', this.closeAnswerSlide.bind(this, close[i].id.replace(/[^0-9]/g, '')));
        }
    }, 
    
    openAnswerSlide: function(id, noExec) {
//        if (navigator.userAgent.toLowerCase().indexOf('safari') > -1) {
//            History.address.load("q&a=" + id, true);
////            location.hash =  "q&a=" + id;
//        } else if (History.address.name == 'IE'){
//            if (!noExec) {
//                History.address.load("#q&a=" + id, true);
//            }
//            
//        } else {
//                History.address.load("q&a=" + id, true);
////            location.hash = "#q&a=" + id;
//        }
        History.address.load("q&a=" + id, true, true);


//        alert('called')
        Event.stopObserving('question_'+id, 'click', ckm.Event.openAnswer);
        Event.stopObserving('question_'+id, 'click', ckm.Event.closeAnswer);
        Event.observe('question_'+id, 'click', ckm.Event.closeAnswer);
    	new Effect.SlideDown('answer_'+id, {
    		duration: 0.3 ,
    		afterFinishInternal: function(effect) {
//alert($('answer_'+id).getStyle('display'))
setTimeout(function(){$('answer_'+id).setStyle({visibility: 'visible', display:'block'});}, 10);
    		}
    	});
        return false;
    },

    closeAnswerSlide: function(id) {
        Event.stopObserving('question_'+id, 'click', ckm.Event.openAnswer);
        Event.stopObserving('question_'+id, 'click', ckm.Event.closeAnswer);
        Event.observe('question_'+id, 'click', ckm.Event.openAnswer);
        Element.hide('answer_'+id);
return;

//        if (navigator.userAgent.toLowerCase().indexOf('safari') > -1) {
//            location.hash =  'list';
//        } else {
//            location.hash = "#list";
//        }
        Event.stopObserving('question_'+id, 'click', ckm.Event.openAnswer);
        Event.stopObserving('question_'+id, 'click', ckm.Event.closeAnswer);
        Event.observe('question_'+id, 'click', ckm.Event.openAnswer);
    	new Effect.SlideUp('answer_'+id, {
    		duration: 0.2,
            transition: Effect.Transitions.linear
//    		afterFinishInternal: function(effect) {
//                Element.hide('mt_comment');
//                Event.stopObserving($('post_info'), 'click', ckm.entry_.closeAnswerSlide);
//                Event.observe($('post_info'), 'click', ckm.entry_.openAnswerSlide);
//    		}
    	});
    	return false;
    },
    
//    openAnswer: function(id) {
//        if (!$('answer_'+id)) {
//            return false;
//        }
//        Event.stopObserving('question_'+id, 'click', ckm.Event.openAnswer);
//        Event.observe('question_'+id, 'click', ckm.Event.closeAnswer);
//        Element.show('answer_'+id);
//    },

    closeAnswer: function(id) {
        Element.hide('answer_'+id);
    },
    
    historyQuestion: function() {
       History.address.getVars();
       var id = History.address.getParam('a');
//       alert(id)
       this.openAnswerSlide(id, true);
    }
    
}

ckm.faq_ = new ckm.FAQ;

ckm.Event = {
    openAnswer: function(evt) {
        var obj = Event.element(evt);
        var id = obj.id.replace(/[^0-9]/g, '');
        
        ckm.faq_.openAnswerSlide(id);

        void(0);
        return false;
    }, 
    closeAnswer: function(evt) {
        var obj = Event.element(evt);
        var id = obj.id.replace(/[^0-9]/g, '');
        ckm.faq_.closeAnswerSlide(id);

        void(0);
        return false;
    } 
}

//function q() {
//    History.address.getVars();
//   alert(History.address.getParam('q'))
//}

Event.observe(window, 'load', ckm.faq_.init.bindAsEventListener(ckm.faq_), false);
