/*Preloader*/
    function preloader(){}

    preloader.prototype.ajaxLoad = function(file){
        var xhr
        if (window.XMLHttpRequest)
            xhr = new XMLHttpRequest();
        else
            xhr = new ActiveXObject("Microsoft.XMLHTTP")
        xhr.file = file
        xhr.onreadystatechange = function(){
            if (xhr.readyState == 4){
                if(xhr.status == 200){
                    xhr.file.data = xhr.responseText
                    var preloaderObject = xhr.file.preloader
                    preloaderObject.processFile(xhr.file)
                    preloaderObject.progress(xhr.file)
                }else if (xhr.status == 404)
                    alert("File not found : " + xhr.file.url)
            }
        }
        xhr.open("GET",file.url,true)
        xhr.send()
    }

    preloader.prototype.constructor = function(parameters){
        object.extend(this,parameters,{
            appendMarkupToBody:true,
            
            filesLoaded:0,
            
            head: document.getElementsByTagName("head")[0],
            
            percentage:0,
            
            videoFilesReady:0
        })    
    }
    
    preloader.prototype.createVideoElements = function(){
        for (var i = 0; i < this.videoFiles.length; i++){
            var video = document.createElement("video")
            
            for (var j = 0; j < this.videoFiles[i].sources.length;j++){
                var source = document.createElement("source")
                source.src = this.videoFiles[i].sources[j].url
                source.type = this.videoFiles[i].sources[j].type
                
                video.appendChild(source)
            }

            document.body.appendChild(video)
        }
    }
    
    preloader.prototype.imageLoad = function(file){
        var image = $("<img/>")
        image.data("file",file)
        image.attr("src",file.url)
        image.load(function(){
            var file = $(this).data("file")
            file.preloader.progress(file)
        })
    }

    preloader.prototype.loadFile = function(file){
        file.preloader = this
        
        switch (file.type){
            case "image":
                this.imageLoad(file)
                break;
            default:
                this.ajaxLoad(file)
                break;
        }
        
    }
    
    preloader.prototype.progress = function(){
        
        preloader.prototype.preProgress.call(this)
        
        preloader.prototype.postProgress.call(this)

    }
    
    preloader.prototype.postProgress = function(){
        if (this.filesLoaded == this.files.length)
            this.complete()
        else{
            if (typeof(this.onProgress) != 'undefined')
                this.onProgress(this)
        }
    }

    preloader.prototype.preProgress = function(){        
        this.filesLoaded += 1
        this.percentage = this.filesLoaded / this.files.length
    }

    preloader.prototype.processFile = function(file){
        switch(file.type){
            case "js":
                var script =  document.createElement("script");
                
                script.setAttribute("type", "text/javascript")
                
                if (this.debug)
                    script.setAttribute("src", file.url)
                else
                    script.text = file.data
                
                this.head.appendChild(script);
            break;
            
            case "css":
                var style = document.createElement("style")
                
                style.innerHTML = file.data
                
                this.head.appendChild(style)
            break;
            
            case "html":
                if (this.appendMarkupToBody)
                    document.body.insertAdjacentHTML('beforeEnd',file.data)
                else
                    this.html = $(file.data)
            break;
            
            case "json":
                try{
                    this.json = eval(file.data)
                }catch(error){
                    this.json = eval("(" + file.data + ")")
                }
            break;
        }
    }

    preloader.prototype.start = function(){
        for (var i = 0; i < this.files.length; i++)
            this.loadFile(this.files[i])
    }
    
    
