function RanchMap(w,h){this.cursor=document.createElement("DIV");this.cursor.style.display="none";this.cursor.style.position="absolute";}
RanchMap.prototype.init_cursor=function(id,width,height,image,name,price,description,unique){document.getElementById('b_price').innerHTML=price;document.getElementById('b_name').innerHTML=name;document.getElementById('b_image').src=asset_from_str(image)+'/roads/'+image+'.gif';document.getElementById('b_desc').innerHTML=description;this.cursor.style.width=fs_getPx(this.cell_size*width);this.cursor.style.height=fs_getPx(this.cell_size*height);this.cursor.width=width;this.cursor.height=height;this.cursor.active=true;this.cursor.innerHTML='<img src="'+asset_from_str(image)+'/roads/'+image+'.gif"/>';this.cursor.img=id;this.cursor.price=price;this.cursor.unique=unique;}
RanchMap.prototype.init=function(w,h,init_array,cell_size){this.width=w;this.height=h;this.cell_size=35;this.ids=new Array();this.d_ids=new Array();this.positions=new Array();var holder=document.getElementById('map_holder');this.map=new Array(this.width);for(i=0;i<this.width;i++)
this.map[i]=new Array(this.height);holder.style.width=fs_getPx(this.cell_size*this.width);holder.style.height=fs_getPx(this.cell_size*this.height);fs_addListener(holder,"mousemove",fs_ranch_map.onmove,true);fs_addListener(holder,"mouseover",fs_ranch_map.onover,true);fs_addListener(holder,"mouseout",fs_ranch_map.onout,true);fs_addListener(holder,"click",fs_ranch_map.onclick,true);for(i=0;i<this.width;i++)
for(j=0;j<this.height;j++){this.map[i][j]=((init_array[j*this.width+i]==null)?-1:init_array[j*this.width+i]);}
for(i=0;i<this.width;i++)
for(j=0;j<this.height;j++){var di=document.createElement('DIV');di.style.left=i*this.cell_size+'px';di.style.top=j*this.cell_size+'px';di.x=i;di.y=j;di.id='c_'+i+'_'+j;di.className='holder';if(this.map[i][j]==-2)
di.taken=true;else
if(this.map[i][j]!=-1){di.innerHTML='<img src="'+asset_from_str(buildings[this.map[i][j]].image)+'/roads/'+buildings[this.map[i][j]].image+'.gif"/>';di.taken=true;}
holder.appendChild(di);}
holder.appendChild(this.cursor);}
RanchMap.prototype.onmove=function(ev){if(!window.fs_ranch_map||!fs_ranch_map.cursor.active)
return;var obj=fs_getTargetElement(fs_getEvent(ev));if(obj.parentNode.className=='holder')
obj=obj.parentNode;if(obj.className!='holder')return;fs_ranch_map.check_position(obj);fs_ranch_map.cursor.style.left=fs_getPx(obj.offsetLeft);fs_ranch_map.cursor.style.top=fs_getPx(obj.offsetTop);}
RanchMap.prototype.onover=function(ev){if(fs_ranch_map.cursor.active)
fs_ranch_map.cursor.style.display='block';}
RanchMap.prototype.onclick=function(ev){if(!window.fs_ranch_map||!fs_ranch_map.cursor.active)
return;if(fs_ranch_map.cursor.img!=-1){if(fs_ranch_map.can_build!=true){if(fs_ranch_map.current_cell.innerHTML=='')
alert('That area has recently been demolished. You must click the Buy button, before building there!');else
alert('You cannot build on that location!');}else{fs_ranch_map.build();}}
else{if(fs_ranch_map.can_build==true){alert('There is nothing there to demolish!');}
else{fs_ranch_map.demolish();}}}
RanchMap.prototype.onout=function(ev){if(!window.fs_ranch_map||!fs_ranch_map.cursor.active)
return;fs_ranch_map.cursor.style.display='none';}
function Cell(x,y,id){this.x=x;this.y=y;this.id=id;}
Cell.prototype.toString=function(){return"b["+this.x+","+this.y+"]="+this.id;}
RanchMap.prototype.demolish=function(){var cell=this.current_cell;var im=this.map[cell.x][cell.y];if(im==-2||im>49){alert("You cannot demolish that building!");}else{var w=buildings[im].width;var h=buildings[im].height;for(var i=0;i<w;i++)
for(var j=0;j<h;j++){if(i==0&&j==0)
cell.innerHTML='';this.map[cell.x+i][cell.y+j]=-1;this.d_ids.push(new Cell(cell.x+i,cell.y+j,this.cursor.img));document.getElementById('b_cost').innerHTML=parseInt(document.getElementById('b_cost').innerHTML)+this.cursor.price;}}}
RanchMap.prototype.build=function(){var cell=this.current_cell;cell.innerHTML=this.cursor.innerHTML;document.getElementById('b_cost').innerHTML=parseInt(document.getElementById('b_cost').innerHTML)+this.cursor.price;this.ids.push(new Cell(cell.x,cell.y,this.cursor.img));for(var i=0;i<this.cursor.width;i++)
for(var j=0;j<this.cursor.height;j++){this.map[cell.x+i][cell.y+j]=fs_ranch_map.cursor.img;document.getElementById('c_'+(cell.x+i)+'_'+(cell.y+j)).taken=true;}
if(this.cursor.unique==true){document.getElementById('bb_'+this.cursor.img).style.display='none';this.cursor.active=false;}}
RanchMap.prototype.set_build=function(status,cell){this.can_build=status;this.cursor.style.border=("1px solid "+((status==true)?'white':'red'));this.current_cell=cell;}
RanchMap.prototype.check_position=function(cell){if(cell.taken==true||cell.x+this.cursor.width>this.width||cell.y+this.cursor.height>this.height)
this.set_build(false,cell);else{var ok=true;for(var i=0;i<this.cursor.width;i++)
for(var j=0;j<this.cursor.height;j++)
if(this.map[cell.x+i][cell.y+j]!=-1){this.set_build(false);return;}
this.set_build(true,cell);}}
RanchMap.prototype.can_buy=function(err_str,confirm_str){if(this.ids.length==0&&this.d_ids.length==0){alert(err_str);return false;}
return confirm(confirm_str)}
RanchMap.prototype.reset=function(){this.ids=new Array();for(var i=0;i<this.d_ids.length;i++){var cell=this.d_ids[i];$('c_'+cell.x+'_'+cell.y).taken=false;}
this.d_ids=new Array();document.getElementById('b_cost').innerHTML=0;}
RanchMap.prototype.get_ids=function(){var r=this.d_ids.join('&')+'&'+this.ids.join('&');return this.d_ids.concat(this.ids).join('&');}
var roads_len=11;var road_bs=new Array(new Building('Crossing','road_c',30,1,1,500,'A crossing'),new Building('Turn SW','road_dl',31,1,1,500,'Turning left'),new Building('Turn SE','road_dr',32,1,1,500,'Turning right'),new Building('Horizontal road','road_h',33,1,1,500,'A horizontal road'),new Building('Crossing down','road_md',34,1,1,500,'Downwards crossing'),new Building('Crossing left','road_ml',35,1,1,500,'Left crossing'),new Building('Vertical road','road_v',36,1,1,500,'Vertical road'),new Building('Crossing right','road_mr',37,1,1,500,'Right crossing'),new Building('Crossing up','road_mu',38,1,1,500,'Up crossing'),new Building('Turn NW','road_ul',39,1,1,500,'North-west'),new Building('Turn NE','road_ur',40,1,1,500,'North-east'),new Building('A pond','pond',41,2,1,1000,'A nice pond.'),new Building('Garden','garden',42,1,1,500,'A lovely garden'));var demolish_bs=new Array(new Building('Demolish','bulldozer',-1,1,1,100,'The bulldozer clears the terrain, allowing you to rebuild. Before you can rebuild on the area, you must click the Buy button.'))
var other_buildings_bs=new Array(new Building('Home','home',0,2,2,0,'This is your home.',true),new Building('Stable','stable',1,2,2,0,'This is the main stable.',true),new Building('Shop','shop',2,2,2,0,'In a shop you can sell tack or horses.',true),new Building('Workshop','workshop',3,2,2,0,'In a workshop players manufacture tack.',true),new Building('Arena','arena',4,2,2,0,'This is the training arena.',true),new Building('Barn','barn',5,2,2,0,'This is the riders barn.',true),new Building('The secondary stable','boarding',6,3,2,25000,'A second stable allows you to split your horses and keep them in 2 stables.',true),new Building('Third stable','stable3',7,2,2,0,'This is the main stable.',true),new Building('The tack room','storage',8,2,2,15000,'In the tack room you can store tack you wish to keep.',true));var mines_bs=new Array(new Building('Woodmill','woodmill',20,3,2,10000,'A woodmill will produce 10 pieces of wood every month.'),new Building('Apple tree','appletree',21,1,1,10000,'An apple tree is producing 2 apples every month.'),new Building('Iron Mine','ironmine',22,2,2,12000,'An iron mine is producing 5 pieces of iron every month.'),new Building('Grain silo','grainsilo',23,2,2,30000,'The grain silo is producing 2 sacks of grain every month.'),new Building('Carrot field','carrotsfield',24,1,1,15000,'The carrot field is producing 7 carrots every month.'));var boulders_bs=new Array(new Building('Boulder','boulder',50,1,1,0,'A boulder'),new Building('Boulders','boulder2',51,2,1,0,'Some boulders'));var buildings=new Array();for(var i=0;i<road_bs.length;i++){buildings[road_bs[i].id]=road_bs[i];}
for(var i=0;i<mines_bs.length;i++){buildings[mines_bs[i].id]=mines_bs[i];}
for(var i=0;i<other_buildings_bs.length;i++){buildings[other_buildings_bs[i].id]=other_buildings_bs[i];}
for(var i=0;i<boulders_bs.length;i++){buildings[boulders_bs[i].id]=boulders_bs[i];}
var fs_ranch_map=null;function show_image(building){var on_cl="fs_ranch_map.init_cursor("+building.pass()+")";r="<DIV id='bb_"+building.id+"' class='"+('x'+building.width)+building.height+"' onclick='"+on_cl+"'>";r+='<img src="'+asset_from_str(building.image)+'/roads/'+building.image+'.gif"/>';r+="</div>";return r;}
function show_images(tag,buildings){var len=buildings.length;r="<table class='horizontal_table' ><tr class='horizontal_table_categories'><td width='80px'><b>"+tag+"</b></td>";for(var i=0;i<len;i++){r+=("<td>"+show_image(buildings[i])+"</td>");}
r+="</tr></table>";return r;}
function init_drag(){fs_ranch_map=new RanchMap();}
function Building(name,image,id,width,height,price,description,uniq){this.name=name;this.id=id;this.width=width;this.height=height;this.price=price;this.description=description;this.image=image;this.unique=uniq;if(this.unique==null)
this.unique=false;}
Building.prototype.pass=function(){return new Array(this.id,this.width,this.height,('"'+this.image+'"'),('"'+this.name+'"'),this.price,('"'+this.description+'"'),this.unique).join(',');}