/*
@Author: Paul Visco of http://elmwoodstrip.org?u=paul
@Version: 3.0
@Release: 06/06/07
@Package: surebert.sticker
@Desciption: Allows communications between surebert.swf flash sticker and the surebert toolkit, extending javascript by allowing it to borrow functionality from flash.  Currently it can play sounds, save data to the flash storage space, and allow multi file uploads.  scOnStickerLoad event fires once these methods are loaded an available.
*/

if(typeof sb.swf =='undefined'){
	sb.include('sb.swf');
}

sb.onstickerload = [];

/**
@Name: sb.sound
@Description: A constructor for creating new sound object instances.  Allows javascript to load, play and stop mp3 sounds.  Also has hooks for changing pan(left and right speaker), volume, and position of track, and reading id3 tag data from the song.
@Param String url The address of the sound file e.g. http://myexample.com/mySound.mp3
@Param Number vol The volume to play the song at
@Example:
var mySound = new sb.sound('http://myexample.com/mySound.mp3);
mySound.play();
*/

sb.sound = function(url, vol){
	
	if(typeof url == 'undefined'){return;}
	this.url = url;
	this.vol = vol || sb.sound.globalVolume;
	sb.sound.sounds.push(this);
}; 

sb.sound.extend({
	
	/**
	@Name: sb.sound.stopAll
	@Description: Stops all sounds currently on the page
	@Example:
	sb.sounds.stopAll();
	*/
	stopAll : function(){
		sb.sound.sounds.forEach(function(v){
			v.stop();
		});
	},
	
	/**
	@Name: sb.sound.muteAll
	@Description: Mutes all sounds currently on the page, does not stop them from playing.
	@Example:
	sb.sounds.muteAll();
	*/
	muteAll : function(){
		sb.sound.sounds.forEach(function(v){
			v.setVolume(0);
		});
	},
	
	/**
	@Name: sb.sound.globalVolume
	@Description: The globalVolume on the page
	*/
	globalVolume : 50,

	/**
	@Name: sb.sound.sounds
	@Description: An array of all the sound object instances on the page.
	*/
	
	sounds : [],
	
	/**
	@Name: sb.sound.muted
	@Description: When set to 0 all sounds on the page are not muted when set to 1 all sounds are muted
	*/
	muted : 0,
	
	/**
	@Name: sb.sound.handlers
	@Description: Used internally. Passes events to individual sound instances when the events fire from the flash sticker.
	*/
	handlers : {
		/**
		@Name: sb.sound.handlers.oncomplete
		@Description: Used internally. Fires when a sound is completed and triggers the firing of the sound instances oncomplete handler if it exists
		*/
		oncomplete : function(info){
			if(typeof sb.sound.sounds[info.id].oncomplete=='function'){
				sb.sound.sounds[info.id].oncomplete(info);
			}
		},
		/**
		@Name: sb.sound.handlers.onid3
		@Description: Used internally. Fires when a sound's id3 data is loaded and triggers the firing of the sound instance's onid3 handler if it exists
		*/
		onid3 : function(info){
			if(typeof sb.sound.sounds[info.id].onid3=='function'){
				sb.sound.sounds[info.id].onid3(info);
			}
		},
		/**
		@Name: sb.sound.handlers.onload
		@Description: Used internally. Fires when a sound is onload and triggers the firing of the sound instances onload handler if it exists
		*/
		onload : function(info){
			if(typeof sb.sound.sounds[info.id].onload=='function'){
				sb.sound.sounds[info.id].onload(info);
			}
		}
	}
});


