gridcols=32;
gridrows=32;
plotmode=0;
plotulx=0;
plotuly=0;
growplotw=0;
growploth=0;
cpr=0;
cellsize=16;
plottop=0;
plotbgcolor="#0ea90a";
paypal=0;


function showpuzzle () {
	document.getElementById('ads').style.display="none";
	
}

function startplot (cellobj,x,y) {
	//Allow user to select upper left corner cell
	if (plotmode==1) {
		if (!cellobj.style.backgroundColor) {
			cellobj.style.backgroundColor=plotbgcolor;
			plotmode=0;
			plotulx=x;
			plotuly=y;
			//position price window
			document.getElementById('plotprice').style.left=cellobj.style.left;
			document.getElementById('plotprice').style.top=cellobj.style.top;
			
			plottop=parseFloat(cellobj.style.top.replace("px","")); 
			document.getElementById('plotprice').style.width=cellsize+"px";
			document.getElementById('plotprice').style.height=cellsize+"px";
			//write initial price
			document.getElementById('plotprice').innerHTML="<span class='pricetag'>$"+cpr+"</span>"; 
			//update instructions and display plot growth arrows
			if (!paypal) {
				document.getElementById('plotnavinstr').innerHTML="Use the arrows to expand or shrink your plot or <a href='sponsor.php'>reset and start over</a>";
				document.getElementById('plotnav').style.visibility="visible";
			}
			//render initial price tag
			growplot('none');
			//update buy ad submit form with start cell position and enable image submit form
			if (!paypal) {
				document.forms['buyad'].elements['startrow'].value=y;
				document.forms['buyad'].elements['startcol'].value=x;
				document.forms['buyad'].elements['adimg'].disabled=0;
			}
		}
		else {
			alert('This plot is already occupied -- select an available (grey) cell');	
		}
		return;
	}
}

function growplot (direction) {
	//Expand or shrink plot area based on input
	//alert('grow plot '+direction);
	oldgrowplotw=growplotw;
	oldgrowploth=growploth;
	switch (direction) {
		case('right'):
			growplotw++;
			break;
		case ('left'):
			growplotw--;
			break;
		case ('down'):
			growploth++;
			break;
		case ('up'):
			growploth--;
			break;
		case ('none'):
			growploth=growploth;
			growplotw=growplotw;
	}
	
	//alert('expand plot to width '+growplotw+' and height '+growploth);
	if (growplotw<0) { growplotw=0; } //cannot shrink to left of ul cell
	if (growploth<0) { growploth=0; } //cannot shrink above ul cell

	//check that growth direction does not run beyond edge of grid
	if (plotulx+growplotw>gridcols) {
		growplotw=oldgrowplotw;
		return; //bail
	}
	if (plotuly+growploth>gridrows) {
		growploth=oldgrowploth;
		return; //bail
	}
	
	//check that growth direction does not run into occupied territory
	if (direction=='right') { //test proposed right edge
		for (y=plotuly; y<=plotuly+oldgrowploth; y++) {
			if (admatrix['r'+y+'c'+(plotulx+growplotw)]) {
				growplotw=oldgrowplotw; //hit wall on right, rollback
			}
		}
	}
	
	if (direction=='down') { //test proposed bottom edge
		for (x=plotulx; x<=plotulx+oldgrowplotw; x++) {
			if (admatrix['r'+(plotuly+growploth)+'c'+x]) {
				growploth=oldgrowploth; //hit wall at bottom, rollback
			}
		}
	}
	
	//grow: iterate through new matrix and apply cell colors
	for (y=plotuly; y<=plotuly+growploth+1; y++) {
		for (x=plotulx; x<=plotulx+growplotw+1; x++) {
			if ((x<=plotulx+growplotw) && (y<=plotuly+growploth)) {
				document.getElementById('r'+y+'c'+x).style.backgroundColor=plotbgcolor;
			}
			else {
				//remove color from outer cell only if shrinking left or up
				if (direction=='up') {
					if (x<=plotulx+growplotw) {
						if (!admatrix['r'+y+'c'+x]) { //unless cell occupied
							document.getElementById('r'+y+'c'+x).style.backgroundColor='';
						}
					}
				}
				if (direction=='left') {
					if (y<=plotuly+growploth) {
						if (!admatrix['r'+y+'c'+x]) { //unless cell occupied
							document.getElementById('r'+y+'c'+x).style.backgroundColor='';
						}
					}
				}				
			}
		}
	}
	//expand price window to grown plot
	document.getElementById('plotprice').style.width=(growplotw+1)*cellsize+"px";
	document.getElementById('plotprice').style.height=(growploth+1)*cellsize+"px";
	//write realtime cost
	realcost=(((growploth+1)*(growplotw+1))*cpr);
	totalpxw=(growplotw+1)*cellsize;
	totalpxh=(growploth+1)*cellsize;
	fsize=cellsize;
	if (totalpxw>totalpxh) { fsize=totalpxh; }
	if (totalpxw<=totalpxh) { fsize=totalpxw; }
	fsize=fsize*.25;
	if (fsize<12) { fsize=12; }
	//Update realtime price box, proportion font size, and vertical center
	document.getElementById('plotprice').innerHTML="<span id='plotpricetag' class='pricetag'>$"+realcost+"</span>";
	ChangeFontSize('pricetag',fsize);
	document.getElementById('plotpricetag').style.top=((totalpxh/2)-(fsize/2))-1+"px";
	document.getElementById('plotpricetag').style.visibility='visible';
	if (!paypal) {
		document.forms['buyad'].elements['price'].value=realcost;
		//Update ad pixel dimensions in buy box
		document.getElementById('addimensions').innerHTML="Dimensions:<br/>"+totalpxw+"px wide x "+totalpxh+"px high";
		document.forms['buyad'].elements['adwidth'].value=totalpxw;
		document.forms['buyad'].elements['adheight'].value=totalpxh;
		//Update ad dimensions by cell count
		document.forms['buyad'].elements['cellsw'].value=growplotw;
		document.forms['buyad'].elements['cellsh'].value=growploth;
	}
}



