var rules = function(){
	this.init();
}

rules.prototype = {
	init: function(){
		this.combinations_container = $('#coupon .about .inner');
		this.combinations_block = this.combinations_container.find('.combinations');
		this.combinations_table = this.combinations_block.find('table');
		this.combinations_toggle_link = this.combinations_container.find('.open .pseudo_link');
		this.combinations_toggle_link_text = ['Развернуть', 'Свернуть'];
		
		this.ticket_container = $('#ticket .ticket');
		this.ticket_items = this.ticket_container.find('.left li, .right li');
		this.ticket_links = this.ticket_items.find('.pseudo_link');
		this.highlighted = this.ticket_container.find('.image ul');
		this.highlighted_items = this.highlighted.find('li');
		
		this.examples = $('#examples');
		this.examples_blocks = this.examples.find('.examples');
		this.toggle_all_link = this.examples.find('h2 .pseudo_link');
		this.examples_links = this.examples.find('h3 .pseudo_link');
		
		this.toggle_combinations();
		this.toggle_examples();
		this.highlight();
	},

	toggle_combinations: function(){
		var that = this;
		
		this.combinations_toggle_link.click(function(){
			if( !that.combinations_block.hasClass('opened') ){
				that.combinations_block.animate({height: that.combinations_table[0].offsetHeight}, function(){
					that.combinations_block.addClass('opened');
				});
				$(this).text(that.combinations_toggle_link_text[1]);
			} else {
				that.combinations_block.animate({height: '14em'}, function(){
					that.combinations_block.removeClass('opened');
				});
				$(this).text(that.combinations_toggle_link_text[0]);
			}
		});
	},
	
	toggle_examples: function(){
		var
			that = this,
			toggle_all_link_text = ['развернуть все', 'свернуть все'];
		
		this.toggle_all_link.click(function(){
			if( that.examples_blocks.hasClass('not_display') ){
				that.examples_blocks.removeClass('not_display');
				that.toggle_all_link.text(toggle_all_link_text[1]);
			} else {
				that.examples_blocks.addClass('not_display');
				that.toggle_all_link.text(toggle_all_link_text[0]);
			}
		});
		
		this.examples_links.click(function(){
			$(this).parent().next('.examples').toggleClass('not_display');
			
			if( that.examples_blocks.hasClass('not_display') ){
				that.toggle_all_link.text(toggle_all_link_text[0]);
			} else {
				that.toggle_all_link.text(toggle_all_link_text[1]);
			}
		});
	},
	
	highlight: function(){
		var that = this;

		for (var i = 0; i < this.ticket_links.size(); i++){
			this.ticket_links.eq(i).click(function(){
				that.ticket_links.removeClass('selected');
				that.highlighted_items.addClass('not_display');
				that.highlighted.find('.' + $(this).addClass('selected').parent().attr('class')).removeClass('not_display');
			});
		}
	}
}

$(function(){
	new rules();
});
