﻿// *************************************************    
// this file was written by Ofir Zvik,
// all rights reserved to Evolution Online Ltd.
// 972-3-5622511
// http://www.evolution.co.il
// *************************************************    
// W A R N I N G !
// *************************************************    
// YOU MAY NOT USE THIS FILE, COPY IT OR ANY PART OF IT
// WITHOUT WRITTEN PERMISSION FROM EVOLUTION ONLINE LTD!
// *************************************************    

        // *************************************************    
        // changes the big image
        // *************************************************    
        function put_big_picture(big_picture_panel,caption_panel, picture, caption)
        {
            document.getElementById(big_picture_panel).innerHTML = "<img src='" + picture +"'>";
            document.getElementById(caption_panel).innerHTML = caption;
        }
        // *************************************************    
        // small function to allow not mandetory attributes
        // in strings
        // *************************************************    
        function add_cssName(css_name)
        {
            if (css_name!="")
                return "class='" + css_name +"'";
            else
                return "";
        }
        // *************************************************    
        // replace function for javascript
        // *************************************************    
        function replaceAll(sString, sReplaceThis, sWithThis) { 
            if (sReplaceThis != "" && sReplaceThis != sWithThis) {
              var counter = 0;
              var start = 0;
              var before = "";
              var after = "";
              while (counter<sString.length) {
                start = sString.indexOf(sReplaceThis, counter);
                if (start == -1){
                 break;
                 } else {
                   before = sString.substr(0, start);
                   after = sString.substr(start + sReplaceThis.length, sString.length);
                   sString = before + sWithThis + after;
                   counter = before.length + sWithThis.length;
                  }
                }
              }
           return sString;
          }
        
        // *************************************************    
        // Evolution gallery object
        // *************************************************    
        function evolution_gallery()
        {
            this.current_page = 1;      // on what page are we?
            this.repeat_columns = 2;    // how many columns for the thumbnails
            this.page_size =8;          // how many results in a page
            this.pictures = new Array();// the pictures objects
            this.thumbnail_panel;       // the object (i.e div) where the thumbnails sits in
            this.thumbnail_pager_panel; // the object (i.e div) where the next previous sits in
            this.big_picture_panel;     // the object (i.e div) where the big picture sits in
            this.caption_panel;         // the object (i.e div) where the caption of the pictures sits in
            this.slide_show_panel;      // the object (i.e div) where the slide show sits in
            this.slide_show_delay = 4;  // the delay (in seconds) of the slide show
            this.thumb_width = '50px';  // thumbnail width
            this.thumb_height = '50px'; // thumbnail height
            this.item_css = '';         // thumbnail pictures CSS class
            this.pager_css = '';        // pager CSS class
            this.next_image ='';        // next page image filename
            this.previous_image ='';    // previous page image filename
		    // *************************************************    
            // add new picture to the gallery
    	    // *************************************************    
            this.add_picture = function (thumbnail,picture,caption)
                    {
                        this.pictures[this.pictures.length] = new evolution_picture(thumbnail, picture, caption);
                    }
    	    // *************************************************    
            // jump to a page
    	    // *************************************************    
            this.goto_page = function(evolution_gallery_object_name,page_number)
                    {
                        this.current_page = page_number;
                        this.draw_thumbnails (evolution_gallery_object_name);
                    }
    	    // *************************************************    
            // draw page next / previous
    	    // *************************************************    
    	    this.draw_pager = function(evolution_gallery_object_name)
    	    {
                        var str_output = "";
                        var last_page;
                        last_page = parseInt(this.pictures.length/this.page_size);
                        if (last_page<this.pictures.length/this.page_size)
                            last_page++;
                            
                        if (this.current_page>1)
                            str_output +="<td><a href='javascript:void(0)' onClick='" + evolution_gallery_object_name + ".goto_page(\"" +evolution_gallery_object_name+"\"," +(this.current_page-1)+");'><img src='"+this.previous_image+"' border='0'></a></td>";
                        else
                            str_output += "<td>&nbsp;</td>";
                            str_output += "<td>עמוד " + this.current_page +" מתוך " +  last_page +" </td>";
                        if (this.current_page<last_page)
                            str_output +="<td><a href='javascript:void(0)' onClick='" + evolution_gallery_object_name + ".goto_page(\"" +evolution_gallery_object_name+"\"," +(this.current_page+1)+");'><img src='"+this.next_image+"' border='0'></a></td>";
                        else
                            str_output += "<td>&nbsp;</td>";
    	                document.getElementById(this.thumbnail_pager_panel).innerHTML = "<table "+add_cssName(this.pager_css)+"><tr>" + str_output + "</tr></table>";
    	    }
    	    // *************************************************    
            // draws the thumbnail panel
    	    // *************************************************    
            this.draw_thumbnails = function(evolution_gallery_object_name, first_time)
                    {
                    
                        var str_output = "";
					    // *************************************************    
                        // which page to display?
					    // *************************************************    
					    var i_start = ((this.current_page-1) * this.page_size);
					    var i_end;
					    if (i_start + this.page_size>this.pictures.length)
					        i_end = this.pictures.length;
					    else
					        i_end = i_start + this.page_size;
					    // *************************************************    
                        // generate html
					    // *************************************************    
					        var j=-1;
    					    for (var i=i_start;i<i_end;i++)
                            {
                                if(i==0)
                                {
                                    str_output += "<tr>";
                                    if (first_time)
                                        eval("put_big_picture(\"" +this.big_picture_panel + "\",\""+ this.caption_panel+"\",\"" + this.pictures[i].m_picture + "\",\" " + replaceAll(this.pictures[i].m_caption,"'","\'") + "\")");
                                 }
                                j++;
                                if (j==this.repeat_columns)
                                    if (i==this.pictures.length)
                                    {
                                        j=0;
                                        str_output += "</tr>";
                                    }
                                    else
                                    {
                                        j=0;
                                        str_output += "<tr></tr>";
                                     }
                                str_output +="<td "+add_cssName(this.item_css)+">" +                                                                
                                                    "<a href='javascript:put_big_picture(\"" +this.big_picture_panel + "\",\""+ this.caption_panel+"\",\"" + this.pictures[i].m_picture + "\",\" " + replaceAll(this.pictures[i].m_caption,"'"," ") + "\")'>" +
                                                    "<img src='" + this.pictures[i].m_thumbnail + "' alt='"+this.pictures[i].m_caption+"' border='0' style='width:"+this.thumb_width+";height:"+this.thumb_height+"'>"+
                                                    "</a>" +
                                                "</td>";
                            }
                        // draw output
                        document.getElementById(this.thumbnail_panel).innerHTML = "<table>" + str_output + "</table>";
                        this.draw_pager(evolution_gallery_object_name);
                    }
            
        }
        function evolution_picture(thumbnail, picture, caption)
        {
            this.m_thumbnail = thumbnail;
            this.m_picture = picture;
            this.m_caption = caption;
        }