function goplot (flag,cp) {
	plotmode=1;
	cpr=cp;
}

function regrowplot (y,x,w,h) {
	startplot(document.getElementById('r'+y+'c'+x),x,y);
	for ($i=0;$i<w;$i++) { growplot('right'); }
	for ($i=0;$i<h;$i++) { growplot('down'); }
}

function openad (obj,puzzleid,adid,url) {
	//Open ad window
	heightpct=0.90;
	nudge=25;
	if ((url!='none') && (url!='')) {
		document.getElementById('adnewwin').href=url; //set open in new window link
	} else { 
		document.getElementById('adnewwin').href='#'; 
		url="sponsornolink.html";
	}
	document.getElementById('adcontentframe').style.height=(browserSize('height')*heightpct)+"px"; //set height to % of browser
	//center vert
	document.getElementById('adcontent').style.top=((browserSize('height')-(browserSize('height')*heightpct))/2)-nudge+"px"; 
	document.getElementById('adcontent').style.left=(browserSize('width')-(browserSize('width')*heightpct))/2+"px"; 
	//center horz
	document.getElementById('adcontentframe').src=url;
	document.getElementById('adcontent').style.visibility="visible";

	//replace ad on grid with puzzle piece
	imgobj=obj.id.replace("adlink","adimg");
	obj.onclick="";
	document.getElementById(imgobj).src="scripts/getslice.php?puzzleid="+puzzleid+"&adid="+adid;
	document.getElementById(imgobj).title="What is it?";
	document.getElementById(imgobj).alt="What is it?";
}

function openfaq (url) {
	//Open FAQ window	
	heightpct=0.90;
	nudge=25;
	document.getElementById('faqframe').style.height=(browserSize('height')*heightpct)+"px"; //set height to % of browser
	//center vert
	document.getElementById('faqwin').style.top=((browserSize('height')-(browserSize('height')*heightpct))/2)-nudge+"px"; 
	document.getElementById('faqwin').style.left=(browserSize('width')-(browserSize('width')*heightpct))/2+"px"; 
	//center horz
	document.getElementById('faqframe').src=url;
	document.getElementById('faqwin').style.visibility="visible";
}

function updatecheckouttotal (cktotal) {
	//ckstr=cktotal.toString();
	ckstr=parseFloat(cktotal).toFixed(2);
	document.getElementById('realtotal').innerHTML="$"+ckstr;
}


/* FONT SIZE CHANGER FUNCTIONS */
function ChangeFontSize(classname,newsize) {
  //var currentSize = getClassProperty(classname,"fontSize");
  //currentSize = currentSize.replace("pt","");
  //var newSize = parseInt(currentSize) + amt;
  //if (newSize >= minSize && newSize <= maxSize) {
    changeClassProperty(classname,"fontSize",newsize + "px");
  //}
}

// get the value of a property of a CSS Rule
function getClassProperty(sClassName,sProperty) {
  sClassName="."+sClassName;
  var sheets = document.styleSheets;
  var rules;
  var styleObj;
  for (i=0;i< sheets.length; i++) {
    rules=sheets[i].cssRules || sheets[i].rules;
    for (var j=0; j<rules.length; j++) {
      if (rules[j].selectorText && rules[j].selectorText==sClassName) {
        styleObj=rules[j].style;

        return styleObj[sProperty];
      }
    }
  }
}

// set the value of a property of a CSS Rule
function changeClassProperty(sClassName,sProperty,sValue) {
  sClassName="."+sClassName;
  var sheets = document.styleSheets;
  var rules;
  var styleObj;
  for (i=0;i< sheets.length; i++) {
    rules=sheets[i].cssRules || sheets[i].rules;
    for (var j=0; j<rules.length; j++) {
      if (rules[j].selectorText && rules[j].selectorText==sClassName) {
        styleObj=rules[j].style;
        styleObj[sProperty]=sValue;
        break;
      }
    }
  }
}

/*RETRIEVE BROWSER WINDOW SIZE*/
function browserSize(dim) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
	if (dim=='height') { return myHeight; }
	if (dim=='width') { return myWidth; }
}