if(typeof(Tracker) == 'undefined') Tracker = false;
if(Tracker) {
	Tracker.init("Promos");
	Tracker.track("editWithPicnik");
}
var fwpicnik = {
	
	/* Picnik Integration Flow:
		Integration Page (IP) includes this file, picnik.js
		IP calls fwpicnik.load() with the mode (IP's function i.e. albums), current userID, current user's directory, and the token.
		IP calls fwpicnik.edit() with the relative path and filename of the image.
		FwPicnik calls ImgID, which passes back the chosen image's ID and calls fwpicnik.run()
		FwPicnik activates PicnikBox
		User edits photo
		PicnikBox sends the image data to PicnikCallback
		PicnikCallback processes and saves the new image and calls parent.fwpicnik.close()
		FwPicnik adds the image to the IP, and closes PicnikBox
		Presto Changeo!
	*/
	
	load: function(mode, uid, dir, token) {
		var split = window.location.href.split("../../index.html"); 
		this.mode = mode;
		this.uid = uid;
		this.dir = dir;
		this.token = token.substring(7);
		this.server = split[0] + "//" + split[2] + "/Misc/Picnik/picnikCallback.jsp";
	},
	
	url: function() {
		var base = "http://www.picnik.com/service?_apikey=bf7815f2af94dd70c0f33f46ca70a8c2";
		var url = ""
		
		/* Picnik API Parameters */
		url += "&_import="+encodeURIComponent(this.dir+this.img);
		url += "&_export=" + encodeURIComponent(this.server);
		url += "&_host_name=Freewebs";
		url += "&_export_agent=browser";
		url += "&_export_title=" + encodeURIComponent("Save To Webs");
		url += "&_close_button=yes";
		if(this.iid) url += "&_original_thumb=" + encodeURIComponent("http://thumbs.freewebs.com/Members/viewThumb.jsp?fileID=" + this.iid);
		if(this.uid!="") url += "&_userid="+encodeURIComponent(this.uid);
		if(this.iid) url += "&_imageid="+encodeURIComponent(this.iid);
		
		/* Freewebs-defined Parameters */
		url += "&token="+encodeURIComponent(this.token);
		url += "&mode="+encodeURIComponent(this.mode);
		
		var path = this.img.replace(/https?:\/\/[^/]+/, "");
		var dirSlashBreak = path.lastIndexOf("../../index.html") +1;
		if( dirSlashBreak >= 1 ){
			url += "&dir="+encodeURIComponent(path.substring(0, dirSlashBreak));
			url += "&name="+encodeURIComponent(path.substring(dirSlashBreak));
		} else {
			url += "&dir=";
			url += "&name="+encodeURIComponent(path);
		}
		if (this.mode == "photoAlbums") {
//                    url += "&setDomain=1";
                    url += "&_replace=confirm";
                } else {
		    url += "&_replace=ask";
                }
		
		var api_call = base + url;
		return api_call;
	},
	
	jjax: function(src) {
		var elm = document.createElement("script");
		elm.src = src;
		document.body.appendChild(elm);
	},
	
	edit: function(img) {
		var i = img.indexOf('?');
		if(i > 0) {
			img = img.substring(0,i);
		}
		this.img = img;
		switch (this.mode) {
			case "pageBuilderNS":
				window.resizeTo(800,600);
				break;
		}
		
		/* ImgID grabs the unique ID of the image and calls fwpicnik.run() */
		this.jjax( "../../Misc/Picnik/imgIDdc11.html?img=" + escape(img) + "&token="+this.token);
		this.addX();
		return false;
	},

	edit2: function(img, fileid) {
		var i = img.indexOf('?');
		if(i > 0) {
			img = img.substring(0,i);
		}
		this.img = img;
		switch (this.mode) {
			case "pageBuilderNS":
				window.resizeTo(800,600);
				break;
		}
		
		this.iid = fileid;
		this.thumb = "http://thumbs.freewebs.com/Members/viewThumb.jsp?fileID="+fileid;
		this.error=false;
		try {
			this.run();
		} catch(e) {
			this.error=true;
			this.showError();
		}
		this.addX();
		return false;
	},

	run: function() {
		if(Tracker)
			Tracker.startConversion("photoEdited",2);
		var pbox = new picnikbox( { href:this.url() } );
		pbox.activate();
		$('picnikbox').focus();
	},
	
	close: function(newPath) {
		if(Tracker)
			Tracker.completeConversion("photoEdited",2);
		//The setTimeouts fix a weird Firefox bug.  I don't know why, they just do.		
		switch (this.mode) {
			case "albums":
				setTimeout(function(){
					addPic(newPath);
					if ($("thumb_"+newPath)) //Refreshes album thumbnail
						$("thumb_"+newPath).src = $("thumb_"+newPath).src + "?"+Math.floor(Math.random()*500);
				},0);
				break;
                        case "photoAlbums":
                            // if reloadImageCallback is attached, run it
                            if (this.reloadImageCallback) 
                                setTimeout(this.reloadImageCallback, 0);
			case "fileManager":
				setTimeout(function(){loadFiles();},0);
				break;
			case "pageBuilderNS":
				var dirBreak = newPath.indexOf("../../index.html")+1;
				var name = (dirBreak>=1) ? newPath.substring(0, dirBreak) : newPath;
				var dir = (dirBreak>=1) ? newPath.substring(dirBreak) : '';
				setTimeout(function(){returnpic(name,dir);},0);
				break;
			case "siteBuilder":
				if (typeof SiteBuilder!="undefined") SiteBuilder.execCommand("fwPicnik", false, newPath);
				else if (frames.fwEditor && frames.fwEditor.SiteBuilder) frames.fwEditor.SiteBuilder.execCommand("fwPicnik", false, newPath);
				break;
		}
		
		fwpicnik.remX();
	},
	addX: function() {
		var elm = document.createElement("img");
		elm.src = "../../../images.freewebs.com/Images/Picnik/close.gif";
		elm.id = "picnikclose";
		elm.alt = "Close Picnik";
		addEvent(elm, "click", fwpicnik.remX);
		document.body.appendChild(elm);

//		SiteBuilder.pageDocument.getElementById('sbFocusBox').focus();
	},
	
	remX: function() {
		document.body.removeChild($("picnikclose"));
		onPicnikClose();
	},
	showError: function() {
		this.remX();
		alert('This image can not be edited.  Please re-insert the image and try again.');
	}
};

function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}
