//var baseURL = "http://localhost:8888/puzzlejet/";
var baseURL = "http://puzzlejet.com/";

var counter = 1;
var limit = 500;
function addCwInput(olName)
{
     if (counter == limit)
	 {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else
	 {
          var newLi = document.createElement('li');
          newLi.innerHTML = "<span class='actionLink deleteSmall' onClick='removeCwWord(this.parentNode);'>Remove</span><p>Word<br/><input type='text' name='words[]' value='' maxlength='45' size='35' id='word' onblur='updateCrossword();' /></p><p>Hint<br/><input name='hints[]' maxlength='45' size='35' id='hint' ></input></p>";
          parent = document.getElementById(olName);
		  parent.insertBefore(newLi, parent.firstChild);
          counter++;
     }
}

function removeCwWord(w)
{
	Effect.Fade(w, { afterFinish: function(){
		w.remove(); 
		updateCrossword();
		}});	
}

function refreshCwPreview()
{
	document.puzzlePreview.TPlay("_level0");
	//document.puzzlePreview.remove();
	
}


function writeCwMaker(puzzleId, w, h)
{
		  
   document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+w+'" height="'+h+'">');
   document.write('<param name="movie" value="'+baseURL+'swf/crossword_maker.swf">');
   document.write('<param name="quality" value="best">');
   document.write('<param name="FlashVars" value="puzzleId='+puzzleId+'">');   

   document.write('<embed name="puzzlePreview" id="puzzlePreview" src="'+baseURL+'swf/crossword_maker.swf" FlashVars="puzzleId='+puzzleId+ '" quality="best" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>');
   document.write('</object>');
}


function writeCwPuzzle(puzzleId, w, h)
{
		  
   document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+w+'" height="'+h+'">');
   document.write('<param name="movie" value="'+baseURL+'/swf/crossword.swf">');
   document.write('<param name="quality" value="best">');
   document.write('<param name="salign" value="t">');
   document.write('<param name="FlashVars" value="puzzleId='+puzzleId+'">');   

   document.write('<embed src="'+baseURL+'swf/crossword.swf" FlashVars="puzzleId='+puzzleId+ '" quality="best" salign="t" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>');
   document.write('</object>');
}

function writeCwPuzzlePrint(puzzleId, w, h)
{
		  
   document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+w+'" height="'+h+'">');
   document.write('<param name="movie" value="'+baseURL+'/swf/crossword_print.swf">');
   document.write('<param name="quality" value="best">');
   document.write('<param name="FlashVars" value="puzzleId='+puzzleId+'">');   

   document.write('<embed src="'+baseURL+'swf/crossword_print.swf" FlashVars="puzzleId='+puzzleId+ '" quality="best" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>');
   document.write('</object>');
}

function writeCwPuzzlePrintAnswers(puzzleId, w, h)
{
		  
   document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+w+'" height="'+h+'">');
   document.write('<param name="movie" value="'+baseURL+'/swf/crossword_print.swf">');
   document.write('<param name="quality" value="best">');
   document.write('<param name="FlashVars" value="puzzleMode=answer&puzzleId='+puzzleId+'">'); 
   document.write('<embed src="'+baseURL+'swf/crossword_print.swf" FlashVars="puzzleMode=answer&puzzleId='+puzzleId+ '" quality="best" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>');
   document.write('</object>');
}

function writeCwPreview(puzzleId, w, h)
{
		  
   document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+w+'" height="'+h+'">');
   document.write('<param name="movie" value="'+baseURL+'swf/crossword.swf">');
   document.write('<param name="quality" value="best">');
   document.write('<param name="FlashVars" value="puzzleId='+puzzleId+'&puzzleMode=preview">');   

   document.write('<embed name="puzzlePreview" id="puzzlePreview" src="'+baseURL+'swf/crossword.swf" FlashVars="puzzleId='+puzzleId+ '&puzzleMode=preview" quality="best" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>');
   document.write('</object>');
}


function updateCrossword()
{
	
	var form_data = $('puzzleFrm').serialize();
	var url = baseURL+'puzzles/update_xml';
	
	new Ajax.Request(url, {
	        method: 'post',
	        parameters: form_data,
	        onCreate: function() {
	            
	        },
	        onSuccess: function() {
	            refreshCwPreview();
	
	        }
	    });	
}


function addToFavorites(p, u)
{
	if($('favLink').className == 'favInactive')
	{
		var form_data = "user_id="+u+"&puzzle_id="+p; 
		var url = baseURL+'puzzles/add_to_favorites';
	
		new Ajax.Request(url, {
	        method: 'post',
	        parameters: form_data,
	        onCreate: function() {
	            
	        },
	        onSuccess: function() {
	            $('favLink').className = 'favActive';
	
	        }
	    });
	}
	else
	{
		var form_data = "user_id="+u+"&puzzle_id="+p; 
		var url = baseURL+'puzzles/remove_from_favorites';
	
		new Ajax.Request(url, {
	        method: 'post',
	        parameters: form_data,
	        onCreate: function() {
	            
	        },
	        onSuccess: function() {
	            $('favLink').className = 'favInactive';
	
	        }
	    });
	}
	
}


function approvePuzzle(p, c)
{
	if(c)
	{
		var form_data = "puzzle_id="+p; 
		var url = baseURL+'admin/puzzles/approve';
	
		new Ajax.Request(url, {
	        method: 'post',
	        parameters: form_data,
	        onCreate: function() {
	            
	        },
	        onSuccess: function() {	            
	
	        }
	    });
	}
	else
	{
		var form_data = "puzzle_id="+p; 
		var url = baseURL+'admin/puzzles/disapprove';
	
		new Ajax.Request(url, {
	        method: 'post',
	        parameters: form_data,
	        onCreate: function() {
	            
	        },
	        onSuccess: function() {	            
	
	        }
	    });
	}
	
}


function toggleSBPuzzles(t)
{
	if(t == "recent")
	{
		$('popularTab').className = 'sidebarTabInActive';
		$('recentTab').className = 'sidebarTabActive';
		$('sidebarRecent').show();
		$('sidebarPopular').hide();		
	}
	else
	{
		$('recentTab').className = 'sidebarTabInActive';
		$('popularTab').className = 'sidebarTabActive';
		$('sidebarRecent').hide();
		$('sidebarPopular').show();
	}
	
	
}