/**
@Name: sb.sound.prototype
@Description: The properties and methods of all sound object instances.  All examples refer to a sound object instance called mySound which was created like this
@Example:
var mySound = new sb.sound('http://myexample.com/mySound.mp3);
*/
sb.sound.prototype = {
	
	/**
	@Name: sb.sound.prototype.playing
	@Description: Boolean The playing status of a sound object. 0 = not playing, 1 is playing
	*/
	playing :0,
	
	/**
	@Name: sb.sound.prototype.play
	@Description: Function Plays the sound file specified in the url property of the sound object.
	@Param: Number vol The volume to play the sound at measured between 0 and 100
	@Example:
	mySound.play();
	*/
	play : function(vol){
		if(typeof sb.stickerInit=='undefined'){
			
			if(typeof this.interval !='undefined'){return;}
			
			var t=this;
			this.tries = 0;
			this.interval = window.setInterval(function(){
				if(typeof sb.stickerInit !='undefined'){
					t.play();
					window.clearInterval(t.interval);
				}
				this.tries++;
				
				if(this.tries > 10){
					window.clearInterval(t.interval);
					sb.consol.error(sb.messages[16]+t.url);
				}
			}, 100);
			
			return;
		}
		
		if(sb.sound.muted===1){return;}
		vol = vol || this.vol;
		if(typeof this.id == 'undefined'){
			
			this.id = sb.sticker.soundCreate(this.url, vol);
		} else {
			this.setVolume(vol);
			this.start();
		}
		return this.id;
	},
	
	/**
	@Name: sb.sound.prototype.start
	@Description: Function Starts a sound if it was stopped
	@Example:
	mySound.start();
	*/
	start : function(){
		this.playing =1;
		sb.sticker.soundStart(this.id);
	},
	
	/**
	@Name: sb.sound.prototype.stop
	@Description: Function Stops a sound taht was started
	@Example:
	mySound.stop();
	*/
	stop : function(){
		this.playing =0;
		sb.sticker.soundStop(this.id);
	},
	
	/**
	@Name: sb.sound.prototype.setVolume
	@Description: Function Sets the volume of a sound object instance
	@Param Number vol The volume measured between 0 and 100
	@Example:
	mySound.setVolume(20);
	*/
	setVolume : function(vol){
		sb.sticker.soundSetVolume(this.id, vol);
	},
	
	/**
	@Name: sb.sound.prototype.mute
	@Description: Function Mutes the volume of a sound object instance
	@Example:
	mySound.mute();
	*/
	mute : function(){
		this.setVolume(0);
	},
	
	/**
	@Name: sb.sound.prototype.setPan
	@Description: Function Sets the pan of a soudn object instance
	@Param: Number pan Pan bewteen -100 (far left) and 100 (far right)
	@Param: String pan You can also pass it the shortcuts 'left', right', 'middle'
	@Example:
	mySound.setPan('left');
	mySound.setPan(-100);
	*/
	setPan : function(pan){
		switch(pan){
			case 'left':
				pan = -100;
				break;
			case 'right':
				pan = 100;
				break;
			case 'middle':
				pan = 0;
				break;
		}
		
		sb.sticker.soundSetPan(this.id, pan);
	},
	
	/**
	@Name: sb.sound.prototype.getPan
	@Description: Function Gets the pan of a sound object instance
	@Return: Number Pan between -100(far left) and 100(far right)
	@Example:
	var pan = mySound.getPan();
	//pan = -100 //<-possible result
	*/
	getPan : function(){
		return sb.sticker.soundGetPan(this.id);
	},
	
	/**
	@Name: sb.sound.prototype.setPosition
	@Description: Function Sets the position of a sound object instance
	@Param: Number position A position between 0% and 100%
	@Example:
	//sets the sound position to 50%
	mySound.setPosition(50);
	*/
	setPosition : function(position){
		sb.sticker.soundSetPosition(this.id, position);
	},
	
	/**
	@Name: sb.sound.prototype.getPosition
	@Description: Function Gets the position of a sound object instance
	@Example:
	var post = mySound.getPosition(50);
	//pan = 50 //<-possible result
	*/
	getPosition : function(){
		return sb.sticker.soundGetPosition(this.id);
	}
	
};

