
$(function()
{
	message();
	status();
	modal();
	bookmarks();
//	blocks();
	pagelists();
	keep_alive();
});

/* toggle submit */

	function toggleUpdate(which,value,action)
	{
		$('#submit').val(value);
		$('#item_edit').attr('action',action);
		switch(which.value)
		{
			case 'update':
				$('#item_edit').attr('target','');
				break;
			case 'new':
				$('#item_edit').attr('target','');
				break;
			case 'preview':
				$('#item_edit').attr('target','_blank');
				break;
		}
	}
	function toggleSubmit(which,value)
	{
		if(which.checked==true)
			$('#submit').val('Save as new');
		else
			$('#submit').val(value);
	}

/* date range */

	function dateRange(input)
	{
		var dateMin = null;
		var dateMax = null;

		switch(input.id)
		{
			case "date_from":
				dateMax = $("#date_to").datepicker("getDate");
				break;

			case "date_to":
				dateMin = $("#date_from").datepicker("getDate");
				break;
		}


		return {
			minDate: dateMin,
			maxDate: dateMax
		};

	}

/* insert at cursor */

	function insertAtCursor(myField, myValue)
	{
		//IE support
		if (document.selection) {
			myField.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
		}
		//MOZILLA/NETSCAPE support
		else if (myField.selectionStart || myField.selectionStart == 0) {
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			myField.value = myField.value.substring(0, startPos)
			+ myValue
			+ myField.value.substring(endPos, myField.value.length);
		} else {
			myField.value += myValue;
		}
	}

/* keep alive */

	var alive_timer = null;

	function keep_alive()
	{
		alive_timer = setTimeout("stay_alive()",1000*300);
	}

	function stay_alive()
	{
		$.post(admin_url+'admin/content/keep_alive');
		keep_alive();
	}

/* page lists */

	function pagelists()
	{

		$('.content_list').hover(
		function(){
			$('div',this).addClass('active');
		},
		function(){
			$('div',this).removeClass('active');
		});
		
	}

/* blocks */

	function blocks()
	{
		
		$('dl').append('<div class="clear"></div>');
		
		$('.block').each(function(){

			$(this).append('<div class="clear"></div>');
			
			// current setup
			var expand = false;
			
			if($('dl',this).hasClass('off'))
				expand = true;	
				
			// get cookie and overide if necesary
			var id = $(this).attr('id');
			if(id)
			{
				var cookie = readCookie(id);
				if(cookie)
				{
					expand = (cookie=='expand') ? false : true;
					if(!expand)
						$('dl',this).show();
					else
						$('dl',this).hide();
				}
			}
			
			// link class
			var classname = 'collaspe';
			if(expand)
				classname = 'expand';
			$('h2:first',this).wrapInner('<a rel="'+id+'" href="#" class="'+classname+'"></a>');
			$('h2:first a',this).prepend('<span></span>');
			
			// link click
			$('h2:first a',this).click(function(){
				
				// id
				var id = $(this).attr('rel');
				var classname = $(this).attr('class');
				
				// save cookie
				createCookie(id,classname);
												
				$('dl',$(this).parent().parent()).toggle();
				$(this).toggleClass('expand');
				$(this).toggleClass('collaspe');
				return false;
			});			
			
		})

		

	}

/* modal window */

	function modal()
	{
		$('#modal .header a').click(closeModal);
		$('#overlay').click(closeModal);
	}
	function openModal(path)
	{
		$("#overlay").show();
		$("#modal iframe").attr('src',path);
		$("#modal").show();	
	}
	function openFilemanager(id,path)
	{
		path = path + '&path=' + $('#'+id).attr('value')
		openModal(path);	
	}
	function closeModal()
	{
		$("#overlay").hide();	
		$("#modal").hide();			
	}
	
/* tinymce */	
	
	var tinyMCE_window;
	var tinyMCE_field;
		
	function return_tinyMCE_window()
		{
			return tinyMCE_window;	
		}
	function return_tinyMCE_field_name()
		{
			return tinyMCE_field_name;	
		}	
	
/* status */
	
	function status()
	{
		$('a.status').click(function(){
			path = $(this).attr('href');
			$(this).load(path,function(data){
				data = data.split('||');
				$(this).html(data[1]);
				$(this).attr('href',data[0]);
			})
			return false;
		});
	}
	
/* message */

	function message()
	{
		$('#msg')
			.show()
			.animate({ opacity: 1.0 }, 2000 )
			.switchClass( 'yellow', 'blue', 2000)
			.animate({ height: 'hide', opacity: 'hide', paddingBottom:0, paddingTop:0, marginBottom:0 }, 1000);		
	}

/* you sure */

	function youSure()
	{
	
		var check = confirm('Are you sure?');
		if(check)
			{
				return true;
			}			
		else
			{
				return false;	
			}
	
	}


/* bookmarks */	

	function bookmarks()
	{
		$('.bookmark-delete').click(function(){
			var id = $(this).attr('rel');
			$.post(admin_url+'admin/bookmarks/remove',{id:id});
			$(this).parent().remove();
		})
		$('.bookmark').click(function(){
			//alert(admin_url+'admin/bookmarks/add');
			$.post(admin_url+'admin/bookmarks/add',{url:location.href,title:document.title},function(data){
				$('.bookmark').before(data);
				bookmarks();
			});			
		});
	}


/* checkbox */
	
	function checkBox(input,clss)
		{
			if(input.checked==true)
				checkAll(clss);	
			else
				uncheckAll(clss);
		}
		
	function checkAll(which)
		{
			$('input[@type=checkbox].'+which).attr('checked', 'checked');
			return false;
		}
	
	function uncheckAll(which)
		{
			$('input[@type=checkbox].'+which).removeAttr('checked');
			return false;
		}		
		
/* cookies */
	
	function createCookie(name,value,days) {
		var expires = "";
		if (!days) {
			var date = new Date();
			date.setTime(date.getTime()+(365*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}