function confirm_remove(id){
	var confirmation=window.confirm("Are you sure you want to delete subscriber #"+id);
	if (confirmation){
		window.location = base_url+"admin/newsletter/delete_subscriber/"+id;
	}
}

function confirm_newsletter_remove(id){
	var confirmation=window.confirm("Are you sure you want to delete newsletter #"+id);
	if (confirmation){
		window.location = base_url+"admin/newsletter/delete_newsletter/"+id;
	}
}

function preview_newsletter(id){
	//window.open(base_url+"admin/newsletter/preview/"+id, "", "width=800px, height=800px, resizable=yes, scrollbars=yes");
	var preview_window = window.open('','',"width=800px, height=800px, resizable=yes, scrollbars=yes");
	if($('html')){
		//var content = $('html').value;
		var content = tinyMCE.get('html').getContent();
	}else{
		var content = m_html.getCode();
	}
	
	preview_window.document.write(content);
	
	preview_window.focus();
}

function launch_tutorial(){
	window.open(base_url+"admin/tutorials/newsletter", "", "width=800px, height=800px, resizable=yes, scrollbars=yes");
}

window.onload = function(){
	if($('subscribe_date')){
		var dpck	= new DatePicker({
			relative	: 'subscribe_date',
			language	: 'en',
			disableFutureDate: false,
			dateFormat 	: [ ["yyyy", "mm", "dd"], "-" ]
		});
	}
	
	if($('post_date')){
		var dpck	= new DatePicker({
			relative	: 'post_date',
			language	: 'en',
			disableFutureDate: false,
			dateFormat 	: [ ["yyyy", "mm", "dd"], "-" ]
		});
	}
	
}

function watch_search_input(elm){
	if(elm.value == 'last_export' || elm.value == 'subscribe_date'){
		$('search').value = "YYYY-MM-DD";
	}
}

function get_code(){
	$("html").value = m_html.getCode();
	return true;
}

function copy_newsletter(){
	var newsletter = m_html.getCode();
	copy(newsletter);
}

var open_img = base_url+'public/images/layout/arrow_down.gif';
var closed_img = base_url+'public/images/layout/arrow.gif';
function init_newsletter_archive(newsletters){
	newsletters.years.each(function(y_obj, y_index){
		var year_div = Builder.node('div', {
			id: "year_"+y_index,
			className: 'year'
		},[
			Builder.node('div', {
				className: 'year_name'
			},[
				Builder.node('img', {
					src: closed_img
				}),
				y_obj.name
			])
		]);
		
		var months_div = Builder.node('div', {className: "month_container", style: "display: none;"});
				
		y_obj.months.each(function(m_obj, m_index){
			var month_div = Builder.node('div', {
				id: "month_"+m_index,
				className: 'month'
			},[
				Builder.node('div', {
					className: 'month_name'
				},[
					Builder.node('img', {
						src: closed_img
					}),
					m_obj.name
				])
			]);
			
	
			var newsletters_div = Builder.node('div', {className: "newsletter_container", style: "display: none;"});
			
			m_obj.newsletters.each(function(n_obj, n_index){
				var newsletter_div = Builder.node('div', {
					id: "newsletter_"+n_index,
					className: 'newsletter'
				});
				newsletter_div.innerHTML = n_obj.link;
				
				if(n_obj.summary != undefined && n_obj.summary != ''){
					var summary_div = Builder.node('div', {
						className: 'summary'
					}, [n_obj.summary]);
					newsletter_div.appendChild(summary_div);
				}
				
				newsletters_div.appendChild(newsletter_div);
				
				
				/*links = newsletter_div.select('a');
				if(n_obj.summary != undefined && n_obj.summary != ''){
					new Tip(links[0], n_obj.summary, {
						className: "products",
						title: n_obj.post_date,
						effect: "appear",
						hook: {target: "topRight", tip: "topLeft"},
						offset: {x:5, y:0}
					});
					
				}*/
				
			});
			month_div.appendChild(newsletters_div);
			
			months_div.appendChild(month_div);
				
		});
		
		year_div.appendChild(months_div);
		
		$('archives_div').appendChild(year_div);
	});
	
	$$('div.year_name').each(function(elm, index) {
		elm.observe('click', function(event){
			toggle_year(Event.element(event));
		});
	});
	
	$$('div.month_name').each(function(elm, index) {
		elm.observe('click', function(event){
			toggle_month(Event.element(event));
		});
	});
	
	//toggle newest newsletter
	var years = $$('div.year_name');
	var months = $$('div.month_name');
	toggle_year(years[0]);
	toggle_month(months[0]);
	delete years;
	delete months;
	
	//make latest newsletter div
	var latest = newsletters.years[0].months[0].newsletters[0];
	
	var newsletter_div = Builder.node('div', {
		className: 'newsletter'
	});
	newsletter_div.innerHTML = latest.link;
	
	if(latest.summary != undefined && latest.summary != ''){
		var summary_div = Builder.node('div', {
			className: 'summary'
		}, [latest.summary]);
		newsletter_div.appendChild(summary_div);
	}
	
	$('latest_newsletter').appendChild(newsletter_div);
}


function toggle_year(year_elm){
	$$('div.year').each(function(elm, index) {
		var month_div = elm.select('div.month_container');
		var arrow = month_div[0].adjacent('img');
		arrow = arrow[0];
		if(elm != year_elm.up('div.year')){
			//month_div[0].hide();
			arrow.src = closed_img;
			Effect.BlindUp(month_div[0], {duration: 0.4});
		}else{
			if(month_div[0].getStyle('display') == 'none'){
				arrow.src = open_img;
				Effect.BlindDown(month_div[0],{duration: 0.5});
			}else{
				arrow.src = closed_img;
				Effect.BlindUp(month_div[0], {duration: 0.4});
			}
		}
	});
}

function toggle_month(month_elm){
	$$('div.month').each(function(elm, index) {
		var news_div = elm.select('div.newsletter_container');
		var arrow = news_div[0].adjacent('img');
		arrow = arrow[0];
		if(elm != month_elm.up('div.month')){
			arrow.src = closed_img;
			Effect.BlindUp(news_div[0], {duration: 0.4});
		}else{
			if(news_div[0].getStyle('display') == 'none'){
				arrow.src = open_img;
				Effect.BlindDown(news_div[0], {duration: 0.5});
			}else{
				arrow.src = closed_img;
				Effect.BlindUp(news_div[0], {duration: 0.4});
			}
		}
	});
}