function DISPLAY(el, trigger) {
	$(trigger).live("click", function(e) {
		e.preventDefault();
		$(el).slideToggle();
	});
}

/**
 * Hoitaa kohdelistauksen uudelleenlatauksen 
 * select:n arvon muuttuessa.
 * Käytössä: asunnto.php, loma-asunnot.php, 
 * toimitilat.php, vuokra-asunnot.php, tontit.php
 */
var LIMIT = {
	init: function() {
		$('#limit').live("change", function() {
			LIMIT.run();
		});
	},
	run: function() {
		var $el = $('#limit'),
			value = $el.val(),
			page;
		
		if ($el.hasClass('asunnot')) {
			page = "asunnot";
		} else if ($el.hasClass('tontit')) {
			page = "tontit";
		} else if ($el.hasClass('vuokra-asunnot')) {
			page = "vuokra-asunnot";
		} else if ($el.hasClass("loma-asunnot")) {
			page = "loma-asunnot";
		}
		window.location = 'http://leajakama.fi/' + page + '.php?limit=' + value;
	}
};

/**
 * Näyttää kuvien lukumäärän esim( 2/14 ).
 * Käytössä: kohde.php
 */
var CYCLECOUNT = {
	current: 1,
	total: null,
	
	placeholder: {
		current: null,
		total: null
	},
	
	init: function() {
		var _this = this;
		
		_this.total = $('#gallery-images').find('img').length;
		_this.placeholder.total = $('#total');
		_this.placeholder.current = $('#current');
		
		$('.control').click(function(e) {
			e.preventDefault();
			_this.count(e);
		});
		_this.updateUI();
	},
	count: function(e) {
		var _this = this;
		if (e.target.id == "next") {
			if (_this.current < _this.total) {
				_this.current++;
			} else {
				_this.current = 1;
			}
		} else {
			if (_this.current == 1) {
				_this.current = _this.total;
			} else {
				_this.current--;
			}
		}
		_this.updateUI();
	},
	updateUI: function() {
		var _this = this;
		_this.placeholder.total.text(_this.total);
		_this.placeholder.current.text(_this.current);
	}
};


function popitup(url) {
	var newwindow=window.open(url,'name','height=800,width=1240,scrollbars=1');
	if (window.focus) {newwindow.focus();}
	return false;
}

$(document).ready(function() {
	var search = new DISPLAY('#search', '.search'),
		contact = new DISPLAY('#realtyContact', '.realtyContact');
	LIMIT.init();
});
