// JavaScript Document

function loadView(tool,place){
	
					 
	$('#pageholder').load('/components/'+tool+'/views/'+place);
	return false;
	
}


function addImage(){
	$("#dialog").dialog('destroy');
	
	$("#dialog").dialog({
			title: "Image Selection",
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			height:400,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				
			}
		});
	
	$("#dialog").dialog('open');
	$("#addimage").show();
	$("#addtext").hide();
	$("#deleteitem").hide();
	$("#previewadv").hide();
	
	$('#addimage').load("/components/adventures/views/images.php?fetch=true");
	

	$("#dialog").dialog('option', 'width', 600);
	$("#dialog").dialog('option', 'height', 400);
	$("#dialog").dialog('option', 'position', 'center');

}


function addCover(){
	$("#dialog").dialog('destroy');
	
	$("#dialog").dialog({
			title: "Cover Selection",
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			height:400,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				
			}
		});
	
	$("#dialog").dialog('open');
	$("#addimage").show();
	$("#addtext").hide();
	$("#deleteitem").hide();
	$("#previewadv").hide();
	
	$('#addimage').load("/components/adventures/views/images.php?fetch=true");
	

	$("#dialog").dialog('option', 'width', 600);
	$("#dialog").dialog('option', 'height', 400);
	$("#dialog").dialog('option', 'position', 'center');

}




function deleteRow(row, id,type){
	$("#dialog").dialog('destroy');
	$("#dialog").dialog({
			title: "Delete Item",
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			height:140,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				
			}
		});
	$("#addimage").hide();
	$("#addtext").hide();
	$("#deleteitem").show();
	$("#previewadv").hide();
	$("#dialog").dialog('open');
	$("#dialog").dialog('option', 'width', 300);
	$("#dialog").dialog('option', 'height', 200);
	$("#dialog").dialog('option', 'position', 'center');
	$("#dialog").dialog('option', 'buttons', {
						
		'Delete this Item': function() {
			$(this).dialog('close');
			
			$.post("/scripts/php/deletedata.php", { id: id, type: type } );
			//alert(row);
			$('#'+row).hide('highlight',{},1000,null);
		},
		Cancel: function() {
			$(this).dialog('close');
		}
	});

	return false;
}

function deleteCover(){
	$("#dialog").dialog('destroy');
	$("#dialog").dialog({
			title: "Remove Cover",
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			height:140,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				
			}
		});
	$("#addimage").hide();
	$("#addtext").hide();
	$("#deleteitem").show();
	$("#previewadv").hide();
	$("#dialog").dialog('open');
	$("#dialog").dialog('option', 'width', 300);
	$("#dialog").dialog('option', 'height', 200);
	$("#dialog").dialog('option', 'position', 'center');
	$("#dialog").dialog('option', 'buttons', {
						
		'Delete this Item': function() {
			$(this).dialog('close');
			
			$('#coverdel').hide();
			//alert(row);
			$('#coverimg').fadeOut(function () {						   
				$('#coverimg').remove();					   
			});
		},
		Cancel: function() {
			$(this).dialog('close');
		}
	});

	return false;
}

function deleteRow2(row, id,type){
	if(window.confirm('Are you sure you want to delete this image?')){
			
			$.post("/scripts/php/deletedata.php", { id: id, type: type } );
			//alert(row);
			$('#'+row).hide('highlight',{},1000,null);

	}

	return false;
}

function selectImage(user,file){
	
	if(document.getElementById('cover'))
	{
		var items = '<img id="coverimg" src="/advmedia/'+user+'/'+file+'" />';
		document.getElementById('cover').value = file;
		$('#coverholder').get(0).innerHTML = items;
		$("#dialog").dialog('close');
		$('#coverdel').show();
		runDraggable();
	} else {
		var items = '<li class="ui-widget-content ui-corner-tr ui-state-default ui-sortable"><img src="/advmedia/'+user+'/'+file+'" /><a href="javascript:;"  title="Delete this item" class="ui-icon ui-icon-trash">Delete</a></li>';
		$('#gallery').append(items).end().fadeIn();
		$("#dialog").dialog('close');
		runDraggable();
	}	
}

