﻿// JScript File

     var _ScrollPosition=31;
     var _ScrollWidth=0;
     var _ImageChanging=false;
     var nodeId = 1048;
     var CountRepeat=0;
     var scrollTimer;
     var CountRepeatLoadScroll = 0;
     var CountRepeatLoadImageMain = 0;
     var CountColumns;
     var _FirstColumn = 1;
    
        
    function LoadImagery(property) 
    {
        if(property != "" && property != "0")
        {
            nodeId = property;
        }
        else
        {
            nodeId = 1048;
        }
        $.getJSON("/Imagery.axd?request=getListAll&nodeId="+nodeId,BuildImagery)
    }

    function BuildImagery(ImagesList) 
    {
        _ScrollPosition=31;
        _ScrollWidth=0;
        $("#scroller").css("left","31px");
        $("#scroller").css("width","0");
        $("#btnRight").css("display","block");
        $("#btnLeft").css("display","block");
        
        document.getElementById("scroller").innerHTML="";

       // $("#scroller").css("display","block");        
        $("#scroller").empty();
        if(ImagesList.length > 0)
        {
           ShowImageMain(ImagesList[0].id);
        }
        else
        {
            _MenuChanging=false;
            HideImageMain();
        }
        
        CountColumns = ((ImagesList.length - (ImagesList.length % 4)) / 4) + 1;
        var table= "";
        var tr1= "";
        var tr2= "";
        var tr3= "";
        var tr4= "";
        tr1 += "<div  class=\"all\">";
        tr2 += "<div  class=\"all\">";
        tr3 += "<div  class=\"all\">";
        tr4 += "<div  class=\"all\">";
        for(var pos=0;pos-4<ImagesList.length;pos+=4)
        {
           if(ImagesList[pos] != null)
           {
                tr1 += "<div><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"width:100px; height:75px;margin:0;padding:0;\" ><tr><td class=\"imagery_block\" style=\"width:100px; height:75px;margin:0;padding:0;\" valign=\"middle\"><a href=\"javascript:void(0)\" onclick=\"javascript:ShowImageMain("+ImagesList[pos].id+");\"><img src=\"" + ImagesList[pos].thumbnailImage + "\" alt=\"" + ImagesList[pos].altText + "\" /></a></td></tr></table></div>";
           }
           if(ImagesList[pos + 1] != null)
           {
                tr2 += "<div><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"width:100px; height:75px;margin:0;padding:0;\" ><tr><td class=\"imagery_block\" style=\"width:100px; height:75px;margin:0;padding:0;\" valign=\"middle\"><a href=\"javascript:void(0)\" onclick=\"javascript:ShowImageMain("+ImagesList[pos + 1].id+");\"><img src=\"" + ImagesList[pos + 1].thumbnailImage + "\" alt=\"" + ImagesList[pos + 1].altText + "\" /></a></td></tr></table></div>";
           }
           if(ImagesList[pos + 2] != null)
           {
                tr3 += "<div><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"width:100px; height:75px;margin:0;padding:0;\" ><tr><td class=\"imagery_block\" style=\"width:100px; height:75px;margin:0;padding:0;\" valign=\"middle\"><a href=\"javascript:void(0)\" onclick=\"javascript:ShowImageMain("+ImagesList[pos + 2].id+");\"><img src=\"" + ImagesList[pos + 2].thumbnailImage + "\" alt=\"" + ImagesList[pos + 2].altText + "\" /></a></td></tr></table></div>";
           }
           if(ImagesList[pos + 3] != null)
           {
                tr4 += "<div><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"width:100px; height:75px;margin:0;padding:0;\" ><tr><td class=\"imagery_block\" style=\"width:100px; height:75px;margin:0;padding:0;\" valign=\"middle\"><a href=\"javascript:void(0)\" onclick=\"javascript:ShowImageMain("+ImagesList[pos + 3].id+");\"><img src=\"" + ImagesList[pos + 3].thumbnailImage + "\" alt=\"" + ImagesList[pos + 3].altText + "\" /></a></td></tr></table></div>";
           }
       }
        tr1 += "</div>";
        tr2 += "</div>";
        tr3 += "</div>";
        tr4 += "</div>";
       table += tr1 + tr2 + tr3 + tr4;
        CountColumns = ((ImagesList.length - (ImagesList.length % 4)) / 4) + 1;
        AppendScroll(table);
  //      alert(table);
      //  if (navigator.userAgent.indexOf ("MSIE 6") != -1)
      //  { 
  	  //      setTimeout("ReSizeScroller()",1000);
  	  //  }
   }
    
   function ShowImageMain(id)
   {

        if(_ImageChanging){return;}
        _ImageChanging=true;
        
      //  $("#ImageMain").fadeOut("fast",function(){
      //          $("#ImageMain").css("display","none");
                $.getJSON("/Imagery.axd?request=get&propertyImageId="+id,BuildBody);
      //      });

   }
   
    function BuildBody(image)
    {
        document.getElementById("hiImage").value = image.id;
      //  $("#ImageMain").attr("height","");
      //  $("#ImageMain").attr("width","");
      //  $("#ImageMain").attr("src",image.largeImage);
      //  $("#ImageMain").attr("alt",image.altText);
      //  $("#ImageMain").attr("title",image.altText);
        
        imageMain = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"width:100%; height:100%;\" ><tr><td class=\"imagery_block\"><img id=\"ImageMain\" style=\"display:none;\" src=\"" + image.largeImage + "\" title=\"" + image.altText + "\" /></td></tr></table>"

        $("#divImageMain").empty();
        $("#divImageMain").append(imageMain);

        $("#PropertyName").empty();
        $("#PropertyName").append(image.properyName);
        
        $("#Caption").empty();
        $("#Caption").append(image.caption);
        
       _ImageChanging=false;
       // ReSizeImageMain();
        $("#ImageMain").load(function(){
             $("#ImageMain").css("display","");
             ReSizeImageMain();
         });
   //     $.getJSON("/Imagery.axd?request=setSelectImage&image="+image.id,function(){});
   }

    function HideImageMain()
    {
        $("#ImageMain").fadeOut("fast",function(){
            $("#ImageMain").css("display","none");
        });
    }

    function AppendScroll(productList)
    {
        var ImageMainWidth = 0;
        var ImageMainHeigth = 0;
        document.getElementById('hiImageList').value = "";
        
        $("#ImageMain").each(function(){

            ImageMainWidth = this.width;
            ImageMainHeigth = this.height;
        });
        if((ImageMainWidth > 0 && ImageMainHeigth > 0) || CountRepeat >= 50)
        {
            CountRepeat = 0;
            $("#scroller").append($(productList));
            $("#scroller").animate({width:"100%"},"slow",function(){
            $("#scroller").css("width","");
            });
	        setTimeout("ReSizeScroller()",15000);
            CheckButtons();            
        }
        else
        {
            document.getElementById('hiImageList').value = productList;
            setTimeout("AppendScrollT()",100);
            CountRepeat = CountRepeat + 1;
        }
    }
    function AppendScrollT()
    {
        var ImageMainWidth = 0;
        var ImageMainHeigth = 0;
        productList = document.getElementById('hiImageList').value;
        document.getElementById('hiImageList').value = "";
        
        $("#ImageMain").each(function(){

            ImageMainWidth = this.width;
            ImageMainHeigth = this.height;
        });
        if((ImageMainWidth > 0 && ImageMainHeigth > 0) || CountRepeat >= 50)
        {
            CountRepeat = 0;
           $("#scroller").append($(productList));
            $("#scroller").animate({width:"100%"},"slow",function(){
            $("#scroller").css("width","");
           });
	        setTimeout("ReSizeScroller()",15000);
            CheckButtons();
        }
        else
        {
            document.getElementById('hiImageList').value = productList;
            setTimeout("AppendScrollT()",100);
            CountRepeat = CountRepeat + 1;
        }
   }   

