<div id="game">

  <div class="upbar-container">
    <div class="upbar" id=levelbar></div>
  </div>
  <br>
  <div id="sun"></div>
  <center>
    <div id="season"></div>
    <div class="upbar-container" style="width: 250px; float: none; margin: 0 auto;">
      <div class="upbar" id=seasonbar></div>
    </div>
  </center>
  <button id=openMenuBtn>Menu</button>
  <div id="navbar">
    <ul>
      <li id=raceTimeLeft style="text-align: center;"></li>
      <li><a href="#unlockTab" id=menuUnlock><i class="fa fa-unlock-alt"></i> Unlockables</a></li>
      <li><a href="#upgradesTab" id=menuUpgrades><i class="fa fa-arrow-circle-up"></i> Upgrades</a></li>
      <li><a href="#skillTab" id=menuSkill>&#9861; Skills</a></li>
      <li><a href="#achievementsTab" id=menuAchievements>	&#127942; Achievements</a></li>
      <li><a href="#debtTab" id=menuDebt style="display: none;">Debt challenge</a></li>
      <li><a href="#statsTab" id=menuStats>&#128202; Statistics</a></li>
      <li><a href="#settingsTab"><i class="fa fa-cog fa-spin"></i> Settings</a></li>
      <li><a href="#creditsTab">&copy; Credits</a></li>
    </ul>
  </div>

  <div id="contents">
    <menu id=unlockTab></menu>
    <menu id="upgradesTab" style="overflow: hidden;">
      <code>To quick buy click the button and hold enter.</code><br>

      <button id=moreSpaceBtn onclick="buyMoreSpace();" class="btn-style"></button>
      <button id=growthSpeedBtn onclick="buyMoreGrowthSpeed();" class="btn-style info"></button>
      <button id=replantUpgradeBtn onclick="buyReplantChance();" class="btn-style info"></button>
      
      <button onclick="buyMaxEff();">Buy 10x efficiency</button>

      <button onclick="buyFill();" id=buyFillBtn class="btn-style info" title="£1 Million.">Unlock "fill all" button</button>
    </menu>
    <menu id="settingsTab">
      <label for="numberPopUps">Number pop ups</label>
      <div id=numberPopUps class=checkbox></div><br>
      <label for="simplifyNumbers">Simplify numbers</label>
      <div id=simplifyNumbers class="checkbox active"></div><br>
      <label for="useSelectedPlant">Use selected plant in "Fill all".</label>
      <div id="useSelectedPlant" class="checkbox active"></div><br>
      <label for="sunEnabled">Show sun</label>
      <div id="sunEnabled" class="checkbox active"></div><br>
      <label for="plantTime">Toggle % / time</label>
      <div id="plantTime" class="checkbox"></div><br>
      <label for="replantEnable">Enable replant</label>
      <div id="replantEnable" class="checkbox active"></div>
      <br><br>
      <label for="fps">FPS</label>
      <select id=fps>
      <optgroup label="Choose FPS:" id=fpsOption></optgroup>
    </select><br>
      <label for="tileSize">Tile size</label>
      <select id=tileSize>
      <optgroup label="Tile size:" id=tileSizeOption></optgroup>
    </select>
      <br>
      <p onclick="saveTest();"><i class="fa fa-floppy-o" aria-hidden="true"></i> Save</p>
      <p onclick="history.go(0);"><i class="fa fa-spinner fa-spin" aria-hidden="true"></i> Load</p>
      <p style="background-color: red;" onclick="reset();">Reset game</p>
    </menu>
    <menu id="creditsTab">
      <ul>
        <u>Creator:</u>
        <li>killereks (game design, scripts)</li>
      </ul>
      <ul>
        <u>Helpers:</u>
        <li>Richfest (glitches & ideas)</li>
        <li>LeEclipse (glitches & ideas)</li>
      </ul>
    </menu>
    <menu id=skillTab>
      <div id=skilltree>
        <div id="skillpoints"></div>
        <span class="title">Skill tree</span>
      </div>
    </menu>
    <menu id=achievementsTab>
      <div id="achievements">
        <div id=achievementPercentage>Here</div>
      </div>

    </menu>
    <menu id=statsTab>
      <div id="stats"></div>
    </menu>
    <menu id=debtTab>
      <div id="timeLeft"></div>
      <div id=debtLeft></div>
      <button id=debtBtn>Pay 100% of money</button>
    </menu>
  </div>
  
  <div id="menuQuest">
    <div id="questTimeLeft"></div>
    <div id=quest></div>
  </div>

  <button onclick="fillAll();" disabled id=fillAllBtn><img src="http://downloadicons.net/sites/default/files/paint-bucket-icon-61620.png" style="width: 25px; height: 25px;"> Fill all</button><br>
  <div id="money"></div>
  <div id="mps"></div>
  <select id="plant" style="width: 350px;"></select>
  <div id="info"></div>
  <div id=log></div>
  <div id=map></div>


  <div class="ach">
    <div class="icon"><i class="fa fa-bolt" aria-hidden="true"></i></div>
    <div class="text_wrap">
      <div class="title"></div>
      <span class="detail"></span>
    </div>
  </div>
  
  <img src="http://vignette1.wikia.nocookie.net/community-sitcom/images/4/46/Arrow_up.png/revision/latest?cb=20131015142243" id=arrow style="width: 50px; height: 90px;">
  <div id=tutorialMsg></div>

</div>

<div id=mainMenu>
  <center><img src="https://1001freedownloads.s3.amazonaws.com/vector/thumb/136232/nicubunu_RPG_map_symbols_Farm.png" alt="" class="logo"></center>
  <br><br><br><br>
  <center id=gameTitle>Welcome to farming idle v2</center>
  <br><br>
  <button class=mainMenuBtn onclick="startGame();">Start</button>
  <br>
  <div id="currentChallenge"></div>
  <br>
  <label style="float: none;">
    Tutorial?
    <div class=checkbox id=tutorial></div>
  </label>
  <br><br><br>
  <select style="width: 200px;" id=challengeOption>
    <option value="No challenge">No challenge</option>
    <optgroup label="Easy challenges">
      <option value="wheat only">Wheat only</option>
      <option value="winter only">Only winter</option>
    </optgroup>
    <optgroup label="Medium challenges">
      <option value="hideout">Hideout</option>
      <option value="race">Race</option>
      <option value="slot loss">Slot loss</option>
    </optgroup>
    <optgroup label="Hard challenges">
      <option value="five slots">5 Slots max</option>
      <option value="debt">Debt</option>
      <option value="quest chaos">Quest chaos</option>
      <option value="missing plants">Missing plants</option>
      <option value="no upgrades">No upgrades</option>
    </optgroup>
  </select>
  <br>
  <div id="challengeDesc" style="width: 200px; text-align: center; margin: 0 auto; border: 2px double black; padding: 10px;"></div>
  <br>
  <br>
  <select id=difficultySelect>
    <option>Easy</option>
    <option disabled>Medium</option>
    <option disabled>Hard</option>
  </select>
  <button onclick="reset();" style="position:absolute;left:10px; bottom: 10px; background-color: red;">Reset game</button>
</div>
@import url('https://fonts.googleapis.com/css?family=Caveat+Brush');
@keyframes wiggle {
  0% {
    transform: none;
  }
  10% {
    transform: rotate(10deg) scale(1.2);
  }
  25% {
    transform: rotate(0deg);
  }
  50% {
    transform: rotate(-10deg) scale(0.8);
  }
  100% {
    transform: rotate(0deg) scale(1);
  }
}
#menuQuest {
  width: 200px;
  border: 2px double black;
  text-align: center;
  padding: 5px;
  cursor: move;
  background: linear-gradient(#EFBC9B,#EDB29E);
}
.logo {
  position: absolute;
  left: calc(50% - 50px);
  width: 100px;
  height: 100px;
}
.golden {
  border: 2px solid goldenrod !important;
}
#mainMenu {
  text-align: center;
}
#gameTitle {
  animation-name: wiggle;
  animation-duration: 4s;
  animation-timing-function: cubic-bezier(0,0,0,1);
  animation-iteration-count: infinite;
  font-size: 48px;
  text-shadow: 0 1px 0 #999999, 0 2px 0 #888888, 0 3px 0 #777777, 0 4px 0 #666666, 0 5px 0 #555555, 0 6px 0 #444444, 0 7px 0 #333333, 0 8px 7px rgba(0, 0, 0, 0.4), 0 9px 10px rgba(0, 0, 0, 0.2);
  font-family: 'Caveat Brush', cursive;
}
.mainMenuBtn {
  width: 25%;
  height: 4em;
}
#tutorialMsg {
  width: 250px;
  border: 3px double #848484; 
  background-color: white;
  outline:0;
  padding: 20px 5px 5px 5px;
  z-index: 99;
  position: absolute;
  display: block;
  border-radius: 10px;
}
#tutorialMsg > button {
  padding: 16px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  -webkit-transition-duration: 0.2s; /* Safari */
  transition-duration: 0.2s;
  cursor: pointer;
  background-color: white; 
  color: black; 
  border: 2px solid #008CBA;
}
#tutorialMsg > button:hover {
  background-color: #008CBA;
  color: white;
}
#tutorialMsg > label {
  position: absolute;
  top: 2px;
  left: 2px;
  color: grey;
  font-size: 14px;
  text-align: center;
  font-weight: 700;
  letter-spacing: 2px;
}
#arrow {
  position: absolute;
  display: none;
}

body {
  background: #6190E8;
  background: -webkit-linear-gradient(to left, #6190E8 , #A7BFE8);
  background: linear-gradient(to left, #6190E8 , #A7BFE8);
        
  overflow-x: hidden;
  font-family: "Indie flower", cursive;
}

#contents menu {
  display: none;
}

#contents menu:target {
  display: block;
}

.tile {
  float: left;
  border: 2px solid black;
  width: 40px;
  height: 40px;
  cursor: default;
  margin: 1px;
  text-overflow: ellipsis;
}
.tile:hover {
  transform: scale(1.1);
  cursor: pointer;
  z-index: 99;
  background-color: inherit;
}


