var VanAqua = VanAqua || {};

(function($){

// Globals
VanAqua = {

	facebook: {
	
		// vars
		loggedIn: false,
		perms: '',
		
		feedPublish: function( name, nameLink, caption, description, picture, message ){
		
		    FB.ui(
		    {
		        method: 'feed',
      		    name: name,
		        link: nameLink,
		        picture: picture,
		        caption: caption,
      		    description: (description),
		        message: message
		    },
		    function(response) {
		 		if (response && response.post_id) {
			      // alert('Post was published.');
			     } else {
			      //alert('Post was not published.');
			     }
		    });
		    
		}, // end feePlblish
		
		
		shareEntry: function( e, link, picture, message ) {

			//e.preventDefault();
			VanAqua.facebook.feedPublish(
				'Family Pass forever', 
				link, 
				'http://apps.facebook.com/vanaqua/',
				'Imagine yourself in our world forever. This fun photo app lets you put your friends\' and family\'s faces in four underwater scenes. The photo that gets the most votes in the contest gallery will win a lifetime family membership!',
				picture,
				message
			);
			
			return false;
		}, // end shareEntry
		
		
		login: function(e)
		{
			var link = $(this);
		
			// skip the login/auth check on the client side
			if( false == VanAqua.facebook.loggedIn ){
		
				//e.preventDefault();
				// TODO: check if logged in first (sometimes user sees window pop-up and close)
				// http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
				
				FB.login(function(response) {
				  if (response.session) {
				    if (response.perms) {
				      // user is logged in and granted some permissions.
				      // perms is a comma separated list of granted permissions
				      
    				  // continue with the link'
				      if(  link.attr( 'target' ) == '_top' ){
				      	window.top.location = link.attr( 'href' );
				      	
				      }else {
				      	window.location = link.attr( 'href' );
				      }
				      
				    } else {
				      // user is logged in, but did not grant any permissions
		      		 
				    }
				  } else {
				    // user is not logged in
				  }
				}, {perms: VanAqua.facebook.perms });
				
				return false;
			}
		} // end login
		
		
	}, // end facebook functions
	
	gallery: {
	
		hoverImg: '',
		hoverOverImg: '../images/gallery/select_image_over.png',
		
		hoverOver: function()
		{
			VanAqua.gallery.hoverImg = $(this).attr("src");
			$(this).attr("src", VanAqua.gallery.hoverOverImg);
		},
		hoverOut: function() 
		{
			$(this).attr("src", VanAqua.gallery.hoverImg);
		},
		
		vote: function( e )
		{
			var button = $(this);
			var link = button.attr( 'href' ) + '/json';
			
			var isLarge = button.hasClass( 'vote_large' );
			
			if( true == VanAqua.facebook.loggedIn ){
			
				button.replaceWith( '<img id="loading_image" src="/images/gallery/loading.gif">' );
			
				$.ajax({
				   type: "GET",
				   url: link,
				   success: function( result ){
				   
				    	// now we need to change the button and the # of votes
				    	//vote_count
				    	
				    	if( result != 'false' ){
				    	
				    		if( true == isLarge ){
				    		
				    			// incr. the vote count
						    	$('#loading_image').parent().siblings( '.vote_col' ).children( '.vote_count' ).html( result );
						    	
						    	// change the button
						    	$('#loading_image').replaceWith( '<img class="checkmark_large" src="/images/gallery/checkmark_large.png">' );
				    		
				    		} else {
				    	
						    	// incr. the vote count
						    	$('#loading_image').parent().siblings( '.vote_count' ).html( result );
						    	
						    	// change the button
						    	$('#loading_image').replaceWith( '<img src="/images/gallery/checkmark.png">' );
					    	}
					    	
				    	}else {
				    		alert( 'An Error Occurred while trying to vote. Please try again later.' );
				    	}
				     
				   }
				 });
			 
			 }
			 
			 return false;
		},
		
		
		
		postToAlbum: function( e )
		{
			var button = $(this);
			var link = button.attr( 'href' );
			
			button.replaceWith( '<img id="loading_image" src="/images/gallery/loading.gif">' );
			
			$.ajax({
			   type: "GET",
			   url: link,
			   success: function( result ){
			   
			    	// now we need to change the button and the # of votes
			    	//vote_count
			    	
			    	if( result != 'false' ){
			    		
			    		$('#loading_image').replaceWith( '<img src="/images/gallery/checkmark.png"> Photo has been posted. View it <a href="' + result + '" target="_blank">here</a>.' );
			    						    	
			    	}else {
			    		alert( 'An Error Occurred while trying to post to your album. Please try again later.' );
			    	}
			     
			   }
			 });
			 
			 return false;
		
		}
		
	
	} // end gallery functions 

}; // end VanAqua


// Init
$(function() {

	// login/authenticate the App if we need to
	$('.user_login').click( VanAqua.facebook.login );

	// setup ajax voting	
	$('a.vote').click( VanAqua.gallery.vote );
	$('a.vote_large').click( VanAqua.gallery.vote );
    
	$('a#tweet').click(function(){
		window.open(this.href, '', 'width=550,height=450' );
		return false;
	});
	
	$('.gallery_hover').hover( VanAqua.gallery.hoverOver, VanAqua.gallery.hoverOut );
	
	$('#privacy_policy_link').fancybox({
		'titlePosition'		: 'inside',
		'titleShow'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayOpacity'	: 0.45
	});
	
	$(".contest_rules").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayOpacity'	: 0.45,
		'titleShow'			: true
	});
	
	// post to album
	$("#post_album_popup_link").fancybox({
		'titlePosition'		: 'inside',
		'titleShow'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayOpacity'	: 0.45
	});
	
	$('a#post_album_link' ).click( VanAqua.gallery.postToAlbum  );
	
	
	// add abosulte anchor links b/c we're using the base tag
	/*
	$("a[href^='\#']").click(function(e){
  		e.preventDefault();
  		document.location.hash=this.href.substr(this.href.indexOf('#')+1);
	})*/
	
	/* Fix a bug with Firefox & Flash (The Flash Settings buttons were not clickable) 
	see -- http://fbexchange.net/questions/85/unable-to-interact-with-flash-settings-dialog-in-firefox */
	try{
	    if ($.browser.mozilla) {
	        $(window).resize( $("#flash").css('margin-left', window.outerWidth % 2 ? '0' : '0.5px') ).resize();
	    }
	}catch(ex){}
	
});


})(jQuery);
