/*! $Id: fb.min.js 8911 2010-03-10 09:43:46Z jagarwal $ */
/*global jQuery: false, setFlashParams: true, window: false, FB: false */

//noinspection BadExpressionStatementJS
"use strict";
/**
 * Lorem Ipsum!
 *
 * @author	www.ignitiongroupe.com
 * @version	0.1.0
 */
// JavaScript Document FB stuff start
if (window.fbPublishTitle === undefined) {
	var fbPublishTitle = 'THE NEW CHEVROLET SPARK - IT ALL STARTS WITH A SPARK';
}
if (window.fbPublishCaption === undefined) {
	var fbPublishCaption = '{*actor*} has just customised the SPARK world with Facebook.'; 
}

if (window.fbPublishDescription === undefined) {
	var fbPublishDescription = 'Spark your world NOW!';
}


var appApiKey = "d8dc8d583089a0c1ff578060ede7e7b6",
//var appApiKey = "38bc74ecba177ef8a9ed64a9eb9b1c0d",
	crossDomainFile = "/fbcrossdomain.html",
	facebookServPath = "http://community.chevroleteurope.com/delivery-service/?type=facebook",
//	facebookServPath = "http://preview.fbtest.chevroleteurope.com/socialmedia/?type=facebook",
	imageStartIndex,
	imageCount,
	textStartIndex,
	textCount,
	newLogin = 0;

// This check is necessary for the experience section in order to avoid run time errors on pages
// that do not include the FB import 
/*if ("object" === typeof(FB)) {
	FB.init(appApiKey, crossDomainFile);
}*/

function readCookie(cookieName) {
	var theCookie = "" + document.cookie,
		ind = theCookie.indexOf(cookieName),
		ind1;

	if (ind === -1 || "" === cookieName) {
		return "";
	}

	ind1 = theCookie.indexOf(';', ind);
	if (ind1 === -1) {
		ind1 = theCookie.length;
	}
	return decodeURIComponent(theCookie.substring(ind + cookieName.length + 1, ind1));
}
function publish() {
	//var userName = readCookie('lxe');	// UNUSED?!
/*	var	attachment = {'media': [
			{'type': 'image',
				'src': 'http://www.em.spark-launch-ms.chevroleteurope.com/assets2/images/global/funny-pictures-your-cat-is-bursting-with-joy1.jpg',
				'href': window.location.href}
		]};*/
		//commentMsg = " has just customised the SPARK world with Facebook.Spark your world NOW!";
	var url = 'http://' + document.location.host + '/assets2/content/global/spark-launch/fb_image.jpg';
	var attachment = { 'name': fbPublishTitle , 
			   'href': 'http://' + document.location.host, 
			   'caption': fbPublishCaption , 
			   'description': fbPublishDescription , 
			   'media': [{ 'type': 'image', 'src': url , 
			                'href': window.location.href , 'width': '90', 'height': '60'}] };
	FB.Connect.streamPublish(null, attachment, null, null, null, null, false, null);
}
function loadData() {
	var sessionKey = readCookie(appApiKey + '_session_key'),
		userId = readCookie(appApiKey + '_user'),
		url = facebookServPath;

	//url = url + "&sid=" + Math.random();
	url +=	"&textStartOrder=" +
			textStartIndex +
			"&textCount=" +
			textCount +
			"&imgStartOrder=" + imageStartIndex +
			"&imgCount=" + imageCount +
			"&sessionKey=" +
			sessionKey +
			"&userId=" + userId + "&callback=?";

	jQuery.ajax({
		url: url,
		type: "GET",
		dataType: "json",
		success: function (result) {
			setFlashParams("s002", null, result.toString());
			if (1 === newLogin) {
				publish();
				newLogin = 0;
			}
		}
	});

}
function fbShare(e) {
	var i = "http://www.facebook.com/sharer.php?u=$(UrlToShare)&t=$(TitleOfContent)",
		c = encodeURIComponent,
		u = c(window.location.href),
		t = c(window.document.title),
		s = i.replace(/\$\(UrlToShare\)/, u).replace(/\$\(TitleOfContent\)/, t);

	window.open(s,"share_fb","toolbar=0,status=0,width=626,height=436");
	e.returnValue = false;
	if (e.preventDefault) {
		e.preventDefault();
	}
	return false;
}
//function requestFacebookData() {
function requestFacebookData(parImageStartIndex, parImageCount, parTextStartIndex, parTextCount) {
//alert('req fb call');
	imageStartIndex = parImageStartIndex;
	imageCount = parImageCount;
	textStartIndex = parTextStartIndex;
	textCount = parTextCount;
	var loggedIn = false;
	FB.init(appApiKey, crossDomainFile);
	FB.ensureInit(function() { 
	FB.Connect.get_status().waitUntilReady(function (status) {

		switch (status) {
		case FB.ConnectState.connected:
			loggedIn = true;
			break;
		case FB.ConnectState.appNotAuthorized:
		case FB.ConnectState.userNotLoggedIn:
			newLogin = 1;
			FB.Connect.requireSession(loadData, deleteFlashCookie);
			loggedIn = false;
		}
		});
		if (loggedIn) {
			loadData();
		}
	}); 
}
jQuery(document).ready(function () {
	jQuery("div.shareOn a.shareOnLink[@href=#fbjscall]").click(fbShare);
});