

function MLX_Fader(ctlName, myName)
{
	this.name = myName;
	this.ctlName = ctlName;
    this.photos = new Array();
	this.propListingID = new Array();
	this.propPrice = new Array();
	this.propAddress = new Array();
	this.propCity = new Array();
	this.propBeds = new Array();
	this.propBaths = new Array();
	this.propSqFt = new Array();
	this.propLotSize = new Array();
	this.slideShowDelay = 4;
	this.requestShowingLink = "";
	this.propertyDetailLink = "";
	this.counterTemplate = "##listingNo## of ##listingCount##";
//	this.cssText = ""; //"font-size:10pt;padding-left:6px;padding-right:6px";
	
	this._ctlPropPhoto = document.getElementById(ctlName + "_propPhoto");
	this._ctlPauseSlideShow = document.getElementById(ctlName  + "_spnPauseSlideShow");
	this._ctlPlaySlideShow = document.getElementById(ctlName  + "_spnPlaySlideShow");
	this._ctlPropPhotoHref = document.getElementById(ctlName + "_propPhotoHref");
	this._ctlPropAddress = document.getElementById(ctlName + "_propAddress");
	this._ctlPropPrice = document.getElementById(ctlName + "_propPrice");
	this._ctlPropCity = document.getElementById(ctlName + "_propCity");
	this._ctlPropBeds = document.getElementById(ctlName + "_propBeds");
	this._ctlPropBaths = document.getElementById(ctlName + "_propBaths");
	this._ctlPropSqFt = document.getElementById(ctlName + "_propSqFt");
	this._ctlPropLotSize = document.getElementById(ctlName + "_propLotSize");
	this._ctlPropDetailLink = document.getElementById(ctlName + "_propDetailLink");
	this._ctlPropRequestLink = document.getElementById(ctlName + "_propRequestLink");
	this._ctlCounter = document.getElementById(ctlName + "_propCounter");
	this._ctlPropRequestLinkSpan = document.getElementById(ctlName + "_spnRequestShowing");
	this._slideShowButton =  document.getElementById(ctlName + "_btnSlideShow");
	this._SlideShowOn = false;
	this._SlideShowTimeout = 0;
	this._SlideShowIsPaused = false;
	
	// --------- public methods --------
	this.initImage = _MLX_Fader_InitImage;
	this.nextPhotoFade = _MLX_Fader_NextPhotoFade;
	this.cancelSlideShow = _MLX_Fade_CancelSlideShow;
	this.updateCounter = _MLX_Fader_UpdateCounter;
	this.prevPhotoFade = _MLX_Fader_PrevPhotoFade;
	this.showPropInfo = _MLX_Fader_ShowPropInfo;
	this.slideShow = _MLX_Fader_SlideShow;
	this.pauseSlideShow = _MLX_Fader_PauseSlideShow
	this.restartSlideShow = _MLX_Fader_RestartSlideShow

//	this.setcssText= _MLX_Fader_SetCssText;	
	this._SlideShowFade = _MLX_Fader_SlideShowFade;
	this._setOpacity = _MLX_Fader_SetOpacity;
	this._fadeIn = _MLX_Fader_FadeIn;
	this._currentIndex = 0;
	this._setDetailValue = _MLX_Fader_SetDetailValue;
}

function _MLX_Fader_InitImage()
{
	this._setOpacity(0);	
	this._ctlPropPhoto.style.width = this.imageWidth + "px";
	this._ctlPropPhoto.style.height = this.imageHeight + "px";
	this._ctlPropPhoto.style.visibility = "visible";
	this._fadeIn(0);
}

function _MLX_Fader_FadeIn(opacity)
{
	if (opacity <= 100)
	{
		this._setOpacity(opacity);
		opacity += 20;
		window.setTimeout(this.name + "._fadeIn(" + opacity + ")", 20);
	}
}

function _MLX_Fader_SetOpacity(opacity)
{ 
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	try {this._ctlPropPhoto.style.filter = "alpha(opacity:" + opacity + ")";
	}
	catch(e){}
	// Safari<1.2, Konqueror
	this._ctlPropPhoto.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	this._ctlPropPhoto.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	this._ctlPropPhoto.style.opacity = opacity/100;
}

function _MLX_Fader_NextPhotoFade(hasCount)
{
	this.cancelSlideShow();
    this.initImage();
	this._currentIndex ++
    if (this._currentIndex >= this.photos.length)
	    this._currentIndex = 0;
	
	this.showPropInfo();
}

function _MLX_Fader_PrevPhotoFade(hasCount)
{
	this.cancelSlideShow();
    this.initImage();
    
    if (this._currentIndex > 0)
	    this._currentIndex--;
	else
	    this._currentIndex = this.photos.length - 1;

	this.showPropInfo();
}

function _MLX_Fader_SlideShow(isNotAnimated)
{
	
	if (isNotAnimated)
    {
        this.initImage();
        this.showPropInfo();
        return;
    }   
	
	if(!this._SlideShowOn)
	{
		this._SlideShowOn = true;
		this._currentIndex--;
		this._SlideShowFade();
		if (this._ctlPauseSlideShow)
		    this._ctlPauseSlideShow.style.display = "";
	    if (this._ctlPlaySlideShow)
	        this._ctlPlaySlideShow.style.display = "none";

	}
	else
	{
		if (this._ctlPauseSlideShow)
		    this._ctlPauseSlideShow.style.display = "none";
	    if (this._ctlPlaySlideShow)
	        this._ctlPlaySlideShow.style.display = "";
		this._SlideShowOn = false;
	}
}

