/*

		GLENTWORTH LETTINGS
		VERSION 1.0

		ONLOAD FUNCTIONS
		
		- 



*/

var timer;
var currentProperty = 1;

$('document').ready(function() {
	initFeaturedProperties();
	initThumbnails();
	initFilter();
	initAds();
	$("div#s3slider").s3slider({timeOut: 4500});
});

function initFeaturedProperties() {
	if ($('body.home').length > 0) {
		$('#featured-properties').append('<img src="/images/miscellaneous/arrow.png" alt="" id="arrow" width="25" height="49" />').find("#arrow").css({
			'left': '170px',
			'position': 'absolute',
			'top': '95px',
			'visibility': 'visible',
			'z-index': '106'
		})
		$('#featured-properties .property div').css('cursor', 'pointer').mouseenter(function() {
			if (!$(this).parent().hasClass('open')) {
				clearTimeout(timer);
				timer = setTimeout(nextFeaturedProperty, 10000);
				currentProperty = parseInt($(this).parent().attr('id').substr(9));
				var arrow = new Array('', '95px', '130px', '165px', '200px', '235px');
				$('#featured-properties div.open>img')
					.fadeOut('fast');
				$(this).next()
					.css('visibility','visible')
					.fadeIn('fast');
				$('#featured-properties div')
					.removeClass('open')
					.find('div')
					.children()
					.not('h3')
					.stop(true, true)
					.fadeOut('fast');
				$(this).parent().addClass('open');
				$("#featured-properties .property div").each(function() {
					var currentNo = parseInt($(this).parent().attr('id').substr(9));
					var open = new Array('', '0px', '-35px', '-70px', '-105px', '-140px');
					var closed = new Array('', '0px', '-100px', '-135px', '-170px', '-205px');
					if (currentNo <= currentProperty && currentNo > 1) {
						$(this).animate({'bottom': open[currentNo]}, 'fast');
					} else if (parseInt($(this).parent().attr('id').substr(9)) > 1) {
						$(this).animate({'bottom': closed[currentNo]}, 'fast');
					}
				})
				$('#arrow').animate({'top': arrow[currentProperty]}, 'fast');
				$('#featured-properties div.open')
					.find('div')
					.children()
					.not('h3')
					.stop(true, true)
					.css('visibility','visible')
					.fadeIn('fast');
			};
		});
		
		timer = setTimeout(nextFeaturedProperty, 3000);
		
	}
}

function nextFeaturedProperty() {
	(currentProperty < 5) ? currentProperty++ : currentProperty = 1;
	$('#featured_' + currentProperty + ' div').mouseenter();
	clearTimeout(timer);
	timer = setTimeout(nextFeaturedProperty, 3000);
}

function initThumbnails() {
	if ($('#thumbnails').length > 0) {
		$('.fullsize').wrap('<div />').parent().css({
			height: '360px',
			position: 'relative',
			width: '594px'
		});
		$('#thumbnails a').click(function() {
			$('.fullsize').fadeOut('fast').attr('src',$(this).closest('a').attr('href')).fadeIn('fast');
			return false;
		})
	};
}

function initFilter() {
	if ($('#sort-options input:submit').length > 0) {
		$('#sort-options input:submit').remove();
		$('#sort-options select').change(function() {
			$(this).parent().submit();
		})
	}
}

function initAds() {
	if ($('#home-ad div').length > 1) {
		rotateAds(1);
	}
}

function rotateAds(currentAd) {
	var numberAds = $('#home-ad div').length;
	currentAd = currentAd % numberAds;
	$('#home-ad div').eq(currentAd).fadeOut(1000, function() {
		$('#home-ad div').each(function(i) {
			$(this).css('z-index', ((numberAds - i) + currentAd) % numberAds);
		})
		$(this).show();
		setTimeout(function() { rotateAds(++currentAd); }, 5000)
	});
}
/* ------------------------------------------------------------------------
	s3slider

	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.0

	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */


(function($){
    $.fn.s3slider = function(vars) {

        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
        var current     = null;
        var timeOutFn   = null;
        var faderStat   = true;
        var mOver       = false;
        var items       = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsSpan   = $("#" + element[0].id + "Content ." + element[0].id + "Image span");

        items.each(function(i) {

            $(items[i]).mouseover(function() {
               mOver = true;
            });

            $(items[i]).mouseout(function() {
                mOver   = false;
                fadeElement(true);
            });

        });

        var fadeElement = function(isMouseOut) {
            var thisTimeOut = (isMouseOut) ? (timeOut/2) : timeOut;
            thisTimeOut = (faderStat) ? 10 : thisTimeOut;
            if(items.length > 0) {
                timeOutFn = setTimeout(makeSlider, thisTimeOut);
            } else {
                console.log("Poof..");
            }
        }

        var makeSlider = function() {
            current = (current != null) ? current : items[(items.length-1)];
            var currNo = jQuery.inArray(current, items) + 1;
            currNo = (currNo == items.length) ? 0 : (currNo - 1);
            var newMargin   = $(element).width() * currNo;
            if(faderStat == true) {
                if(!mOver) {
                    $(items[currNo]).fadeIn((750), function() {
                        if($(itemsSpan[currNo]).css('bottom') == 0) {
                            $(itemsSpan[currNo]).slideUp((750), function() {
                                faderStat = false;
                                current = items[currNo];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        } else {
                            $(itemsSpan[currNo]).slideDown((750), function() {
                                faderStat = false;
                                current = items[currNo];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        }
                    });
                }
            } else {
                if(!mOver) {
                    if($(itemsSpan[currNo]).css('bottom') == 0) {
                        $(itemsSpan[currNo]).slideDown((750), function() {
                            $(items[currNo]).fadeOut((750), function() {
                                faderStat = true;
                                current = items[(currNo+1)];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        });
                    } else {
                        $(itemsSpan[currNo]).slideUp((750), function() {
                        $(items[currNo]).fadeOut((750), function() {
                                faderStat = true;
                                current = items[(currNo+1)];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        });
                    }
                }
            }
        }

        makeSlider();

    };
})(jQuery);
