//

jQuery(document).ready(function() {
    jQuery(".popup").each(function() {
        jQuery(this).bind('click', function() {
            popupRaisin(jQuery(this).attr('href'));
            return false;
        });
    });

   // jQuery("a.fb_share").bind("click", function() { fb_wallpost(); return false; });
});

function getRasinURI(dedication_id) {
    var uri = raisin_uri + '&CF4=' + dedication_id;
    jQuery('.navigation .donate').attr('href', uri);
    return uri;
}

function popupRaisin(url) {
    newwindow = window.open(url,'PMHF','height=700,width=900,scrollbars=yes');
    if (window.focus) {newwindow.focus()}
    return false;
}

function fb_wallpost(dove_id, fname) {
	
    var title = 'Doves of Hope';
    // flash may call this, passing in a dove id and name
    if (dove_id && fname) {
        var image       = "http://www.dovesofhope.ca/images/doves/dove_" + dove_id + ".png";
        var description = fname + " just dedicated this Dove of Hope through The Princess Margaret Hospital Foundation to inspire everyone fighting to conquer cancer!";
        var href        = "http://apps.facebook.com/dovesofhope/index/dove-gallery/dove_id/" + dove_id;
    } else {
        var title       = jQuery('meta[property=og:title]').attr("content");
        var image       = jQuery('meta[property=og:image]').attr("content");
        var description = jQuery('meta[property=og:description]').attr("content");
        var href        = jQuery('meta[property=og:url]').attr("content");
    }

    FB.ui({
        method: 'stream.publish',
        user_message_prompt: 'Tell your friends about Doves of Hope',
        message: '',
        attachment: {
            'name' : title,
            'href' : href,
            'caption' : '',
            'description' : description,
            'media' : [{'type' : 'image',
                        'src' : image,
                        'href' : href
                      }]
        },
        action_links: [
           { text: 'Action Link', href: 'http://apps.facebook.com/dovesofhope/' }
         ]
    },
    function(response) {
        if (response && response.post_id) {
            //alert('Post was published!');
        } else {
            //alert('Post was not published!!');
        }
    });
    return false;
}

//post dove to friend's wall
Doves_FB = {};
function showPostModal(access_token) {
    var width  = 620;
   // var height = 250;
    var height = 350;

    jQuery('<iframe frameborder="0" name="modalpost" src="http://bart.dovesofhope.shareframe.net:8801/index/friends-post/?doveid=' + Doves_FB.dove_id + '&access_token=' + access_token + '" />').dialog({
        title: 'Send your Dove of Hope to your friends',
        autoOpen: true,
        width: width,
        height: height,
        position: [70, 20],
        modal: true,
        resizable: false,
        autoResize: true,
        overlay: {
            opacity: 0.5,
            background: "black"
        }
    }).width(640).height(370);
    return false;
}

function friendspost(dove_id) {
    Doves_FB.dove_id = dove_id;

    FB.getLoginStatus( function(response) {
        if(response.session) {
            var q = FB.Data.query( 'SELECT publish_stream FROM permissions WHERE uid = ' + response.session.uid);
            q.wait( function(perms) {
               if ('1' == perms[0].publish_stream) {
                   showPostModal(response.session.access_token);
               } else {
                   fb_login();
               }
            });
        } else{
            fb_login();
        }
    });
    return false;
}

function fb_login() {
    FB.login(function(response) {
        if (response.session) {
          if (response.perms) {
            showPostModal(response.session.access_token)
          } else {
    //console.log("Logged in no perms");
          }
        } else {
    //console.log("Not logged in");
        }
      }, {perms:'publish_stream'});
}

function sendShare(sh_message, sh_name, sh_desc)
{
	alert(canvas_url);
	FB.ui({
		method:		'stream.publish',
		display: 	'dialog',
		message: 	sh_message,
		app_id:		this.app_id,
		attachment: {
						name		: sh_name, 
						description	: sh_desc,
						href		: share_url,
						media		: [{'type':'image', 'src':'http://nourish2.xtual.com/img/pmhf_dove.jpg', 'href':share_url}]
					}
	});
}

/* Wrapper for sendShare() above */
//function facebookShare()
//{
//	var sh_message 	= "Doves of Hope";
//	var sh_name		= "Doves of Hope";
//	var sh_desc		= "Doves of Hope are flying at Princess Margaret Hospital to honour and inspire patients, cancer survivors, caregivers, and medical staff. See hope fly at www.dovesofhope.ca";
//	sendShare(sh_message, sh_name, sh_desc);
//}
