$(document).ready(function() {
	
	$(".menu ul li").hover(
		function() { $(this).toggleClass("menu_off"); $(this).toggleClass("menu_over"); },
		function() { $(this).toggleClass("menu_off"); $(this).toggleClass("menu_over"); }
	);
	
	
	$(".login_register").click(
		function() {
			$("a#login_tip").trigger('click'); }
	);
	
	
	
	//setup functionalities
	
	var submenu_sections = new Array("bills:recent", "bills:popular", "bills:highest_rated", "bills:featured",
									 "bill_sections:recent", "bill_sections:popular", "bill_sections:highest_rated", "bill_sections:featured",
									 "sections:number", "sections:recent", "sections:featured", "sections:highest_rated");
	renew_ajax();
	
	
	
	//functionalities
	
	function set_submenu_ajaxclick(section) {
		var section_split = section.split(':');
		
		var sectiontype = section_split[0];
		var filter = section_split[1];
		
		var section_id = '#'+sectiontype+'_'+filter;
		
		$(section_id).click(
			function() {
				$.ajax({
					url: Croogo.basePath+sectiontype+'_ajax',
					type: 'POST',
					data: { filter: filter,
							q: q,
							session: Croogo.params['named']['session'],
							type: Croogo.params['named']['type'] },
					beforeSend: function() {
						unbind_ajax();
						$('#'+sectiontype).find('div.loading').show();
					},
					success: function(data) {
						ajax = data.split('{--content--}');
						
						//ajax[0] == filter, ajax[1] == paging, ajax[2] == content
						window.filter = ajax[0];
						$('#'+sectiontype).find('div.paging').html(ajax[1]);
						$('#'+sectiontype).find('div.snapshots').html(ajax[2]);
						$(".pagopt").unbind('click');
					},
					complete: function() {
						$('#'+sectiontype).find('div.loading').hide();
						renew_ajax();
					}
				});
			}
		);
		
		$(".pagopt").click(
			function() {
				sectiontype = $(this).parent().parent().attr('id');
				$.ajax({
					url: Croogo.basePath+sectiontype+'_ajax',
					type: 'POST',
					data: { offset: $(this).html(),
							filter: window.filter,
							q: q,
							session: Croogo.params['named']['session'],
							type: Croogo.params['named']['type'],
							number: Croogo.params['named']['number'] },
					beforeSend: function() {
						unbind_ajax();
						$('#'+sectiontype).find('div.loading').show();
					},
					success: function(data) {
						ajax = data.split('{--content--}');
						
						//ajax[0] == filter, ajax[1] == paging, ajax[2] == content
						window.filter = ajax[0];
						$('#'+sectiontype).find('div.paging').html(ajax[1]);
						$('#'+sectiontype).find('div.snapshots').html(ajax[2]);
						$(".pagopt").unbind('click');
					},
					complete: function() {
						$('#'+sectiontype).find('div.loading').hide();
						if(sectiontype == 'sections') {
							tinymce_init(); }
						renew_ajax();
					}
				});
			}
		);
	}
	
	
	
	function set_tracking_ajax() {
		$(".track_button").click(
			function() {
				this_element = $(this).parent();
				old_content = this_element.html();
				$.ajax({
					url: Croogo.basePath+'trackings/ajax_track',
					type: 'POST',
					data: { bill_or_section: $(this).next().html(),
							id: $(this).parent().attr('id') },
					beforeSend: function() {
						this_element.html('<img src="http://simplifycongress.org/img/star_sm.gif" />');
						unbind_ajax();
					},
					success: function(data) {
						if(data.indexOf("{--login--}") >= 0) {
							$("a#login_tip").trigger('click');
							this_element.html(old_content); }
						else {
							this_element.replaceWith("<li class='bill_already_tracking'>now tracking</li>"); }
					},
					complete: function() {
						renew_ajax(); }
				});
			}
		);
	}
	
	
	
	function set_vote_ajax() {
		$(".vote").click(
			function() {
				var vote_parent = $(this).parent();
				var vote_loading_elem = $(this).parent().prev();
				var old_value;
				
				$.ajax({
					url: Croogo.basePath+'simplifications/ajax_vote',
					type: 'POST',
					data: { yaynay: $(this).find('div').html(),
							id: $(this).parent().attr('id') },
					beforeSend: function() {
						old_value = vote_loading_elem.html();
						vote_loading_elem.html('<img src="http://simplifycongress.org/img/star_sm.gif" />');
						unbind_ajax();
					},
					success: function(data) {
						if(data.indexOf("{--login--}") >= 0) {
							vote_loading_elem.html(old_value);
							$("a#login_tip").trigger('click');
						}
						else {
							data_split = data.split('::');
							rating_elem = $('#'+data_split[1]).prev();
						
							if(data_split[0] == 1) {
								if(!rating_elem.hasClass('positive')) { rating_elem.toggleClass('positive'); }
								if(rating_elem.hasClass('neutral')) { rating_elem.toggleClass('neutral'); }
							}
							else if(data_split[0] == -1) {
								if(!rating_elem.hasClass('negative')) { rating_elem.toggleClass('negative'); }
								if(rating_elem.hasClass('neutral')) { rating_elem.toggleClass('neutral'); }
							}
							else if(data_split[0] == 0) {
								if(rating_elem.hasClass('positive')) { rating_elem.toggleClass('positive'); }
								if(rating_elem.hasClass('negative')) { rating_elem.toggleClass('negative'); }
								rating_elem.toggleClass('neutral');
							}
							
							vote_loading_elem.html(data_split[0]);
							vote_parent.replaceWith("<div class='already_voted'>Thank you for voting<br />on this summary.</div>");
						}
					},
					complete: function() {
						renew_ajax(); }
				});
			}
		);
	}
	
	
	
	
	
	
	//ajax function
	
	function renew_ajax() {
		for(var i=0;i<submenu_sections.length;i++) {
			set_submenu_ajaxclick(submenu_sections[i]); }
		set_vote_ajax();
		set_tracking_ajax();
	}
	function unbind_ajax() {
		$('.ajaxbutton').unbind('click');
		$('.vote').unbind('click');
		$('.pagopt').unbind('click');
		$('.track_button').unbind('click');
	}
	



	
	//fancybox functions
	
	$("a#login_tip").fancybox({
		'hideOnContentClick': false,
		'centerOnScroll': true,
		'frameWidth' : 500,
		'frameHeight' : 500
	});

	
	
	
	
	//general functions
	
	Array.prototype.has=function(v){
		for (i=0; i<this.length; i++){
			if (this[i]==v) return true; }
		
		return false;
	}
	

});
