/*************************
  Coppermine Photo Gallery
  ************************
  Copyright (c) 2003-2009 Coppermine Dev Team
  v1.1 originaly written by Gregory DEMAR

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 3
  as published by the Free Software Foundation.

  ********************************************
  Coppermine version: 1.5.1
  $HeadURL: https://coppermine.svn.sourceforge.net/svnroot/coppermine/trunk/cpg1.5.x/js/jquery.slideshow.js $
  $Revision: 6135 $
  $LastChangedBy: abbas-ali $
  $Date: 2009-06-10 14:15:22 +0200 (Mi, 10 Jun 2009) $
**********************************************/

/**
 * This file contains dispalyimge.php specific javascript
 */


$(document).ready(function(){

        /** set variable from php  */
        var Time    =   js_vars.Time;
        var pos     =   js_vars.position;
        var album   =   js_vars.album;
        var PiCount =   js_vars.Pic_count;
        var Pid     =   js_vars.Pid;
        var run_slideshow = js_vars.run_slideshow;
        var Title   =   "";

        /** create a Image object */
        var i = new Image();
     
        /** implement ajax call to get pic url and title */
        function loadImage (j){
        $.getJSON("displayimage.php?ajax_show=1&pos="+j+"&album="+album, function(data){
                i.src   = data['url'];
                Title   = data['title'];
                Pid     = data['pid'];
              }); 
        }
        
        /**  next pic view and keeping hold the previous pitcure ID */
        var PidTemp = Pid;
        var timer   = '';
        
        /** start the slideshow */
        if(PiCount>1) {
            runSlideShow();
        }
            
        /** set time to run slideshow */
        function runSlideShow(){
         timer =    setTimeout( showNextSlide,Time);
        }
    
        function showNextSlide(){
            
             /** clear time out */
            clearTimeout(timer);
            
            /** now load a image */         
            pos = parseInt(pos) + 1;
            if (pos  == (PiCount)){ pos=0; }
            loadImage(pos);
                        
            var temp = i.src;
            
            i.onload = function() {
                
                //if(i.complete){
                $("#showImage").attr({
                    src: i.src,
                    title: Title,
                    alt: "jQuery Logo",
                    style: "display: none;"
                }).fadeIn();
                
                $("#title").html(Title);
                /** set Pid to temp */
                PidTemp = Pid; 

            
            //now set time to loaded image.
            runSlideShow();
            //}
        }       
    }

    
    /** close the slide show and will load the current show imags details*/
    $("#back-to").click(function () { 
     self.document.location = 'displayimage.php?album='+album+'&pid='+PidTemp+'#top_display_media' ;
    });
});