function addText(){
	$("#dialog").dialog('destroy');
	
	$("#dialog").dialog({
			title: "Text Editor",			
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			height:355,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				
			}
		});
	$("#dialog").dialog('option', 'buttons', {
				'Save this Text': function() {
					$(this).dialog('close');
					var textblob = document.getElementById('textblock').value;
					textblob = nl2br(textblob);
					var items = '<li class="ui-widget-content ui-corner-tr ui-state-default ui-sortable">'+textblob+'<a href="javascript:;" title="Delete this item" class="ui-icon ui-icon-trash">Delete</a> <a href="javascript:;" title="Edit this item" class="ui-icon ui-icon-pencil">Edit</a><br clear="all"></li>';
					$('#gallery').append(items).end().fadeIn();
					runDraggable();

					
				},
				Cancel: function() {
					$(this).dialog('close');
				}
	});
	
	$("#dialog").dialog('open');
	$("#addimage").hide();
	$("#addtext").show();
	$("#deleteitem").hide();
	$("#previewadv").hide();
	$('#addtext').load("/components/adventures/views/text.php?fetch=true");
	

	$("#dialog").dialog('option', 'width', 550);
	$("#dialog").dialog('option', 'height', 350);
	$("#dialog").dialog('option', 'position', 'center');
	
}


function editRow(tool,id){
	$('#pageholder').load("/components/"+tool+"/views/add.php?fetch=true&id="+id);
	/*					 
	$('#pageholder').fadeOut('normal', function(){						 
		$('#pageholder').load("/components/<?=$_GET['page']?>/views/add.php?fetch=true",'',function(){
			$('#pageholder').fadeIn('slow');																		  
		})
	})
	*/
	return false;
};

function preview(id){
	$("#dialog").dialog('destroy');
	
	
	
	$("#dialog").dialog({
			title: "Preview Adventure",
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				
			}
		});
	
	$("#dialog").dialog('open');
	$("#addimage").hide();
	$("#addtext").hide();
	document.getElementById("previewframe").src = "/cover/id/"+id;
	$("#previewadv").show();
	$("#deleteitem").hide();
	$('#addtext').load("/components/adventures/views/text.php?fetch=true");
	

	$("#dialog").dialog('option', 'width', 410);
	//$("#dialog").dialog('option', 'height', 480);
	$("#dialog").dialog('option', 'position', 'center');
	
}


function showWeek() {
	
	$('#pageholder').load("/components/calendar/views/week.php?fetch=true");					  
	return false;
}

function publish(id) {
	var setting = document.getElementById('pub'+id).innerHTML;
	if(setting == 'Un-Publish')
	{
		document.getElementById('pub'+id).innerHTML = 'Publish';
		$.post("/scripts/php/publishstory.php", { id: id, type: 'unpublish' } );
	} else if(setting == 'Publish')
	{
		document.getElementById('pub'+id).innerHTML = 'Un-Publish';
		$.post("/scripts/php/publishstory.php", { id: id, type: 'publish' } );
	}
}

function addTool(tool){
	$('#pageholder').load("/components/"+tool+"/views/add.php?fetch=true");
	/*					 
	$('#pageholder').fadeOut('normal', function(){						 
		$('#pageholder').load("/components/<?=$_GET['page']?>/views/add.php?fetch=true",'',function(){
			$('#pageholder').fadeIn('slow');																		  
		})
	})
	*/
	return false;
};

function addScene(id){
	$('#pageholder').load("/components/adventures/views/scene.php?fetch=true&advid="+id);
	/*					 
	$('#pageholder').fadeOut('normal', function(){						 
		$('#pageholder').load("/components/<?=$_GET['page']?>/views/add.php?fetch=true",'',function(){
			$('#pageholder').fadeIn('slow');																		  
		})
	})
	*/
	return false;
};

function makeScene(){
	//alert("working");
	document.getElementById('scene').value = document.getElementById('gallery').innerHTML;
	document.addscene.submit();
	/*					 
	$('#pageholder').fadeOut('normal', function(){						 
		$('#pageholder').load("/components/<?=$_GET['page']?>/views/add.php?fetch=true",'',function(){
			$('#pageholder').fadeIn('slow');																		  
		})
	})
	*/
	return false;
};

function detailTool(tool,id){
	$('#pageholder').load("/components/"+tool+"/views/detail.php?fetch=true&id="+id);
	/*					 
	$('#pageholder').fadeOut('normal', function(){						 
		$('#pageholder').load("/components/<?=$_GET['page']?>/views/add.php?fetch=true",'',function(){
			$('#pageholder').fadeIn('slow');																		  
		})
	})
	*/
	return false;
};

function listTool(tool){
	$('#pageholder').load("/components/"+tool+"/views/list.php?fetch=true");					  
	
	/*
	$('#pageholder').fadeOut('normal', function(){						 
		$('#pageholder').load("/components/<?=$_GET['page']?>/views/list.php?fetch=true",'',function(){
			$('#pageholder').fadeIn('slow');																		  
		})
	})
	*/
	return false;
};

function addDecision(id,advid)
{
	var title = document.getElementById('stitle').value;
	
	var dropdown = document.getElementById('sceneid');
    var index = dropdown.selectedIndex;
    var actionid = dropdown[index].value;
	
	$.post("/scripts/php/addDecision.php", { id: id, advid: advid, title: title, actionid: actionid }, function (){
		$('#decisions').load('/scripts/php/addDecision.php?id='+id);																											 
		
	});	
}