function ReSizeScroller()
{
	maxWidth = 100;
	maxHeigth = 75;

    LoadCountImages = 0;
    CountImages = 0;

    $('#scroller img').each(function(){

        originalWidth = this.width;
        originalHeigth = this.height;

        if(originalWidth > maxWidth || originalHeigth > maxHeigth || (originalWidth == 0 && originalHeigth == 0))
        {
            CountImages++;
	        if(originalWidth > 0 && originalHeigth  > 0)
	        {
		    // Resample image

		        if (originalWidth  > maxWidth)
		        {
			        $(this).attr("height",(originalHeigth  * (maxWidth / originalWidth)));
			        $(this).attr("width", maxWidth);

        		        originalWidth = this.width;
        		        originalHeigth = this.height;
		        }

		        if (originalHeigth  > maxHeigth)
		        {
			        $(this).attr("height", maxHeigth);
			        $(this).attr("width", (originalWidth * (maxHeigth / originalHeigth)));
		        }
		        LoadCountImages++;
	        }
	    }
    });
    
    
    if(LoadCountImages < CountImages)
    {
	    setTimeout("ReSizeScroller()",150000);
    }

    CountRepeatLoadScroll++;

}

function ReSizeImageMain()
{
	maxWidth = 480;
	maxHeigth = 360;

    LoadCountImages = 0;
    CountImages = 0;

    $('#ImageMain').each(function(){

        originalWidth = this.width;
        originalHeigth = this.height;

        CountImages++;
	    if(originalWidth > 0 && originalHeigth  > 0)
	    {
		// Resample image

		    if (originalWidth  > maxWidth)
		    {
			    $(this).attr("height",(originalHeigth  * (maxWidth / originalWidth)));
			    $(this).attr("width", maxWidth);

        		    originalWidth = this.width;
        		    originalHeigth = this.height;
		    }

		    if (originalHeigth  > maxHeigth)
		    {
			    $(this).attr("height", maxHeigth);
			    $(this).attr("width", (originalWidth * (maxHeigth / originalHeigth)));
		    }
		    LoadCountImages++;
	    }
    });

    if(LoadCountImages < CountImages && CountRepeatLoadImageMain < 2000)
    {
	    setTimeout("ReSizeScroller()",300);
    }

    CountRepeatLoadImageMain++;

}
    function btnLeft_Mousedown()
    {
        _ScrollPosition+=115;
         _FirstColumn--;
     
        $("#scroller").animate({left: _ScrollPosition}, "fast");
        if(CheckButtons())
        {
            scrollTimer = setTimeout('btnLeft_Mousedown()',500); 
        } 
    }
    function btnRight_Mousedown()
    {
        _ScrollPosition-=115;
         _FirstColumn++;
        
        $("#scroller").animate({left: _ScrollPosition}, "fast");
        if(CheckButtons())
        {
            scrollTimer = setTimeout('btnRight_Mousedown()',500); 
        }  
   }
   
   function killScroll()
   {
        clearTimeout(scrollTimer);
 //       alert("killScroll");
   }
    
    function CheckButtons()
    {
        //get scroll actual width
        
        var isChange = false;
        
        $("#btnRight").css("display","block");
        $("#btnLeft").css("display","block");
        
        if(_FirstColumn + 3 > CountColumns)
        {
            $("#btnRight").css("display","none");
            isChange = true;
        }
        if(_FirstColumn == 1)
        {
            $("#btnLeft").css("display","none");
            isChange = true;
        }
        return !isChange;

    }
    
