$(document).ready(function() {
	
	var images = new Array();
	var api = null;
	
	try {
		images = $( '.scroller .items .item' ).find( 'a' );
	} catch (e) {
		
	}
	
	$('#cc #content_right .sponsors .slider').cycle({
		fx: 'fade',
		timeout: 2000 
	});
	
	$('#conference_title').cycle({
			fx: 'fade',
			random:  1
	});
	
	$('#slide_speakers_sidebar').cycle({
			fx: 'fade',
			random:  1
	});
	
	// gallery
	var api = $( '#content_mid div.scrollable' ).scrollable({
		size : 4,
		api  : true
	});
	
    $( '#content_mid div.scroller .next' ).click(function() {
        return false;
    });
	
    $( '#content_mid div.scroller .prev' ).click(function() {
        return false;
    });
	
	$( '#content_mid div.scrollable .item' ).click(function() {
		
		var link = $( this ).find( 'a' );
		
        loadImage( link );
		
		return false;
	});
	
	function loadImage( link ) {
		
        var id            = parseInt( link.attr( 'rel' ) );
        var image         = link.attr( 'href' );
        var description   = link.attr( 'title' );
        
        var tmp = image.split( '/' );
        tmp.splice( 0,4 );
        var path = '';
        var i;
        for (i=0; i<tmp.length; i++) {
            path += '/' + tmp[i];
        }       
        
        $.get(
          "http://www.glas-znanosti.si/images/image.gif", {
            cimage : path,
            type   : 'gallery',
            size   : 509,
            h_size : 339
          }, function( image ) {
              $( '.image-preview img.img' ).attr( 'src', image );
              $( '.image-preview .image_desc' ).html( description );
          });
        
        $( '.image-preview a.prev' ).attr( 'rel', (id-1) );
        $( '.image-preview a.next' ).attr( 'rel', (id+1) );
		
	}
	
	$( '.image-preview' ).mouseover(function() {
		$( this ).find( '.prev' ).show();
		$( this ).find( '.next' ).show();
	});
	
    $( '.image-preview' ).mouseout(function() {
        $( this ).find( '.prev' ).hide();
	    $( this ).find( '.next' ).hide();
    });
	
    $( '.image-preview .next' ).click(function() {
		var id = parseInt( $( this ).attr( 'rel' ) );
		if ( id < images.length ) {
			var link = $( images[ id ] );
			loadImage( link );
		}
        return false;
    });
	
	$( '.image-preview .prev' ).click(function() {
		var id = parseInt( $( this ).attr( 'rel' ) );
		if ( id >= 0 ) {
			var link = $( images[id] );
			loadImage( link );
		}
		return false;
	});
	
});
