/**
 * See companies lightbox viewer
 * 
 * @author Ollie Maitland
 * @copyright Byng Systems LLP
 * @requires Mootools-1.2
 */
Se.companies = new Class(
{
	/**
	 * Implement Options and ByngView
	 *  
	 */
	Implements: [Options, ByngView],
	
	/**
	 * Holds meta data for ByngView
	 * 
	 */
	meta: {
		file    : 'se.companies.js',
		version : '0.1',
		gateway : {'package' : 'site', 'module' : 'company_view'}
	},
	
	/**
	 * Stack to hold the companies being compared
	 * 
	 * @type Array
	 */
	comparing : [],
	
	/**
	 * Holds the selected topic
	 * 
	 * @type Integer
	 */
	topicId 	: null,
	
	/**
	 * Holds the questionId
	 * 
	 * @type Integer
	 */
	questionId 	: null,
	
	/**
	 * Holds the relative question param
	 * 
	 * @type Integer
	 */
	relativeTo 	: null,	
	
	/**
	 * Start the company view interface
	 * 
	 */
	initialize : function ()
	{
		switch (this.getProperty('view')) {
			case "profile":
				this.loadView();
				
				var companyInfo = $('se-tab_block');
				if ( companyInfo ) {
					// attach tips
					var tips = companyInfo.getElements('.sponsor_tip');
					tips.each(function(tip) {
						var content = tip.getElement('div').clone().setStyle('display','block');
						tip.store('tip:title', '');
						tip.store('tip:text', content);
						tip.getElement('div').destroy();
					});
					var sponsoredTips = new Tips(tips, {
						
					});
					
				}
				
			break;
			case "list":
			
			break;
		}
	},
	
	/**
	 * Load the lightbox view
	 * 
	 */
	loadView : function ()
	{		
		// get the company view from the script file
		this.companyId  = this.getProperty('companyId');
		this.questionId = this.getProperty('questionId');
		
		$$('.st-company_view').each(function(tbl) {
			
			// loop over view links
			tbl.getElements('a.view').each(function(el){
				SqueezeBox.assign(el, {
					size: {x: '800', y: (window.getHeight()-100)},
					ajaxOptions: {
						method: 'get'
					},
					onUpdate: this.attachBehaviours.bind(this),
					onClose : this.reset.bind(this),
					zIndex : 500
				});	
				el.addEvent('click', this.setQuestionIdByEvent.pass([el], this));
			}.bind(this));
		}.bind(this));

		// attach behaviour to tab area
		var content = $('se-company-response');
		if ( content ) {
			this.attachBehaviours( content );
		}
	},
	
	/**
	 * Reset the view
	 * 
	 */
	reset : function ()
	{
		this.comparing  = [];
		this.questionId = null;
		this.topicId = null;
	},
	
	/**
	 * Get the questionId from the element clicked
	 * 
	 * @param Event e
	 */
	setQuestionIdByEvent : function ( el )
	{
		this.questionId = el.getProperty('accesskey');
	},
	
	/**
	 * Get the generic compare request 
	 * 
	 * @return ByngRequest
	 */
	getCompareRequest : function ()
	{
		return this.getRequest('compare')
					.addParam('companyId', this.companyId)
					.addParam('questionId', this.questionId)
					.addParam('topicId', this.topicId)
					.addParam('comparing', this.comparing.join('|'));
	},
	
	/**
	 * Load a question
	 * 
	 * @param Event e
	 */
	loadQuestion : function ( e )
	{
		var params = Byng.ui.fromIdTag(e);
		this.questionId = params['questionId'];
		var request = this.getCompareRequest()
							.addParam('questionId', this.questionId);
		SqueezeBox.setContent('ajax', request.composeAsString());
	},
	
	/**
	 * Load topics
	 * 
	 * @param Event e
	 */
	loadTopic : function ( e )
	{
		var topicId = e.target.getProperty('value');
		var request = this.getCompareRequest()
							.addParam('questionId', null)
							.addParam('topicId', topicId);
		SqueezeBox.setContent('ajax', request.composeAsString());
	},
	
	/**
	 * Toggle company comparison table
	 * 
	 * @param Event e
	 */
	toggleCompanyComparison : function ( e )
	{
		if (e.target.get('tag') == 'input') {
			var selected = e.target.value;
		} else {
			var selected = this.normalise(e.target);
		}
		if (this.comparing.contains(selected)) {
			this.comparing.erase(selected);
		} else {
			this.comparing.push(selected);
		}
		var request = this.getCompareRequest();
		SqueezeBox.setContent('ajax', request.composeAsString());
	},
	
	/**
	 * Toggle comment box
	 * 
	 * @param Event e
	 */
	toggleComment : function ( e )
	{
		e.stop();
		var comments = $('se-comment_block');
		Byng.ui.toggle( comments );
		if ( comments.hasClass('hide') == false ) {
			Byng.app.comments.attachBehaviours( comments );
		}
		return;
		e.stop();
		var companyId = this.normalise(e.target);
		var request = this.getRequest('comment')
							.addParam('companyId', (companyId ? companyId : this.companyId))
							.addParam('questionId', this.questionId);

		SqueezeBox.setContent('ajax', request.composeAsString());
	},
	
	/**
	 * Toggle compare box
	 * 
	 * @param Event e
	 */
	toggleCompare : function ( e )
	{
		Byng.ui.toggle( $('se-compare_table') );
		if ( $('se-compare_table').getStyle('display') == 'block' ) {
			$(e.target).addClass('se-compare_toggle_hide');
		} else {
			$(e.target).removeClass('se-compare_toggle_hide');			
		}
	},

	/**
	 * Toggle compare box
	 * 
	 * @param Event e
	 */
	toggleGuideline : function ( e )
	{
		Byng.ui.toggle( $('se-guideline_info') );
		if ( $('se-guideline_info').getStyle('display') == 'block' ) {
			$(e.target).addClass('se-guideline_toggle_hide');
		} else {
			$(e.target).removeClass('se-guideline_toggle_hide')
		}		
	},
	
	/**
	 * Move to next question
	 * 
	 * @param Event e
	 */
	nextQuestion : function ( e )
	{
		var request = this.getCompareRequest()
						.addParam('navigation', 'next')
						.addParam('relativeTo', this.questionId)
						.addParam('questionId', null);
		SqueezeBox.setContent('ajax', request.composeAsString());
	},
	
	/**
	 * Move to previous question
	 * 
	 * @param Event e
	 */
	previousQuestion : function ( e )
	{
		var request = this.getCompareRequest()
						.addParam('navigation', 'previous')
						.addParam('relativeTo', this.questionId)
						.addParam('questionId', null);
		SqueezeBox.setContent('ajax', request.composeAsString());
	},
	
	/**
	 * Add attribute
	 * 
	 * @param Event e
	 */
	targetBlank : function ( e )
	{		
		var anchorTarget = $$('.se-q_partner a');
		anchorTarget.set('target', '_blank');
	},
	
	/**
	 * Attach behaviours to the DOM element
	 * 
	 * @param Element content
	 */	
	attachBehaviours : function (content)
	{
		if (content.getElement('.se-compare')) {
			
			// topic/question navigation
			content.getElements('.se-breadcrumb li a').addEvent('click', this.loadQuestion.bind(this));
			
			// next/previous questions
			content.getElements('.se-breadcrumb a.se-next').addEvent('click', this.nextQuestion.bind(this));
			content.getElements('.se-breadcrumb a.se-prev').addEvent('click', this.previousQuestion.bind(this));
			
//			content.getElements('.se-cat_select select').addEvent('change', this.loadTopic.bind(this));
			
			var closeBtns = content.getElements('.se-close_compare');
			if (closeBtns.length > 0) {
//				closeBtns.addEvent('click', this.toggleCompanyComparison.bind(this));
			}
			
			// toggle elements
			content.getElements('.se-comment_toggle').addEvent('click', this.toggleComment.bind(this));
			content.getElements('.se-compare_toggle').addEvent('click', this.toggleCompare.bind(this));
			content.getElements('.se-guideline_toggle').addEvent('click', this.toggleGuideline.bind(this));
			content.getElements('.se-company_current').addEvent('click', function(e) { e.stop(); SqueezeBox.close(); });
			
			// add/remove companies from the compare block
//			content.getElements('.qagrid td input').addEvent('click', this.toggleCompanyComparison.bind(this));
			
			//add atrribute
			content.getElements('.se-q_partner').addEvent('mouseover', this.targetBlank.bind(this));

			// reconcile any step in questionIds
			var questionId = content.getElement('.se-breadcrumb li.current a').getProperty('accesskey');
			if (questionId != this.questionId) this.questionId = questionId;
			
		} else if (content.getElement('.se-comment')) {
			new Se.comments(this).attachBehaviours( content.getElement('#se-comment-entry') );			
		}
		
			
		// create the tooltips  
		 var tipz = new Tips(content.getElements('.se-tool_tips'),{  
			 className: 'se-tool_tips',
			 fixed: false,  
			 hideDelay: 50,  
			 showDelay: 50  
		 });		
	}
});

window.addEvent('domready', function() 
{

	// register companies app
	Byng.app.companies = new Se.companies;	
	
	// Set the input handler
	Byng.setTransit( new ByngTransit );




});