/* vim: set expandtab tabstop=4 shiftwidth=4: */
/* $Id: main.js,v 1.2 2007/04/10 11:59:08 starlingd Exp $ */

function set_cookie (name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function get_cookie (name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    
    return unescape(dc.substring(begin + prefix.length, end));
}

function delete_cookie (name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function newImage(arg) {
	if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
    }
}

function changeImages() {
    if (document.images && (preloadFlag == true)) {
        for (var i = 0; i < changeImages.arguments.length; i += 2) {
            document[changeImages.arguments[i]].src = changeImages.arguments[i + 1];
        }
    }
}

Array.prototype.inArray = function (value) {
	var i;
	for (i = 0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
    
	return false;
};

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e" + type + fn]( window.event ); }
		obj.attachEvent( "on" + type, obj[type + fn] );
		EventCache.add(obj, type, fn);
	} else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if (item[0].removeEventListener) {
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if (item[1].substring(0, 2) != "on") {
					item[1] = "on" + item[1];
				};
				if (item[0].detachEvent) {
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

addEvent(window, 'unload', EventCache.flush);

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function page_nav (increment) {
    if (increment == "next") {
        $('page_num').value++;
    } else if (increment == "prev") {
        $('page_num').value--;
    } else {
        $('page_num').value = increment;
    }
    
    $('page_nav_form').submit();
    
    return false;
}

function change_order (order_by) {
    var current_order_by = $F('order_by');
    var current_order    = $F('order');
    
    if (current_order_by == order_by) {
       $('order').value = (current_order == 'DESC') ? 'ASC' : 'DESC';
    } else {
        $('order').value = 'ASC';
    }
    
    $('page_num').value = 1;
    $('order_by').value = order_by;
    $('page_nav_form').submit();
    
    return false;
}

function fetch_favourites (args,locale) {
	if (locale=='it')
    var url = '/italian/property/favourites/' + args;
  else
    var url = '/property/favourites/' + args;
    
    var opts = {
                    onSuccess:function (request) {
                        $('property_profile').style.opacity = 0.99999;
                        $('property_profile').style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
                        if ($('fav_button').src.indexOf('add_to_favourite') != -1) {
                            $('fav_button').src   = '/images/buttons/remove_from_favourite.gif'
                            $('fav_link').href    = '#';
                            $('fav_link').onclick = function () {
                                remove_from_favourites();
                                return false;
                            }
                        } else {
                            $('fav_button').src   = '/images/buttons/add_to_favourite.gif';
                            $('fav_link').href    = '#';
                            $('fav_link').onclick = function () {
                                add_to_favourites();
                                return false;
                            }
                        }
                        Effect.Pulsate('minfav_msg', {pulses: 1, duration: 1});
                    },
                    
                    asynchronous: true,
					method: 'get'
                };
    
    new Ajax.Updater('minfav_msg', url, opts);
}


function fetch_favourites_id (args,id,locale) {
	if (locale=='it')
    var url = '/italian/property/favourites/' + args;
  else
    var url = '/property/favourites/' + args;
    var idn=id;
    
    var opts = {
                    onSuccess:function (request) {
                        $('property_profile_'+idn).style.opacity = 0.99999;
                        $('property_profile_'+idn).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
                        if ($('fav_button_'+idn).src.indexOf('add_to_favourite') != -1) {
                            $('fav_button_'+idn).src   = '/images/buttons/remove_from_favourite.gif'
                            $('fav_link_'+idn).href    = '#';
                            $('fav_link_'+idn).onclick = function () {
                                remove_from_favourites(idn);
                                return false;
                            }
                        } else {
                            $('fav_button_'+idn).src   = '/images/buttons/add_to_favourite.gif';
                            $('fav_link_'+idn).href    = '#';
                            $('fav_link_'+idn).onclick = function () {
                                add_to_favourites(idn);
                                return false;
                            }
                        }
                        Effect.Pulsate('minfav_msg_'+idn, {pulses: 1, duration: 1});
                    },
                    
                    asynchronous: true,
					method: 'get'
                };
    
    new Ajax.Updater('minfav_msg_'+idn, url, opts);
}


var popupvt=null;


function printProperties (id,language) {
				if (language=='it')
					link="/italian/property/print/?property_id=" + id ;
				else
					link="/property/print/?property_id=" + id ;
					
					
				stile = "top=10, left=10, width=900, height=600, status=no, menubar=no,  scrollbars=1,  toolbar=no, resizable=yes";
				if (window.popupvt)
				{
					if (window.popupvt.closed==false){
						window.popupvt.focus();
						window.popupvt.location.href=link;
					}
					else popupvt=window.open(link,'popupvt',stile);
				}
				else  popupvt=window.open(link,'popupvt',stile);
				window.popupvt.focus();	
}