function MLXPauseSlideShow(ctl)
{
    eval(ctl.id.replace("propPhoto", "Fader") + '.pauseSlideShow()');
}
function _MLX_Fader_PauseSlideShow()
{
    this._SlideShowIsPaused = true;
}

function MLXRestartSlideShow(ctl)
{
    eval(ctl.id.replace("propPhoto", "Fader") + '.restartSlideShow()');
}
function _MLX_Fader_RestartSlideShow()
{
    this._SlideShowIsPaused = false;
}

function _MLX_Fader_SlideShowFade()
{
    if(this._SlideShowOn )
	{
		if (!this._SlideShowIsPaused)
		{
		    this.initImage();
    		
		    if (this._currentIndex >= this.photos.length - 1)
			    this._currentIndex = 0;
		    else
		        this._currentIndex++
		    this.showPropInfo();

		}
    		
	    if (this.photos.length > 1)
	        this._SlideShowTimeout = setTimeout(this.name + "._SlideShowFade()", this.slideShowDelay * 1000);
	}
}

function _MLX_Fade_CancelSlideShow()
{
	this._SlideShowOn = false;
   clearTimeout(this._SlideShowTimeout);
}

function _MLX_Fader_ShowPropInfo()
{
    var linkCss = "featureItem css_" + this.ctlName ;
    
    this._ctlPropPhoto.src = this.photos[this._currentIndex];
    
    var thisPropDetailHref =  "";
    if (this.propertyDetailLink != "")
        thisPropDetailHref = this.propertyDetailLink.replace("##listingID##", this.propListingID[this._currentIndex]) ;
            
    if (this._ctlPropPhotoHref != null)
		  this._ctlPropPhotoHref.href = thisPropDetailHref;

    this._setDetailValue(this._ctlPropPrice, this.propPrice[this._currentIndex]);
    
    var address = this.propAddress[this._currentIndex];
    if (address && thisPropDetailHref != "")
         address = "<a href=" + thisPropDetailHref + " class='" + linkCss + "' title='Click for more information'>" + address+ "</a>";  
    this._setDetailValue(this._ctlPropAddress, address);
  
    this._setDetailValue(this._ctlPropCity, this.propCity[this._currentIndex]);
    this._setDetailValue(this._ctlPropBeds, this.propBeds[this._currentIndex]);
    this._setDetailValue(this._ctlPropBaths, this.propBaths[this._currentIndex]);
    this._setDetailValue(this._ctlPropSqFt, this.propSqFt[this._currentIndex]);
    this._setDetailValue(this._ctlPropLotSize, this.propLotSize[this._currentIndex]);
    
	if (this._ctlPropDetailLink != null)
    {
        if (this.propertyDetailLink != "")
        {
            this._ctlPropDetailLink.className = linkCss;
            this._ctlPropDetailLink.href = thisPropDetailHref.trim();
            this._ctlPropDetailLink.style.display = "inline";
        }
        else
            this._ctlPropDetailLink.style.display = "none";
    }

    if (this._ctlPropRequestLink != null)
    {
        if (this.requestShowingLink != "")
        {
            this._ctlPropRequestLink.href = this.requestShowingLink.replace("##listingID##", this.propListingID[this._currentIndex].trim());
            this._ctlPropRequestLink.className = linkCss;
            if (this._ctlPropRequestLinkSpan)
                this._ctlPropRequestLinkSpan.style.display = 'inline';
        }
        else if (this._ctlPropRequestLinkSpan)
            this._ctlPropRequestLinkSpan.style.display = 'none';
    }
    
    this.updateCounter();
}

function _MLX_Fader_SetDetailValue(ctl, myValue)
{
    if (ctl != null)
    {
	    ctl.innerHTML = myValue.trim(); 
	    ctl.setAttribute("class", "featureItem css_" + this.ctlName);
	    ctl.setAttribute("className", "featureItem css_" + this.ctlName);
	    MLX_GetPreviousSibling(ctl).setAttribute("class", "featureItem featureItemTitle css_" + this.ctlName);
	    MLX_GetPreviousSibling(ctl).setAttribute("className", "featureItem featureItemTitle css_" + this.ctlName);
	}
}


function _MLX_Fader_UpdateCounter()
{
	if (this._ctlCounter)
		this._ctlCounter.innerHTML = 
		    this.counterTemplate.replace("##listingNo##", (this._currentIndex + 1)).replace("##listingCount##", this.photos.length);
}

//function _MLX_Fader_SetCssText(myCssText)
//{
//    // not used
//    this.cssText = myCssText;
//    var fFound = false;  
//    var myClassName =  "css_" + this.ctlName;
//    var cssStr = "." + myClassName + "{" + this.cssText + "}";
//     
//    for(styleSheet in document.styleSheets)
//    {
//        if (styleSheet.cssText && styleSheet.cssText.indexOf(myClassName) != -1)
//        {
//            styleSheet.cssText = cssStr;
//            fFound= true;
//            break;
//        }        
//    }
//    if (!fFound)
//    {
//        //
//        var style = document.createElement("style");
//        style.setAttribute("type", "text/css");
//        if(style.styleSheet)
//        {
//            // IE
//            style.styleSheet.cssText = cssStr;
//            document.getElementsByTagName("head")[0].appendChild(style);
//        } 
//        else 
//        {
//            // w3c
//            var cssText = doc.createTextNode(cssStr);
//            style.appendChild(cssText);
//        }
//    }
//}

