// JavaScript Document
$(document).ready(function()
{	

	$('#title_button').click(function() {
		load_letter();		  
	});

	if(!getcookie('flushletter')) {
		//trigger lightbox, set cookie
		load_letter();
		setcookie('flushletter', 'value', '', '/', '', '');
	} 
	
	$('#flush_handle').rotate({
		bind:[
			{"click":function(){
				$(this).rotateAnimation(-30);
				
				if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))) {
					// do nothing
				} else {
					niftyplayer('niftyPlayer1').loadAndPlay('flush.mp3');
				}
				
				
					if($(".flush img").css('top') != '540px') {
						$(".flush img").animate({"top": "+=340px"}, "slow", function() {
							$("#facebox").overlay({ 
								mask: {
									color: '#000',
									loadSpeed: 500,
									opacity: 0.8
								},
								closeOnClick: false,
								load: true
							}).load();															 
						});
					}				
				}
			},
			{"mouseout":function(event){
				$(this).rotateAnimation(0);
			}
		}],
	 });
	
	var rotator_image = $("#flushrotator .scrollable .items div img");
	var rotator_content = $("#flushrotator .scrollable .items div .flushcontent");
	
	// intial display
	$('#fb_message').html('<img src="'+ rotator_image[0].src+ '" class="rotator_image" title="' + rotator_image[0].name + '" />' + rotator_content[0].innerHTML);
	$('.flush').html('<img src="'+ rotator_image[0].src+ '" class="rotator_image"><div class="flush_bottom"></div>');
	
	$("#flushrotator .scrollable").scrollable({
		onSeek: function(event, scroll_index) {
			// here take the current div under items and extract the image
			var image = '<img src="'+ rotator_image[scroll_index].src+ '" class="rotator_image" title="' + rotator_image[scroll_index].name + '" /><div class="flush_bottom"></div>';
			$('.flush').html(image);
			$('#fb_message').html(image + '<p class="description">' + rotator_content[scroll_index].innerHTML + '</p>');
		}
	});
	
	$('#fb_connect_button').click(function(event) {
		var fb_image_src = $('#fb_message img.rotator_image').attr('src');
		var fb_message = $('#fb_message').text();
		var flushed_object = $('#fb_message img.rotator_image').attr('title');
		streamPublish(fb_image_src, fb_message, flushed_object);
	});
});

function load_letter() {
	$("#letter").overlay({ 
		mask: {
			color: '#666',
			loadSpeed: 500,
			opacity: 0.8
		},
		closeOnClick: false,
		load: true,
		fixed: false
	}).load();		
}


<!-- facebook scripts -->
window.fbAsyncInit = function () {
    FB.init({
        appId: '164309266913951',
        status: true,
        cookie: true,
        xfbml: true
    });

    /* All the events registered */
    FB.Event.subscribe('auth.login', function (response) {});

    FB.Event.subscribe('auth.logout', function (response) {});
	
    FB.getLoginStatus(function (response) {

        if (response.session) {}

    });

};

(function () {

    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);

}());

//stream publish method

function streamPublish(fb_img, fb_message, flushed_object) {
    FB.ui(
    {
        method: 'stream.publish',
        message: "The $5.5 trillion the US will spend on national debt interest over the next decade could have been used to: " + fb_message,
        attachment: {

            name: 'Flushing Our Future',
            caption: '{*actor*} just watched '+ flushed_object + ' get flushed down the toilet.',
            description: 'Find out how the government is flushing your future at http://www.ourtab.org/fof',
            href: 'http://www.ourtab.org/fof',
            properties: {
                category: {
                    'text': 'politics',
                    'href': 'http://www.crnc.org'
                },
                ratings: '5 stars'
            },
            media: [{
                type: 'image',
                src: fb_img,
                href: 'http://www.ourtab.org/takeaction'
            }],
        },

        action_links: [
        {
            text: 'Flushing Our Future',
            href: 'http://www.ourtab.org'
        }

        ],
        user_prompt_message: 'Share on Facebook'
    },
    function (response) {
		$("#facebox").overlay().close();
	});
}

// cookie functions
function setcookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ((secure) ? ";secure" : "");
}

function getcookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f
    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}
