$(function() {
	GigyaController.Init();
	
	$("#"+GigyaController.CONT_ID).dialog("destroy").dialog({
		autoOpen:false,
		draggable:true,
		resizable:false,
		modal:true,
		overlay:{opacity:.6,background:"black"},
		dialogClass:"",
		position:"center",
		height:"auto",
		width:513,
		show:{effect:"fade",speed:500},
		hide:{effect:"fade",speed:250},
		open:function(){},
		close:function(){}
	}).show();
});




var GigyaController={
	CONT_ID:'dvWildfireCont',
	GetConfig:function(_type){	
		var sUIConfig = '<config baseTheme="v2"><display showBookmark="false" ';
		switch(_type){
			case "myspace":
				sUIConfig += 'networksToShow="myspace" networksWithCodeBox="*" ';
			break;
			case "twitter":
				sUIConfig += 'networksToShow="twitter" twitterBookmarkURL=""  ';
			break;
			default:
				sUIConfig += "";
		}
		sUIConfig += 'showEmail="false"></display></config>';
		return {
		  useFacebookMystuff: 'false', 
		  includeShareButton: 'true',
		  defaultContent: getEmbedHTML(),				//<------------- Changing this does not seem to affect twitter 
		  myspaceContent: getMySpaceEmbedHTML(),
		  twitterBookmarkURL: getTwitterContent(), 
		  UIConfig:sUIConfig
		};
	},
	Init:function(){
		LoadScript("http://cdn.gigya.com/wildfire/js/wfapiv2.js");
	},
	Open:function(_type){
		$("#" + this.CONT_ID ).dialog("open");
		$('.ui-dialog-titlebar-close').css("display","none");
	
		Wildfire.initPost('757341', 'divWildfirePost', 400, 300, this.GetConfig(_type));
		Wildfire.onPostProfile = GigyaController.handleGigyaPostComplete;
		Wildfire.onEmail = GigyaController.handleGigyaPostComplete;
	},
	Close:function(){
		$("#" + this.CONT_ID ).dialog("close");
	},
	handleGigyaPostComplete: function(eventObj){
		//alert("completed");
	}
};

var zwinkyURL = "";

function showGigyaShortenURL(a_url, a_type){
	var tempArray = [a_url];
	BitlyAPI.setConfig(getConfig());
	BitlyAPI.shortenURL(tempArray,function(a_jsonObj){
			if(a_jsonObj.errorCode =="0")
				showGigya(a_jsonObj.results[a_url]['shortUrl'], a_type);
			else
				alert("There was an error with bit.ly, please try again later.");
		});
}

function getConfig(){
	return {
			bitlyConfig:{
				login: 		"bulldog",
				apiKey: 	"R_38562e6c99903b937119cb4bad551819",
				version:	"2.0.1",
				format:		"json"
			}
		};
}

function showGigya(a_url, a_type){
	zwinkyURL = a_url
	GigyaController.Open(a_type);
}

function LoadScript(url){
 var e = document.createElement("script");
 e.src = url;
 e.type="text/javascript";
 document.getElementsByTagName("head")[0].appendChild(e);
}

//hacks
var sUserName="dog";


function getParentPtnrVal(){

	var sParentPtnrVal= zInterface.getTBPartnerVal()
	if ($('ToolbarCtlMWS').P){
		var tmpArray = $('ToolbarCtlMWS').P.split('_');
		sParentPtnrVal = tmpArray[tmpArray.length-1];
	}
	return sParentPtnrVal;
}

function getTwitterContent(){
	return zwinkyURL; 
}

function getMySpaceEmbedHTML(){
	var sEmbedPtnrVal='ZJzeb007';
	var sParentPtnrVal=getParentPtnrVal();
	var sPVal=sEmbedPtnrVal+'%255F'+sParentPtnrVal;
	
	var embedLink='<div id="fwpAvatar" style="width:226px; height:280px;overflow:hidden;">'+
					'<a href="http://plugin.smileycentral.com/http%253A%252F%252Fzwinky%252Esmileycentral%252Ecom%252Fdownload%252Findex%252Ejhtml%253Fpartner%253D'+
					sPVal+'%2526spu%253D1%2526feat%253Dprof%2526ver%253D2/page.html" >'+
					'<img src="http://plugin.smileycentral.com/http%253A%252F%252Fak%252Eimgfarm%252Ecom%252Fimages%252Ffunwebproducts%252Fpromos%252Fzwinky%252Fprofile%252Egif/image.gif" '+
						'border="0"/><div>'+
					'<img src="'+zwinkyURL+'" />'+
					'</a></div>';
	return embedLink;
}

function getEmbedHTML(){
	var sEmbedPtnrVal='ZJzeb007';
	var sParentPtnrVal=getParentPtnrVal();
	var sPVal=sEmbedPtnrVal+'%255F'+sParentPtnrVal;
	
	var embedLink='<div id="fwpAvatar" style="width:226px; height:280px;overflow:hidden;">'+
					'<a href="http://plugin.smileycentral.com/http%253A%252F%252Fzwinky%252Esmileycentral%252Ecom%252Fdownload%252Findex%252Ejhtml%253Fpartner%253D'+
					sPVal+'%2526spu%253D1%2526feat%253Dprof%2526ver%253D2/page.html" >'+
					'<img src="http://plugin.smileycentral.com/http%253A%252F%252Fak%252Eimgfarm%252Ecom%252Fimages%252Ffunwebproducts%252Fpromos%252Fzwinky%252Fprofile%252Egif/image.gif" '+
						'border="0"/><div>'+
					'<img src="'+zwinkyURL+'" />'+
					'</a></div>';
	return embedLink;
}

