﻿//Version=1 Version=2
function getMaxPageLV()
{
	if(aptsCount<=perPage)
		return 1;
	var tmp = aptsCount%perPage;
	if(tmp==0)
	{
		return aptsCount/perPage;
	}else
	{
		return Math.floor(aptsCount/perPage)+1;	
	}
	
}
function getNextPageLV()
{	
if(page<maxPage)
{
	if(page==0)
	{
		page=1;
	}
	else
	{
		page+=1;
	}
}
	getPageLV();	
}
function getPreviousPageLV()
{
	if(page==0 || page==1)
	{
	page=1;
	}
	else
	{
	page-=1;
	}
	getPageLV();
}
function getPageLV()
{
		document.getElementById('page').innerHTML = page+'/'+maxPage;	
		var endIndex = page*perPage;
		for(var i=0; i<aptsCount;i++)
		{
			if(i<endIndex && i>=endIndex-perPage)			
			{
				try
				{
				    mint.fx.Fade('apt_'+i, 0, 1, 1);
					var apartment = document.getElementById('apt_'+i);
					apartment.style.display = 'block';
					if(i%2!=0)
					{
					apartment.className+=" right";
					}
				    mint.fx.Fade('apt_'+i, 100, 20, 500);
				}catch(e)
				{
				}
			}else
			{
			try
			{
				document.getElementById('apt_'+i).style.display = 'none';
			}catch(e)
			{
			}
			}
			
		}
}
function removeLV(id)
{
    //aptsCount= apartmentsapart.Modules.LastViewedApartments.GetAptsCount().value;
    apartmentsapart.Modules.LastViewedApartments.RemoveAptLV(id);
    var html  = apartmentsapart.Modules.LastViewedApartments.BuildHTMLLV().value;
    aptsCount= apartmentsapart.Modules.LastViewedApartments.GetAptsCount().value;

    maxPage = getMaxPageLV();

    page=0;

    document.getElementById('lvapts').innerHTML = html;

    //getPageLV();
    getNextPageLV();
    var navi = document.getElementById('navLV');
    if(maxPage<=1 && navi!=null)
    {
    navi.style.visibility = "hidden";
    }
    aptsCount= apartmentsapart.Modules.LastViewedApartments.GetAptsCount().value;
    if(aptsCount==0)
    {

    try
    {
    $('SimilarApartments').style.display = 'none';
    }
    catch(err){}
    }
}


