function cauta(ce) {
	if( ce.length < 3 ) {
		alert('Minim 3 caractere!')
		return false;
	} 
	ce = ce.toLowerCase(); 
	//ce = ce.replace(/\s+/g, '');
	ce = ce.replace(/[^a-z0-9/]/gi, '-');
	var final = 'referat-'+ce+'.html';
	window.location = final;
}

function downloadReferat(id) {
	window.location = 'down'+'load'+'.php?id='+id;	
}

function makeCookie(name,value) {
	var date = new Date();
	date.setTime(date.getTime()+(3600*24*1000));
	var expires = ";expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return false; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


function handleEnter(inField, e) {
    var charCode;    
    if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }

    if(charCode == 13) {
		if( inField.value.length < 3 ) {
			alert('Minim 3 caractere!')
			return false;
		} else {
	        cauta(inField.value);
		}
    }
}

function goTo(url,type,a) {
	if( type == 'doc' ) window.location = url+'doc.php?id='+a;
	if( type == 'pdf' ) window.location = url+'pdf.php?id='+a;	
}

	$('document').ready(function(){
 		setTimeout("periodicalOriginals()",10000);
	});
	
	function periodicalOriginals() {
		originals();	
		setTimeout("periodicalOriginals()",10000);
	}
 
 
	function originals() {
		var ROOT_BASE = 'http://referate-x.com/';
 		if( document.location.href.substr(0,10) == 'http://www' ) {
			ROOT_BASE = 'http://www.referate-x.com/';
		}
		
		$.post( ROOT_BASE+'slide.php',
				function(jsonObj){
  					$({}).imageLoader({
						images: [ jsonObj.img1,jsonObj.img2,jsonObj.img3,jsonObj.img4 ],
						async: true,
						allcomplete: function(e, ui) {
							$('#img1').fadeOut('slow', function() { $('#img1').html(jsonObj.html1).fadeIn('slow'); });
							$('#txt1').fadeOut('slow', function() { $('#txt1').html(jsonObj.txt1).fadeIn('slow'); });
							
							$('#img2').fadeOut('slow', function() { $('#img2').html(jsonObj.html2).fadeIn('slow'); });
							$('#txt2').fadeOut('slow', function() { $('#txt2').html(jsonObj.txt2).fadeIn('slow'); });
							
							$('#img3').fadeOut('slow', function() { $('#img3').html(jsonObj.html3).fadeIn('slow'); });
							$('#txt3').fadeOut('slow', function() { $('#txt3').html(jsonObj.txt3).fadeIn('slow'); });
							
							$('#img4').fadeOut('slow', function() { $('#img4').html(jsonObj.html4).fadeIn('slow'); });
							$('#txt4').fadeOut('slow', function() { $('#txt4').html(jsonObj.txt4).fadeIn('slow'); });							
						}
					});					
  				}, 
		 'json');	
	}
	
	
 



(function($) {

$.widget( "ui.imageLoader", { 
    options: { 
        async: true,
        images: [ ]
    },
    total: 0,
    _init: function(){ 
        var self = this;
		var i;

        self.total++;
        //load counter
        self.loaded = 0;

        //local variable to track image attributes
        self.data = [ ];
        self.stats = {
			loaded:0,
			errored:0,
			allcomplete:false
		};

		//if images option is a selector, collect all image sources from matched elements
		if( typeof self.options.images === 'string' ) {
			var images = [ ];
			$.map( $( self.options.images ), function(el, i){
	            images.push($(el).attr('src'));
			});		
			self.options.images = images;
		}
		
        for ( i = 0; i < self.options.images.length; i++ ){ 
            self.data.push( { 
                init: false,
                complete: false,
                error: false,
                src: self.options.images[ i ],
                img: new Image(),
                i: i
            });
        }

        //controls the number of threads simmultaneously loading unloaded images
        for ( i = 0;  ( ( i < self.data.length ) && ( ( self.options.async === true || i === 0 ) || i < parseInt( self.options.async, 10 ) ) ); i++ ){ 
        
            self._loadImg( i );

        }
        return self;
    },
    _loadImg: function( i ){ 
        var self = this;
        if ( i !== false && i < self.data.length ){ 

            if ( !self.data[ i ].init ){ 
            
                //lock image
                self.data[ i ].init = true;
                
                self._trigger( "start", null, { 
                    i: i,
                    data: self.getData( )
                });

                //using setTimeout to force multiple threading and give some time for garbage collection between image loads
                setTimeout( function(){ 
										
						//error event
	                    self.data[ i ].img.onerror = function(){ 

	                        self.loaded++;
							self.stats.errored++;
	                        self.data[ i ].error = true;
	                        self._trigger( "error", null, { 
	                            i: i,
	                            data: self.getData( )
	                        });
	
	                        self._complete( i );
	                    };

	                    self.data[ i ].img.onload = function(){ 
						
							//redirect to error for when a malformed image is passed in using firefox (thanks Frank Boers)
							if(self.data[ i ].img.width < 1 ) {
								return self.data[ i ].img.onerror();
							}
						
	                        self.loaded++;
							self.stats.loaded++;
	                        self.data[ i ].complete = true;

	                        self._trigger( "complete", null, { 
	                            i: i,
	                            data: self.getData( )
	                        });

	                        self._complete( i );
	                    };
	
                    //setting the src after the onload event incase image is already cached
                    self.data[ i ].img.src = self.data[ i ].src;
                    
                    
                }, 1 );
            }
        }
    },
    _complete: function( i ){ 
        var self = this;

		
        //if thread is synchronous move on to next unloaded image
        if ( !self.options.async || typeof self.options.async === "number" ){ 
            var next = self._next( i );
            self._loadImg( next );
        }

        //if last image, trigger allcomplete event
        if ( self.loaded === self.data.length ){ 
            //triger complete
            self._trigger( "allcomplete", null, self.getData( ) );
			self.stats.allcomplete = true;
        }
    },

    //returns index of next image that isn't already loading
    _next: function( j ){ 
        var self = this;
		var i;
		
        for ( i = 0; i < self.data.length; i++ ){ 
            if ( i !== j && !self.data[ i ].init ){ 
                return i;
            }
        }
        return false;
    },
    getData: function(){ 
        return $.extend( true, [ ], this.data );
    },
    getStats: function(){ 
        return $.extend( true, [ ], this.stats );
    },
    destroy: function(){ 
        $.Widget.prototype.destroy.apply( this, arguments );
    }
 });


})(jQuery);

 

