/********COOKIE START**************/
var _name="MyShortlistCookie";
var _pref="a";
var _separator="_";

var propertyUnit=new Object();

propertyUnit.getMethodSeparator=function(){	
		return "|";
	};
	
propertyUnit.stringToProperty = function(str){
		var list=str.split(propertyUnit.getMethodSeparator());
		var newProp=new propertyClass(
			{id:list[0],
				type:list[1]
			}
		);
		return newProp;
	};
	
propertyUnit.isListContains = function(list,prop){
		for(var i=0;i<list.length;i++){
			if(list[i].id==prop.id &&list[i].type==prop.type)
			return list[i];
		}
		return null;
	};

var propertyClass = new Class({
	initialize: function(options) {
		this.id=options.id;
		this.type=options.type;
		this.methodSeparator=propertyUnit.getMethodSeparator();
	},
	toString: function(){
		return this.id+this.methodSeparator+this.type;
	}
});

function addToMyShortlist(addLabel,removeLabel,owner){
	var id=owner.id.split(_separator)[1];
	var type=owner.id.split(_separator)[2];
	var pr=new propertyClass({id:id,type:type});
	var list=getCookieArray();
	var tmpPr=propertyUnit.isListContains(list,pr);
	if(tmpPr!=null){
		list.remove(tmpPr);
		owner.innerHTML=addLabel;
	}else{
		list.push(pr);
		owner.innerHTML=removeLabel;
	}
	if(list.length>0){
		Cookie.set(_name,getListToString(list),{duration: 30});
	}	
	else{
		Cookie.remove(_name);
	}
	
	
}

function getListToString(list){
	var str="";
	for(var i=0;i<list.length;i++){
		if(str!="")
			str+=_separator
		str+=list[i].toString();
	}	
	return str;
}

function getCookieArray(){
	var list=Cookie.get(_name);
	if(list===false||list==""){
		list = new Array();
	}else{
		list=list.split(_separator);
	}
	var prList=new Array();
	for(var i=0;i<list.length;i++){
		prList.push(propertyUnit.stringToProperty(list[i]));
	}
	return prList;
}

function setMyShortlistLabel(addLabel,removeLabel){
	var list=getCookieArray()
	var AList=$$(".MyShortlist");
	if(AList === false)
	 return;
	if(!$defined(AList))
		AList=new Array();
	if($type(AList) != 'array') 
		AList=new Array(AList);
	for(var i=0;i<AList.length;i++){
		var id=AList[i].id.split(_separator)[1];
		var type=AList[i].id.split(_separator)[2];
		var newProp=new propertyClass({id:id,type:type});
		if(propertyUnit.isListContains(list,newProp)){
			AList[i].innerHTML=removeLabel;
		}else{
			AList[i].innerHTML=addLabel;
		}
	}
}
/********COOKIE END**************/




function showListMap(event,latitude,longitude,map) {
	if (latitude == 0) return;
	var event = new Event(event);
	var target = $(event.target);
	var left=$(target).getCoordinates().left-250;
	showListMapDiv(latitude,longitude,$(target).getCoordinates().bottom-220,left,map);
}

function showListMapDiv(latitude,longitude,top,left,map) {
	var div = $('mapsearchdyn');
    var cont = $('mapcont');
    if($defined(top))
    	div.style.top=top+"px";
    if($defined(left))
    	div.style.left=left+"px";
    var gc = div.getCoordinates();
    if(gc.left+gc.width>window.getWidth()){
	 left=left-(gc.left+gc.width-window.getWidth());
	 div.style.left=left+"px";
	 }
    if(gc.left<0){
	 left=0;
	 div.style.left=left+"px";
	 }
    
	map.placeProperty(new Property(latitude,longitude));
}
function hideListMap(map){
	$('mapsearchdyn').setStyle('left',"-1000px");
	map.deleteProperties();
}

function showBranchMap(event,address,map){
	var event = new Event(event);
	searchForCoordinates(address,showBranchMapCallBack.pass([event,map]));
}
function showBranchMapCallBack(event,map){
	if (getGoogleLatitude()!=0 && getGoogleLongitude()!=0)
		showListMap(event,getGoogleLatitude(),getGoogleLongitude(),map);
}

