 $(document).ready(function() {
	 $('div.hasPages > div > ul.pagecontrol > li a').click(function() {
		 var MAX = $(this).parents('div:first').attr('class').split('_')[1];
		 MAX = MAX.replace('_','');
		
		 var ID = $(this).attr('class').split('_')[1];
		 ID = ID.replace('_','');
		 var N = ID * 1;
		 var P = ID * 1;
		 N = N + 1;
		 P = P - 1;
		 $(this).parents('ul.pagecontrol').children('li').children('a:regex(id, showpageprev_.*)').removeClass();
		 $(this).parents('ul.pagecontrol').children('li').children('a:regex(id, showpageprev_.*)').addClass('showpage_'+P);		 
		 $(this).parents('ul.pagecontrol').children('li').children('a:regex(id, showpagenext_.*)').removeClass();
		 $(this).parents('ul.pagecontrol').children('li').children('a:regex(id, showpagenext_.*)').addClass('showpage_'+N);


		 if (ID == 1) {
			 $(this).parents('ul.pagecontrol').children('li').children('a:regex(id, showpageprev_.*)').hide();
		 }
		 else {
			 $(this).parents('ul.pagecontrol').children('li').children('a:regex(id, showpageprev_.*)').show();
		 }
		 
		 if (ID == MAX) { $(this).parents('ul.pagecontrol').children('li').children('a:regex(id, showpagenext_.*)').hide();}
		 else {$(this).parents('ul.pagecontrol').children('li').children('a:regex(id, showpagenext_.*)').show();}
		 
		 $(this).parents('div.hasPages').addClass('empty');
		 $(this).parents('div.hasPages').children('.isPages').children('.active_page').fadeOut('fast',function() {
			 
			 
			 
			 $(this).parents('div.hasPages').children('.isPages').children('.active_page').hide();
			 $(this).parents('div.hasPages').children('.isPages').children('.active_page').removeClass('active_page');
			 $(this).parents('div.hasPages').children('.isPages').children('.page_'+ID).fadeIn('fast',function() {
				 $(this).parents('div.hasPages').children('.isPages').children('.page_'+ID).show();
				 $(this).parents('div.hasPages').children('.isPages').children('.page_'+ID).addClass('active_page');
				 
			 });
			 $(this).parents('div.hasPages').removeClass('empty');
		 });
		 
		 return false;
	 });
	 $('form.ajaxcommentforms').live('submit',function() { 
		 
    	 var id = $(this).attr('action').split('/')[2];
//    	alert( id); 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
    	 var options = { 
    		        target:        '#ajaxcommentforms_response',   // target element(s) to be updated with server response 
    		        beforeSubmit:  showRequest_Comments,  // pre-submit callback 
    		        success:       showResponse_Comments,  // post-submit callback 
    		 
    		        // other available options: 
    		        url:  '/forms/'+id+'/?ajaxreturn' // override for form's 'action' attribute 
    		        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
    		        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
    		        //clearForm: true        // clear all form fields after successful submit 
    		        //resetForm: true        // reset the form after successful submit 
    		 
    		        // $.ajax options can be used here too, for example: 
    		        //timeout:   3000 
    		    }; 
    	
    	 $(this).ajaxSubmit(options);
//    	 $.scrollTo($("div#form_"+id),400);
    	 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    });
	
	 $('#comments .blockContent').append('<div class="row"><a href="#" id="addanothercomment">Add Another Comment</a></div>');
	 $('#comments a#addanothercomment').hide();
	 $('a#addanothercomment').live('click',function() {
		 $('#comments textarea ').show('fast');
		$('#comments .button').show('fast');
		$('#comments a#addanothercomment').hide('fast');
			return false;
	 });
	 $('.site_helper a').hover(function() {
		 
		 closeTouchFeedBack();
		 $(this).parents('div.reward-detail-col').parents('li').next('li').children('div.reward-detail-col').addClass('behind');
		 $('.site_helper a').addClass('inactive');
		 $('.site_helper').removeClass('active');
		 $('.site_helper.active').removeClass('inactive');
		 $(this).parent('.site_helper').addClass('active');
		 return false;
	 }, 
	  function () {
		 closeHelpers();
		  }
	 );
	 $('html').click(function() {
		closeHelpers();
		closeTouchFeedBack();
	 });
	 
	 $('#player-options li#vote').addClass('fadeout sft');
	 
	 $('#player-options li#vote a.vlink').fadeOut(1);
	 $('#player-options li#vote div.site_helper').hide();
	 $('#player-options li#vote a.vlink').delay(20000).fadeIn('slow', function() {
		 $('#player-options li#vote').removeClass('sft');
		 $('#player-options li#vote div.site_helper').fadeIn('slow');
	 });
		 
	 $('li#vote').append('<div class="feedback" style="display: none;"><h4>Touch Me</h4><div class="inner"><div class="content"></div><p><a href="#" class="ok">OK</a></p></div><div class="bot"></div>');
	 $('#player-options li#vote a.vlink').click(function () {
		 
		 closeHelpers();
		 var href = $(this).attr('href');
		 
		 $.get(href+'&ajx', function(data){
			 	$('div.site_helper').addClass('inactive');
			 	$('div.feedback div.inner div.content').html(data);
			 	$('div.feedback').fadeIn('fast');
			 	var success = $('#vaf_reason').attr('class');
			 	if (success == 'votesuccessful') {
			 		reduceTouchVotes();
			 	}
			});
			return false;
	 });
	 $('div.feedback a.ok').live('click',function() {
		 closeTouchFeedBack();
		 
		 return false;
	 });
	 $('.aj_action a').live('click',function() {
		 var href = $(this).attr('href');
		 var title = $(this).attr('title')
		 $.get(href+'&ajx', function(data){
			 var feedback = data.split('_'); 
			 ajaxFeedback(title, feedback[1]);
			 if (feedback[2]) {
				 $('#'+feedback[2]).unbind();
				 $('#'+feedback[2]).remove();
			 }
			 
		 });
		 return false;
	 });	 
	 $('div#ajax_feedback a').live('click',function() {
		 closeAjaxFeedback();
	 });
	 $('html.hasAjaxFeedback').live('click',function() {
		 closeAjaxFeedback();
	 });
	 
	 
 });
 function closeAjaxFeedback () {
	 $('div#ajax_feedback').fadeOut('200',function() {
		 $('html').removeClass('hasAjaxFeedback');
		 $('div#ajax_feedback').unbind();
		 $('div#ajax_feedback').remove();
	 });
 }
 function ajaxFeedback(title,message) {
	 $('body').prepend('<div id="ajax_feedback" style="display: none;"><h2 class="grey">'+title+'</h2><p>'+message+'</p><p><a href="#">Ok</a></p></div>');
	 $('div#ajax_feedback').fadeIn('200')
	 $('html').addClass('hasAjaxFeedback');
	 
 }
 function closeTouchFeedBack() {
 	 $('div.reward-detail-col.behind').removeClass('behind');
	 $('div.site_helper').removeClass('inactive');
	 $('div.feedback').fadeOut('fast', function() {
		 //$(this).parent('div.feedback').unbind();
		 //$(this).parent('div.feedback').remove();
		 //alert('deleted');
	 });
 }
 function closeHelpers() {
	 $('.site_helper').removeClass('active');
	 $('.site_helper a').removeClass('inactive');
	 
 }

