var Clipboard = Class.create();

Clipboard.prototype = {
	initialize : function(options)
	{
		this.rent = [];
		this.buy = [];
		this.recent = [];
		this.bldg = [];
		this.new_item_added = false;
		this.sch_type = 2; 
		
		this.options = null;
		this.container = null;
		if(options != null)
		{
			this.options = options;
		}
	},
	setHTML : function()
	{
		this.htm = '';
this.htm += '<table width="213" border="0" cellspacing="0" cellpadding="0">';
this.htm += '  <tr>';
this.htm += '    <td height="58" colspan="3" background="'+sRelativePath+'images/clipboard/clipboard_01.gif">&nbsp;</td>';
this.htm += '  </tr>';
this.htm += '  <tr>';
this.htm += '    <td width="28" height="15" background="'+sRelativePath+'images/clipboard/clipboard_02.gif"></td>';
this.htm += '    <td width="155" bgcolor="#BDD748" height="15"><div style="height:15px;overflow:hidden;" id="clipboard_tag_container">';
this.htm += '<img src="'+sRelativePath+'images/clipboard/btn_buy_off.gif" width="29" height="14" align="absmiddle" border="0" onClick="javascript:clipboard.changeTypeClick(2);" style="cursor:pointer;">';
this.htm += '<img src="'+sRelativePath+'images/clipboard/btn_rent_off.gif" width="30" height="14" align="absmiddle" border="0" onClick="javascript:clipboard.changeTypeClick(1);" style="cursor:pointer;">';
this.htm += '<img src="'+sRelativePath+'images/clipboard/btn_recent_off.gif" width="87" height="14" align="absmiddle" border="0" onClick="javascript:clipboard.changeTypeClick(3);" style="cursor:pointer;">';
this.htm += '	</div></td>';
this.htm += '    <td width="30" background="'+sRelativePath+'images/clipboard/clipboard_03.gif"></td>';
this.htm += '  </tr>';
this.htm += '  <tr>';
this.htm += '    <td height="28" colspan="3" background="'+sRelativePath+'images/clipboard/clipboard_04.gif">&nbsp;</td>';
this.htm += '  </tr>';
this.htm += '  <tr>';
this.htm += '    <td colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
this.htm += '      <tr>';
this.htm += '        <td width="72" height="48" background="'+sRelativePath+'images/clipboard/clipboard_05.gif">&nbsp;</td>';
this.htm += '        <td bgColor="#FFFFFF" vAlign="middle" align="center"><div style="height:48px; width:64px; overflow:hidden;"><img id="clipboard_img_container" src="'+sRelativePath+'images/nbsp.gif" height="48"></div></td>';
this.htm += '        <td width="77" background="'+sRelativePath+'images/clipboard/clipboard_06.gif">&nbsp;</td>';
this.htm += '      </tr>';
this.htm += '    </table></td>';
this.htm += '  </tr>';
this.htm += '  <tr>';
this.htm += '    <td colspan="3" height="15" background="'+sRelativePath+'images/clipboard/clipboard_07.gif"></td>';
this.htm += '  </tr>';
this.htm += '  <tr>';
this.htm += '    <td colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
this.htm += '      <tr>';
this.htm += '        <td width="27" height="157" background="'+sRelativePath+'images/clipboard/clipboard_08.gif">&nbsp;</td>';
this.htm += '        <td valign="top" width="158" height="157" bgcolor="#EDEDE3" id="clipboard_property_container" class="clipboard"></td>';
this.htm += '        <td width="28" height="157" background="'+sRelativePath+'images/clipboard/clipboard_09.gif">&nbsp;</td>';
this.htm += '      </tr>';
this.htm += '    </table></td>';
this.htm += '  </tr>';
this.htm += '  <tr>';
this.htm += '    <td colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
this.htm += '      <tr>';
this.htm += '        <td width="50" height="17" background="'+sRelativePath+'images/clipboard/clipboard_10.gif"></td>';
this.htm += '        <td align="center" valign="middle"><img id="clipboard_property_compare" src="'+sRelativePath+'images/clipboard/btn_compare_off.gif" width="112" height="17" style=""></td>';
this.htm += '        <td width="51" background="'+sRelativePath+'images/clipboard/clipboard_11.gif"></td>';
this.htm += '      </tr>';
this.htm += '    </table></td>';
this.htm += '  </tr>';
this.htm += '  <tr>';
this.htm += '    <td height="20" colspan="3" background="'+sRelativePath+'images/clipboard/clipboard_12.gif"></td>';
this.htm += '  </tr>';
this.htm += '</table>';			
	},
	initializeClipboardComponent : function(container)
	{
		if(container == null)	
			return;
		this.container = container;

		var mode = 2;
		if($("clipboard_widget_layout") != null)
		{
			this.container.innerHTML = $("clipboard_widget_layout").innerHTML;
			$("clipboard_widget_layout").innerHTML = "";
			mode = 1;
		}
		else
		{
			this.container.innerHTML = this.setHTML();
		}

		this.property_container = document.createElement("DIV");
		$("clipboard_property_container").appendChild(this.property_container);
		this.property_container.style.overflow = "auto";
		this.property_container.style.width = this.property_container.parentNode.offsetWidth+"px";
		this.property_container.style.height = (this.property_container.parentNode.offsetHeight-4)+"px";

		$("clipboard_property_compare").style.cursor = "pointer";
		$("clipboard_property_compare").onclick = this.propertyCompare.bindAsEventListener(this);
		$("clipboard_property_enquiry").style.cursor = "pointer";
		$("clipboard_property_enquiry").onclick = this.propertyEnquiry.bindAsEventListener(this);
		$("clipboard_property_email").style.cursor = "pointer";
		$("clipboard_property_email").onclick = this.propertyEmail.bindAsEventListener(this);
		this.loadCookie();
		htm = null;
	},
	propertyCompare : function()
	{
		var inputs = $("clipboard_property_container").getElementsByTagName("input");	
		var selected_val = [];
		for(var i=0; i < inputs.length; i++)
		{
			if(inputs[i].name == "property_checkbox" && inputs[i].type == "checkbox" && inputs[i].checked)
			{
				selected_val.push(inputs[i].value);	
			}
		}

		if(property_info_viewer != null && selected_val.length > 0)
		{
			property_info_viewer.showPropertyComparison(selected_val);		
		}			
	},
	propertyEnquiry : function()
	{
		var pid = '';
		var inputs = $("clipboard_property_container").getElementsByTagName("input");	
		var selected_val = [];
		for(var i=0; i < inputs.length; i++)
		{
			if(inputs[i].name == "property_checkbox" && inputs[i].type == "checkbox" && inputs[i].checked)
			{
				if (pid!='')
					pid += ','
				pid += inputs[i].value;
				//selected_val.push(inputs[i].value);	
			}
		}
		if (pid!=''){
			var type = this.sch_type;
			//var url = "http://www.hongkonghomes.com/hong_kong_property/eng/email/email.php?pro_id_set="+id+",&action=tellMore&type="+type;
			//var url = sRelativePath+"email/email.php?pro_id_set="+id+",&action=tellMore&type="+type;
			var url = sRelativePath+"email/tell_me_more.php?pid="+pid+"&type="+type;
			var win = newWindowFixed(url,'',580,500)
			try{
				win.focus();
			}catch(e){}		
		}
		/*if(property_info_viewer != null && selected_val.length > 0)
		{
			property_info_viewer.showPropertyComparison(selected_val);		
		}*/
	},
	propertyEmail : function()
	{
		var pid = '';
		var inputs = $("clipboard_property_container").getElementsByTagName("input");	
		var selected_val = [];
		for(var i=0; i < inputs.length; i++)
		{
			if(inputs[i].name == "property_checkbox" && inputs[i].type == "checkbox" && inputs[i].checked)
			{
				if (pid!='')
					pid += ','
				pid += inputs[i].value;
				//selected_val.push(inputs[i].value);	
			}
		}
		if (pid!=''){
			var type = this.sch_type;
			//var url = "http://www.hongkonghomes.com/hong_kong_property/eng/email/email.php?pro_id_set="+id+",&action=tellMore&type="+type;
			//var url = sRelativePath+"email/email.php?pro_id_set="+id+",&action=tellMore&type="+type;
			var url = sRelativePath+"email/tell_a_friend.php?pid="+pid+"&type="+type;
			var win = newWindowFixed(url,'',580,500)
			try{
				win.focus();
			}catch(e){}		
		}
		/*if(property_info_viewer != null && selected_val.length > 0)
		{
			property_info_viewer.showPropertyComparison(selected_val);		
		}*/
	},
	addCommas : function(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	},		
	loadProperty : function(type)
	{
		
	},
	checkProperty : function(obj, type)
	{
		if(obj != null && type != null)
		{
			if(obj.id == null || obj.id == "" || obj.bname == null || obj.bname == "")
			{
				return false;	
			}
			return true;
		}
		else
			return false;
	},
	addProperty : function(obj, type)
	{
		if(obj != null && type != null && this.checkProperty(obj, type))
		{
			switch(type)
			{
				case "1":
				case 1:
					for(var i=0; i<this.rent.length; i++)
					{
						if(this.rent[i].id == obj.id)
							return false;	
					}
					this.rent.push(obj);
					if(this.rent.length > 50)
					{
						this.rent.reverse();	
						var tmp = this.rent.splice(0,50);
						tmp.reverse();
						this.rent = tmp;
					}
					break;
				case "2":
				case 2:
					for(var i=0; i<this.buy.length; i++)
					{
						if(this.buy[i].id == obj.id)
							return false;	
					}
					
					this.buy.push(obj);
					if(this.buy.length > 50)
					{
						this.buy.reverse();	
						var tmp = this.buy.splice(0,50);
						tmp.reverse();
						this.buy = tmp;
					}					
					break;
				case "3":
				case 3:
					var tmp = [];
					var cnt = 0;

					for(var i=0; i<this.recent.length; i++)
					{
						if(this.recent[i].id != obj.id)
						{
							tmp.push(this.recent[i])
							cnt++;		
						}
						if(cnt == 9)
							break;
					}
					this.recent = [];
					this.recent.push(obj);
					for(var i=0; i<tmp.length; i++)
					{
						this.recent.push(tmp[i]);	
					}
					break;	
			}
		}
		var has_match = false;
		for(var i=0; i < this.bldg.length; i++)
		{
			if(this.bldg[i].bid == obj.bid)
			{
				has_match = true;
				break;	
			}
		}
		if(!has_match)
		{
			var b = {"bid":obj.bid,"bname":obj.bname,"ln":obj.bln,"la":obj.bla};
			this.bldg.push(b);
		}
		this.new_item_added = true;
		this.changeType(type);
		this.saveCookie(type);
		return obj;
	},
	changeTypeClick : function(type)
	{
		this.changeType(type);
	},
	changeType : function (type)
	{
		if(type != null)
		{
			this.sch_type = parseInt(type);
			var tags = $("clipboard_tag_container").getElementsByTagName("IMG");
			switch(type)
			{
				case "1":
				case 1:
					tags[0].src = sRelativePath+"images/clipboard/btn_buy_off.gif";
					tags[1].src = sRelativePath+"images/clipboard/btn_rent_on.gif";
					tags[2].src = sRelativePath+"images/clipboard/btn_recent_off.gif";
					break;
				case "2":
				case 2:
					tags[0].src = sRelativePath+"images/clipboard/btn_buy_on.gif";
					tags[1].src = sRelativePath+"images/clipboard/btn_rent_off.gif";
					tags[2].src = sRelativePath+"images/clipboard/btn_recent_off.gif";
					break;
				case "3":
				case 3:
					tags[0].src = sRelativePath+"images/clipboard/btn_buy_off.gif";
					tags[1].src = sRelativePath+"images/clipboard/btn_rent_off.gif";
					tags[2].src = sRelativePath+"images/clipboard/btn_recent_on.gif";
					break;	
			}	
			this.showProperty();
		}	
	},
	showProperty : function ()
	{
		switch(this.sch_type)
		{
			case 1:
				this.showPropertyAction(this.rent);
				break;
			case 2:
				this.showPropertyAction(this.buy);
				break;
			case 3:
				this.recent.reverse();
				this.showPropertyAction(this.recent);
				this.recent.reverse();
				break;	
		}
	},
	getBufferPropertyList : function()
	{
		var tbls = this.property_container.getElementsByTagName("TABLE");
		var buffer = [];
		for(var i=0; i<tbls.length; i++)
		{
			var obj = tbls[i].obj;	
			if(obj != null)
				buffer.push(obj);
		}
		return buffer;
	},
	propertyInfoClickListener : function(evt)
	{
		var ele = Event.element(evt);
		if(ele.tagName.toLowerCase() == "input")
			return;
		if(ele.id.indexOf("checkbox_area_") != -1)
			return;
				
		var obj = ele;
		while(obj!=null)
		{
			if(obj.tagName.toLowerCase() == "table")
				break;
			obj=obj.offsetParent;
		}
		
		if(property_info_viewer != null && obj != null && obj.tagName.toLowerCase() == "table")
		{
			property_info_viewer.cleanBuffer();
			
			property_info_viewer.show(obj.obj);		
			var from_str = " listing(s) from Clipboard ";
			switch(this.sch_type)
			{
				case 1:
					from_str += "- Rent Section "
					break;
				case 2:
					from_str += "- Buy Section "
					break;
				case 3:
					from_str += "- Recent Viewed Section "
					break;	
			}
			property_info_viewer.setBuffer(this.getBufferPropertyList(), {from:from_str});			
		}		
	},
	propertyInfoMouseoverListener : function(id, photo, div)
	{
		$("clipboard_img_container").style.visibility = "visible";
		$("clipboard_img_container").style.height = "48px";
		if(photo != null && photo != "")
			$("clipboard_img_container").src = "http://www.hongkonghomes.com/tmp/pro_info/"+id+"/"+photo;
		else
			$("clipboard_img_container").src = sRelativePath+"images/64x48.gif";
	},
	propertyInfoMouseoutListener : function()
	{
		//$("clipboard_img_container").src = sRelativePath+"images/64x48.gif";
	},	
	createPropertyDivObject : function(obj)
	{
		if(typeof(obj.bln) == "undefined" || typeof(obj.bla) == "undefined")
		{
			if(typeof(obj.bid) != "undefined")
			{
				var matched = null;
				for(var i=0; i<this.bldg.length; i++)
				{
					if(this.bldg[i].bid == obj.bid)
					{
						obj.bname = this.bldg[i].bname;
						obj.bln = this.bldg[i].bln;
						obj.bla = this.bldg[i].bla;
					}
				}
			}
		}		
		var htm = '<table border="0" cellspacing="0" cellpadding="2" class="general_tbl" width="100%">';
		htm += '  <tr>';
		htm += '    <td width="18" align="center" valign="top" nowrap id="checkbox_area_'+obj.id+'" style="cursor:default;"><input type="checkbox" name="property_checkbox" value="'+obj.id+'"></td>';
		htm += '    <td><b>'+obj.bname+'</b><br>'+this.addCommas(obj.size)+'s.f./'+obj.br+'br<br>HK$'+this.addCommas(obj.price)+'</td>';
		htm += '  </tr>';
		htm += '</table>';
		obj.div = document.createElement("DIV");
		obj.div.style.width = "100%";
		obj.div.style.cursor = "pointer";
		obj.div.innerHTML = htm;
		obj.div.photo = obj.photo;
		obj.div.pro_id = obj.id;
		obj.div.b_id = obj.bid;
		obj.div.childNodes[0].obj = obj;

		Event.observe(obj.div, "mouseover", this.propertyInfoMouseoverListener.bind(this, obj.id, obj.photo, obj.div));
		Event.observe(obj.div, "click", this.propertyInfoClickListener.bindAsEventListener(this));
		Event.observe(obj.div, "mouseout", this.propertyInfoMouseoutListener.bind(this));
	},
	showPropertyAction : function(obj_arr)
	{
		while(this.property_container.childNodes.length > 0)
			this.property_container.removeChild(this.property_container.childNodes[0]);
		var target_child = null;
		
		for(var i=(obj_arr.length-1); i >= 0; i--)
		{
		
			if(target_child == null)
			{
				target_child = 	obj_arr[i];
			}
			if(obj_arr[i].div == null)
			{
				this.createPropertyDivObject(obj_arr[i]);	
			}	

			this.property_container.appendChild(obj_arr[i].div);
		}
		if(target_child == null || target_child.id == null || target_child.photo == null)
		{
			$("clipboard_img_container").src = sRelativePath+"images/64x48.gif";
		}
		else
		{
			this.propertyInfoMouseoverListener(target_child.id, target_child.photo, target_child.div);
		}
		if(obj_arr.length == 0)
		{
			this.property_container.innerHTML = "<span class='general_text' style='font-size:10px;'><b>Save</b> the properties in which you are interested for further enquiry and share them with your friends!</span>";	
		}
		if(this.new_item_added)
		{
			new Effect.Highlight(this.property_container.childNodes[0], {endcolor:"#EDEDE3"});
			this.new_item_added = false;	
		}
	},
	removeProperty : function(obj, type)
	{
		if(obj != null && type != null && this.checkProperty(obj, type))
		{
			
		}		
	},
	removePropertyById : function (id, type)
	{
		if(parseInt(type) != 1 && parseInt(type) != 2)
			return false;

		var new_arr = [];
		var default_arr = [];
		switch(type)
		{
			case "1":
			case 1:
				default_arr = this.rent;
				break;
			case "2":
			case 2:
				default_arr = this.buy;
				break;	
		}
				
		var saved = getCookie(type).split(",+");
		
		var got_remove = false;
		
		for(var i=0; i < default_arr.length; i++)
		{
			if(parseInt(id) != default_arr[i].id)
			{
				new_arr.push(default_arr[i]);
			}
			else
			{
				got_remove = true;
			}
		}

		if(!got_remove)
			return false;
		
		switch(type)
		{
			case "1":
			case 1:
				this.rent = new_arr;
				break;
			case "2":
			case 2:
				this.buy = new_arr;
				break;	
		}
		
		this.changeType(type);
		this.saveCookie(type);	
	},
	getPropertyList : function(type)
	{
		
	},
	saveCookie : function(type)
	{
		try{
		if(type == null)
		{
			return false;
		}
			
		var arr = null;
		var cookie_val = [];
		switch(parseInt(type))
		{
			case 1:
				arr = this.rent;
				break;
			case 2:
				arr = this.buy;
				break;
			case 3:
				arr = this.recent;
				break;	
		}
		
		if(arr == null)
		{
			return false;
		}
			
		cookie_val.push(0);
		for(var i=0; i<arr.length; i++)
		{
			if(arr[i].id != null)
			{
				if(parseInt(type) == 3)
				{
					var xtype = arr[i].type;
					if(xtype == 3)
						xtype = 1;
					cookie_val.push(arr[i].id+"_"+xtype);
				}
				else
					cookie_val.push(arr[i].id);
			}
		}
		var cookie_val2 = cookie_val.join(",+");
        var today = new Date();
        var expire_date = new Date(today.getTime()+3600000*24*100);

        setCookie(type, cookie_val2, expire_date, "/");
    	}
    	catch(e)
    	{
    	}
	},
	loadCookie : function ()
	{
		var url = sRelativePath+"xml/getClipboardItem.php";	
		//var url = sRelativePath+"xml/demodata.php";	
		this.ajax_request = new Ajax.Request(
			url,
			{
				method: 'get', 
				onComplete: this.loadCookieResponse.bind(this)
			}
		)		
	},
	loadCookieResponse : function(req)
	{
		var xml = req.responseXML;
		var t1 = xml.getElementsByTagName("t1");
		var t2 = xml.getElementsByTagName("t2");
		var t3 = xml.getElementsByTagName("t3");
		var b = xml.getElementsByTagName("b");
		
		this.bldg = [];
		for(var i=0; i<b.length; i++)
		{
			var barr = b[i].firstChild.nodeValue.split("##");	
			var bobj = {"bid":barr[2],"bname":barr[3],"bln":barr[0],"bla":barr[1]};
			this.bldg.push(bobj);
		}
		
		this.rent = [];
		for(var i=0; i<t1.length; i++)
		{
			var tarr = t1[i].firstChild.nodeValue.split("**");	
			var tobj = {"id":tarr[0],"bid":tarr[1],"price":tarr[4],"type":1,"bname":tarr[11],"br":tarr[7],"en":tarr[9],"size":tarr[6],"dist":tarr[10],"photo":tarr[8]};
			this.rent.push(tobj);
		}
		
		this.buy = [];
		for(var i=0; i<t2.length; i++)
		{
			var tarr = t2[i].firstChild.nodeValue.split("**");	
			var tobj = {"id":tarr[0],"bid":tarr[1],"price":tarr[5],"type":2,"bname":tarr[11],"br":tarr[7],"en":tarr[9],"size":tarr[6],"dist":tarr[10],"photo":tarr[8]};
			this.buy.push(tobj);
		}		
		this.recent = [];
		for(var i=0; i<t3.length; i++)
		{
			var tarr = t3[i].firstChild.nodeValue.split("**");	
			var price = 0;
			if(tarr[12] == "1")
				price = tarr[4];
			else
				price = tarr[5];
				
			var tobj = {"id":tarr[0],"bid":tarr[1],"price":price,"sch_type":tarr[12],"type":3,"bname":tarr[11],"br":tarr[7],"en":tarr[9],"size":tarr[6],"dist":tarr[10],"photo":tarr[8]};
			this.recent.push(tobj);
		}	
		this.changeType(this.sch_type);
	},
	syncClipboard : function()
	{
		
	},
	syncClipboardResponse : function(req)
	{
		
	},
	checkPropertyIsSaved : function(pid, type)
	{
		var arr = null;
		switch(parseInt(type))
		{
			case 1:
				arr = this.rent;
				break;
			case 2:
				arr = this.buy;
				break;
			case 3:
				arr = this.recent;
				break;	
		}
		
		for(var i=0; i < arr.length; i++)
		{
			if(	arr[i].id == pid )
				return true;
		}
		return false;
	}
}

