/*

Code for all facebook items

*/

var FaceBookAppID = "102655026472493";
var BaseURL = "http://www.sunglasshut.com.au/";

function login() {
   FB.login(function (response) {
        if (response.session) {
            if (response.perms) {
                //alert(response.perms);
                userID = response.session.uid;
                var flash = getMovie('myFlashApp');
                flash.fbConnected();
            } else {
                //alert("Logged in but no permission")
            }
        } else {
            //alert("user cancelled or not logged in")
        }
    }, { perms: 'publish_stream,user_photo_video_tags' });
}

function logout() {
        
}

function TestBrands()
{
	// Get the description from the XML
	$.ajax(
		{
			url: BaseURL +"images/flash/brands.xml",
			async: false,
			error: function(xhr, ajaxOptions, thrownError){
				alert(xhr.status);
				alert(thrownError);
			},
			success : function(data){

				//alert( $("brands brand title:contains('Burberry')",data).next().text() );
				
			}
		}
	);
}


function PostProductToWall()
{	
	var productID = document.location.href.split("/")[document.location.href.split("/").length-1];	
	var imageUrl = "";
	var brandTitle = "";
	var brandDescription = "";
	
	brandTitle = productID;
	
	// Get the Product ID from teh URL		
	// Get the image using the product id
	imageUrl = BaseURL +"images/catalogue/"+ productID +"_large.png";	
	
	$.ajax({
	  url: BaseURL +"images/flash/sunglasses.xml",
	  async : false,
	  error: function(xhr, ajaxOptions, thrownError){
			alert("Error Code: "+ xhr.status +" \nDescription: "+ thrownError);
			//alert(thrownError);
	  },
	  success: function(data) {
		$(data).find('item').each(function(){ 
			if($(this).attr('code') == productID)
			{				
				brandTitle = $(this).attr('brand');
			}
		});
	  }
	});	
	
	// Get the brand description
	/*
	$.ajax(
		{
			url: BaseURL +"images/flash/brands.xml",
			async: false,
			error: function(xhr, ajaxOptions, thrownError){
				alert("Error Code: "+ xhr.status +" \nDescription: "+ thrownError);			
			},
			success : function(data){
				brandDescription = $("brands brand title:contains('"+ brandTitle +"')",data).next().text();
			}
		}
	);
	*/
	brandDescription = "All I want is this pair of Sunglasses. Hint Hint.";
		

	// POST IT!
	var title = brandTitle +" - "+ productID
	streamPublish(title, brandDescription, imageUrl , title,  document.location.href , "User Prompt");	
}

//stream publish method
function streamPublish(name, description, imageUrl, hrefTitle, hrefLink, userPrompt) {	
    
	FB.ui({
        method: 'stream.publish',
        message: '',
        attachment: {
            name: name,
            caption: '',
            description: (description),
            href: hrefLink,
			media: [{ 
					type: "image", 
					src: imageUrl, 
					href: imageUrl
			}]	
        },

        action_links: [
                        { text: hrefTitle, href: hrefLink }
                    ],
        user_prompt_message: userPrompt
    },
    function(response) {
			if (response && response.post_id) {
				//alert('Post was published.');
			} else {
				//alert('Post was not published.');
			}
		}
	);	
}

function showStream() {
    FB.api('/me', function(response) {
        //console.log(response.id);
        streamPublish(response.name, 'Thinkdiff.net contains geeky stuff', 'hrefTitle', 'http://thinkdiff.net', "Share thinkdiff.net");
    });
}

function share() {
    var share = {
        method: 'stream.share',
        u: 'http://thinkdiff.net/'
    };

    FB.ui(share, function(response) { console.log(response); });
}