//pre-submit callback 
function showRequest_Comments(formData, jqForm, options) { 
  // formData is an array; here we use $.param to convert it to a string to display it 
  // but the form plugin does this for you automatically when it submits the data 
  var queryString = $.param(formData); 
  // jqForm is a jQuery object encapsulating the form element.  To access the 
  // DOM element for the form do this: 
  // var formElement = jqForm[0]; 

//  alert('About to submit: \n\n' + queryString); 

  // here we could return false to prevent the form from being submitted; 
  // returning anything other than false will allow the form submit to continue 
  return true; 
} 

//post-submit callback 
function showResponse_Comments(responseText, statusText, xhr, $form)  {
  // for normal html responses, the first argument to the success callback 
  // is the XMLHttpRequest object's responseText property 

  // if the ajaxSubmit method was passed an Options Object with the dataType 
  // property set to 'xml' then the first argument to the success callback 
  // is the XMLHttpRequest object's responseXML property 

  // if the ajaxSubmit method was passed an Options Object with the dataType 
  // property set to 'json' then the first argument to the success callback 
  // is the json data object returned by the server 
	var user = $('input#commentdata_user').val();
	var commenting_on = $('input#commentdata_commenting_on').val();
	var for_id = $('input#commentdata_for_id').val();
//	$('#comments textarea').val('');
//	  alert('status: "' + statusText + '"\n\nresponseText: \n"' + responseText + 
//      '"\n\nThe output div should have already been updated with the responseText.');
	
	if (responseText == '') {
		$('#comment_innner').fadeOut('100',function() {
			$.get("/ajax/getandformatcomments/"+user+"_"+commenting_on+"_"+for_id+"/", function(data){
				$('#comment_innner').html(data);
				$('#comment_innner').fadeIn('fast');
				$('#comments textarea ').val('');
				$('#comments textarea ').hide('fast');
				$('#comments .button').hide('fast');
				$('#comments a#addanothercomment').show('fast');
			});
			
		});
		
	}

} 

function reduceTouchVotes () {
	var notv = $('#votes_remaining_indicator span').html();
	notv = parseInt(notv) - 1;
	$('#votes_remaining_indicator span').html(notv);
}


