jQuery(document).ready(function($) {

	var siteurl = "http://" + window.location.hostname;
	if(siteurl == 'http://localhost'){
		siteurl = window.location.href.split('.com');
		siteurl = siteurl[0] + '.com';
	}
	// TODO Change Theme Directory
	var ajaxURL = siteurl + "/wp-content/themes/learnwake/includes/requests_proc.php";

	// Add current class to the Library navigation menu
	$('#main_nav .requests_navlink').addClass('current');

	$('#request_tabs').tabs();

	function initVoteButtons(){

		$('a.vote_button').button().click(function(e){

			var memberVotes = $(this).text();
			var memberVotesLeft = $('div.votes_left span.number').text();
			var valRadio1;
			var valRadio2;
			var valRadio3;

			$('div.mouseout').remove();
			$('div.popup').remove();

			if(memberVotes == 1){
				valRadio1 = '<input type="radio" name="value" id="value1" value="1" checked="checked" /><label for="value1">1</label>';
			} else {
				valRadio1 = '<input type="radio" name="value" id="value1" value="1" /><label for="value1">1</label>';
			}
			if(memberVotes == 2){
				valRadio2 = '<input type="radio" name="value" id="value2" value="2" checked="checked" /><label for="value2">2</label>';
			} else {
				valRadio2 = '<input type="radio" name="value" id="value2" value="2" /><label for="value2">2</label>';
			}
			if(memberVotes == 3){
				valRadio3 = '<input type="radio" name="value" id="value3" value="3" checked="checked" /><label for="value3">3</label>';
			} else {
				valRadio3 = '<input type="radio" name="value" id="value3" value="3" /><label for="value3">3</label>';
			}
			if (memberVotesLeft < 2){ valRadio2 = ''; }
			if (memberVotesLeft < 3){ valRadio3 = ''; }
			if (memberVotesLeft < 1){
				$(this).parent().append( $('<div class="mouseout"></div>') ).append( $('<div class="popup ui-corner-all"><h4>No Votes</h4><div>You don\'t have any votes left. Try removing some of the votes you\'ve already made.</div></div>') );
			} else {
				$(this).parent().append( $('<div class="mouseout"></div>') ).append( $('<div class="popup ui-corner-all"><h4>How Many?</h4><div id="vote_values">' + valRadio1 + valRadio2 + valRadio3 + '</div></div>') );
			}

			$("#vote_values").buttonset();

			$('input[name=value]').change(function(){
				$('div.mouseout').remove();
				var votes = $(this).val();
				var $voteControls = $(this).parent().parent().parent();
				var requestID = $voteControls.attr('rel');
				var data_string = 'ajax=1&action=update_vote&votes=' + votes + '&id=' + requestID;
				$.ajax({
					type: "POST",
					url: ajaxURL,
					data: data_string,
					success: function(response){
						// Prepare Response
						var r = $.parseJSON(response);
						// Update DOM
						$('#member_stats').html(r.member_stats);
						$('.request').each(function(){
							if( $(this).hasClass(requestID) ){
								$(this).find('.vote_wrapper').html(r.vote_wrapper);
							}
							$('#supporters').html(r.supporters);
						});
						// Remove Popup
						$('div.popup').remove();
						initVoteButtons();
					}
				});
			});

			$('div.mouseout').mouseover(function(){
				$('div.mouseout').remove();
				$('div.popup').remove();
			});
			e.preventDefault();
		});

		$('a.change_vote_button').button().click(function(e){

			var memberVotes = $(this).text();
			var memberVotesLeft = $('div.votes_left span.number').text();
			var valRadio0;
			var valRadio1;
			var valRadio2;
			var valRadio3;

			$('div.mouseout').remove();
			$('div.popup').remove();

			valRadio0 = '<input type="radio" name="value" id="value0" value="0" /><label for="value0">0</label>';
			if(memberVotes == 1){
				valRadio1 = '<input type="radio" name="value" id="value1" value="1" checked="checked" /><label for="value1">1</label>';
			} else {
				valRadio1 = '<input type="radio" name="value" id="value1" value="1" /><label for="value1">1</label>';
			}
			if(memberVotes == 2){
				valRadio2 = '<input type="radio" name="value" id="value2" value="2" checked="checked" /><label for="value2">2</label>';
			} else {
				valRadio2 = '<input type="radio" name="value" id="value2" value="2" /><label for="value2">2</label>';
			}
			if(memberVotes == 3){
				valRadio3 = '<input type="radio" name="value" id="value3" value="3" checked="checked" /><label for="value3">3</label>';
			} else {
				valRadio3 = '<input type="radio" name="value" id="value3" value="3" /><label for="value3">3</label>';
			}
			if ( (parseInt(memberVotesLeft) + parseInt(memberVotes)) < 2){ valRadio2 = ''; }
			if ( (parseInt(memberVotesLeft) + parseInt(memberVotes)) < 3){ valRadio3 = ''; }

			$(this).parent().append( $('<div class="mouseout"></div>') ).append( $('<div class="popup ui-corner-all"><h4>Change Vote?</h4><div id="vote_values">' + valRadio0 + valRadio1 + valRadio2 + valRadio3 + '</div></div>') );

			$("#vote_values").buttonset();

			$('input[name=value]').change(function(){
				$('div.mouseout').remove();
				var votes = $(this).val();
				var $voteControls = $(this).parent().parent().parent();
				var requestID = $voteControls.attr('rel');
				var data_string = 'ajax=1&action=update_vote&votes=' + votes + '&id=' + requestID;
				$.ajax({
					type: "POST",
					url: ajaxURL,
					data: data_string,
					success: function(response){
						// Prepare Response
						var r = $.parseJSON(response);
						// Update DOM
						$('#member_stats').html(r.member_stats);
						$('.request').each(function(){
							if( $(this).hasClass(requestID) ){
								$(this).find('.vote_wrapper').html(r.vote_wrapper);
							}
							$('#supporters').html(r.supporters);
						});
						// Remove Popup
						$('div.popup').remove();
						initVoteButtons();
					}
				});
			});

			$('div.mouseout').mouseover(function(){
				$('div.mouseout').remove();
				$('div.popup').remove();
			});
			e.preventDefault();
		});

		$('#reset_votes_link').click(function(e){
			var targetUrl = $(this).attr("href");
			$("#reset-confirm").dialog({
				resizable: false,
				title: 'Are You Sure?',
				draggable: false,
				modal: true,
				width: 400,
				buttons: {
					'Reset Votes': function() {
						window.location.href = targetUrl;
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});
			e.preventDefault();
		});

		$('a.member_votes').click(function(e){
			var requestID = $(this).attr("rel");
			$("#remove-confirm").dialog({
				resizable: false,
				title: 'Are You Sure?',
				draggable: false,
				modal: true,
				width: 400,
				buttons: {
					'Remove Vote': function() {
						var data_string = 'ajax=1&action=remove_votes&id=' + requestID;
						$.ajax({
							type: "POST",
							url: ajaxURL,
							data: data_string,
							success: function(response){
								// Prepare Response
								var r = $.parseJSON(response);
								// Update DOM
								$('#member_stats').html(r.member_stats);
								$('.request').each(function(){
									if( $(this).hasClass(requestID) ){
										$(this).find('.vote_wrapper').html(r.vote_wrapper);
									}
								});
								$('#supporters').html(r.supporters);
								// Remove Popup
								$('div.popup').remove();
								initVoteButtons();$(this).dialog('close');
							}
						});
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});
			e.preventDefault();
		});

	}

	initVoteButtons();

	$(".top_link").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});

	$('#post_title').keyup(function(){
		var searchString = $(this).val();
		if( searchString != '' ){
			$('#request_suggestions').slideDown();
			var data_string = 'ajax=1&action=search_requests&search='+searchString;
			$.ajax({
				type: "POST",
				url: ajaxURL,
				data: data_string,
				success: function(response){
					// Prepare Response
					var r = response.split('||');
					// Update DOM
					$('#results_heading').html(r[0]);
					$('#search_results').html(r[1]);
					initVoteButtons();
				}
			});
		} else {
			$('#request_suggestions').slideUp();
		}
	});

	$('#show_add_form').click(function(e){
		$('#hidden_inputs').slideDown();
		$('#request_suggestions').slideUp();
		$('#add_request_form h3 label').text('Title:');
		$('#add_request_form').validate({
			rules: {
				'post_title': {required:true},
				'post_content': {required:true}
			},
			messages: {
				'post_title': 'Please enter a title for your request',
				'post_content': 'Please provide a few details about your request'
			}
		});
		e.preventDefault();
	});

	$('#cancel_request').click(function(e){
		$('#hidden_inputs').slideUp();
		$('#request_suggestions').slideUp();
		$('#add_request_form h3 label').text('Make a Request...');
		$('#post_title').val('');
		e.preventDefault();
	});

	$('#add_request_form').submit(function(e){
		if( ! $(this).valid() ){
			$.jGrowl("ERROR! Please check the form", { theme: "ui-state-error", sticky:true });
			e.preventDefault();
		}
	});

	

	// FIXED SIDEBAR
	/*
	var msie6 = $.browser == 'msie' && $.browser.version < 7;
	if (!msie6) {
		var top = $('#request_sidebar').offset().top - parseFloat($('#request_sidebar').css('margin-top').replace(/auto/, 0));
		$(window).scroll(function (event) {
			// if sidebar is smaller than the window viewport, run the javascripts
			if($(window).height() > $('#request_sidebar').height()){
				// what the y position of the scroll is
				var y = $(this).scrollTop();
				// whether that's below the form
				if (y >= top) {
					// if so, ad the fixed class
					$('#request_sidebar').addClass('fixed');
				} else {
					// otherwise remove it
					$('#request_sidebar').removeClass('fixed');
				}
			}
		});
	}
	*/
});

