// Ion.Sound
// version 1.3.0 Build: 20
// © 2013 Denis Ineshin | IonDen.com
//
// Project page:    http://ionden.com/a/plugins/ion.sound/en.html
// GitHub page:     https://github.com/IonDen/ion.sound
//
// Released under MIT licence:
// http://ionden.com/a/plugins/licence-en.html
// =====================================================================================================================

        (function($) {
//alert("ss");
            //var notLoadedSounds = [];
            if ($.ionSound) {
                return;
            }


            var settings = {},
                    soundsNum,
                    gallerySoundsNum,
                    canMp3,
                    url,
                    i,
                    sounds = {},
                    playing = false,
                    VERSION = "1.3.0";


            var createSound = function(soundInfo, currPath) {
                var notLoaded = "";
                var name,
                        volume;

                if (soundInfo.indexOf(":") !== -1) {
                    name = soundInfo.split(":")[0];
                    volume = soundInfo.split(":")[1];
                } else {
                    name = soundInfo;
                }

                sounds[name] = new Audio();
                canMp3 = sounds[name].canPlayType("audio/mp3");
                if (canMp3 === "probably" || canMp3 === "maybe") {
                    //url = settings.path + name + ".mp3";
                    url = currPath + name + ".mp3";
                } else {
                    //url = settings.path + name + ".ogg";
                    url = currPath + name + ".ogg";
                }
                
                //alert(url);

                $(sounds[name]).prop("src", url);
                sounds[name].load();
                sounds[name].preload = "auto";
                sounds[name].volume = volume || settings.volume;                
                
              //  alert(soundNames);
                
               /* if (("soundNames" in window)){
                    if ($.inArray(name, soundNames ) == -1){
                         var currSName = sectionName + slideName;

                        if (currSName == name) {
                            //$("#playControl").unbind("click");
                            //$("#stopControl").unbind("click");

                            $("#playControl img.normal").css("opacity", "0");
                            $("#playControl img.disabled").css("opacity", "1");
                            //console.log($("#" + currSName).find("#playControl"), "myfin");
                        }

                        $("#" + name).addClass("noSound");      

                    }                
                }else{
                         $("#playControl img.normal").css("opacity", "0");
                         $("#playControl img.disabled").css("opacity", "1");
                         $("#" + name).addClass("noSound");   
                    
                }*/
                
                

//alert("test");
                //$(sounds[name]).bind('error', function(e) {                  
                    //switch (e.target.error.code) {
                        //case e.target.error.MEDIA_ERR_ABORTED:
                            //alert('You aborted the video playback.');
                            //break;
                        //case e.target.error.MEDIA_ERR_NETWORK:
                            //alert('A network error caused the audio download to fail.');
                           // break;
                        //case e.target.error.MEDIA_ERR_DECODE:
                            //alert('The audio playback was aborted due to a corruption problem or because the video used features your browser did not support.');
                            //break;
                        //case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
                            //notLoadedSounds.push(name);
                            //notLoaded = name;
                            //alert(sectionName + slideName);
                            //var currSName = sectionName + slideName;

                            //if (currSName == name) {//alert("s");
                                //$("#playControl").unbind("click");
                                //$("#stopControl").unbind("click");
                                //$("#playControl img.normal").css("opacity", "0");
                                //$("#playControl img.disabled").css("opacity", "1");
                                //console.log($("#" + currSName).find("#playControl"), "myfin");
                            //}

                            //$("#" + name).addClass("noSound");

                            //alert(name);
                            //alert('The video audio not be loaded, either because the server or network failed or because the format is not supported.');
                            //break;
                        //default:
                            //alert('An unknown error occurred.');
                            //break;
                    //}
                //});
//alert(soundNames);
            };


            var playSound = function(info) {
                //alert("play");
                var $sound,
                        name,
                        volume,
                        playing_int;



//$sound.bind('ended', function(){
                // done playing
                // alert("Player stopped");
//});


                if (info.indexOf(":") !== -1) {
                    name = info.split(":")[0];
                    volume = info.split(":")[1];
                } else {
                    name = info;
                }

                $sound = sounds[name];

                if (typeof $sound !== "object" || $sound === null) {
                    return;
                }


                if (volume) {
                    $sound.volume = volume;
                }

                if (!settings.multiPlay && !playing) {

                    $sound.play();
                    playing = true;

                    playing_int = setInterval(function() {
                        //console.log("dddd");
                        //alert("start");
                        if ($sound.ended) {
                            //alert("end");
                            clearInterval(playing_int);
                            playing = false;
                        }
                    }, 250);

                } else if (settings.multiPlay) {

                    if ($sound.ended) {
                        $sound.play();
                    } else {
                        try {
                            //$sound.currentTime = 0;
                        } catch (e) {
                        }

                        //alert("plzy");                
                        $sound.play();

                        $($sound).bind('ended', function() {
                            // done playing
                            $("#stopControl").trigger("click");
                            //alert("Player stopped");
                        });
                    }
                }

                // $sound.on('ended', function() {
                //alert("EEEEEND");
                // enable button/link
//});

            };

            var pauseSound = function(name) {
                var $sound = sounds[name];

                if (typeof $sound !== "object" || $sound === null) {
                    return;
                }

                $sound.pause();
            };

            var stopSound = function(name) {
                var $sound = sounds[name];

                if (typeof $sound !== "object" || $sound === null) {
                    return;
                }

                $sound.pause();
                try {
                    $sound.currentTime = 0;
                    // $($sound).unbind('ended');
                } catch (e) {
                }
            };


            var killSound = function(name) {
                var $sound = sounds[name];

                if (typeof $sound !== "object" || $sound === null) {
                    return;
                }

                try {
                    sounds[name].src = "";
                } catch (e) {
                }
                sounds[name] = null;
            };


            // Plugin methods
            $.ionSound = function(options) {

                settings = $.extend({
                    sounds: [
                        "water_droplet"
                    ],
                    path: "static/sounds/",
                    gallerySounds: [
                        "water_droplet"
                    ],
                    galleryPath: "static/sounds/",
                    multiPlay: true,
                    volume: "0.5",
                    notLoadedSounds: []
                }, options);

                soundsNum = settings.sounds.length;
                gallerySoundsNum = settings.gallerySounds.length;
                //alert( gallerySoundsNum);

                if (typeof Audio === "function" || typeof Audio === "object") {
                    for (i = 0; i < soundsNum; i += 1) {
                        createSound(settings.sounds[i], settings.path);                       
                    }
                    
                    for (i = 0; i < gallerySoundsNum; i += 1) {                        
                        createSound(settings.gallerySounds[i], settings.galleryPath);                       
                    }

                    // alert(settings.notLoadedSounds);
                }

                $.ionSound.play = function(name) {
                    playSound(name);
                };
                $.ionSound.stop = function(name) {
                    stopSound(name);
                };

                $.ionSound.pause = function(name) {
                    pauseSound(name);
                };

                $.ionSound.kill = function(name) {
                    killSound(name);
                };
            };


            $.ionSound.destroy = function() {
                for (i = 0; i < soundsNum; i += 1) {
                    sounds[settings.sounds[i]] = null;
                }
                soundsNum = 0;
                $.ionSound.play = function() {
                };
                $.ionSound.stop = function() {
                };
                $.ionSound.pause = function() {
                };
                $.ionSound.kill = function() {
                };
            };

        }(jQuery));