/**
@Name: sb.sharedObject
@Description: Object Allows the sotring and retreiving of data in the flash shared object space on the client's computer.  This space is virtually unlimited and is not emptied when a user empties their cookies.  The calls work exactly the same as with sb.cookies.
*/
sb.sharedObject = {

	/**
	@Name: sb.sharedObject.remember
	@Description: Used to make the clients computer remember a value as a in the flash shared object space
	@Param: String name The name (key) of the cookie which will hold the valuee
	@Param: String value The value the cookie holds
	@Example:
	sb.sharedObject.remember('name', 'paul');
	*/
	remember :function(key, v){
	
		try{
			sb.sticker.remember(key,escape(v));
		} catch(e){
			window.setTimeout(function(){sb.sharedObject.remember(key,v);}, 1000);
		}
	},
	
	/**
	@Name: sb.sharedObject.recall
	@Description: Used to recall flash shared object stored values
	@Param: String name The name of the shared object who's value you are trying to recall
	@Return: String Returns the value stored for the shared object or false if the shared object is not found
	@Example:
	var answer = sb.sharedObject.recall('myData');
	//answer = the value the shared object was set to with sb.sharedObject.remember
	*/
	recall : function(key){
		try{
			var val = unescape(sb.sticker.recall(key));
			if(val == 'null'){return false;} else {return val;}
		} catch(e){return false;}
	},
	
	/**
	@Name: sb.sharedObject.forget
	@Description: Used to make the clients computer forget a flash shared object stored value
	@Param: String name The name (key) of the shared object which will be forgotten
	@Example:
	sb.sharedObject.forget('myData');
	*/
	forget : function(key){
		try{
			sb.sticker.forget(key);
		} catch(e){return false;}
		return true;
	}
};

/**
@Name: sb.persistentData
@Description: Used Internally. Maps the cookies to try and use flash shared object space if it exisst and teh cookie value overflows the cookie space which is limited to 4k
*/
sb.persistentData = {
	remember : function(name, value){
		sb.cookies.remember(name, value);
		if(sb.cookies.recall(name) != value){
			
			sb.sharedObject.remember(name, value);
		}
	},

	recall : function(name){
		
		if(sb.cookies.recall(name)){
			return sb.cookies.recall(name);
		} else {
			return sb.sharedObject.recall(name);
		}
	},

	forget : function(name){
		if(sb.cookies.recall(name)){
			sb.cookies.forget(name);
		}
		
		if(sb.sharedObject.recall(name)){
			sb.sharedObject.forget(name);
		}
	}
};

/**
@Name: sb.sticker.upload
@Description: Using the flash multifile uploads it is possible to track progress of file uploads, upload files without leaving the page, select multiple files to uplaod at once, and provide a file type mask for the file browser.  See sb.uploadHandlers for more information on defining upload handlers.
@Param string mask The file types allowed to be uploaded, you can specify multiple file types by separating values with a semicolon e.g *gif;*.pdf;*.jpg;
@Param string serverSideFile The server side file to pass the uplaoded files to.  Each file is passed separately to the serverSideFile and the file key value is Filedata.  In PHP you would refer to it as $_FILES['Filedata']
@Example:
//open a flash multifile file upload browser when the button with the id 'uploadFiles' is clicked
sb.events.add('#uploadFiles', 'click', function(){
	sb.sticker.upload('*.gif;*.jpg;', 'upload.php');
});
*/

