MAXFEEDS = 3;
FEEDSUSED = 3;
function update_feed_count(direction) {
   if (direction == '-')
   {
	    FEEDSUSED--;
        return true;
   } else {
      if (FEEDSUSED < MAXFEEDS)
      {
	    FEEDSUSED++;
        return true;
      } else {
         return false;
      }
   }

}
function toggleCheckBox(element) {
         i = $(element).attr('id');
        if (!$(element).is(':checked')) {
            update_feed_count('-');
            $('span#enabledword_'+i).html('Click to Enable');
            $("div#feed-content"+i).slideUp("fast");
            $('div#feedcounterror').hide();

        } else {
            if (!update_feed_count('+', element)) {
                  eleOffset = $(element).offset();
              $('div#feedcounterror').css({
              	'position': 'absolute',
              	'top': eleOffset.top - 5,
                  'left': eleOffset.left + 20
              }).html('Maximum of ' + MAXFEEDS + ' total feeds allowed.').show();

                 return false;
            }
            $('span#enabledword_'+i).html('Enabled');
            $("div#feed-content"+i).slideDown("fast");

        }
        $("#rssfeedtitle"+i).toggleClass('deselectedrss');
        return true;
}
function validateCustomRssInput(element, enable) {
        i = $(element).attr('label');
        if (!$(element).is(':checked')) {
            update_feed_count('-');
            /*Change word to disaled and add grey clas*/
            $('span#enabledcustomrssword_'+i).html('Click to Enable');
            /*Disable textboxes and checkboxes*/
            $("input#customrssfeed"+i).attr("disabled", "disabled").val('');
            $("input#customrssfeedfilter"+i).attr("disabled", "disabled").val('');
            $('.showrssfilter[label='+i+']').attr("disabled", "disabled");
            /*Hide error*/
            $('div#feedcounterror').hide();

        } else {
            if (!update_feed_count('+')) {
              /*Display error message beside checkbox*/
              eleOffset = $(element).offset();
              $('div#feedcounterror').css({
              	'position': 'absolute',
              	'top': eleOffset.top - 5,
                  'left': eleOffset.left + 20
              }).html('Maximum of ' + MAXFEEDS + ' total feeds allowed.').show();

                 return false;
            }
            /*Bind filter textbox to checkbox*/
          $('.showrssfilter').unbind().click(function() {
            id = $(this).attr('label');
            if ($('#customrssfeedfilter'+id).css('display') == 'none')
            {
                            $('#customrssfeedfilter'+id).css('display','inline');

            } else {
                             $('#customrssfeedfilter'+id).css('display','none').val('');

            }
          });
          /*Show/enable elements*/
          $('span#enabledcustomrssword_'+i).html('Enabled');
          $("input#customrssfeed"+i).removeAttr("disabled");
          $("input#customrssfeedfilter"+i).removeAttr("disabled");
          $('.showrssfilter[label='+i+']').removeAttr("disabled");


        }
        return true;
}
jQuery(document).ready(function() {

		if ($('#inputreferral').val() != "")
		{
		$('#inputreferral').attr("readonly", true);
		$('#inputreferral').css('backgroundColor','#eeeeee');
		}

        $('form#activateInvitation').submit(function() {
            /*if the user is logged in, proceed as normal. Otherwise open up the ajaxlogin screen*/
           if ($(this).attr('title') == 'loggedin')
           {
              return true;
           } else {
            $().bind('cbox_complete', function(){
            ajax_login_setup('/park');
             });
            $.fn.colorbox({width:"820px", height:"600px", href:'/users/displayajaxscreen?invite='+$('input#activationCode').val(),open:true, title:""});
            return false;
           }
        });
		
		
    	jQuery('a#slidetoggle_customrssfeeds').click(function() {
    		$('div#customrssfeeds').slidetoggle();
    		return false;
    		});

		jQuery('form#ParkAddForm').unbind().submit(function() {

			$('#submitButton').attr('disabled','disabled');
			setOauthCookie();
		    valid = parkaddformvalidate();
			if (valid) {
			   $(this).ajaxSubmit({
						target: '#parkerrors',
						beforeSubmit:  function() {  $('#parkerrors').empty(); $('#img_aparkjaxsubmit').show();},
						success: function(data) {$('#img_aparkjaxsubmit').hide(); $('#submitButton').removeAttr('disabled'); }
				});
			} else {
				$('#submitButton').removeAttr('disabled'); 
			}
            return false;
    		});

		/*Bind filter textbox to checkbox*/
		jQuery('input#showrssfilter1').unbind().click(function() {
			if ($('#customrssfeedfilter1').css('display') == 'none')
				$('#customrssfeedfilter1').css('display','inline');
			else
				$('#customrssfeedfilter1').css('display','none').val('');
		});

		jQuery('input#showrssfilter2').unbind().click(function() {
			if ($('#customrssfeedfilter2').css('display') == 'none')
				$('#customrssfeedfilter2').css('display','inline');
			else
				$('#customrssfeedfilter2').css('display','none').val('');
		});

		jQuery('input#showrssfilter3').unbind().click(function() {
			if ($('#customrssfeedfilter3').css('display') == 'none')
				$('#customrssfeedfilter3').css('display','inline');
			else
				$('#customrssfeedfilter3').css('display','none').val('');
		});

        $("a.togglegetFeeds").click(function() {
            return false;
		}).toggle(
            function() {
            toggle_getFeeds($(this).attr('id'));
            $("img#ajaxgetfeed"+$(this).attr('id')).show();
            },
             function() {
             $("div#ajax_getFeeds"+$(this).attr('id')).hide();
            $(this).html('Show Feeds');
            }
            );

		function toggle_getFeeds(park_id) {
            $.ajax({
               type: "GET",
               url: "/parks/myaccounts_getfeeds/?park_id="+park_id,
			   target: 'div#ajax_getFeeds'+park_id,
               success: function(html){
                  $("a.togglegetFeeds#"+park_id).html('Close');
                  $("div#ajax_getFeeds"+park_id).html(html).show();
                  $("img#ajaxgetfeed"+park_id).hide();


                }
             });
			 return false;
          }
	  });

        function parkaddformvalidate() {
	        var flag=true;
            customfeeds = '';
            $(".feed").each(function() {
               customfeeds += $(this).val();
            });
			$('.error-message').hide();
			$('#parkerrors').html('');
			if(document.getElementById("inputusername").value==""){
				window.scrollTo(0,0);
				$('div#usernameError').show();
				flag=false;
			}
			if ($('[name=session[verified]]').val() == "") {
				//if(document.getElementById("inputpassword").value==""){
					window.scrollTo(0,0);
					$('div#generalError').show();
					document.getElementById("generalError").innerHTML="Please authenticate with Twitter";
					flag=false;
				//}
			}


			return flag;
        }

function setOauthCookie() {
	var exp = new Date();
	exp.setTime(exp.getTime() + (1000 * 60 * 60));     //set it 1 hour ahead 
	document.cookie = "accountname=" + escape($('#inputusername').val()) + "; path=/" + ((exp == null) ? "" : "; expires=" + exp.toGMTString());
}
