
$(function()
{
	message();
	status();
	modal();
	bookmarks();
//	blocks();
	pagelists();
	keep_alive();
	filemanager_preview();
});

/* 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;
		}
	}
	function insertAtCursor2(myField, myValue)
	{
		var field = document.getElementById(myField);
		var value = document.getElementById(myValue).value;

		//IE support
		if (document.selection)
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = value;
		}
		//MOZILLA/NETSCAPE support
		else if (field.selectionStart || field.selectionStart == '0')
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			var scrollTop = field.scrollTop;
			field.value = field.value.substring(0, startPos)
						  + value
				  + field.value.substring(endPos,field.value.length);
			field.focus();
			field.selectionStart = startPos + value.length;
			field.selectionEnd = startPos + value.length;
			field.scrollTop = scrollTop;
		} else {
			field.value += value;
			field.focus();
		}
	}

/* 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()
	{
		
		// check if modal and if so collapse header
		if(window.parent.location!=window.location)
		{
			$('body').addClass('modal');
		}
		
		$('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();
		if(path!=null)
			$("#modal iframe").attr('src',path);
		$("#modal").show();
		return false;
	}

		function setIframeAttr()
		{
			iframeSrc = $("#modal iframe").attr('src');
			iframeQuery = $.query.load(iframeSrc);
		}

		function openMedia(path,id)
		{
//			if(id!=null)
//				path = path + '&path=' + $('#'+id).attr('value');

//			var iframePath = $("#modal iframe").attr('src');
//			if(iframePath=='')
//			{
//				openModal(path);
//				return;
//			}

			pathQuery = new Querystring(path)
			var pathRoute = pathQuery.get('r');

			if(id!=null && id!=undefined)
			{
//				iframeQuery.set('field_id',id);
				pathQuery.set('path',$('#'+id).attr('value'));
			}

			openModal(pathQuery.toString());
			return;

			iframeQuery = new Querystring(iframePath);
			var iframeRoute = iframeQuery.get('r');
			
			if(iframeRoute!=pathRoute)
			{
				openModal(path);
				return;
			}

			if(pathRoute=='admin/sitemap')
			{
				openModal(null);
				return;
			}
			if(id!=null && id!=undefined)
			{
				iframeQuery.set('field_id',id);
				iframeQuery.set('path',$('#'+id).attr('value'));
			}
			
			alert(iframeQuery.toString());
			openModal(iframeQuery.toString());

		}

		function openFilemanager(id,path)
		{
			setIframeAttr();
			path = path + '&path=' + $('#'+id).attr('value');
			if(!iframeSrc)
				openModal(path);
			else
			{
				if(iframeSrc.get('r')!='admin/filemanager')
					openModal(path);
				else
					openModal(path);
			}
		}
		function openMceFilemanager(path)
		{
			setIframeAttr();
			if(!iframeSrc)
				openModal(path);
			else
			{
				if(iframeSrc.get('r')!='admin/filemanager')
					openModal(path);
				else
					openModal(path);
			}
			openModal(path);
		}
		function openSitemap(path)
		{
			setIframeAttr();
			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);
	}
	
/* preview */

function filemanager_preview()
{
	
	$('.filemanager-preview').each(function(){
		
		var scope = $(this);
		var id = scope.attr('id');
		var field_id = id.replace('filemanager_preview_','');
		
		$(scope).click(function(){
			var field_value = $('#'+field_id).val();
			$.post(admin_url+'admin/filemanager_preview',{value:field_value,id:field_id},function(data){
//				alert(data);
				var response = JSON.parse(data);
				if(response.error) return;
				if(response.window)
				{
					var win = window.open(response.url,'View');
				}
				else
				{
					$('.inner',scope.parent()).html(unescape(response.html));
					$('.inner a',scope.parent()).fancybox();
					$('.inner a:first',scope.parent()).trigger('click');
				}
			});
			return false;
		})
		
		
		
//		if(!field_value || field_value==undefined) return;
//		var html = ' <a target="_blank" href="'+media_url+field_value+'" class="icon icon-view"><span></span>View</a>';
//		scope.html(html);
//		
	})
}

function updateImageInput(field_id){
    var inp = $('#'+field_id);
    var src = inp.val();
    var img = $('img', inp.closest('div.imageinput'));
    
    
    if(inp.val().indexOf('http')==-1) //if it's a local file then load a resized version
    {
	var w = img.attr('width');
	var h = img.attr('height');
	src = site_url + 'index.php?r=image&width='+w+'&height='+h+'&crop=1&rgb=255-255-255&cache=1&path='+src;
    }
    
   // $('label span',inp.prev()).html(inp.val());
        
    img.attr('src', src);
    
    return false;
}