/**
@Name: sb.uploadHandler
@Description: These are the upload handlers for multi file uploads.  You would override them in your own code, they are simply here to provide an overview of which handlers exist.
*/
sb.uploadHandlers = {
	
	/**
	@Name: sb.uploadHandler.onallbegin
	@Description: Fires if when files begin to upload
	@Example:
	sb.uploadHandlers.onallbegin = function(file){
		alert('uploading begins');
	}
	*/
	onallbegin : function(total){
		
	},
	
	/**
	@Name: sb.uploadHandler.onallbegin
	@Description: Fires if when all files have uploaded
	@Example:
	sb.uploadHandlers.onallcomplete = function(file){
		alert('uploading complete');
	}
	*/
	onallcomplete : function(data){
		
	},
	
	/**
	@Name: sb.uploadHandler.onallbegin
	@Description: Fires if when all files have uploaded
	@Example:
	sb.uploadHandlers.onallprogress = function(files){
		alert(files.total+' '+files.percent+' '+files.remaining);
	}
	*/
	onallprogress : function(files){
		
	},
	
	/**
	@Name: sb.uploadHandler.oncancel
	@Description: Fires if the user hits the cancel button in the file browser window
	@Example:
	sb.uploadHandlers.onstart = function(file){
		alert('user has canceled upload');
	}
	*/
	oncancel : function(){
		
	},
	
	/**
	@Name: sb.uploadHandler.onstart
	@Description: Fires when the user starts the file upload by clicking the upload button in the file browser.  Each file will fire an onstart event of its it own and the properties of the file are passed as the only argument to this handler. e.g.
	@Example:
	sb.uploadHandlers.onstart = function(file){
		alert(file.name+' has started uploading');
	}
	
	//the file object passed has the following properties
	file = {
		name : 'New Ride Banner.jpg',
		size : 417310,
		type : '.jpg'
	}
	*/
	onstart : function(file){
		
	},
	
	/**
	@Name: sb.uploadHandler.oncomplete
	@Description: Fires when the file finishes uploading. Each file will fire an oncomplete event of its it own and the properties of the file are passed as the only argument to this handler.
	@Example:
	sb.uploadHandlers.oncomplete = function(file){
		alert(file.name+' has been uploaded');
	}
	
	//the file object passed has the following properties
	file = {
		name : 'New Ride Banner.jpg',
		size : 417310,
		type : '.jpg'
	}
	*/
	oncomplete : function(file){
	
	},
	
	/**
	@Name: sb.uploadHandler.onprogress
	@Description: Fires incrementally as the upload progressed. Each file will fire an onprogress event of its it own and the properties of the file are passed as the only argument to this handler.  The example assumes you make a paragraph with an id of 'file1', 'file2', 'file3', etc for every file uploaded during the onstart handler.  It then updates the progress each time the onprogress handler fires for that file.  Remember, every file fires it's onprogress progressively as it uploads and passes it's properties as the only argument
	@Example:
	sb.uploadHandlers.onprogress = function(file){
		//sets the innerHTML of the element with id 'file1' to the percent of the file
		$('#file1').innerHTML = (file.bytesLoaded/file.bytesTotal)*100+' %';
	}
	
	//the file object passed has the following properties

	file = {
		name : 'New Ride Banner.jpg',
		size : 417310,
		type : '.jpg'
		bytesLoaded : 22,
		bytesTotal : 417310
		
	}
	*/
	onprogress : function(file){
		
	},
	
	/**
	@Name: sb.uploadHandler.onerror
	@Description: Fires if an error occurs. Passes an error object to the handler
	
	*/
	onerror : function(e){
	}
};



/**
@Name: sb.stickerloaded
@Description: Fires when sticker loads.  To make eventss fire after stciker loads push them into the sb.onstickerload array

*/
sb.upload = function(){
	sb.consol.error(sb.messages[15]);
};

sb.stickerloaded = function(){
	setTimeout(
		function(){
	
			sb.upload = function(type, file){
				sb.sticker.upload(type, file);
			};
			
			sb.onstickerload.forEach(function(v){
				if(typeof v =='function'){v();}
			});
			
			sb.stickerInit=1;
	}, 5);
	
	
};

sb.stickerInclude = function(){
	
	var surebertSwf = new sb.element({
		id : 'surebertSwf',
		nodeName : 'div',
		styles: {
			display : 'inline'
		}
		
	});
	
	surebertSwf.appendToTop(document.body);
	
	if(sb.browser.agent =='ff'){
		surebertSwf.mv(0,0,999);
		if(window.screenX < 0){
			var screenX =(window.screenX*-1)+20;
			surebertSwf.mv((window.screenX*-1)+20,0,999);
		}
	}
	
	sb.swfBox = new sb.swf({
		src : sb.base+'/surebert.swf',
		width : 1,
		height : 1,
		bgColor :'#000000',
		wmode: 'transparent'
	});
	
	sb.swfBox.id = 'sb_sticker';
	sb.sticker = sb.swfBox.embed(surebertSwf);
	
};

sb.onbodyload.push(sb.stickerInclude);

if(typeof sbNoGlobals === 'undefined'){
	remember = sb.persistentData.remember;
	recall = sb.persistentData.recall;
	forget = sb.persistentData.forget;
}