/*Preloader skin interface*/
    function preloaderSkin(parameters){}

    preloaderSkin.prototype = new preloader()

    preloaderSkin.prototype.constructor = function(parameters){

        preloader.prototype.constructor.call(this,parameters)

        object.extend(this,{},{
            duration : 500,

            ease : "swing",

            parent : $("body")
        })
        
        this.loadMarkup()
        
    }

    preloaderSkin.prototype.animateIn = function(){

        this.markup.css("opacity",0.0).stop().animate({opacity:1}, this.duration, this.ease)

    }

    preloaderSkin.prototype.animateOut = function(){

        this.markup.data("preloaderSkin", this)

        this.markup.stop().animate({opacity:0}, this.duration, this.ease, function(){
            
            var preloaderSkin = $(this).data("preloaderSkin")
            
             preloaderSkin.markup.remove()
            
            if (typeof(preloaderSkin.onComplete) != 'undefined')
                preloaderSkin.onComplete(preloaderSkin)
        })

    }

    preloaderSkin.prototype.complete = function(){

        this.animateOut()

    }

    preloaderSkin.prototype.create = function(){
        
        this.preCreate()
        
        this.postCreate()
        
    }

    preloaderSkin.prototype.preCreate = function(){
        
        this.parent.append(this.markup)
        
    }

    preloaderSkin.prototype.postCreate = function(){
        this.resize()
    
        this.animateIn()

        /*Start Preloading*/    

            this.start()
    }
    
    preloaderSkin.prototype.loadMarkup = function(){
        var xhr
        if (window.XMLHttpRequest)
            xhr = new XMLHttpRequest();
        else
            xhr = new ActiveXObject("Microsoft.XMLHTTP")
        xhr.parameters = {preloaderSkin: this}
        xhr.onreadystatechange = function(){
            if (xhr.readyState == 4 && xhr.status == 200){
                var parameters = xhr.parameters
                parameters.preloaderSkin.markup = $(xhr.responseText)
                parameters.preloaderSkin.create()
            }
        }
        xhr.open("GET",this.markupPath,true)
        xhr.send()
    }
    
    preloaderSkin.prototype.resize = function(){
        if (typeof(this.markup) != 'undefined'){
            this.markup.css({
                position:"absolute",
                
                left:(browser.width - this.markup.width())/2,

                top:(browser.height - this.markup.height())/2,

                zIndex:9999
            })
        }
    }

/*Preloader Skins*/
    /*Toms club*/
        function p_tomsclub(parameters){
            
            object.extend(this,parameters,{
                
                markupPath:"plugins/javascript/preloader/skins/tomsClub/markup/tomsClub_markup.php"
                
            })
            
            preloaderSkin.prototype.constructor.call(this,parameters)
            
        }
        
        p_tomsclub.prototype = new preloaderSkin()
        
        p_tomsclub.prototype.create = function(){
             /*Append*/
                preloaderSkin.prototype.preCreate.call(this)
            
            /*Define*/
                this.mask = this.markup.find(".mask")
            
                this.silhouette = this.markup.find(".silhouette")
                
                var self = this
                this.silhouette.load(function(){self.resize()})
                
                this.slider = this.markup.find(".slider")
                
                this.sliderLeft = this.slider.find(".left")
                
                this.sliderRight = this.slider.find(".right")
                
            /*Animate In*/
                preloaderSkin.prototype.postCreate.call(this)
        }
        
        p_tomsclub.prototype.progress = function(){
            /*Compute percentage*/
                preloader.prototype.preProgress.call(this)
            
            /*Update mask width & slider*/    
                this.mask.stop().animate({width:(this.silhouette.width() * this.percentage)},500,"swing")
                
                this.sliderLeft.stop().animate({width:(this.slider.width() * this.percentage)})
                
                this.sliderRight.stop().animate({width : this.slider.width() * (1 - this.percentage)})
                
            /*Check if complete*/
                preloader.prototype.postProgress.call(this)
            
        }
        
    /*Flowing gradient*/
        function p_flowingGradient(parameters){
            
            object.extend(this,parameters,{
                
                markupPath:"plugins/javascript/preloader/skins/flowingGradient/markup/flowingGradient_markup.php"
                
            })
            
            preloaderSkin.prototype.constructor.call(this,parameters)
            
        }
        
        p_flowingGradient.prototype = new preloaderSkin()
        
        p_flowingGradient.prototype.resize = function(){}

/*Manager*/
    var preloadManager = new function(){
        this.SKIN_TOMS_CLUB = 0;
        
        this.SKIN_FLOWING_GRADIENT = 1;
        
        this.preloaders = new Array()

        this.preloadFiles = function(parameters){
            browser.getDimensions()
            
            switch(parameters.skin){
                
                case preloadManager.SKIN_TOMS_CLUB:
                    
                    preloadManager.preloaders.push(new p_tomsclub(parameters))
                    
                    break;
                    
                case preloadManager.SKIN_FLOWING_GRADIENT:
                    
                    preloadManager.preloaders.push(new p_flowingGradient(parameters))
                    
                    break;
            }
            
        }
        
        this.resize = function(){
            for (var i = 0; i < preloadManager.preloaders.length; i++)
                preloadManager.preloaders[i].resize()
            
        }
    }