//Version=3 Version=4
function LVaptsList(div, length, ver) {
    this.lg_intervalID;
    this.current_item = 0;
    this.contentDIV = div;
    this.nrOfApts = length;
    this.isPaused = false;
    this.animationRunning = false;
    this.minimumApts = (ver == 5 ? 3 : 4);
    this.version = ver;

    this.next = function() {
        if (this.nrOfApts < this.minimumApts) {
            this.stopAnimation();
            return;
        }
        if (this.animationRunning)
            return;
        if (jQuery('#PW').css('display') != 'none') {
            return;
        }
        this.animationRunning = true;
        this.current_item += 1;
        if (jQuery('#' + this.contentDIV + '>.apt').length == this.nrOfApts) {
            var content = document.getElementById(this.contentDIV);
            content.innerHTML += content.innerHTML;
            jQuery('#' + this.contentDIV).animate(
                { marginLeft: '-=285' }, 1000, function() {
                    LVObject.animationRunning = false;
                });

        }
        else if (this.current_item >= this.nrOfApts) {
            this.current_item = 0;
            var content = document.getElementById(this.contentDIV);
            content.innerHTML += content.innerHTML;
            jQuery('#' + this.contentDIV).animate(
                { marginLeft: '-=285' }, 1000, function() {

                    var tmpapts = jQuery('#' + LVObject.contentDIV + '>.apt');
                    if (tmpapts.length == 4 * LVObject.nrOfApts) {
                        for (var i = 0; i < 2 * LVObject.nrOfApts; i++) {
                            jQuery(tmpapts[i]).remove();
                        }

                        var tmp = LVObject.version == 5 ? 225 : 265;
                        jQuery('#' + LVObject.contentDIV).css('margin-left', '-' + tmp.toString() + 'px');
                    }
                    LVObject.animationRunning = false;
                });
        }
        else {
            jQuery('#' + this.contentDIV).animate(
                { marginLeft: '-=285' }, 1000, function() { LVObject.animationRunning = false; });
        }
    };

    this.prev = function() {
        if (this.animationRunning)
            return;
        this.animationRunning = true;
        this.current_item -= 1;
        if (this.current_item < 0) {
            this.current_item = this.nrOfApts - 1;
            var content = document.getElementById(this.contentDIV);
            content.innerHTML += content.innerHTML;
            var offset = (this.version == 5 ? 225 : 265);
            var tmp = 285 * (this.current_item + 1) + offset;
            jQuery('#' + this.contentDIV).css('margin-left', '-' + tmp.toString() + 'px');
            jQuery('#' + this.contentDIV).animate(
            { marginLeft: '+=285' }, 1000, function() {
                var tmpapts = jQuery('#' + LVObject.contentDIV + '>.apt');
                if (tmpapts.length == 4 * LVObject.nrOfApts) {
                    for (var i = 2 * LVObject.nrOfApts; i < 4 * LVObject.nrOfApts; i++) {
                        jQuery(tmpapts[i]).remove();
                    }

                    var tmp = 285 * LVObject.current_item + offset;
                    jQuery('#' + LVObject.contentDIV).css('margin-left', '-' + tmp.toString() + 'px');
                }
                LVObject.animationRunning = false;
            });
        }
        else {
            jQuery('#' + this.contentDIV).animate(
                { marginLeft: '+=285' }, 1000, function() { LVObject.animationRunning = false; });
        }
    };

    this.Animate = function() {
        if (this.lg_intervalID == null) {
            this.startAnimation();
            this.isPaused = false;
        } else {
            this.stopAnimation();
            this.isPaused = true;
        }
    };

    this.startAnimation = function() {
        if (this.lg_intervalID != null) {
            this.next();
        }
        this.lg_intervalID = setInterval("LVObject.next()", 8000);
    };
    this.stopAnimation = function() {
        if (this.lg_intervalID != null) {
            clearInterval(this.lg_intervalID);
        }
        this.lg_intervalID = null;
    };

    this.checkIsScrollingNeeded = function() {
        if (this.version != 5) {
            if (this.nrOfApts < this.minimumApts) {
                jQuery('#SimilarApartments>div>.next').css('display', 'none');
                jQuery('#SimilarApartments>div>.prev').css('display', 'none');
                jQuery('#SimilarApartments>div>.header').css('width', '930px');
                if (this.nrOfApts == 3) {
                    jQuery('#' + this.contentDIV).css('margin-left', '25px');
                }
                else if (this.nrOfApts == 2) {
                    jQuery('#' + this.contentDIV).css('margin-left', '0px');
                    jQuery('#' + this.contentDIV + '>.apt').css('margin-left', '120px');
                }
                else if (this.nrOfApts == 1) {
                    jQuery('#' + this.contentDIV).css('margin-left', '310px');
                }
                else if (this.nrOfApts == 0) {
                    jQuery('#SimilarApartments').css('display', 'none');
                }
            }
            else if (this.nrOfApts == this.minimumApts) {
                jQuery('#' + this.contentDIV).css('margin-left', '25px');
            }
            else {
                jQuery('#' + this.contentDIV).css('margin-left', '-265px');
            }
        }
        else {
            if (this.nrOfApts < this.minimumApts) {
                jQuery('#SimilarApartments>div>.next').css('display', 'none');
                jQuery('#SimilarApartments>div>.prev').css('display', 'none');
                jQuery('#SimilarApartments>div>.header').css('width', '715px');
                if (this.nrOfApts == 2) {
                    jQuery('#' + this.contentDIV).css('margin-left', '0px');
                    jQuery('#' + this.contentDIV + '>.apt').css('margin-left', '55px');
                }
                else if (this.nrOfApts == 1) {
                    jQuery('#' + this.contentDIV).css('margin-left', '200px');
                }
                else if (this.nrOfApts == 0) {
                    jQuery('#SimilarApartments').css('display', 'none');
                }
            }
            else {
                jQuery('#' + this.contentDIV).css('margin-left', '-225px');
            }
        }
    }
}
function removeLVA(id) {
    apartmentsapart.Modules.LastViewedApartments.RemoveAptLV(id);
    var html = apartmentsapart.Modules.LastViewedApartments.BuildHTMLLV().value;
    var aptsCount = apartmentsapart.Modules.LastViewedApartments.GetAptsCount().value;

    document.getElementById('lv_content').innerHTML = html;

    LVObject = new LVaptsList('lv_content', aptsCount, LVObject.version);
    LVObject.checkIsScrollingNeeded();
    if (jQuery('#lv_content>.apt').length == 0) {
        jQuery('#SimilarApartments').css('display', 'none');
    }
}
if(jQuery().touchwipe){
    jQuery("#lv_content").touchwipe({
        wipeLeft: function() { LVObject.next(); },
        wipeRight: function() { LVObject.prev(); },
        min_move_x: 20,
        min_move_y: 20,
        preventDefaultEvents: true
    });
}
