//COPYRIGHTED CODE! Sondre Holan Olsen 2013-2014
//DON'T STEAL MY CODE
var candy = 0;
var dollar = 0;
alarmCookie = true;

getTime1 = new Date().getTime();
getDay1 = new Date().getDay();
getMonth1 = new Date().getDate();

var candyCountEveryTime;

//Cookie ALARM
alert('We are using Cookies to save the stats. If it\'s ok for you, press OK! If not, close this page or play this game without saving!');
function rulesAlarm()
{
    ruleText = 'Welcome to Candy Clicker! Rules and tricks: -- Click the candy button to get candy. -- You can sell your candy to dollars when you have enough. -- With Dollars you can buy Randomize*, trophy and unlocks some cool things!';
    ruleTextStar = '* Randomizer is a button where you can win candy or lose candy. Sometimes you can even win over 10000 candy.';

    alarm(ruleText);
    alarm(ruleTextStar);
};

function sleep(milliSecondss){
    var startTime = new Date().getTime(); // get the current time
    while (new Date().getTime() < startTime + milliSecondss); // hog cpu
}

function news() //---------------------------------NEWS (FUNNNY)
{
    news1 = 'Columbian Ape Busted For Illegal Candy Production';
    news2 = 'PC gamer gone rage in New York streets. Surviving victim says "Noone called missing';
    news3 = 'KCF went bankrupt';
    news4 = 'Family was kidnapped. The police payed the kidnappers candy to release the family';
    news5 = 'Plane crashed in Texas. The price to fix this damage cost 4295843 candy';
    news6 = 'Man running around in New York and said "WTF"';
    news7 = 'Candy may cost more than gold in the future';
    news8 = 'People are using candy to make a happy drug';
    news9 = 'A rapist won 1 000 000 candy in Lottery';
    news10 = 'Columbian Ape busted for illegal candy production for SECOUND time';
    news11 = 'Your mother likes candy';
    news12 = '"I might be adicted to candy.." Said a random person';
    news13 = '';
    news14 = '';
    news15 = '';
    news16 = '';
    news17 = '';

     var loop = Math.floor((Math.random() * 17)+1);

     switch(loop){
         case 1:
             alert(news1);
             break;
         case 2:
             alert(news2);
             break;
         case 3:
             alert(news3);
             break;
         case 4:
             alert(news4);
             break;
         case 5:
             alert(news5);
             break;
         case 6:
             alert(news6);             
             break;
         case 7:
             alert(news7);
             break;
         case 8:
             alert(news8);
             break;
         case 9:
             alert(news9);
             break;
         case 10:
             alert(news10);
             break;
         case 11:
             alert(news11);
             break;
         case 12:
             alert(news12);             
             break;
         case 13:
             break;
         case 14:
             break;
         case 15:
             break;
         case 17:
             break;
         case 18:
             break;
     }


}

function candyClick() //------------------------------ADDS A CANDY WHEN YOU CLICK THE CANDY
{
    candy++;
    candyCountEveryTime++;
    title();
    document.getElementById("scoreCandy").innerHTML = candy;
    return candy;
}

function wipeSave() // -----------------------DELETE/WIPE THE SAVE
{
    var r = confirm("You are about to delete your save! Are you sure!? (Press OK if you are sure or press CANCEL if you don't want to wipe your save!");
    if(r == true)
    {
        setCookie('saveC', 0, 0);
        alert("SAVE DELETED");
        checkCookie();
    }else{
        return;
    }
};

function sell50() //--------------------- SELL 50 CANDY FOR 4 $
{
    if(candy >= 50)
    {
        candy = candy - 50;
        dollar = dollar + 4;

        document.getElementById("scoreCandy").innerHTML = candy;
        document.getElementById("dollar").innerHTML = dollar;

        setInterval(title(), 0);

        return candy, dollar;

    }else if(candy < 50)
    {
        alert("You don't have enough candy!");
    }else
    {
        alert("ERROR! Something went wrong! Pleas refresh this page!");
    }
};

function sell150() //---------------------SELL 150 CANDY FOR 14 $
{
	if(candy >= 150)
	{
		candy = candy - 150;
		dollar = dollar + 14;
		
		document.getElementById("scoreCandy").innerHTML = candy;
        document.getElementById("dollar").innerHTML = dollar;

        setInterval(title(), 0);

        return candy, dollar;
	}else if(candy < 150)
	{
		alert("You don't have enough candy!");
	}
}

function sell400() //---------------------SELL 40 CANDY FOR 35 $
{
	if(candy >= 400)
	{
		candy = candy - 400;
		dollar = dollar + 35;
		
		document.getElementById("scoreCandy").innerHTML = candy;
        document.getElementById("dollar").innerHTML = dollar;

        setInterval(title(), 0);

        return candy, dollar;
	}else if(candy < 400)
	{
		alert("You don't have enough candy!");
	}
}

function sell1000() //---------------------SELL 1000 CANDY FOR 90 $
{
	if(candy >= 1000)
	{
		candy = candy - 1000;
		dollar = dollar + 90;
		
		document.getElementById("scoreCandy").innerHTML = candy;
        document.getElementById("dollar").innerHTML = dollar;

        setInterval(title(), 0);

        return candy, dollar;
	}else if(candy < 1000)
	{
		alert("You don't have enough candy!");
	}
}			