.plant {
  width: 100%;
  height: 100%;
  text-align: center;
  text-transform: capitalize;
}


p {
  background-color: lightblue;
  float: left;
  padding: 10px 16px;
  cursor: pointer;
  font-weight: 900;
  border: 2px solid black;
  border-radius: 10px;
}

* {
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
}

button {
  background-color: #4CAF50;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 14px;
  cursor: pointer;
  outline: none;
  transition: all 0.4s cubic-bezier(0,0,1,0);
  border: 2px solid darkgreen;
  border-radius: 5px;
}

.animation {
  position: absolute;
  font-size: 24px;
  z-index: -1;
}
#console {
  position: absolute;
  right: 10px;
  top: 10px;
  resize: none;
  overflow: auto;
  border: 2px solid black;
  width: 250px;
  height: calc(100% - 100px);
}

.upbar-container {
  width: calc(100% - 4px);
  height: 15px;
  float: right;
  background-color: white;
  border: 2px solid black;
  clear: both;
  display: block;
  border-radius: 10px;
}

.upbar {
  background-color: #167ac6;
  height: 100%;
  line-height: inherit;
  transition: all 0.5s cubic-bezier(.24, .07, 0, 1);
  text-align: center;
  font-size: 14px;
  width: 0%;
  white-space: pre;
  border-radius: 10px;
}
img {
  width: 35px;
  height: 35px;
  float: left;
}
button:disabled {
  background-color: grey;
  cursor: not-allowed;
}
#money {
  font-size: 24px;
  font-family: monospace;
}
#season {
  font-size: 24px;
}
select {
  border: 1px solid #111;
  width: 150px;
  padding: 5px 35px 5px 5px;
  font-size: 16px;
  border: 1px solid #ccc;
  height: 34px;
  outline: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  cursor: pointer;
}
#plant {
  background: url(http://www.freeiconspng.com/uploads/plant-icon-21.png) 96% / 10% no-repeat white;
  text-transform:capitalize;
}
#fps 
{background: url(http://board.4players.pl/uploads/monthly_2016_10/icon175x175.png.ebedf77abfe6da9c05d149bbd6612aa4.png) 96% / 15% no-repeat white;
}
#tileSize {
  background: url(https://cdn2.iconfinder.com/data/icons/designers-and-developers-icon-set/32/increased_proportionally_square_button-512.png) 96% / 15% no-repeat white;
}
#sun { 
  position: absolute;
  top: 50px;
  left: calc(50% + 80px);
	width:70px;
	height:70px;
	border-radius:50%;
	background:white;
	opacity:0.6;
	box-shadow: 0px 0px 40px 15px white;
  transition: opacity 2s linear;
}

#map {
  float: left;
  width: calc(100% - 600px);
}

#contents {
  position: fixed;
  right: 250px;
  width: 400px;
  overflow-y: auto;
  max-height: 450px;
  overflow-x: hidden;
}
menu > button {
  width: 100%;
}

#navbar {
  position: fixed;
  right: 10px;
}

#navbar ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 200px;
    background-color: #f1f1f1;
}

#navbar li a {
    display: block;
    color: #000;
    padding: 8px 16px;
    text-decoration: none;
}

#navbar li a:hover {
    background-color: #555;
    color: white;
}
#openMenuBtn {
  position: fixed;
  bottom: 10px;
  right: 10px;
}

.checkbox {
  width: 20px;
  height: 20px;
  background-color: red;
  border-radius: 2px;
  border: 2px solid black;
  font-size: 17px;
  margin: 2px;
  text-align: center;
  cursor: pointer;
  line-height: 24px;
  display: inline-block;
}
label {
  width: 250px;
  float: left;
}
.active {
  background-color: green;
}

#skilltree {
  padding: 10px;
  width: 350px;
  float: right;
}

.title {
  text-align: center;
  font-size: 20px;
  display: block;
}

#skilltree > button {
  width: 87.5px;
  padding: 0;
  color: black;
  font-size: 11px;
  height: 60px;
  background-color: orange;
  cursor: pointer;
  border: 2px solid darkorange;
  outline: none;
  float: left;
  white-space: nowrap !important;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0,0,1,0);
}
#skilltree > button:disabled {
  background-color: red;
  color: black;
  border: 2px solid #aa0303;
  cursor: default;
  transition: all 0.3s cubic-bezier(0,0,1,0);
}

.achievement {
  width: 30px;
  height: 30px;
  border: 2px solid black;
  float: left;
  font-family: "Monospace";
  text-align: center;
/*   background-color: black;
  color: white; */
  font-size: 28px;
  margin: 1px;
}
.achievement > img {
  width: 30px;
  height: 30px;
}
#menuSkill {
  transition: all 0.4s cubic-bezier(0,0,0,1);
}



.tooltipster-punk {
	border-radius: 5px; 
	border-bottom: 3px solid #004fce;
	background: #2a2a2a;
	color: #fff;
}
.tooltipster-punk .tooltipster-content {
	font-family: 'Courier', monospace;
	font-size: 14px;
	line-height: 16px;
	padding: 8px 10px;
}

::-webkit-scrollbar-track {
	-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
	border-radius: 10px;
	background-color: #F5F5F5;
}

::-webkit-scrollbar {
  border-radius: 10px;
	width: 12px;
	background-color: #F5F5F5;
}

::-webkit-scrollbar-thumb {
	border-radius: 10px;
	-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
	background-color: #555;
}

select {
  border: 2px solid black;
  border-radius: 5px;
}

#unlockTab > button {
  width: 100%;
  height: 75px;
  float: left;
  padding: 5px;
  font-size: 16px;
  margin: 1px;
  border-radius: 5px;
}
.btn-style {
  width: 32.5%;
  height: 75px;
  float: left;
  padding: 5px;
  font-size: 16px;
  margin: 1px;
  border-radius: 5px;
}

hr {
    padding: 0;
    border: none;
    border-top: medium double #333;
    color: #333;
    text-align: center;
}

#achievementPercentage {
  text-align: center;
  text-decoration: underline;
}
.achievementText {
  color: #f97400;
}
.achievementRewardText {
  color: #01bc05;
}
code {
  color: white;
}

