/*==============================================================================
Name: 		Slideshow Engine 
Version: 	1.0
Author:		Martin Donchev
Created: 	October 27, 2008
================================================================================
Copyright (c) 2008 Martin Donchev
==============================================================================*/


function Slideshow(SlideshowDOMName, BigImageElement, SubtitleElement, PlayElement, PauseElement, IndexElement, CountElement) {
	

	this.Playing=1;
	
	this.ImagesListing = new Array();
	this.ImagesListing.length=0;

	this.SubtitlesListing = new Array();
	this.SubtitlesListing.length=0;


	this.CurrentImageNumber=0;
	this.CurrentBroiach=0;

	this.SlideshowDOMName=SlideshowDOMName;
	
	this.BigImageElement=BigImageElement;
	this.SubtitleElement=SubtitleElement;
	
	this.PlayElement=PlayElement;
	this.PauseElement=PauseElement;
	
	this.IndexElement=IndexElement;
	this.CountElement=CountElement;

	this.OpenAdsElement = "";
	this.OpenAdsZone = "";
	// ----------------------------------------------------------------



	// Add to Slideshow
	this.AddToSlideshow = function(image_path,subtitle) {
		
		//alert("AddToSlideshow("+image_path+")");
		this.ImagesListing.push(image_path);
		
		this.SubtitlesListing.push(subtitle);
		
	}
	
	this.LoadImage = function (image_number) {
		
		

		this.CurrentImageNumber=image_number;
		this.CurrentBroiach=image_number;
		
		// Loading the image
		document.getElementById(this.BigImageElement).src=this.ImagesListing[image_number];
		
		
		// Loading the subtitle
		document.getElementById(this.SubtitleElement).innerHTML=this.SubtitlesListing[image_number];
		
		// Set the index and the count texts
		if ($(this.IndexElement)!=undefined) {$(this.IndexElement).innerHTML=this.CurrentImageNumber+1;}
		if ($(this.CountElement)!=undefined) {$(this.CountElement).innerHTML=this.ImagesListing.length;}
		
		
		// Loading the new openads adv
		if (this.OpenAdsElement!="") {
			document.getElementById(this.OpenAdsElement).innerHTML="<a href='http://www.disco.bg/NewSite/openads/adclick.php?n=a68444b2' target='_blank'><img src='http://www.disco.bg/NewSite/openads/adview.php?what=zone:"+this.OpenAdsZone+"&amp;n=a68444b2&amp;random="+Math.random()+"' border='0' alt=''></a>";
		}
	}
	
	
	this.PlayNext = function () {
		
	
		if (this.Playing==1) {	// Working only if the playing flag is on !!!
			
			this.CurrentBroiach=this.CurrentBroiach+1;
			
			if (this.CurrentBroiach==this.ImagesListing.length) {
				this.CurrentBroiach=0;	
			}
	
			this.LoadImage(this.CurrentBroiach);
		}

	}
	
	
	this.TogglePlayPause = function () {
		
		$(this.PlayElement).toggle();
		$(this.PauseElement).toggle();
	
		if (this.Playing==1) {this.Playing=0;} else {this.Playing=1;}
		
		if (GeneralPlaying==1) {GeneralPlaying=0;} else {GeneralPlaying=1;}
		
	}
	
	this.Stop = function () {
		this.Playing = 0;
	}
	
	
	this.btnPlayPrev = function () {

			this.CurrentBroiach=this.CurrentBroiach-1;
			
			if (this.CurrentBroiach==-1) {
				this.CurrentBroiach=this.ImagesListing.length;	
			}
	
			this.LoadImage(this.CurrentBroiach);

	}
	
	
	
	this.btnPlayNext = function () {


			this.CurrentBroiach=this.CurrentBroiach+1;
			
			if (this.CurrentBroiach==this.ImagesListing.length) {
				this.CurrentBroiach=0;	
			}
	
			this.LoadImage(this.CurrentBroiach);

	}
	
	
	
	//
    //  enableKeyboardNav()
    //
    this.enableKeyboardNav = function() {
        document.observe('keydown', this.keyboardAction); 
    }
    
    
     //
    //  disableKeyboardNav()
    //
    this.disableKeyboardNav = function() {
        document.stopObserving('keydown', this.keyboardAction); 
    }
	
	//
    //  keyboardAction()
    //
    this.keyboardAction = function(event) {
    
        var keycode = event.keyCode;

        var escapeKey;
        if (event.DOM_VK_ESCAPE) {  // mozilla
            escapeKey = event.DOM_VK_ESCAPE;
        } else { // ie
            escapeKey = 27;
        }

        var key = String.fromCharCode(keycode).toLowerCase();
        
        if (keycode == escapeKey){ // close lightbox
            
            	PopupClose();
            
        } else if (keycode == 37){ // display previous image
          
                // Play Prev
                Slideshow_btnPlayPrev();

        } else if (keycode == 39){ // display next image
 
				// Play Next
				Slideshow_btnPlayNext();
			
        }
    }
	

}