function runDraggable() {
		
		var recycle_icon = '<a href="javascript:;" title="Recycle this image" class="ui-icon ui-icon-refresh">Recycle image</a>';
							
		function deleteImage($item) {
			$("#dialog").dialog('destroy');
			$("#dialog").dialog({
					autoOpen: false,
					bgiframe: true,
					resizable: false,
					height:140,
					modal: true,
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					},
					buttons: {
						
					}
				});
			$("#addimage").hide();
			$("#addtext").hide();
			$("#deleteitem").show();
			$("#previewadv").hide();
			$("#dialog").dialog('open');
			$("#dialog").dialog('option', 'width', 300);
			$("#dialog").dialog('option', 'height', 200);
			$("#dialog").dialog('option', 'position', 'center');
			$("#dialog").dialog('option', 'buttons', {
								
				'Delete this Item': function() {
					$(this).dialog('close');
				
					$($item).fadeOut(function () {
											   
						$item.remove();					   
						});
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			});
		
			return false;
		}

		// image recycle function
		var trash_icon = '<a href="javascript:;" title="Delete this image" class="ui-icon ui-icon-trash">Delete image</a>';
		function recycleImage($item) {
			$item.fadeOut(function() {
				$item.find('a.ui-icon-refresh').remove();
				$item.css('width','100%').append(trash_icon).find('img').css('height','72px').end().appendTo($gallery).fadeIn();
			});
		}
		
		function editText($item){
			$("#dialog").dialog('destroy');
			
			$("#dialog").dialog({
					autoOpen: false,
					bgiframe: true,
					resizable: false,
					height:355,
					modal: true,
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					},
					buttons: {
						
					}
				});
			$("#dialog").dialog('option', 'buttons', {
						'Save this Text': function() {
							$(this).dialog('close');
							var textblob = document.getElementById('textblock').value;
							textblob = nl2br(textblob);
							//alert(textblob);
							var items = textblob + '<a href="javascript:;" title="Delete this item" class="ui-icon ui-icon-trash">Delete</a> <a href="javascript:;" title="Edit this item" class="ui-icon ui-icon-pencil">Edit</a><br clear="all">';
							$item.get(0).innerHTML = items;
							runDraggable();
		
							
						},
						Cancel: function() {
							$(this).dialog('close');
						}
			});
			
			var starttext = $item.get(0).innerHTML;
			//alert($item.get(0).innerHTML);
			
			starttext = starttext.replace('<a href="javascript:;" title="Delete this item" class="ui-icon ui-icon-trash">Delete</a> <a href="javascript:;" title="Edit this item" class="ui-icon ui-icon-pencil">Edit</a><br clear="all">','');
			starttext = replaceAll(starttext, '<br>', "\n");
			starttext =  escape(starttext);
			$("#dialog").dialog('open');
			$("#addimage").hide();
			$("#addtext").show();
			$("#previewadv").hide();
			$("#deleteitem").hide();
			$('#addtext').load("/components/adventures/views/text.php?fetch=true&text="+starttext);
			
		
			$("#dialog").dialog('option', 'width', 550);
			$("#dialog").dialog('option', 'height', 350);
			$("#dialog").dialog('option', 'position', 'center');
			
		}
			
							// image preview function, demonstrating the ui.dialog used as a modal window
						
// resolve the icons behavior with event delegation
		
		$('ul > li').click(function(ev) {
			var $item = $(this);
			var $target = $(ev.target);

			if ($target.is('a.ui-icon-trash')) {
				deleteImage($item);
			} else if ($target.is('a.ui-icon-zoomin')) {
				viewLargerImage($target);
			} else if ($target.is('a.ui-icon-pencil')) {
				editText($item);
			}

			//return false;
		});
				
}

function nl2br(text){
	text2 = escape(text);
	if(text2.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(text2.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(text2.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	}
	if (typeof(re_nlchar) == 'undefined') 
	{
		return text;
	} else {
		return unescape( text2.replace(re_nlchar,'<br>') );
	}
}

function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}

function setFirst(id, adv)
{

	void(d2=document);
	void(el=d2.getElementsByName('check'));
	for(i=0;i<el.length;i++)
	{
		void(el[i].checked=0);
	}
	
	document.getElementById('first'+id).checked = true;
	$.post("/scripts/php/setfirst.php", { id: id, advid:adv } );
	
}

escapeHTML = function (stringid) {                                       
        return(                                                                 
            stringid.replace(/&/g,'&amp;').                                         
                replace(/>/g,'&gt;').                                           
                replace(/</g,'&lt;').                                           
                replace(/"/g,'&quot;')                                         
        );                                                                     
    };