function randomPrize() //---------------------LOTTERY
{
    if(dollar >= 15)
    {
        rander = Math.floor((Math.random() * 100)+ 1);
        dollar = dollar - 15;
        document.getElementById("dollar").innerHTML = dollar;

        wonText = 'Yeah You Won ';
        losText = 'No! You lost ';

        if(rander == 1)
        {
            alert(wonText + "167 Candy!");
            candy = candy + 167;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;
        }

        if(rander == 2)
        {
            alert(wonText + "4000 Candy!");
            candy = candy + 4000;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;
        }

        if(rander == 3)
        {

        }

        if(rander == 4)
        {
            alert(wonText + "400 Candy!");
            candy = candy + 400;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;
        }

        if(rander == 5)
        {

        }

        if(rander == 6)
        {
            alert(wonText + "1 Candy!");
            candy++;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;

        }

        if(rander == 7)
        {

        }

        if(rander == 8)
        {
            alert(wonText + "90 Candy!");
            candy = candy + 90;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;
        }

        if(rander == 9)
        {

        }

        if(rander == 10)
        {

        }

        if(rander == 11)
        {
            alert(wonText + "123 Candy!");
            candy = candy + 123;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;
        }

        if(rander == 12)
        {
            alert(wonText + "187 Candy!");
            candy = candy + 187;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;
        }

        if(rander == 13)
        {
            alert(wonText + "678 Candy!");
            candy = candy + 678;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;
        }

        if(rander == 14)
        {

        }

        if(rander == 15)
        {
            alert(wonText + "800 Candy!");
            candy = candy + 800;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;

        }

        if(rander == 16)
        {

        }

        if(rander == 17)
        {

        }

        if(rander == 18)
        {

        }

        if(rander == 19)
        {
            alert(wonText + "453 Candy!");
            candy = candy + 453;
            document.getElementById("scoreCandy").innerHTML = candy;
            return candy;

        }

        if(rander == 20)
        {

        }

        if(rander >= 21)
        {
           alert(news1);
        }

    }else if(dollar < 15){
           alert("You don't have enough dollars!");
        }
    return dollar;
};

function setCookie(c_name,value, value2) //------CREAT SAVE COOKIE
{
    c_value = value +" "+ value2 + ".";
    document.cookie=c_name + "=" + c_value;
};

function getCookie(c_name)// DECODE/GET COOKIE
{
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1)
    {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1)
    {
        c_value = null;
    }
    else
    {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1)
        {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start,c_end));
    }
    return c_value;
};

function checkCookie() //-----------------------LOAD SAVE/COOKIE
{
    getCookie('saveC');

    candyCheck=getCookie("saveC");


    if (candyCheck != '')
    {
         n = candyCheck.indexOf(' ');
        finalCand = candyCheck.substring(0, n);
        candy = parseInt(finalCand);
        document.getElementById('scoreCandy').innerHTML = candy;

        f = candyCheck.indexOf('.');
        finalDolla = candyCheck.substring(n, f);
        dollar = parseInt(finalDolla);
        document.getElementById('dollar').innerHTML = dollar;


    }
    setInterval(title(), 0);
    return candy, dollar;

};

function title() //----------------------------UPDATE TITLE
{
    upTit = candy;
    document.getElementById('title').innerHTML = "Candy: " + upTit;
};

function cheatMode() //------------------------CHEAT
{
  cheatInput = prompt("Enter The Cheat Command! (Always start with -  ex. -cheat fly)");
  checkIfStrek = cheatInput.indexOf('-');

 var cheatChecker1 = cheatInput.substring(0,6);
  if(cheatChecker1 == '-cheat')
  {
     var checkIfCheatCand = cheatInput.substring(7,12);
     var checkIfCheatMusic = cheatInput.substring(7,13);
	 var checkIfCheatDollar = cheatInput.substring(7,13);
     if(checkIfCheatCand == 'candy')
     {
         checkIfEnd = cheatInput.indexOf('^');
         var cheatNumberCandy = cheatInput.substring(13, checkIfEnd);

         if(cheatNumberCandy > 0)
         {
             alert("CHEAT ACTIVATED (CANDY)");
             candy = cheatNumberCandy;
             document.getElementById('scoreCandy').innerHTML = candy;
             return candy;
         }
     }else if(checkIfCheatMusic == 'music^')
     {
         alert("CHEAT ACTIVATED (MUSIC)");
         playSound('TEST.m4a');
     }else if(checkIfCheatDollar == 'dollar')
	 {
		checkIfEnd2 = cheatInput.indexOf('^');
		cheatNumberDollar = cheatInput.substring(14, checkIfEnd2);

		if(cheatNumberDollar > 0)
		{	alert("CHEAT ACTIVATED (DOLLAR)");
			dollar = parseint(cheatNumberDollar);
			document.getElementById('dollar').innerHTML = dollar;
		}
	 }
	return dollar;
  }
};

function playSound(soundfile) {
    document.getElementById("dummy").innerHTML=
        "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
};