.ach {
  background: #52c234;
  background: -webkit-linear-gradient(to bottom, #52c234 , #061700);
  background: linear-gradient(to bottom, #52c234 , #061700);
  border-radius: 10px;
  display: block;
  position: fixed;
  bottom: -5em;
  left: 50%;
  margin-left: -175px;
  width: 350px;
  height: 65px;
  color: #fff;
  font-size: 16px;
  padding: 0 0 0 0;
}

.ach .icon {
  float: left;
  font-size: 2.2em;
  width: 65px;
  padding: 0.1em 0 0;
  text-align: center;
}

.ach .text_wrap {
  position: absolute;
  left: 65px;
  width: 250px;
  top: 0.7em;
  overflow: hidden;
}

.ach .title {
  font-size: 0.9em;
  width: 260px;
  margin: 0 0 -0.2em;
  padding: 0;
}

.ach .detail {
  font-size: 0.7em;
  width: 260px;
  margin: 0;
  color: rgba(255, 255, 255, 0.7);
}

.ach {
  -webkit-transition: all .1s ease-out;
  -moz-transition: all .1s ease-out;
  -o-transition: all .1s ease-out;
  transition: all .1s ease-out;
}

@keyframes ach_in_anim {
  0% {
    bottom: -5em;
    width: 65px;
    margin-left: -32.5px;
    border-radius: 50%;
  }
  15% {
    bottom: 2.2em;
    width: 65px;
    margin-left: -32.5px;
  }
  25% {
    bottom: 1em;
    width: 65px;
    margin-left: -32.5px;
  }
  75% {
    bottom: 1em;
    width: 65px;
    margin-left: -32.5px;
  }
  85% {
    bottom: 1em;
    width: 65px;
    margin-left: -32.5px;
    border-radius: 50%;
  }
  92% {
    bottom: 1em;
    width: 380px;
    margin-left: -190px;
    border-radius: 0;
  }
  100% {
    bottom: 1em;
    width: 350px;
    margin-left: -175px;
    border-radius: 0;
  }
}

@keyframes ach_current {
  0% {
    bottom: 1em;
    width: 350px;
    margin-left: -175px;
  }
  100% {
    bottom: 1em;
    width: 350px;
    margin-left: -175px;
  }
}

@keyframes ach_out_anim {
  0% {
    bottom: 1em;
    width: 350px;
    margin-left: -175px;
    border-radius: 50%;
  }
  16% {
    bottom: 1em;
    width: 65px;
    margin-left: -32.5px;
    border-radius: 50%;
  }
  20% {
    bottom: 1em;
    width: 50px;
    margin-left: -25px;
  }
  34% {
    bottom: 1em;
    width: 65px;
    margin-left: -32.5px;
  }
  70% {
    bottom: 1em;
    width: 65px;
    margin-left: -32.5px;
  }
  100% {
    bottom: -5em;
    width: 65px;
    margin-left: -32.5px;
    border-radius: 50%;
  }
}

.achieved {
  animation: ach_in_anim 1.4s, ach_current 3s, ach_out_anim 0.8s;
  animation-delay: 0s, 1.4s, 4.4s;
}

@keyframes i_in_anim {
  0% {
    transform: scale(0);
  }
  45% {
    transform: scale(0)
  }
  46% {
    transform: scale(0.4)
  }
  50% {
    transform: scale(1);
  }
  55% {
    transform: scale(1.2);
  }
  60% {
    transform: scale(1);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes i_current {
  0% {
    transform: scale(1);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes i_out_anim {
  0% {
    transform: scale(1);
  }
  46% {
    transform: scale(1);
  }
  47% {
    transform: scale(1.2);
  }
  56% {
    transform: scale(1);
  }
  60% {
    transform: scale(0.4)
  }
  65% {
    transform: scale(0);
  }
  100% {
    transform: scale(0);
  }
}

.achieved .icon i {
  animation: i_in_anim 1.4s, i_current 3s, i_out_anim 0.8s;
  animation-delay: 0s, 1.4s, 4.4s;
}

@keyframes text_in_anim {
  0% {
    width: 0;
  }
  85% {
    width: 0;
  }
  100% {
    width: 260px;
  }
}

@keyframes text_out_anim {
  0% {
    width: 260px;
  }
  12% {
    width: 0;
  }
  100% {
    width: 0;
  }
}

.achieved .text_wrap {
  animation: text_in_anim 1.4s, text_out_anim 0.8s;
  animation-delay: 0s, 4.4s;
}



/*

achievement names contain jokes

coolors.co

buy max eff upgrade button

encrypted safe files

btoa = encrypt
atob = decrypt

max efficiency upgrade -> plant efficiency x1000 and efficiency max increases!

illegal plants -> get caught -> 0 money / give a lot of money

sounds

difficulty

increasing taxes challenge

only summer challenge (peaceful)

IMPORTANT

GOLDEN TILES DO NOT SAVE!

make completed challenges save

font family in settings

tooltip break for achievements

fix tooltip on replant upgrade

change log in the main menu

plants which have grown need to be sold on market.

golden tiles which allow you to replant plant X times

fertiliser, nearby tiles are growing faster.

*/

spop.defaults = {
  icon: false,
  position: "bottom-right",
  autoclose: 4500,
}

var achievements = {
  wheat: {
    amount: [],
    achieved: [],
    achievementName: ["Baby steps...","Learner...","Farmer student.","<img src=http://game-icons.net/icons/delapouite/originals/svg/farmer.svg>","Can't... stop... clicking.","Wheat so yellow...","Well done... for playing so long.","Wheat is soo cool","Farming so fun","Remember to mill the wheat!","Get a tractor already!","Super farmer","Pro farmer","You're getting good!","Wheat so cheap.","Try new plants.","почему вы перевести данный текст?","Plant problems ?","You don't have to play wheat all the game...","Flowers are better than wheat.","Wheat > Flowers","Haha you're not trying to win.","Want 100% Achievements?","So close...","Keep going.","One more to go!","There ya go!"]
  },
  flower: {
    amount: [],
    achieved: [],
    achievementName: ["Fresh start.","Flowers so much better than wheat.","Good luck getting next plant."],
  },
  herb: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  grass: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  sunflower: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  algae: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  corn: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  cabbage: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  jasmine: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  strawberry: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  chocolate: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  potato: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
  coconut: {
    amount: [],
    achieved: [],
    achievementName: [],
  },
}



var achievementNumbers = [1,5,10,25,50,75,100,150,250,500,750,1000,1250,1500,1750,2000,2250,2500,4000,5000,7500,10000,12500,15000,25000,50000,100000];

for (var key in achievements){
  var length = achievementNumbers.length;
  for (var g = 0; g < length; g++){
    achievements[key].achieved[g] = false;
    achievements[key].amount[g] = achievementNumbers[g];
  }
  
  for (var d = 0; d < length; d++){
    if (d == 0){
      $('#achievements').append("<div style='text-align:center;'>"+key+"</div><div style=clear:both;></div>")
    }
    var id = key+achievements[key].amount[d];
    var element = '<div class=achievement id='+id+'>?</div>';
    $('#achievements').append(element);
    if (d == length - 1){
      $('#achievements').append('<div style="height:20px; width:100%; float:left;"></div>')
    }
  }
  
}

var i = 0;

for (i = 0; i < 1; i++){
  var element = '<div class=tile id=cell'+i+' onclick=plant(this);></div>';
  $('#map').append(element);
}


var keys = [];

$(document).keydown(function(e) {
  keys[e.keyCode] = true;
});

$(document).keyup(function(e) {
  delete keys[e.keyCode];
});

var FPS = 30;

var money = 0;

var tutorial = -1;

var challenge = "";
var debtLeft = 5000000;
var debtTimeLeft = (90/60)*60*60; // 90 mins in sec

var raceTimeLeft = (120/60)*60*60; // 2 hours in sec

var inspectionTimeLeft = 30;

var slotTime = 30;

var voice = 0;

var currentQuest = "5 wheat";
var questTimeLeft = 25;
var questCollected = 0;

var moreSpacePrice = 0.1;
var growthSpeedPrice = 1;
var replantPrice = 1;
var replantChance = 1;

var growthSpeedBoost = 2;
var profitBoost = 1;
var xpBoost = 1;

var currentSeason = 1;
var seasons = ["Spring","Summer","Autumn","Winter"];
var seasonTime = 60;

var growthSpeed = 1;

var holdingMouse = false;

var xp = 0;
var xpN = 1;
var level = 1;
var skillpoints = 0;

var fillAllBtnUnlocked = false;

var skilltree = {};

$(document).mousedown(function(){
  holdingMouse = true;
})
$(document).mouseup(function(){
  holdingMouse = false;
})


var wheat = {
  timeToGrow: 2,
  profit: 0.01,
  cost: 0,
  unlocked: true,
  efficiency: 1,
  effPrice: 0.01,
  efficiencyMax: 20,
  unlockPrice: 0,
  growsIn: "summer",
  totalGrown: 0,
}
var flower = {
  timeToGrow: 10,
  profit: 1.01,
  cost: 1,
  unlocked: false,
  efficiency: 1,
  effPrice: 1,
  efficiencyMax: 20,
  unlockPrice: 5,
  growsIn: "spring",
  totalGrown: 0,
}
var herb = {
  timeToGrow: 20,
  profit: 2500*1.01,
  cost: 2500,
  unlocked: false,
  efficiency: 1,
  effPrice: 2500,
  efficiencyMax: 20,
  unlockPrice: 4000,
  growsIn: "summer",
  totalGrown: 0,
}
var grass = {
  timeToGrow: 40,
  profit: 2.5e5*1.01,
  cost: 2.5e5,
  unlocked: false,
  efficiency: 1,
  effPrice: 2.5e5,
  efficiencyMax: 20,
  unlockPrice: 1.5e6,
  growsIn: "winter",
  totalGrown: 0,
}
var sunflower = {
  timeToGrow: 70,
  profit: 8e9*1.01,
  cost: 8e9,
  unlocked: false,
  efficiency: 1,
  effPrice: 8e9,
  efficiencyMax: 20,
  unlockPrice: 8e10,
  growsIn: "summer",
  totalGrown: 0,
}

var algae = {
  timeToGrow: 120,
  profit: 3e12*1.01,
  cost: 3e12,
  unlocked: false,
  efficiency: 1,
  effPrice: 3e12,
  efficiencyMax: 25,
  unlockPrice: 3e13,
  growsIn: "autumn",
  totalGrown: 0,
}

var corn = {
  timeToGrow: 200,
  profit: 2e15*1.01,
  cost: 2e15,
  unlocked: false,
  efficiency: 1,
  effPrice: 2e15,
  efficiencyMax: 25,
  unlockPrice: 3.14e16,
  growsIn: "summer",
  totalGrown: 0,
}

var cabbage = {
  timeToGrow: 300,
  profit: 4e18*1.01,
  cost: 4e18,
  unlocked: false,
  efficiency: 1,
  effPrice: 4e18,
  efficiencyMax: 25,
  unlockPrice: 5e19,
  growsIn: "spring",
  totalGrown: 0,
}

var jasmine = {
  timeToGrow: 500,
  profit: 2e22*1.01,
  cost: 2e22,
  unlocked: false,
  efficiency: 1,
  effPrice: 2e22,
  efficiencyMax: 25,
  unlockPrice: 14.5e22,
  growsIn: "winter",
  totalGrown: 0,
}

var strawberry = {
  timeToGrow: 800,
  profit: 2e25*1.01,
  cost: 2e25,
  unlocked: false,
  efficiency: 1,
  effPrice: 2e25,
  efficiencyMax: 25,
  unlockPrice: 2e26,
  growsIn: "spring",
  totalGrown: 0,
}

var chocolate = {
  timeToGrow: 1400,
  profit: 2.048e29*1.01,
  cost: 2.048e29,
  unlocked: false,
  efficiency: 1,
  effPrice: 2.048e29,
  efficiencyMax: 25,
  unlockPrice: 80.0e29,
  growsIn: "autumn",
  totalGrown: 0,
}

var potato = {
  timeToGrow: 2500,
  profit: 8.192e32*1.01,
  cost: 8.192e32,
  unlocked: false,
  efficiency: 1,
  effPrice: 8.192e32,
  efficiencyMax: 25,
  unlockPrice: 21.92e32,
  growsIn: "autumn",
  totalGrown: 0,
}

var coconut = {
  timeToGrow: 4650,
  profit: 3.2768e36*1.01,
  cost: 3.2768e36,
  unlocked: false,
  efficiency: 1,
  effPrice: 3.2768e36,
  efficiencyMax: 25,
  unlockPrice: 1.2768e37,
  growsIn: "summer",
  totalGrown: 0,
}



var plants = ["wheat","flower","herb","grass","sunflower","algae","corn","cabbage","jasmine","strawberry","chocolate","potato","coconut"];

for (var i = 1; i <= 60; i++){
  if (i == 30){
    $('#fpsOption').append("<option value="+i+" selected>"+i+" FPS</option>");
    continue;
  }
  $('#fpsOption').append("<option value="+i+">"+i+" FPS</option>");
}
for (var ii = 10; ii <= 75; ii++){
  if (ii == 75){
    $('#tileSizeOption').append("<option value="+ii+"px selected>"+ii+" px</option>");
    continue;
  }
  $('#tileSizeOption').append("<option value="+ii+"px>"+ii+" px</option>");
}

for (var i = 0; i < plants.length; i++){
  var elem1 = '<button id='+plants[i]+'EffBtn onclick=upEff("'+plants[i]+'")>'+plants[i]+' profits</button>';
  var elem2 = '<div class="upbar-container"><div class="upbar" id='+plants[i]+'EffBar></div></div>';
  $('#upgradesTab').append(elem1,elem2);
  var price = window[plants[i]].cost;
  var elem3 = '<option value="'+plants[i]+'" id='+plants[i]+'>'+plants[i]+' £'+simplify(price)+'</option>';
  $('#plant').append(elem3);
  var unlockPrice = window[plants[i]].unlockPrice;
  var title = "£"+(simplify(unlockPrice));
  var elem4 = '<button onclick=unlock("'+plants[i]+'"); id='+plants[i]+'UnlockBtn title="'+title+'" class=info></button>';
  $('#unlockTab').append(elem4);
}

function plant(obj,type){
  var item = type;
  if (!type) var item = $('#plant').val();
  
  var profit = window[item].profit;
  var timeToGrow = window[item].timeToGrow;
  var efficiency = window[item].efficiency;
  
  if (money < window[item].cost) return;
  if (obj.innerHTML !== "") return;
  
  money -= window[item].cost;
  var time = $(obj).attr('data-time');
  if (time > 0) return;
  $(obj).attr('data-time',200);
  var id = obj.id;
  var price = window[item].cost;
  add ("<span style='color: red;'>"+"-£"+simplify(price)+"</span>","log",0,100);
  var element = "<div class=plant data-time="+timeToGrow+" data-timeMax="+timeToGrow+" data-profit="+profit+" data-type="+item+" data-efficiency="+efficiency+"></div>";
  $(obj).html(element);
}

function engine(){
  $('#skillpoints').html(skillpoints+" skillpoints left.");
  FPS = $('#fps').val();
  $('#season').html(seasons[currentSeason]+"<br>("+challenge+")");
  $('#money').html("Money: £"+simplify(money));
  $('#moreSpaceBtn').html("+1 plot ("+$('.tile').length+")<br>£"+simplify(moreSpacePrice));
  $('#growthSpeedBtn').html("Faster growth"+"<br>£"+simplify(growthSpeedPrice));
  $('#replantUpgradeBtn').html("Replant chance<br>"+replantChance+"%");
  $('#replantUpgradeBtn').attr('title', "£"+simplify(replantPrice));
  
  $('#seasonbar').html((format(seasonTime)));
  var perc = (seasonTime / 60 * 100);
  var color = "rgb("+Math.floor(255 - (2.55*perc))+","+Math.round(2.55*perc)+",0)";
  $('#seasonbar').css({
    width: perc+"%",
    backgroundColor: color,
  })
  seasonTime -= 1/FPS;
  
  if (seasonTime <= 0){
    currentSeason++;
    if (currentSeason >= seasons.length){
      currentSeason = 0;
    }
    achievementUnlocked("<code style=font-size:20px;>"+seasons[currentSeason]+" has started!</code>",false)
    
    seasonTime = 60;
    for (var y = 0; y < plants.length; y++){
      $('#'+plants[y]).html(plants[y]+" £"+simplify(window[plants[y]].cost));
      var p = window[plants[y]];

      if (p.growsIn == seasons[currentSeason].toLowerCase()){
        $('#'+plants[y]).html(plants[y]+" £"+simplify(window[plants[y]].cost)+" ☀");
      }
    }
    
  }
  
  
  if (currentSeason == 0){
    Math.random() < 0.5 ? add("|","log",0,$(window).width() - 100,true) : add(".","log",0,$(window).width() - 100,true);
  }
  // winter
  if (currentSeason == 3){
    if (Math.random() < 1){
      Math.random() < 0.5 ? add("❄","log",0,$(window).width() - 100,true) : add("❆","log",0,$(window).width() - 100,true);
    }
  }
  
  $('#moreSpaceBtn').css({
    backgroundColor: "grey",
    border: "2px solid black",
  })
  if (money >= moreSpacePrice){
    $("#moreSpaceBtn").css({
      backgroundColor: "#4CAF50",
      border: "2px solid darkgreen",
    })
  }
  
  $('#growthSpeedBtn').css({
    backgroundColor: "grey",
    border: "2px solid black",
  })
  if (money >= growthSpeedPrice){
    $("#growthSpeedBtn").css({
      backgroundColor: "#4CAF50",
      border: "2px solid darkgreen",
    })
  }
  $('#replantUpgradeBtn').css({
    backgroundColor: "grey",
    border: "2px solid black",
  })
  if (money >= replantPrice){
    $('#replantUpgradeBtn').css({
      backgroundColor: "#4CAF50",
      border: "2px solid darkgreen",
    })
  }
  
  var length = $('.tile').length;
  
  var mps = 0;
  
  for (var i = 0; i < length; i++){
    $('#cell'+i).removeAttr("data-time");
    var element = $('#cell'+i).find(".plant");
    
    var time = parseFloat($(element).attr("data-time"));
    
    if (time <= 0) continue;
    var type = $(element).attr('data-type');
    
    
    if (type){
      var canGrowIn = window[type].growsIn;
      canGrowIn = canGrowIn.toLowerCase();
      // doesnt grow in winter and is winter
      var grow = true;
      if (canGrowIn !== "winter" && currentSeason == 3){
        if (Math.random() < 0.5){
          grow = false;
          newTime = parseFloat(time);
        }
      }
      
      var thisSeason = seasons[currentSeason].toLowerCase();
      if (canGrowIn == thisSeason && grow){
        var newTime = parseFloat(time) - (growthSpeed * growthSpeedBoost)/FPS * 3;
      }
      else {
        if (grow){
          var newTime = parseFloat(time) - (growthSpeed * growthSpeedBoost)/FPS;
        }
      }
      var object = window[type];
      var profit = object.profit*object.efficiency - object.cost
      var speed = (object.timeToGrow/growthSpeed);
      mps += ((profit) / speed)*growthSpeedBoost;
    }
    $('#mps').html(simplify(mps)+" / sec");
    // summer
    if (currentSeason == 1){
      $('#sun').css({
        opacity: 1,
      })
    }
    // winter
    if (currentSeason == 3){
      $('#sun').css({
        opacity: 0.5,
      })
    }
    
    
    
    $(element).attr('data-time',newTime);
    var maxTime = $(element).attr('data-timeMax');
    var perc = 100 - (newTime/maxTime)*100;
    
    var plantt = $(element).attr('data-type');
    plantt = String(plantt);
    
    plantt = "<span>"+plantt+"</span>"
    var extra = "";
    if ($('#plantTime').hasClass("active")){
      /*
      
      var totalTime = 100/growthSpeed * growthSpeedBoost;
      var time = totalTime * (buildValue/100);
      var timeLeft = totalTime - time;
      
      */
      var timeLeft = $(element).attr('data-time') / ((growthSpeed * growthSpeedBoost));
      
      if (canGrowIn !== "winter" && currentSeason == 3){
        timeLeft *= 3;
      }
      if (canGrowIn == thisSeason){
        timeLeft /= 3;
      }
      
      extra += ""+format(timeLeft)+"";
      $(element).html("<b>"+plantt+"</b><br>"+extra);
    }
    if ($('#plantTime').hasClass("active") == false){
      $(element).html("<b>"+plantt+"</b><br>"+Math.ceil(perc)+"%");
    }
    
    var color = "rgb("+Math.floor(255 - (2.55*perc))+","+Math.round(2.55*perc)+",0)";
    if (perc > 99){
      $(element).remove();
      $(element).css({
        backgroundColor: "white",
      })
      
      // 50 % of replanting a plant
      
      // 0.5% of plant being next level
      
      var rand = Math.random()*100;
      var replantEnabled = $('#replantEnable').hasClass("active");
      if (rand <= replantChance && replantEnabled){
        if (Math.random() < 0.005){
          var newPlant = plants.indexOf(type)+1;
          plant($("#cell"+i)[0],plants[newPlant]);
        }
        plant($("#cell"+i)[0],type);
      }
      
      var profit = $(element).attr('data-profit');
      var efficiency = $(element).attr('data-efficiency');
      
      
      var plantToAdd = $(element).attr('data-type');
      window[plantToAdd].totalGrown++;
      
      add ("<span style='color: green;'>"+"+£"+simplify(profit*efficiency)+"</span>","log",0,100);
      
      money += parseFloat(profit) * parseFloat(efficiency) * profitBoost;
      xp += parseFloat(profit * efficiency * 10 * xpBoost);
      var quest = currentQuest.split(" ");
      if (quest[1] == $(element).attr('data-type')){
        questCollected++;
      }
      continue;
    }
    $(element).css({
      backgroundColor: color,
      width: perc+"%",
    })
  }
  
  
  
  for (var s = 0; s < plants.length; s++){
    var p = window[plants[s]];
    $('#'+plants[s]).css("display","none");
    if (p.unlocked){
      $('#'+plants[s]).css("display","block");
    }
    var info = plants[s]+" efficiency<br>£"+simplify(window[plants[s]].effPrice);
    
    $('#'+plants[s]+"EffBtn").html(info);
    $('#'+plants[s]+"UnlockBtn").html("Unlock "+plants[s])
    if (p.unlocked){
      $('#'+plants[s]+"UnlockBtn").css({display: "none"})
    }
    var price = p.effPrice;
    $('#'+plants[s]+"EffBtn").css({backgroundColor: "grey"})
    if (money >= price){
      $('#'+plants[s]+"EffBtn").css({backgroundColor: "#4CAF50"})
    }
    $('#'+plants[s]+"UnlockBtn").css({
      backgroundColor: "grey",
      border: "2px solid black",
    })
    if (money >= p.unlockPrice){
      $('#'+plants[s]+"UnlockBtn").css({
        backgroundColor: "#4CAF50",
        border: "2px solid darkgreen",
      })
    }
    
    $('#'+plants[s]+"EffBtn").hide();
    $('#'+plants[s]+"EffBar").parent().hide();
    
    var selectedPlant = $('#plant').val();
    
    $('#'+selectedPlant+"EffBtn").show();
    $('#'+selectedPlant+"EffBar").parent().show();
    
    $('#'+plants[s + 1]+"UnlockBtn").hide();
    if (window[plants[s]].unlocked){
      $('#'+plants[s + 1]+"UnlockBtn").show();
    }
    
    if (money >= p.cost){
      $('#'+plants[s]).css({backgroundColor: "#4CAF50"})
    } else {
      $('#'+plants[s]).css({backgroundColor: "grey"})
    }
    var obj = plants[s];
    var perc = window[obj].efficiency/window[obj].efficiencyMax*100;
    var color = "rgb("+Math.floor(255 - (2.55*perc))+","+Math.round(2.55*perc)+",0)";
    $('#'+obj+"EffBar").html("x"+(Math.round(window[obj].efficiency*100))/100);
    $('#'+obj+"EffBar").css({
      width: perc+"%",
      backgroundColor: color,
    })
    
    var l = window[obj].efficiency;
    var lMax = window[obj].efficiencyMax;
    
    if (window[obj].efficiency >= window[obj].efficiencyMax && l < 1e6){
      window[obj].efficiency *= 1e6;
      window[obj].efficiencyMax = window[obj].efficiency;
    }
    
    
  }
  
  if (money >= 1e6){
    $('#buyFillBtn').css({backgroundColor: "#4CAF50"})
  }
  if (money < 1e6){
    $('#buyFillBtn').css({backgroundColor: "grey"})
  }
  
  var selected = $('#plant').val();
  var object = window[selected];
  info = "<ul>";
  info += "<li>Profit: £"+simplify((object.profit*object.efficiency - object.cost) * profitBoost)+"</li><li>Cost: £"+simplify(object.cost)+"</li><li>Growth speed: "+Math.floor((object.timeToGrow/growthSpeed*100)/growthSpeedBoost)/100+"s<li>Grows in: "+object.growsIn+"</li>";
  document.getElementById("info").innerHTML = info+"</ul>";
  
  var perc = xp/xpN * 100;
  
  $('#levelbar').css({
    width: perc+"%",
  })
  $('#levelbar').html(simplify(xp)+"/"+simplify(xpN)+" Level "+level)
  
  if (xp >= xpN){
    level++;
    xp -= xpN;
    xpN *= Math.PI;
    skillpoints++;
  }
  
  if (fillAllBtnUnlocked){
    $('#fillAllBtn').prop("disabled",false);
    $('#buyFillBtn').remove();
  }
  
  
  var achievementPercentage = 0;
  
  var totalAchievementLength = 0;
  var achieved = 0;
  
  for (var key in achievements){
    totalAchievementLength += achievements[key].amount.length;
    for (var m = 0; m < achievements[key].amount.length; m++){
      if (achievements[key].achieved[m]){
        achieved++;
      }
    }
  }
  achievementPercentage = achieved / totalAchievementLength * 100;
  
  $('#achievementPercentage').html("Achievements ("+Math.round(achievementPercentage*10)/10+"%)")
  
  
  $('#menuUnlock').hide();
  $('#menuUpgrades').hide();
  $('#menuSkill').hide();
  $('#menuAchievements').hide();
  $('#menuStats').hide();
  
  if (level >= 2) $('#menuUpgrades').show();
  if (level >= 3) $('#menuUnlock').show();
  if (level >= 4) $('#menuSkill').show();
  if (level >= 5) $('#menuAchievements').show();
  if (level >= 6) $('#menuStats').show();
  
  var size = parseInt($('#tileSize').val());
  
  var fontSize = Math.floor(size/4)
  
  $('.tile').css({
    width: size,
    height: size,
    fontSize: fontSize+"px",
  })
  $('#menuSkill').css({
    color: "black",
  })
  if (skillpoints >= 1){
    $('#menuSkill').css({
      color: "red",
    })
  }
  var sun = $('#sunEnabled').hasClass("active");
  if (!sun){ $('#sun').hide(); }
  if (sun){ $('#sun').show(); }
  
  
  
  setTimeout(engine,1000/FPS);
}
engine();

function tutNext(){
  tutorial++;
}

function tut(){
  if (tutorial == 1){
    $('#arrow').animate({
      left: "150px",
    })
    $('#tutorialMsg').html("<label>1/7</label>Welcome to farming idle!<br>Click this square to plant your wheat! <br>Keep planting until you get £0.1.");
    $('#tutorialMsg').animate({
      left: "250px",
    })
    $("#arrow").pointat({ target: "#cell0" });
    if (money >= 0.1){
      tutorial++;
    }
    if (voice == 0){
      voice++;
      speak("Welcome to farming idle! Click this square to plant your wheat! Keep planting until you get £0.1.")
    }
  }
  if (tutorial == 2){
    $('#arrow').animate({
      left: "800px",
      top: "150px",
    })
    $('#tutorialMsg').html("<label>2/7</label>Click on upgrades and purchase your slot upgrade.");
    $('#tutorialMsg').animate({
      left: "650px",
      top: "100px",
    })
    $("#arrow").pointat({ target: "#menuUpgrades" });
    if ($('.tile').length >= 2){
      tutorial++;
    }
    if (voice == 1){
      voice++;
      speak("Click on upgrades and purchase your slot upgrade.")
    }
  }
  if (tutorial == 3){
    $('#arrow').animate({
      left: "800px",
      top: "150px",
    })
    $('#tutorialMsg').html("<label>3/7</label>Now keep planting wheat and upgrade wheat efficiency to 1.3x");
    $('#tutorialMsg').animate({
      left: "650px",
      top: "100px",
    })
    $("#arrow").pointat({ target: "#menuUpgrades" });
    if (wheat.efficiency >= 1.3){
      tutorial++;
    }
    if (voice == 2){
      voice++;
      speak("Now keep planting wheat and upgrade wheat efficiency to 1.3 times")
    }
  }
  if (tutorial == 4){
    $('#arrow').animate({
      left: "800px",
      top: "150px",
    })
    $('#tutorialMsg').html("<label>4/7</label>Well done!<br>Now keep planting until you reach level 3. <br><b>Remember</b> you can also click and hold left click then hover over the squares to quick plant.");
    $('#tutorialMsg').animate({
      left: "650px",
      top: "100px",
    })
    $("#arrow").pointat({ target: "#cell0" });
    if (level >= 3){
      tutorial++;
    }
    if (voice == 3){
      voice++;
      speak("Well done! Now keep planting until you reach level 3. Remember you can also click and hold left click then hover over the squares to quick plant.")
    }
  }
  if (tutorial == 5){
    $('#arrow').animate({
      left: "300px",
      top: "250px",
    })
    $('#tutorialMsg').html("<label>5/7</label>Good job! Here you can see plant stats, notice that there is <b>Grows In</b> which tells you the season the plant grows fastest in.<br><button onclick=tutNext();>Next</button>");
    $('#tutorialMsg').animate({
      left: "450px",
      top: "200px",
    })
    $("#arrow").pointat({ target: "#info" });
    if (voice == 4){
      voice++;
      speak("Good job! Here you can see plant stats, notice that there is Grows In which tells you the season the plant grows fastest in.")
    }
  }
  if (tutorial == 6){
    $('#arrow').animate({
      left: "500px",
      top: "150px",
    })
    $('#tutorialMsg').html("<label>6/7</label>Once you unlock new plants they will become available in this dropdown.<button onclick=tutNext();>Next</button>");
    $('#tutorialMsg').animate({
      left: "650px",
      top: "100px",
    })
    $("#arrow").pointat({ target: "#plant" });
    if (voice == 5){
      voice++;
      speak("Once you unlock new plants they will become available in this dropdown.")
    }
  }
  if (tutorial == 7){
    $('#arrow').animate({
      left: "80%",
      top: "350px",
    })
    $('#tutorialMsg').html("<label>7/7</label>You can also click this button to hide menu for less stuff on the screen. Have fun!<button onclick=tutNext();>Close</button>");
    $('#tutorialMsg').animate({
      left: "75%",
      top: "200px",
    })
    $("#arrow").pointat({ target: "#openMenuBtn" });
    if (voice == 6){
      voice++;
      speak("You can also click this button to hide menu for less stuff on the screen. Have fun!")
    }
  }
  if (tutorial >= 8 || tutorial == 0){
    $('#tutorialMsg').remove();
    $('#arrow').remove();
  }
  
  if (tutorial > 0){
    $('#arrow').css({
      display: "block",
    })
  }
  $('#challengeOption').show();
  if (challenge){
    $('#challengeOption').hide();
  }
  $('#currentChallenge').html("Your current challenge: "+challenge);
  if (!challenge){
    $('#currentChallenge').html("You are not in a challenge.");
  }
  var chal = $('#challengeOption').val();
  if (chal == "winter only") $('#challengeDesc').html("Winter has been heavy for sometime, but no snow can stop you!");
  if (chal == "hideout") $('#challengeDesc').html("Government has made farming illegal, and will inspect every farm every so often. Make sure you don't get caught!");
  if (chal == "race") $('#challengeDesc').html("Race with your neighbour for the best plant competition. To win you have to reach Jasmine before time limit!");
  if (chal == "debt") $('#challengeDesc').html("Your grandad has fallen into debt, now it has been passed onto you. Make sure to pay it off before time limit!");
  if (chal == "quest chaos") $('#challengeDesc').html("Your boss has been not happy with you lately taking so many days off, he is giving you a lot of work while he is on holidays. How unfair!");
  if (chal == "missing plants") $('#challengeDesc').html("There was a plague and half of the plants are missing! But that can't stop you from becoming greatest farmer of all time!");
  if (chal == "five slots") $('#challengeDesc').html("Ground has been infected with unknown creatures. They like land and will fight for it if needed. You can have maximum of 5 slots before getting their attention.");
  if (chal == "slot loss") $('#challengeDesc').html("Unknown creatures attack again! This time they will attack every 30 seconds if you have more than 2 slots. (Lose 1 slot every 30 sec.)");
  if (chal == "wheat only") $('#challengeDesc').html("Every plant except wheat has been poisoned by unknown creatures. You can only plant wheat but because it is the only plant, the upgrades became cheaper.");
  if (chal == "no upgrades") $('#challengeDesc').html("It is 300BC, technology has not evolved yet and you have to rely on the only land that king gave you.");
  
  
  setTimeout(tut,500);
}
tut();

function roundTo(number,dp){
  if (!dp) dp = 2;
  dp = Math.pow(10,dp);
  return Math.floor((number*dp))/dp;
}


function buyMoreSpace(){
  i = $('.tile').length;
  var element = '<div class=tile id=cell'+i+' onclick=plant(this);></div>';
  if (money >= moreSpacePrice){
    money -= moreSpacePrice;
    add ("<span style='color: red;'>"+"-£"+simplify(moreSpacePrice)+"</span>","log",0,100);
    moreSpacePrice *= 1.35;
    $('#map').append(element);
  }
}

function buyMoreGrowthSpeed(){
  if (money >= growthSpeedPrice){
    money -= growthSpeedPrice;
    add ("<span style='color: red;'>"+"-£"+simplify(growthSpeedPrice)+"</span>","log",0,100);
    growthSpeedPrice *= 10;
    growthSpeed += 0.5;
  }
}

function buyReplantChance(){
  if (money >= replantPrice && replantChance < 25){
    money -= replantPrice;
    replantPrice *= 2;
    replantChance++;
    tooltip();
  }
}



function simplify(number) {
  var simplifyNumbers = $("#simplifyNumbers").hasClass("active");
  if (!simplifyNumbers) return (Math.floor(number*100)/100).toLocaleString();
  var show = number;
  
  var numberNames = ["","thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","quattuordecillion","quindecillion","sexdecillion","septendecillion","octodecillion","novemdecillion","vigintillion","unvigintillion","duovigintillion","tresvigintillion","quattuorvigintillion","quinvigintillion","sesvigintillion","septemvigintillion","Octovigintillion","Novemvigintillion","Trigintillion","Untrigintillion","Duotrigintillion","Trestrigintillion","Quattuortrigintillion","Quintrigintillion","Sestrigintillion","Septentrigintillion","Octotrigintillion","Noventrigintillion","Quadragintillion","Unquadragintillion","Duoquadragintillion","Tresquadragintillion","Quattuorquadragintillion","Quindragintillion","Sesquadragintillion","Septenquadragintillion","Octoquadragintillion","Novenquadragintillion","Quinquagintillion","Unquinquagintillion","Duoquinquagintillion","Tresquinquagintillion","Quattuorquinquagintillion","Quinquinquagintillion","Sesquinquagintillion","Septenquinquagintillion","Octoquinquagintillion","Novenquinquagintillion","Sexagintillion","Unsexagintillion","Duosexagintillion","Tresexagintillion","Quattuorsexagintillion","Quinsexagintillion","Sesexagintillion","Septensexagintillion","Octosexagintillion","Novensexagintillion","Septuagintillion","Unseptuagintillion","Duoseptuagintillion","Treseptuagintillion","Quattuorseptuagintillion","Quinseptuagintillion","Seseptuagintillion","Septenseptuagintillion","Octoseptuagintillion","Novenseptuagintillion","Octogintillion","Unoctogintillion","Duooctogintillion","Tresoctogintillion","Quattuoroctogintillion","Quinoctogintillion","Sexoctogintillion","Septemoctogintillion","Octooctogintillion","Novemoctogintillion","Nonagintillion","Unnonagintillion","Duononagintillion","Trenonagintillion","Quattuornonagintillion","Quinnonagintillion","Sexnonagintillion","Septenonagintillion","Octononagintillion","Novenonagintillion","Centillion","Uncentillion","Duocentillion","Trescentillion"]
  
  for (var g = 1; g < numberNames.length; g++){
    if (number >= Math.pow(10,g*3) && number < Math.pow(10,(g*3)+3)){
      return Math.floor(show / Math.pow(10,(g*3)-2)) / 100 +" "+numberNames[g];
      break;
    }
    if (g == numberNames.length - 1) {
      return Math.floor(show * 100) / 100;
    }
  }
}

setInterval(function(){
  $('.animation').each(function(){
    $(this).animate({
      top: $(window).height() - 50,
      opacity: 0.2,
    },1000,"swing",function(){
      $(this).remove();
    })
  });
  
},100);

function add(value,id,min,max,enabled){
  var popups = $("#numberPopUps").hasClass("active");
  if (!popups && !enabled) return;
  var rand = Math.floor((Math.random()*max)+min);
  var element = '<div class=animation style="left: RANDpx; top: -20px;">'+value+'</div>';
  element = element.replace(/RAND/gi,rand);
  $("#"+id).append(element);
}

// $('select').selectmenu();

function fillAll(){
  var checked = $('#useSelectedPlant').hasClass("active");
  var tileLength = $('.tile').length;
  var reversed = [];
  for (var l = 0; l < plants.length; l++){
    reversed[l] = plants[plants.length - 1 - l]
  }
  
  if (checked){
    for (var i = 0; i < tileLength; i++){
      var selected = $('#plant').val();
      var tile = $('#cell'+i);
      var unlocked = window[selected].unlocked;
      var price = window[selected].cost;
      if (tile.html() == "" && unlocked && money >= price){
        plant(tile[0],selected);
      }
    }
    return;
  }
  
  for (var f = 0; f < tileLength; f++){
    for (var g = 0; g < plants.length; g++){
      var tile = $('#cell'+f);
      var unlocked = window[reversed[g]].unlocked;
      var price = window[reversed[g]].cost;
      if (tile.html() == "" && unlocked && money >= price){
        plant(tile[0],reversed[g]);
        break;
      }
      
    }
  }
}

function upEff(obj){
  var price = window[obj].effPrice;
  if (money >= price && window[obj].efficiency < window[obj].efficiencyMax){
    money -= price;
    window[obj].effPrice *= 1.005;
    window[obj].efficiency += 0.01;
    var perc = window[obj].efficiency / window[obj].efficiencyMax * 100;
    
  }
}

function unlock(obj){
  var price = window[obj].unlockPrice;
  if (money >= price){
    money -= price;
    $('#'+obj+"UnlockBtn").remove();
    window[obj].unlocked = true;
    var name = "£"+window[obj].cost+" "+obj;
  }
}

function buyFill(){
  if (money >= 1e6){
    money -= 1e6;
    fillAllBtnUnlocked = true;
    $('#fillAllBtn').prop("disabled",false);
    $('#buyFillBtn').remove();
  }
}



function achievementUnlocked(text,title){
  var hasClass = $('.ach').hasClass('achieved');
  if (hasClass) return;
  if (!title) title = false;
  if (title) $('.title').html("Achievement unlocked!");
  $('.detail').html(text);
  $('.ach').addClass("achieved");
  setTimeout(function(){
    $('.ach').removeClass("achieved");
  },5000)
}


window.onmouseover=function(e) {
  if (!holdingMouse) return;
  if (e.target.innerHTML){
    return;
  }
  // className == "tile"
  if (e.target.innerHTML == "" && e.target.className == "tile"){
    plant(e.target);
  }
};


for (var i = 0; i < plants.length; i++){
  var obj = plants[i];
  var perc = window[obj].efficiency/window[obj].efficiencyMax*100;
  $('#'+obj+"EffBar").html("x"+(Math.round(window[obj].efficiency*100))/100);
  $('#'+obj+"EffBar").css({
    width: perc+"%",
  })
}


$('.checkbox').click(function(){
  $(this).toggleClass("active");
  var selector = $(this).hasClass("active");
  $(this).html("")
});


function openNav() {
  $('#navbar').animate({
    width: "200px",
    right: "10px",
  })
  $('#contents').animate({
    right: "250px",
  })
}

function closeNav() {
  $('#navbar').animate({
    width: "0px",
    right: "-250px",
  })
  $('#contents').animate({
    right: "-500px",
  })
}

jQuery.fn.clickToggle = function(a,b) {
  var ab = [b,a];
  return this.on("click", function(){ ab[this._tog^=1].call(this); });
};

$("#openMenuBtn").clickToggle(function() {   
  closeNav();
}, function() {
  openNav();
});


/////////////////////////////////
//        IN PROGRESS          //
/////////////////////////////////
var property = ["cost","efficiency","efficiencyMax","effPrice","growsIn","profit","timeToGrow","unlocked","unlockPrice","totalGrown"];
var variables = ["xp","xpN","level","growthSpeed","currentSeason","moreSpacePrice","growthSpeedPrice","money","skillpoints","fillAllBtnUnlocked","challenge","debtLeft","debtTimeLeft","raceTimeLeft","inspectionTimeLeft","questTimeLeft","questCollected","currentQuest","slotTime","replantPrice","replantChance","seasonTime"];

function saveTest(){
  achievementUnlocked("<code style=font-size:20px;>Game has been saved!</code>",false);
  var saveFile = {};
  for (var i = 0; i < plants.length; i++){
    for (var j = 0; j < property.length; j++){
      saveFile[plants[i] + property[j]] = window[plants[i]][property[j]];
      
    }
  }
  var variabless = {};
  for (var i = 0; i < variables.length; i++){
    variabless[variables[i]] = window[variables[i]];
  }
  
  localStorage["variabless"] = JSON.stringify(variabless);
  localStorage["saveFile"] = JSON.stringify(saveFile);
  
  var tiles = "";
  
  for (var i = 0; i < $('.tile').length; i++){
    var cell = $('#cell'+i);
    if (i < $('.tile').length - 1){
      tiles += "|";
    }
  }
  localStorage["tiles"] = tiles;
  
  localStorage["skilltree"] = JSON.stringify(skilltree);
  
  localStorage["achievements"] = JSON.stringify(achievements);
  
}

function loadTest(){
  var saveFile = JSON.parse(localStorage.getItem("saveFile"));
  if (!saveFile) return;
  achievementUnlocked("<code style=font-size:20px;>Game has been loaded!</code>",false);
  for (var i = 0; i < plants.length; i++){
    for (var j = 0; j < property.length; j++){
      if (!plants[i] || !property[j]) continue;
      if (property[j] == "cost" || property[j] == "growsIn" || property[j] == "profit" || property[j] == "unlockPrice") continue;
      window[plants[i]][property[j]] = saveFile[plants[i]+property[j]];
    }
  }
  var tiles = localStorage.getItem("tiles");
  
  tiles = tiles.split("|");
  $('.tile').remove();
  for (var i = 0; i < tiles.length; i++){
    var element = '<div class=tile id=cell'+i+' onclick=plant(this);></div>';
    $('#map').append(element);
    $('#cell'+i).html(tiles[i]);
  }
  var variabless = JSON.parse(localStorage["variabless"]);
  
  for (var i = 0; i < variables.length; i++){
    if (!variabless[variables[i]]) continue;
    window[variables[i]] = variabless[variables[i]];
  }
  // skill tree
  
  skilltree = JSON.parse(localStorage["skilltree"]);
  
  achievements = JSON.parse(localStorage["achievements"]);
  
  for (var key in achievements){
    for (var s = 0; s < achievements[key].amount.length; s++){
      if (achievements[key].achieved[s]){
        $('#'+key+achievements[key].amount[s]).html("");
        var title = "Grow "+achievements[key].amount[s].toLocaleString()+" "+key+"(s).";
        $('#'+key+achievements[key].amount[s]).html("<img src=http://game-icons.net/icons/delapouite/originals/svg/trophy-cup.svg class=info title='"+title+"'>");
      }
    }
  }
}


setTimeout(loadTest,100);

setInterval(saveTest,30000);



var ml = [
  10,2,10,10,
  10,4,10,10,
  14,6,14,14,
  10,8,10,10
];
var n = [
  "Growth speed I","Space I","Profit I","More XP I",
  "Growth speed II","Space II","Profit II","More XP II",
  "Growth speed III","Space III","Profit III","More XP III",
  "Growth speed IV","Space IV","Profit IV","More XP IV"
];
var t = [
  "growthSpeed","space","profit","xp",
  "growthSpeed","space","profit","xp",
  "growthSpeed","space","profit","xp",
  "growthSpeed","space","profit","xp",
];
var g = [
  5,1,15,20,
  10,2,25,50,
 25,3,50,100,
 50,4,100,150,
];
for (var i = 0; i < 16; i++){
  skilltree["btn"+i] = {
    level: 0,
    maxlevel: ml[i],
    name: n[i],
    type: t[i],
    gives: g[i],
  }
}

for (var a = 0; a < 16; a++){
  var maxlevel = skilltree["btn"+a].maxlevel;
  var upgradeType = skilltree["btn"+a].type;
  
  var element = '<button disabled data-level=0 data-maxlevel='+maxlevel+' onclick="levelUp(this);"></button>';
  $('#skilltree').append(element);
}
// tooltips on top of buttons!
// resetable skill tree !

//https://silviomoreto.github.io/bootstrap-select/examples/

function loop(){
  
  var length = $('#skilltree > button').length;
  var rowNumber = 4;
  for (var i = 0; i < length; i++){
    var btn = $('#skilltree > button')[i];
    
    var level = skilltree["btn"+i].level;
    var maxlevel = skilltree["btn"+i].maxlevel;
    
    var nextBtn = $('#skilltree > button')[i+rowNumber];
    
    var name = skilltree["btn"+i].name;
    var gives = skilltree["btn"+i].gives;
    var type = skilltree["btn"+i].type;
    
    var perc = level*gives+"% <b>+"+gives+"%</b>";
    var msg = level*gives+"%";
    if (type == "space"){
      perc = perc.replace(/%/g,"");
      msg = msg.replace(/%/g,"");
    }
    
    
    btn.innerHTML = "<b>"+name+"</b><br>lvl "+level+" / "+maxlevel+"<br>"+perc;
    if (i < 4){
      btn.disabled = false;
    }
    var nextBtnLevel = Math.floor(maxlevel);
    if (level >= nextBtnLevel && nextBtn){
      nextBtn.disabled = false;
    }
    if (maxlevel == level){
      btn.style.backgroundColor = "green";
      btn.style.border = "2px solid darkgreen";
      btn.innerHTML = name+"<br>Max level<br>"+msg;
    }
  }
  
  $('#skillpoints').html(skillpoints+" skillpoints left.");
  
  /////////////////////////////////
  //       ACHIEVEMENTS          //
  /////////////////////////////////
  
  
  for (var key in achievements){
    var amount = achievements[key].amount;
    var obj = window[key].totalGrown;
    
    for (var i = 0; i < amount.length; i++){
      var achieved = achievements[key].achieved[i];
      if (obj >= amount[i] && achieved == false){
        var text = achievements[key].achievementName[i] + "<br><span class=achievementText>Grow "+amount[i].toLocaleString()+" "+key+"</span>";
        achievements[key].achieved[i] = true;
        text = text.replace(/undefined/g,"Achievement Unlocked!")
        window[key].efficiency += 0.04 * i + 0.04;
        
        spop("&#127942;"+text+"<br> <span class=achievementRewardText>+"+(4*i+4)+"% "+key+" efficiency!</span>");
        // var title = achievements[key].achievementName[i]+"\nGrow "+amount[i].toLocaleString()+" "+key+"(s).";
        var title = "Grow "+amount[i].toLocaleString()+" "+key+"(s).";
        $('#'+key+amount[i]).html("<img src=http://game-icons.net/icons/delapouite/originals/svg/trophy-cup.svg class=info title='"+title+"'>");
        tooltip();
        // $('#'+key+amount[i]).attr('title',title);
        // $('#'+key+amount[i]).addClass('info');
      }
    }
  }
  var stats = "";
  stats += "Skill tree<br>XP boost: "+xpBoost+"x<br>Profit boost: "+profitBoost+"x<br>Growth speed boost: "+growthSpeedBoost+"x<hr>";
  for (var l = 0; l < plants.length; l++){
    var p = window[plants[l]];
    if (p.unlocked == false) continue;
    stats += plants[l]+": <br>Total grown: "+(p.totalGrown).toLocaleString()+"<br>Sell price: £"+(p.profit * p.efficiency * profitBoost).toLocaleString()+"<hr>";
  }
  
  
  $('#stats').html(stats);
  
  
  
  
  
  setTimeout(loop,100);
}
loop();

function levelUp(obj){
  var index = $(obj).index() - 2;
  var level = skilltree["btn"+index].level;
  var maxlevel = skilltree["btn"+index].maxlevel;
  if (skillpoints <= 0 || level >= maxlevel) return;
  skillpoints--;
  var upgrades = skilltree["btn"+index].type;
  var index = $(obj).index() - 2;
  if (level >= maxlevel) return;
  var newLevel = parseInt(level)+1;
  skilltree["btn"+index].level++;
  if (upgrades == "space"){
    var gives = skilltree["btn"+index].gives;
    for (var l = 0; l < gives; l++){
      var id = $('.tile').length;
      var element = '<div class=tile id=cell'+id+' onclick=plant(this);></div>';
      $('#map').append(element);
    }
  }
}

setInterval(function(){
  growthSpeedBoost = 1;
  xpBoost = 1;
  profitBoost = 1;
  for (var index = 0; index < 16; index++){
    var l = skilltree["btn"+index].level;
    var gives = skilltree["btn"+index].gives;
    var type = skilltree["btn"+index].type;
    if (type == "growthSpeed"){
      growthSpeedBoost += (l * gives) / 100;
    }
    if (type == "xp"){
      xpBoost += (l * gives) / 100;
    }
    if (type == "profit"){
      profitBoost += (l * gives) / 100;
    }
    
  }
},2500);



function tooltip(){
  $('.info').tooltipster({
    delay: 100,
    animation: "grow",
    theme: 'tooltipster-punk',
    multiple: true,
  })
}
tooltip();
setInterval(tooltip,2500);



function reset(){
  var input = prompt("Type in 'yes' to confirm reset.");
  input = input.toLowerCase();
  if (input == "yes"){
    localStorage.clear();
    history.go(0);
  }
}


$('#game').hide();
$('#mainMenu').css({
  width: "100%",
  height: "100%",
})

function startGame(){
  if (!challenge){
    challenge = $('#challengeOption').val();
  }
  $('#mainMenu').hide();
  $('#game').show();
  if ( $('#tutorial').hasClass("active") ){
    tutorial = 1;
  }
  else {
    tutorial = 0;
  }
  if (challenge == "wheat only"){
    wheat.effPrice = 0.001;
    for (var b = 1; b < plants.length; b++){
      $('#'+plants[b]+"UnlockBtn").prop("disabled",true);
    }
  }
  if (challenge == "no upgrades"){
    $('#map').html("");
    for (var i = 0; i < 5; i++){
      var s = $('.tile').length;
      var element = '<div class=tile id=cell'+s+' onclick=plant(this);></div>';
      $('#map').append(element);
    }
  }
}

function challenges(){
  if (challenge == "winter only"){
    currentSeason = 3;
  }
  if (challenge == "debt"){
    $('#menuDebt').show();
    debtTimeLeft -= 1;
  }
  if (challenge == "debt" && debtTimeLeft < 0){
    alert("You lost! Your family got evicted from your farm. No business!");
    localStorage.clear();
    history.go(0);
  }
  $('#timeLeft').html(format(debtTimeLeft));
  $('#debtLeft').html("£"+debtLeft.toLocaleString())
  if (challenge == "missing plants"){
    for (var k = 0; k < plants.length; k++){
      if (k % 2 == 0){
        $('#'+plants[k+1]+"UnlockBtn").remove();
      }
      
    }
  }
  
  if (challenge == "race"){
    $('#raceTimeLeft').html(format(raceTimeLeft));
    raceTimeLeft -= 1;
    if (jasmine.unlocked){
      alert("You have won race challenge! Well done!");
      localStorage.clear();
      history.go(0);
    }
    if (raceTimeLeft < 0){
      alert("You have lost race challenge! Your neighbour over took you and got all the fame. Better luck next time.");
      localStorage.clear();
      history.go(0);
    }
  }
  
  if (challenge == "hideout"){
    $('#raceTimeLeft').css({
      color: "black",
    })
    inspectionTimeLeft--;
    if (inspectionTimeLeft < 5){
      $('#raceTimeLeft').css({
        color: "red",
      })
    }
    $('#raceTimeLeft').html("Inspection in: "+format(inspectionTimeLeft));
    if (inspectionTimeLeft <= 0){
      for (var h = 0; h < $('.tile').length; h++){
        var html = $('#cell'+h).html();
        
        if (html){
          alert("Government has caught you on farming ! You lose! Better luck next time");
          localStorage.clear();
          history.go(0);
        }
      }
      inspectionTimeLeft = Math.floor(Math.random () * 60);
    }
  }
  var randomPlant = [];
  for (var j = 0; j < plants.length; j++){
    if (window[plants[j]].unlocked){
      randomPlant.push(plants[j]);
    }
  }
  $('#menuQuest').hide();
  if (challenge == "quest chaos"){
    $('#menuQuest').show();
    questTimeLeft--;
    $('#questTimeLeft').html(format(questTimeLeft));
    $('#quest').html("Farm "+currentQuest+"<br>Collected: "+questCollected);
    var quest = currentQuest.split(" ");
    var amount = parseInt(quest[0]);
    if (questCollected >= amount){
      // new quest
      var randPlant = randomPlant[Math.floor(Math.random()*randomPlant.length)];
      var randAmount = Math.floor(Math.random()*level*10) + level * 3;
      var randTime = Math.floor((randAmount * 1.5*window[randPlant].timeToGrow) / $('.tile').length);
      currentQuest = randAmount+" "+randPlant;
      questTimeLeft = randTime;
      questCollected = 0;
      speak("New task! Collect "+randAmount+" "+randPlant);
    }
    
    if (questCollected < amount && questTimeLeft < 0){
      alert("You lost, your boss fires you due to 'laziness'.");
      localStorage.clear();
      history.go(0);
    }
  }
  
  if (challenge == "five slots"){
    var slots = $('.tile').length;
    if (slots >= 5){
      for (var kk = 5; kk < slots; kk++){
        $('#cell'+kk).remove();
      }
      $('#moreSpaceBtn').prop("disabled",true);
      $('#moreSpaceBtn').html("Maximum slots<br>reached!")
    }
  }
  
  if (challenge == "slot loss"){
    slotTime--;
    $('#raceTimeLeft').html("Lose slot in "+format(slotTime));
    if (slotTime <= 0){
      var length = $('.tile').length;
      if (length > 5){
        $('.tile').last().remove();
      }
      slotTime = 30;
    }
    if (slotTime <= 0){
      slotTime = 1;
    }
  }
  if (challenge == "no upgrades"){
    $('#menuUpgrades').hide();
  }
  
  
  setTimeout(challenges,1000);
}
challenges();

$('#debtBtn').click(function(){
  debtLeft -= money;
  money = 0;
  if (debtLeft < 0){
    alert("Well done! You have helped your grandad and successfully won the challenge!");
    localStorage.clear();
    loadTest();
  }
});

function format(number){
  var toDisplay = "";
  
  var time = number;
  
  if (time >= 3600){
    var hours = Math.floor(time/3600);
    time -= hours*3600;
    toDisplay += hours+"h ";
  }
  if (time >= 60){
    var minutes = Math.floor(time/60);
    time -= minutes*60;
    toDisplay += minutes+"min ";
  }
  toDisplay += Math.floor(time*10)/10+"sec";
  
  return toDisplay;
}

$('#menuQuest').draggable({
  containment: '#game',
});

function speak(msg){
  speechSynthesis.cancel();
  var toSay = new SpeechSynthesisUtterance(msg);
  var voices = window.speechSynthesis.getVoices();
  toSay.voice = voices[0];
  window.speechSynthesis.speak(toSay);
}
speak("");


function buyMaxEff(){
  for (var c = 0; c < 1000; c++){
    upEff($('#plant').val())
  }
}





(function(c){var b,d,a={init:function(e){d={hookScroll:true,hookDrag:true,draggable:null,pointAtInit:true,defaultDirection:"up",angleLessThanFunc:null,angleGreaterThanFunc:null,getAngleFrom:null,passAngleTo:null,xCorrection:0,yCorrection:0,pause:false,rotateFunction:"jqrotate",changeTargetTo:"low"};b=c.extend({},d,e);return this.each(function(){var g=c("body").data("pointatcount"),f=c(this).data("myid.pointat"),i=".pointat",h=this;f=+f||0;i=".pointat"+f;if(f>0){oldsettings=c(this).data("settings.pointat");c.each(b,function(j,k){if(k===d[j]){b[j]=oldsettings[j];}});c(this).data("settings.pointat",b);}else{g=+g||0;g++;i=".pointat"+g;c(this).data("settings.pointat",b);c(this).data("myid.pointat",g);c("body").data("pointatcount",g);}if(b.hookScroll){scrollnspace="scroll"+i;c(window).bind(scrollnspace,function(){a.updateRotation.apply(h);});}if(b.hookDrag){if(b.draggable!==null){c(b.draggable).bind("drag.pointat",function(){a.updateRotation.apply(h);});}else{c(h).bind("drag.pointat",function(){a.updateRotation.apply(h);});}}if(b.pointAtInit){a.updateRotation.apply(this);}});},getAngle:function(h){var g=c(this).data("settings.pointat"),e,f,i;if(!g){c.error("Method getAngle used on an element that does not have jQuery.PointAt initialized.");}h=typeof h!=="undefined"?h:g.target;if(h instanceof Array){h=h[0];}f=c(h).offset();i=0;if(g.getAngleFrom!==null){e=c(g.getAngleFrom).offset();}else{e=c(this).offset();}i=Math.atan2(((f.left+g.xCorrection)-e.left),((f.top+g.yCorrection)-e.top))*180/Math.PI;i=180-Math.ceil(i);if(g.defaultDirection==="right"){i=i-90;}else{if(g.defaultDirection==="down"){i=i-180;}else{if(g.defaultDirection==="left"){i=i-270;}else{if(g.defaultDirection==="up"){}else{i=i-parseInt(g.defaultDirection,10);}}}}if(i<0){i=360+i;}else{if(i>360){i=0+i;}}return i;},updateRotation:function(){return c(this).each(function(){var g=c(this).data("settings.pointat"),f=c(this).data("angle.pointat"),l=[],h,e,n,j,k=g.changeTargetTo,m=370;if(!g){c.error("Method updateRotation used on an element that does not have jQuery.PointAt initialized.");}if(g.target instanceof Array){if(isNaN(f)){if(g.changeTargetTo==="low"){f=361;}else{if(g.changeTargetTo==="high"){f=-1;}else{f=-1;}}}if(g.changeTargetTo==="low"){k=0;}else{if(g.changeTargetTo==="high"){k=360;}}for(j=0;j<g.target.length;j++){h=a.getAngle.apply(this,[g.target[j]]);if(m>Math.abs(k-h)){m=Math.abs(k-h);f=h;n=g.target[j];}}e=c(this).data("currentTarget.pointat");if(n!==e){l=[n,e];c(this).data("currentTarget.pointat",n);c(this).trigger("changedTarget",l);}}else{f=a.getAngle.apply(this);}if(g.pause===false){l=[c(this).data("angle.pointat"),f];c(this).trigger("beforeRotate",l);}if(g.angleLessThanFunc!==null){if(f<g.angleLessThan){g.angleLessThanFunc.apply(this);}}if(g.angleGreaterThanFunc!==null){if(f>g.angleGreaterThan){g.angleGreaterThanFunc.apply(this);}}if(g.passAngleTo!==null){if(c(g.passAngleTo).is("input")){c(g.passAngleTo).val(f);}else{c(g.passAngleTo).html(f);}}if(g.pause===false){c(this).data("angle.pointat",f);c(this)[g.rotateFunction](f);}c(this).trigger("afterRotate",l);});},destroy:function(){return this.each(function(){var f=c(this).data("settings.pointat"),e,g;if(!f){c.error("Method destroy used on an element that does not have jQuery.PointAt initialized.");}e=c(this).data("myid.pointat");g=".pointat";e=+e||0;g=".pointat"+e;c(window).unbind(g);if(f.hookDrag){if(f.draggable!==null){c(f.draggable).unbind("drag.pointat");}else{c(this).unbind("drag.pointat");}}c(this).removeData("settings.pointat");c(this).removeData("myid.pointat");});},pause:function(){return this.each(function(){var e=c(this).data("settings.pointat");if(e){e.pause=true;c(this).data("settings.pointat",e);}else{c.error("Method pause used on an element that does not have jQuery.PointAt initialized.");}});},resume:function(){return this.each(function(){var e=c(this).data("settings.pointat");if(e){e.pause=false;c(this).data("settings.pointat",e);}else{c.error("Method resume used on an element that does not have jQuery.PointAt initialized.");}});}};c.fn.pointat=function(e){if(a[e]){return a[e].apply(this,Array.prototype.slice.call(arguments,1));}else{if(typeof e==="object"||!e){return a.init.apply(this,arguments);}else{c.error("Method "+e+" does not exist on jQuery.pointat");}}};})(jQuery);



$.fn.jqrotate=function(degrees,options){var options=$.extend({animate:false},options);return this.each(function(){var $this=$(this);var oObj=$this[0];var deg2radians=Math.PI*2/360;var rad=degrees*deg2radians;var costheta=Math.cos(rad);var sintheta=Math.sin(rad);a=parseFloat(costheta).toFixed(8);b=parseFloat(-sintheta).toFixed(8);c=parseFloat(sintheta).toFixed(8);d=parseFloat(costheta).toFixed(8);$this.css({'-ms-filter':'progid:DXImageTransform.Microsoft.Matrix(M11='+a+', M12='+b+', M21='+c+', M22='+d+',sizingMethod=\'auto expand\')','filter':'progid:DXImageTransform.Microsoft.Matrix(M11='+a+', M12='+b+', M21='+c+', M22='+d+',sizingMethod=\'auto expand\')','-moz-transform':"matrix("+a+", "+c+", "+b+", "+d+", 0, 0)",'-webkit-transform':"matrix("+a+", "+c+", "+b+", "+d+", 0, 0)",'-o-transform':"matrix("+a+", "+c+", "+b+", "+d+", 0, 0)"})})};

External CSS

  1. https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/blitzer/jquery-ui.css
  2. https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css
  3. https://silvio-r.github.io/spop/dist/spop.min.css
  4. https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/css/tooltipster.css
  5. https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.css

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js
  3. https://silvio-r.github.io/spop/dist/spop.min.js
  4. https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/js/jquery.tooltipster.js
  5. https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.jquery.min.js