jQuery(document).ready(function() {
  picPageNum = 1;
  increasePages = true;
	ref=setInterval('scrollAdsUp()', 25000);
	
  helpModal()
  foursquareModal()
  twitterModal()
  
  $('#logout').click(function (e){
    e.preventDefault();
    FB.getLoginStatus(function(response) {
      if (response.session) {
        FB.logout(function(response) {
        window.location = "/logout"
        });
      } else {
        //alert('no sess');
        window.location = "/logout"
      }
    });
  });
  $('.photo-thumbnail').live('click', function(e) {
    var photoID = 'photo-'+this.id
    var $dialog = jQuery('#'+photoID)
     .dialog({
       autoOpen: false,
       width: 'auto',
       height: 'auto',
       modal: true,
       title: this.alt
     });
     $dialog.dialog('open');
  });
  $('#category_filter').toggle(function() {
		$('#drop_down_menu').show();
	}, function() {
		$('#drop_down_menu').hide();
	});
	
	$('.category').click(function() {
		$('#drop_down_menu').hide();
	});
  
  $('.loadingDiv').hide();
  $('.loadingDiv').ajaxStart(function() {
    $(this).show();
  }); 
  $('.loadingDiv').ajaxStop(function() {
    $(this).hide();
  });

	$('#down-arrow').live('click', function(e) {
		scrollAdsUp();
	})
	$('#up-arrow').live('click', function(e) {
		scrollAdsDown();
	})
	$('.nav_ad_banner').live('click', function(e) {
		e.preventDefault();
		$.post('/increment_click_count', {id: this.id}, null, "script");
	});
  // $('#sponsor-box').mouseenter(function() {
  //     $('#sponsor-overlay').show()
  //   });
  //   $('#sponsor-overlay').mouseout(function() {
  //     $('#sponsor-overlay').fadeOut();
  //   });
    
  
  $('.enter_form input').keydown(function(e) {
    if (e.keyCode == '13') {
       $(this).closest('form').find('.button').click()
     }
  });
  
  
  $('#search-icon').live('click', function() {
    if($('#search-box').is(':visible')){
      $('#search-box').hide();      
     } else {
      $('#search-box').show();
     };
  });
  
  $('.scrollto-more').live('click', function(e) {
    e.preventDefault();
    if ($(this).hasClass('enabled') == true) {
      
      var pageNum = parseFloat($('#page-num').html())
      pageNum++
      $('#recently-tweaten').scrollTo('+=610px', 800, { axis:'y' });
      $.get('/more_food_entries', {page: pageNum}, null, "script")
      $('.back-up').show();
      
    };
  });

  $('.scrollto-more').ajaxStart(function(e) {    
    $('.scrollto-more').removeClass('enabled');
    });
  $('.scrollto-more').ajaxStop(function(e) {    
    $('.scrollto-more').addClass('enabled');
    });

  
  $('.scrollto-less').click(function(e) {
    $('#recently-tweaten').scrollTo('-=610px', 800, { axis:'y' });
  })
  
  
  $('.scrollto-more-pics').click(function(e) {
    e.preventDefault();
    $('.picture-window').scrollTo('+=182px', 400, { axis:'x' });
    clearInterval(ref);
    //$('.scrollto-less-pics img').attr('src', 'images/left_arrow_button.png')
    //$('.back-up').show(); change imarge src of right button for blue
  })
  $('.scrollto-less-pics').click(function(e) {
    e.preventDefault();
    $('.picture-window').scrollTo('-=182px', 400, { axis:'x' });
    clearInterval(ref);
    //$('.scrollto-more-pics img').attr('src', 'images/right_arrow_button.png')
  })
  
  
  $('#email_invite_form_link, #email_invite_form_img').click(function(e) {
    e.preventDefault();
    var $dialogContent = $("#message_create_form");

    $dialogContent.dialog({
       modal: true,
       title: "Invite by email",
       width: 500,
       close: function() {}
    }).show();
  });
	
	setTimeout("$('.notice, .warning, .error').slideUp('slow')", 6000);	
	
	jQuery('.profile_pic').live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			jQuery(this).siblings(".pop").children().show()
		} else {
			jQuery(this).siblings(".pop").children().hide()
		}
	
	});
	
	$('.submit_form').live('click', function(e){
		e.preventDefault();
		$(this).closest('form').submit()
	});

	$('.named').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseout') {
      $(this).prev().find('.floated_name').hide();
    } else {
      $(this).prev().find('.floated_name').show();
    };
  });

	$('.survey-button').live('click', function() {
	    $('.survey-dialog').dialog("close");
	});

  
});


function helpModal() {

  $('.help-link').click(function(e) {
    e.preventDefault();
    var $dialogContent = $(".help-modal");

    $dialogContent.dialog({
       modal: true,
       title: "TWYE Help",
       width: 500,
       close: function() {}
    }).show();
  });
}

function foursquareModal() {

  $('.foursquare-modal-link, .location_pin').click(function(e) {
    e.preventDefault();
    var $dialogContent = $(".foursquare-modal");

    $dialogContent.dialog({
       modal: true,
       title: "TWYE with Foursquare",
       width: 500,
       close: function() {}
    }).show();
  });
}

function twitterModal() {

  $('.twitter-modal-link').click(function(e) {
    e.preventDefault();
    var $dialogContent = $(".twitter-modal");

    $dialogContent.dialog({
       modal: true,
       title: "TWYE with Twitter",
       width: 500,
       close: function() {}
    }).show();
  });
}

function rotateFoodPics() {
  
  if (increasePages == true) {
    picPageNum++
    $('.picture-window').scrollTo('+=182px', 400, { axis:'x' });
    if (picPageNum == 5) {
      increasePages = false
    }
  }else{ 
    $('.picture-window').scrollTo('-=182px', 400, { axis:'x' });
    picPageNum--
    if (picPageNum == 1) {
      increasePages = true
    }
  }
}

function scrollAdsUp() {
	$('#sponsor-box').scrollTo('+=52px', 800, { axis:'y' });
}
function scrollAdsDown() {
	$('#sponsor-box').scrollTo('-=52px', 800, { axis:'y' });
}

function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}




