/**
 * Athome search pages ajax interface.
 *
 *
 */
var searchTexts = {
	at: {
		email: 'Email',
		your_email: 'Your Email',
		message: 'Message (optional)',
		contact_us: 'Contact Us',
		contact_info: 'Kontakt',
		send_properties: 'Send saved properties',
		send_property:	'Send Property',
		alert_email: 'Please spacify the contact email!'
	},
	cz: {
		email: 'Email',
		your_email: 'Váš e-mail',
		message: 'Vaše zpráva',
		contact_us: 'Contact Us',
		contact_info: 'Kontakt',
		send_properties: 'Send saved properties',
		send_property:	'Send Property',
		alert_email: 'Please spacify the contact email!'
	},
	en: {
		email: 'Email',
		your_email: 'Your Email',
		message: 'Message (optional)',
		contact_us: 'Contact Us',
		contact_info: 'Contact Information',
		send_properties: 'Send saved properties',
		send_property:	'Send Property',
		alert_email: 'Please spacify the contact email!'
	},
	hu: {
		email: 'Email',
		your_email: 'Ön Email címe',
		message: 'Személyes üzenet',
		contact_us: 'Contact Us',
		contact_info: 'Kapcsolat',
		send_properties: 'Mentett ingatlanok elküldése',
		send_property:	'Ingatlan küldése',
		alert_email: 'Kérem adja meg az email címét!'
	},
	pl: {
		email: 'Email',
		your_email: 'E-mail',
		message: 'Wiadomość (opcjonalnie)',
		contact_us: 'Contact Us',
		contact_info: 'Kontakt',
		send_properties: 'Send saved properties',
		send_property:	'Send Property',
		alert_email: 'Please spacify the contact email!'
	},
	ru:	{
		email: 'Email',
		your_email: 'Ваш E-mail',
		message: 'Отправить сообщение',
		contact_us: 'Contact Us',
		contact_info: 'Контакты',
		send_properties: 'Send saved properties',
		send_property:	'Send Property',
		alert_email: 'Please spacify the contact email!'
	},
	sk: {
		email: 'Email',
		your_email: 'Váš email',
		message: 'Poslať ďalej (priateľom, známym)',
		contact_us: 'Contact Us',
		contact_info: 'Kontakt',
		send_properties: 'Send saved properties',
		send_property:	'Send Property',
		alert_email: 'Please spacify the contact email!'
	}
};
var Search = Class.create({
    _city: null,
    _lang: null,
    _target: null,
    _page: 'search_sale',

    init: function(city, lang, target) {this._city = city; this._lang = lang; this._target = $('#'+target);},
    showSale: function(target_id) {
	var detailed = $('#'+target_id).attr('detailed')?'true':'';
	this._target = target_id;
        $.ajax({
            type: 'GET',
            url: '/'+this._city+'/old/index.php',
            data: 'l='+this._lang+'&p=search_sale&detailed='+detailed+'&'+$.address.queryString(),
            success: function(data){
    	        $('#'+target_id).html(data);
            },
            dataType: 'html'
        });
	//_gaq.push(['_trackEvent', 'property_search'+($('#'+target_id).attr('detailed')?'_detailed':''),this._lang+'_'+this._city,'buy']);
	$.address.externalChange(this.updateSalePage);
    },
    updateSalePage: function() {
	if (search._target)
	    search.showSale(search._target);
    },
    showRent: function(target_id) {
	var detailed = $('#'+target_id).attr('detailed')?'true':'';
	this._target = target_id;
        $.ajax({
            type: 'GET',
            url: '/'+this._city+'/old/index.php',
            data: 'l='+this._lang+'&p=search_rent&detailed='+detailed+'&'+$.address.queryString(),
            success: function(data){
    	        $('#'+target_id).html(data);
            },
            dataType: 'html'
        });
	//_gaq.push(['_trackEvent', 'property_search'+($('#'+target_id).attr('detailed')?'_detailed':''),this._lang+'_'+this._city,'rent']);
	$.address.externalChange(this.updateRentPage);
    },
    updateRentPage: function() {
	if (search._target)
	    search.showRent(search._target);
    },
    showGlobal: function(target_id) {
	this._target = target_id;
        $.ajax({
            type: 'GET',
            url: '/net/old/index.php',
            data: 'l='+this._lang+'&p=search_global',
            success: function(data){
    	        $('#'+target_id).html(data);
            },
            dataType: 'html'
        });
    },
    refineSearch: function() {
	location.href=this._origin+'#/?'+$('#criterias').serialize();
    },
    setCount: function(count) {
	$('#per_page').val(count);
	$('#pg').val(1);
	$.address.value(this._origin+'?'+$('#criterias').serialize());
	search.updateResultPage();
    },
    setPage: function(page_id) {
	$('#pg').val(page_id);
	$.address.value(this._origin+'?'+$('#criterias').serialize());
	search.updateResultPage();
    },
    setOrdering: function(order_col,order_dir) {
	$('#ord_col').val(order_col);
	$('#ord_dir').val(order_dir);
	$.address.value(this._origin+'?'+$('#criterias').serialize());
	search.updateResultPage();
    },
    showResultPage: function(target) {
        $.ajax({
            type: 'GET',
            url: '/'+this._city+'/old/index.php',
            data: 'l='+this._lang+'&search=Search&'+$.address.queryString(),
            success: function(data){
                $('#'+target).html(data);
            },
            dataType: 'html'
        });
    },
    updateResultPage: function() {
	search.showResultPage(search._target);
    },
    showResults: function(target_id) {
	this._target = target_id;
	this._origin = $.address.path();
	if (this._origin)
	    $.address.externalChange(this.updateResultPage);
    },
    showPrintList: function(n) {
	var params = 'l='+this._lang+'&print=true&all='+n+'&search=Search&'+$.address.queryString();
	window.open('/'+this._city+'/old/index.php?'+params);
    },
    showPrintDetailed: function(n) {
	var params = 'l='+this._lang+'&p=show_new&print=true&id='+n;
	window.open('/'+this._city+'/old/index.php?'+params);
    },
    sendMail: function(n) {
		_gaq.push(['_trackEvent', 'property_page',this._lang+'_','send_mail']);
        var cart = this;
        var dialog = $('<form id="contact">'+
                        searchTexts[search._lang].your_email+':<input type="text" name="contact_email" id="contact_email" value=""/><br/>'+
                        searchTexts[search._lang].message+':<br/><textarea name="contact_message" rows="5" cols="36" id="contact_message"/></textarea>'+
                        '</form>').dialog({
            modal: true,
            title: searchTexts[search._lang].send_properties,
            zindex: 3999,
            buttons: {
                Cancel: function() { $(this).dialog("close"); },
                Ok: function() {
                    if (!$('#contact_email').val()) {
                        alert(searchTexts[search._lang].alert_email);
                        return;
                    };
                    $.ajax({
                        type: 'POST',
                        url: '/'+search._city+'/old/index.php',
                        data: 'l='+search._lang+'&all=100&mail=true&contact_email='+$('#contact_email').val()+'&message='+$('#contact_message').val()+'&search=Search&'+$.address.queryString(),
                        success: function(data){
                            if (data!='OK') {
                                alert(data);
                            }
                        },
                        dataType: 'json'
                    });
                    $(this).dialog('close');
                }
            }
        });
    },
    sendContact: function(n) {
		_gaq.push(['_trackEvent', 'property_page',this._lang+'_','contact_us']);
        var cart = this;
		var p_id = n;
        var dialog = $('<form id="contact">'+
                        searchTexts[search._lang].your_email+':<input type="text" name="contact_email" id="contact_email" value=""/><br/>'+
                        searchTexts[search._lang].message+':<br/><textarea name="contact_message" rows="5" cols="36" id="contact_message"/></textarea>'+
                        '</form>').dialog({
            modal: true,
            title: searchTexts[search._lang].contact_us,
            zindex: 3999,
            buttons: {
                Cancel: function() { $(this).dialog("close"); },
                Ok: function() {
                    if (!$('#contact_email').val()) {
                        alert(searchTexts[search._lang].alert_email);
                        return;
                    };
                    $.ajax({
                        type: 'POST',
                        url: '/global/cart.php',
                        data: 'cmd=request&lang='+search._lang+'&contact_email='+$('#contact_email').val()+'&message='+$('#contact_message').val()+'&properties['+search._city+'][]='+p_id,
                        success: function(data){
                            if (data!='OK') {
                                alert(data);
                            }
                        },
                        dataType: 'json'
                    });
					_gaq.push(['_trackEvent', 'property_page',this._lang+'_','contact_us_sent']);
                    $(this).dialog('close');
                }
            }
        });
    },
    sendProperty: function(n) {
        var cart = this;
        var dialog = $('<form id="contact">'+
                        searchTexts[search._lang].your_email+':<input type="text" name="contact_email" id="contact_email" value=""/><br/>'+
                        searchTexts[search._lang].message+':<br/><textarea name="contact_message" rows="5" cols="36" id="contact_message"/></textarea>'+
                        '</form>').dialog({
            modal: true,
            title: searchTexts[search._lang].send_property,
            zindex: 3999,
            buttons: {
                Cancel: function() { $(this).dialog("close"); },
                Ok: function() {
                    if (!$('#contact_email').val()) {
                        alert(searchTexts[search._lang].alert_email);
                        return;
                    };
                    $.ajax({
                        type: 'POST',
                        url: '/'+search._city+'/old/index.php',
                        data: 'l='+search._lang+'&p=show_new&mail=true&contact_email='+$('#contact_email').val()+'&message='+$('#contact_message').val()+'&'+$.address.queryString(),
                        success: function(data){
                            if (data!='OK') {
                                alert(data);
                            }
                        },
                        dataType: 'json'
                    });
                    $(this).dialog('close');
                }
            }
        });
    },
    showPropertyPage: function(target) {
        $.ajax({
            type: 'GET',
            url: '/'+this._city+'/old/index.php',
            data: 'l='+this._lang+'&p=show_new&'+$.address.queryString(),
            success: function(data){
                $('#'+target).html(data);
		$("a[rel=lightbox\[juh\]]").lightBox({
		    imageLoading: 'http://www.athome-network.com/global/images/lightbox-ico-loading.gif',
		    imageBtnClose: 'http://www.athome-network.com/global/images/lightbox-btn-close.gif',
		    imageBtnPrev: 'http://www.athome-network.com/global/images/lightbox-btn-prev.gif',
		    imageBtnNext: 'http://www.athome-network.com/global/images/lightbox-btn-next.gif'
		});
		if (window.addthis)
		{
		   window.addthis.ost = 0;
		   window.addthis.ready();
		}
            },
            dataType: 'html'
        });
	
	_gaq.push(['_trackEvent', 'property_page',this._lang+'_'+this._city,'property']);
    },
    showContactUs: function(div) {
	$(div).find('a').click(function() {_gaq.push(['_trackEvent', 'property_page',this._lang+'_'+this._city,'contact_email']);});
        var dialog = $(div).dialog({
            modal: true,
            title: searchTexts[search._lang].contact_info,
            zindex: 3999,
			width: 400,
            buttons: {
                Ok: function() { $(this).dialog("close"); }
            }
        });
	_gaq.push(['_trackEvent', 'property_page',this._lang+'_'+this._city,'contact_us']);
    },
    showPrintProperty: function() {
	var params = 'l='+this._lang+'&print=true&p=show_new&'+$.address.queryString();
	window.open('/'+this._city+'/old/index.php?'+params);
    }
});

var search;

var myCart;

function athomeInit(city,lang) {
    if (jQuery.browser.version =='6.0' && jQuery.browser.msie) alert('Please Use Decent Browser to view this site.');
    //$(document).ajaxComplete(function(request, settings){ var evt=document.createEvent("HTMLEvents"); evt.initEvent ("DOMContentLoaded", false, false); document.dispatchEvent(evt)}); 
    myCart = new athomeCart('cart');
    search = new Search(city,lang,'search_div');
}
