How would I add a cookie function to this https://ghostbin.com/paste/xtk5q ?
In case you did not solve this as yet, you need to learn how to save a cookie and how to read it back.
Then, code something in the cookie to use as a counter or pointer to indicate what you need.
Here is a simple tutorial on using javascript with cookies: http://www.w3schools.com/js/js_cookies.asp