var kidsgreekplayer = {
	
	version : "1.0",
	
	/***************************************************
	Checks to see if html5 audio is supported
	***************************************************/
	html5Support : function() {
		var a  = document.createElement("audio");
		return (typeof a.canPlayType === "function" && a.canPlayType("audio/mpeg") !== "");
	},
	/***************************************************
	Creates an html5 audio player
	***************************************************/
	createAudioPlayer : function(s) {
		var aud, img, imgcss, csr, clickplay, div = $('#'+s.divId);
		//if the div does not exist, then alert error
		if(div.length == 0 && console) console.log('ERROR: Could not find element with id="'+s.divId+'"');
		//remove everything in div
		div.empty();
		div.css('width', s.width);
		//create mouse over cursor
		csr = $('<img src="'+s.cursor+'" style="display:block; z-index:1000; position:absolute;"/>').hide().appendTo('body');
		//create image element
		imgcss = ' style="display:block;cursor:pointer"';
		img = $('<img src="'+kidsgreekplayer.randChoice(s.imgUrls)+'" width="'+s.width+'" '+imgcss+'/>').appendTo(div);
		//$('<br />').appendTo(div);
		//create audio element and hide it until clicked
		aud = $( kidsgreekplayer.makeAudHtml(s.mediaFile, s.ctrlBar, s.width) ).hide().appendTo(div);
		//setup a click to play audio when the image is clicked
		img.click(function() {
			if(!clickplay) {
				aud.show();
				csr.hide();
				img.css('cursor', 'auto');
				if(aud[0].paused) aud[0].play();
				//else aud[0].pause();
			}
			clickplay = true;
		});
		//setup for cursor to follow mouse
		img.mouseover(function(event) {
			if(aud[0].paused) csr.show();
		}).mouseout(function(event) {
			csr.hide();
		}).mousemove(function(event){
			if(csr.is(":visible")) csr.css('top', event.pageY-csr.height()-2).css('left', event.pageX-(csr.width()/2));
		})	
		//setup listener for end of audio
		aud.bind('ended', function() {
			clickplay = false;
			aud.hide();	
			img.css('cursor', 'pointer');
			csr.show();
			aud[0].pause();
		});	
		//setup space bar press
		$(document).keyup(function(evt) {
			if (evt.keyCode == 32) {
				if(clickplay) {
					if(aud[0].paused) aud[0].play();
					else aud[0].pause();
				} else {
					img.trigger('click');
				}
			}
		})
	},
	/***************************************************
	Creates an audio element's html
	***************************************************/
	audCtrls : function(aud, val) {
		if(val) aud.attr('controls', 'controls');
		else aud.removeAttr('controls');
	},
	/***************************************************
	Creates an audio element's html
	***************************************************/
	makeAudHtml : function(src, ctrls, width) {
		var html = '<audio ';
		if(kidsgreekplayer.strToBool(ctrls)) html += 'controls="controls"';
		html += ' style="width:'+width+'px"';
		html += ' ><source src="'+src+'" type="audio/mpeg" /></audio>';
		return html;
	},
	/***************************************************
	Chooses a random item in a string list of comma separated values
	***************************************************/
	randChoice : function(str) {
		var list = str.split(/\s*,\s*/);
		return list[ kidsgreekplayer.randInt(0, list.length-1) ];
	},
	/***************************************************
	Converts String to Boolean
	***************************************************/
	strToBool : function(str) {
		return !(/^(0|false|no|off|undefined|null)/i.test(String(str)));
	},
	/***************************************************
	Creates a random integer between two values
	***************************************************/
	randInt : function(min, max) {
		if(min == max) return min;
		return Math.round(Math.random() * (max - min)) + min;
	}
}
