
/* ---------- GLOBAL SHIT ---------- */
var browser = navigator.appName;
function editableAreaContents(editableAreaName) {
	if (document.designMode) {
		// Explorer reformats HTML during document.write() removing quotes on element ID names
		// so we need to address Explorer elements as window[elementID]
		if (window[editableAreaName]) return window[editableAreaName].document.body.innerHTML;
		return document.getElementById(editableAreaName).contentWindow.document.body.innerHTML;
	} else {
		// return the value from the <textarea> if document.designMode does not exist
		return document.getElementById(editableAreaName).value;
	}
}
function button_hover(element,path) {
	if(element.src) {
		element.src = path;
	} else {
		element.style.backgroundImage = "url('"+path+"')";
	}
}
Element.extend({
	setIndicator: function() {
		this.setHTML("<div style='padding-top: 50px; text-align: center;'><img src='/system/global/images/indicator_onwhite.gif' alt='Laddar...' /></div>");
	}
});
window.addEvent('domready',function() {
	new Tips($$('.toolbar_button'), {
		showDelay: 100,
		fixed: 'true',
		offsets: {'x':0,'y':30}
	});
});
/* ---------- GLOBAL BLOG STUFF ---------- */
function dialog(message, onYes,type) {
	var screenLock = new Element('div');
	screenLock.id = 'screenLock';
	screenLock.setStyles('position: absolute; width: 100%; height: 100%; top: 0px;');
	document.body.appendChild(screenLock);
	
	
	new Fx.Scroll(window,{duration: 300}).toTop();

	var browserWidth = window.getWidth();

	var dialog = document.createElement("div");
		dialog.id = "dialog";
		dialog.style.position = "absolute";
		dialog.style.left = ((browserWidth/2)-200)+"px";
		dialog.style.top = "-77px";
		
		new Ajax("/system/blog/dialog.php?onYes="+onYes+"&type="+type, {method: 'post',postBody: 'message='+message, update: dialog}).request();
			
	document.body.appendChild(dialog);
	
	new Fx.Style(dialog, 'top',{duration: 600,transition: Fx.Transitions.expoOut}).custom((-77),100);
}
	function dialog_close() {
		if(document.getElementById("dialog")) {
			var screenLock = $('screenLock');
			document.body.removeChild(screenLock);
			var dialog = document.getElementById("dialog");
				dialog.id = "dialogOld";
				
			var top = dialog.getCoordinates().top;
			
			new Fx.Style(dialog, 'top',{duration: 300,transition: Fx.Transitions.expoIn,onComplete: function() {
				var dialogOld = document.getElementById("dialogOld");
				document.body.removeChild(dialogOld);
			}}).custom(100,(-77));
		}
	}
function save_blog(username) {
	var headline = $("headline").value;
	var preamble = $("preamble").value;
	var blog_id = $("blog_id").value;
	var keywords = $("keywords").value;
	var blog_text = editableAreaContents("blog_text");
	var published = 0;
	
	if($("publish").checked) {
		published = 1;
	}
	
	headline = headline.replace(/\&/g, "[amp]");
	preamble = preamble.replace(/\&/g, "[amp]");
	headline = headline.replace(/\'/g, "[fnutt]");
	blog_text = blog_text.replace(/\&amp\;/g, "[amp]");
	blog_text = blog_text.replace(/\&nbsp\;/g, "[nbsp]");
	blog_text = blog_text.replace(/\&lt\;/g, "[lt]");
	blog_text = blog_text.replace(/\&gt\;/g, "[gt]");
	if(!headline) {
		alert("Du måste skriva en rubrik.");
	}
	else {
		new Ajax("/system/blog/blog_interface.php",{method: 'post', postBody: "action=save_blog&headline="+headline+"&preamble="+preamble+"&blog_text="+blog_text+"&blog_id="+blog_id+"&keywords="+keywords+"&published="+published, onComplete: function(responseText) {
			if(responseText != "") {
				window.location = "http://"+responseText;
				new Ajax("/system/user/user_interface.php?action=list_months&username="+username, {method: 'get', onComplete: function(responseText) {
					$("archive_list").innerHTML = responseText;
				}}).request();
			}
		}}).request();
	}
}
function save_edited_blog(username, blog_id) {
	var headline = $("headline").value;
	var preamble = $("preamble").value;
	var keywords = $("keywords").value;

	var blog_text = editableAreaContents("blog_text");
	var published = 0;
	
	if($("publish").checked) {
		published = 1;
	}
	headline = headline.replace(/\&/g, "[amp]");
	preamble = preamble.replace(/\&/g, "[amp]");
	blog_text = blog_text.replace(/\&amp\;/g, "[amp]");
	
	if(!headline) {
		alert("Du måste skriva en rubrik.");
	}
	else {
		new Ajax("/system/blog/blog_interface.php",{method: 'post', postBody: "action=save_edited_blog&headline="+headline+"&preamble="+preamble+"&blog_text="+blog_text+"&blog_id="+blog_id+"&keywords="+keywords+"&published="+published, onComplete: function(responseText) {
			if(responseText != "") {
				window.location = "http://"+responseText;
				new Ajax("/system/user/user_interface.php?action=list_months&username="+username, {method: 'get', onComplete: function(responseText) {
					$("archive_list").innerHTML = responseText;
				}}).request();
			}
		}}).request();
	}
}
function delete_blog(username, user_id, blog_url) {
	new Ajax("/system/blog/blog_interface.php",{method: 'post', postBody: "action=delete_blog&user_id="+user_id+"&blog_url="+blog_url, onComplete: function(responseText) {
		if(responseText=="true") {
			window.location="/"+username;
		} else {
			alert(responseText);
		}
	}}).request();
}
///////////////
/// BETA //////
////////////////////////////
function create_blog(blogtype,username) {
	$('blog_content').setIndicator();
	new Ajax("/system/blog/blog_interface.php?action=create_empty_blog", {method: 'get', onComplete: function(responseText) {
		if(blogtype == "textblog") {
			textblog(username, responseText);
		}
		if(blogtype == "photoblog") {
			photoblog(username, responseText);
		}
	}}).request();
}

function textblog(username, blog_id) {
	new Ajax("/system/blog/blog_interface.php?action=write_blog", {method: 'get', onComplete: function(responseText) {
		if(responseText=="true") {
			if($("blog_comments")) {
				$("blog_comments").innerHTML = "";
			}
			show_blog_editor(username, blog_id);
		}
	}}).request();
}
function show_blog_editor(username, blog_id) {
	new Ajax("/system/blog/blog_interface.php?action=show_blog_editor", {method: 'post', postBody: "username="+username+"&blog_id="+blog_id,onComplete: function(responseText) {
			$('blog_content').setHTML();
			create_inline_blog_box(responseText);
	}}).request();
	
}
function show_presentation_editor(username) {
	$('blog_content').setIndicator();
	new Ajax("/system/blog/blog_interface.php?action=show_presentation_editor&username="+username, {method: 'post', postBody: "username="+username,onComplete: function(responseText) {
			$('blog_content').setHTML();
			create_inline_blog_box(responseText);
	}}).request();
	
}
////////////////////
/// SLUT BETA
//////////////////
function create_inline_blog_box(content) {
	var table_top 		= "<table cellpadding='0px' cellspacing='0px'><tr><td class='innershadow_top_left'></td><td class='innershadow_top'></td><td class='innershadow_top_right'></td></tr><tr><td class='innershadow_left'></td><td class='innershadow_content' id='inner_box_content'>";
	var table_right 	= "</td><td class='innershadow_right'></td></tr>"
	var table_bottom 	= "<tr><td><img style='vertical-align: top;' src='/system/global/images/innershadow_corner_leftbottom.gif' alt='' /></td><td class='innershadow_bottom'></td><td><img style='vertical-align: top;' src='/system/global/images/innershadow_corner_rightbottom.gif' alt='' /></td></tr>"

	var table_tontent 	= document.createElement("div");
		table_tontent.innerHTML = table_top+table_right+table_bottom;
		
	var blog_content = $("blog_content");

	blog_content.appendChild(table_tontent);
	$("inner_box_content").setHTML(content);
}
/* ---------- TEXTBLOG ---------- */

function edit_text_blog(username, blog_id, headline, preamble, keywords) {
	new Ajax("/system/blog/blog_interface.php?action=edit_blog", {method: 'get', onComplete: function(responseText) {
		if(responseText=="true") {
			$("blog_content").innerHTML = "";
			$("blog_comments").innerHTML = "";
			show_blog_editor('blog_text',	400, 500,'[bold][italic][underlined][align-left][justify][align-center][align-right]<br />[edit-area]','font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:Black;', username, blog_id, headline, preamble, keywords); 
		}
	}}).request();
}

/* ---------- PHOTOBLOG ---------- */
function photoblog(username, article_id) {
	new Ajax("/system/blog/blog_interface.php?action=create_photoblog", {method: 'post', postBody: "article_id="+article_id,onComplete: function(responseText) {
		$("blog_content").innerHTML = "";
		if($("blog_comments")) {
			$("blog_comments").innerHTML = "";
		}
		create_inline_blog_box(responseText);
	}}).request();
}
function save_photoblog(article_id) {
	var headline = $("headline").value;
	var tags = $("tags").value;
	var published = 0;
	//var source = $("uploaded_image").src;
	var description = $("photoblog_image_description").value;
	headline = headline.replace(/\'/g, "[fnutt]");
	headline = headline.replace(/\&/g, "[amp]");
	
	description = description.replace(/\&/g, "[amp]");
	
	if($("published").checked) {
		published = 1;
	}
	if(!headline) {
		alert("Du måste skriva en rubrik.");
	}
	else if($("photoblog_content").innerHTML == "") {
		alert("Du måste lägga till minst 1 bild. Klicka på knappen \"Lägg till bild\" om du laddat upp en bild.");
	}
	else {
		new Ajax("/system/blog/blog_interface.php?action=save_photoblog", {method: 'post', postBody: "article_id="+article_id+"&published="+published+"&title=&description="+description+"&headline="+headline+"&keywords="+tags,onComplete: function(responseText) {
			window.location = "http://"+responseText;
		}}).request();
	}
	
}
function edit_photo_blog(blog_id) {
	new Ajax("/system/blog/blog_interface.php?action=edit_photo_blog", {method: 'post', postBody: 'blog_id='+blog_id, onComplete: function(responseText) {
		if(responseText!="false") {
			$("blog_content").innerHTML = "";
			if($("blog_comments")) {
				$("blog_comments").innerHTML = "";
			}
			create_inline_blog_box(responseText);
		}
	}}).request();
}
function new_picture(article_id) {
	var source = $("uploaded_image");
	var description = $("photoblog_image_description");
	
	if($("uploaded_image")) {
	
		new Ajax("/system/blog/blog_interface.php?action=photoblog_new_item", {method: 'post', postBody: "article_id="+article_id+"&source="+source.src+"&title=&description="+description.value, onComplete: function() {
			var photoblog_input_image = $("photoblog_image");
			
			new Fx.Style("image_container","height",{duration: 300, onComplete: function() {
				$("photoblog_image").disabled = "";
				$("photoblog_image").value = "";
				description.value = "";
				source.remove();
				new Ajax("/system/blog/blog_interface.php?action=photoblog_show_latest_item", {method: 'post', postBody: "article_id="+article_id, onComplete: function(responseText) {
					var data = responseText.split(";");
					
					var image = document.createElement("img");
						image.src = data[0];
						image.style.position = "absolute";
						image.style.zIndex = "-99";
						
					var div = document.createElement("div");
					div.appendChild(image);
					$("photoblog_content").appendChild(div);
					
					var image_width = image.width;
					var image_height = image.height;
						image.style.width = "360px";
						image.style.height = "0px";
						
					var divideValue = image_width/360;
					var newHeight = image_height/divideValue;
					
					var descriptionP = document.createElement("p");
					var description = document.createTextNode(data[1]);
						descriptionP.appendChild(description);
					
						div.style.textAlign = "center";
						div.appendChild(descriptionP);
						
						image.style.position = "relative";
						image.style.zIndex = "+99";
					
					
					new Fx.Styles(image,{duration: 600}).start({
						'width':[0,360],
						'height':[0,newHeight]
					});
				}}).request();
			}}).start(160,0);
		}}).request();
	}
	else {
		alert("Du måste välja en bild i din nuvarande ruta");
	}
}
function on_change() {
	document.upload_form.submit();
	new Fx.Style("image_container","height",{duration: 300}).start(0,25);
	$("image_container").innerHTML = "<img src='/system/global/images/indicator_onblack.gif' alt='Laddar...' />";
}
function edit_photo_blog_new_picture() {
	var edit_photoblog_images = $$(".edit_photoblog_image");
	
	var id = edit_photoblog_images.pop().id.substr(-2);
	id = id.replace(/_/g,' ');
	id = parseInt(id);
	id += 1;

	var image_source = $("uploaded_image").src;
	var description = $("photoblog_image_description").value;
	new Ajax("/system/blog/blog_interface.php?action=edit_photo_blog_show_item", {method: "post", postBody: "id="+id+"&image_source="+image_source+"&description="+description, onComplete: function(responseText) {
		var div = document.createElement("div");
			div.id = "photoblog_content_container_"+id;
			div.style.overflow = "hidden";
			div.style.height = "0px";
			div.innerHTML = responseText;
		$("photoblog_content").appendChild(div);
			var imageHeight = div.getFirst().getCoordinates().height;
			if(imageHeight <= 130) {
				imageHeight = 130;
			}
			new Fx.Style("image_container","height",{duration: 300, onComplete: function() {
				new Fx.Style(div,'height').start(0,imageHeight,{onComplete: function() {
					new Fx.Scroll(window).toBottom();
				}});
			}}).start(160,0);
			$("photoblog_image").value = "";
			$("photoblog_image").disabled = "";
			$("photoblog_image_description").value = "";
	}}).request();
}
function save_edited_photoblog(article_id) {
	var edit_photoblog_images = $$(".edit_photoblog_image");
	var length = edit_photoblog_images.length;
	
	if($("published").checked) {
		published = 1;
	}
	var headline	= $("headline").value
	var keywords	= $("tags").value
	var xml = "<photoblog>\n";
	var i = 0;
	
	headline = headline.replace(/\'/g, "[fnutt]");
	headline = headline.replace(/\&/g, "[amp]");
	
	
	while(i<length) {
		var description = $("photoblog_content_image_description_"+i).value;
		description = description.replace(/\&/g, "[amp]");
		xml += "\t<item>\n\t\t<source>"+$("photoblog_content_image_"+i).src+"</source>\n\t\t<title></title>\n\t\t<description>"+description+"</description>\n\t</item>\n";
		i++;
	}
	xml += "</photoblog>";
	if(!headline) {
		alert("Du måste skriva en rubrik.");
	}
	else {
		new Ajax("/system/blog/blog_interface.php?action=save_edited_photoblog",{method: "post", postBody: "article_id="+article_id+"&published="+published+"&headline="+headline+"&keywords="+keywords+"&xml="+xml, onComplete: function(responseText) {
			window.location = "http://"+responseText;
		}}).request();
	}
}
function delete_photoblog_item(id) {
	var container_to_remove = $("photoblog_content_container_"+id);
	$("photoblog_content").removeChild(container_to_remove);
}

function showLargeBlogImage(src,element) {
	var leftPosition = $(element).getCoordinates().left;
	var topPosition = $(element).getCoordinates().top;
	
	var thumbWidth = $(element).getCoordinates().width;
	var thumbHeight = $(element).getCoordinates().height;

	var image = document.createElement("img");
		image.src = src;
		image.style.position = "absolute";
		image.style.top = "0px";
		image.style.zIndex = "-10";
		
		document.body.appendChild(image);
		
	var image_width = image.width;
	var image_height = image.height;
	var browser_width = window.getWidth();
				
		image.style.top = -image.height+"px";
		image.style.cursor = "pointer";
		image.onclick = function() {
			closeLargeBlogImage(leftPosition,topPosition,image_width,browser_width,image_height,thumbWidth,thumbHeight);
		}
		if(document.getElementById("blogImage")) {
			var image2 = document.getElementById("blogImage");
			document.body.removeChild(image2);
		}
		image.id = "blogImage";
		image.style.zIndex = "1";

		var effect = new Fx.Styles(image,{duration: 300});
		effect.start({
			'top': [topPosition,topPosition-((image_height/4))],
			'left': [leftPosition,((browser_width/2)-(image_width/2))],
			'width': [thumbWidth,image_width],
			'height': [thumbHeight,image_height]
		});
}
function closeLargeBlogImage(leftPosition,topPosition,image_width,browser_width,image_height,thumbWidth,thumbHeight) {
		var image = document.getElementById("blogImage");
			
		var effect = new Fx.Styles(image,{duration: 300, onComplete: function() {
			document.body.removeChild(image);
		}});
		effect.start({
			'top': [topPosition-((image_height/4)),topPosition],
			'left': [((browser_width/2)-(image_width/2)),leftPosition],
			'width': [image_width,thumbWidth],
			'height': [image_height,thumbHeight]
		});
}
function hoverInBlogImage(element) {
	//new Fx.Style($(element),"opacity",{}).set(0.9);
}
function hoverOutBlogImage(element) {
	//new Fx.Style($(element),"opacity",{}).set(1);
}
/* ---------- BLOG ARCHIVE ---------- */
function toggle_archive_month(year, month, numRows) {
	var thisElementNewHeight = numRows * 20;
	if(month.substr(2,1)) month = "0"+month;

	var month_array = $$(".month_content_active");
		if(month_array[0]) {
			month_array[0].className = "month_content";
			new Fx.Style(month_array[0],'height').start(0);
		}
	
	var month = $("month_"+month+"_"+year);
		month.className = "month_content_active";
		new Fx.Style(month,'height').start(thisElementNewHeight);
}
function toggle_archive_year(year, numRows) {
	var thisElementNewHeight = numRows * 20;

	var years_array = $$(".year_content_active");
		if(years_array[0]) {
			years_array[0].className = "year_content";
			new Fx.Style(years_array[0],'height').start(0);
		}
	
	var year = $("year_"+year);
		year.className = "year_content_active";
		new Fx.Style(year,'height').start(thisElementNewHeight);
}
/* ---------- PANEL ---------- */
function create_panel(content, title) {
	var panel = document.createElement("div");
		panel.id = "panel";
	
	var panel_header = document.createElement("div");
		panel_header.id = "panel_header";
		panel_header.className = "panel_header";
		var panel_header_content = document.createElement("div");
			panel_header_content.className = "panel_header_content";
			panel_header_content.innerHTML = title;
		var panel_header_delete = document.createElement("img");
			panel_header_delete.src = "/system/global/images/panel/panel_close.png";
			panel_header_delete.onclick = function() { close_panel(); };
			panel_header_delete.className = "panel_header_delete";
	
	var panel_content = document.createElement("div");
		panel_content.className = "panel_content";
		panel_content.innerHTML = content;
			
	var panel_footer = document.createElement("img");
		panel_footer.src = "/system/global/images/panel/panel_footer.png";
		panel_footer.className = "panel_footer";
		
	panel_header.appendChild(panel_header_content);
	panel_header.appendChild(panel_header_delete);		
	panel.appendChild(panel_header);
	panel.appendChild(panel_content);
	panel.appendChild(panel_footer);
	
	document.body.appendChild(panel);
	
	new Drag.Move($("panel"),{handle: $("panel_header")});
}
function close_panel() {
	var panel = $("panel");
	document.body.removeChild(panel);
}

/* ---------- COMMENTS ---------- */
function show_blogs_with_unread_comments(username) {
	new Ajax("/system/blog/blog_interface.php?action=show_blogs_with_unread_comments&username="+username, {method: 'get', onComplete: function(responseText) {
		create_panel(responseText,"Olästa kommentarer");
		var myAcc = new Accordion($$('.panel_elements'), $$('.panel_togglers'), {opacity: false, alwaysHide: true});
	}}).request();
}
function show_blog(blog_url) {
	new Ajax("/system/blog/blog_interface.php?action=show_blog&blog_url="+blog_url, {method: 'get', update: $("content"),onComplete: function() {
		new Fx.Scroll(window).toBottom();
	}}).request();
}

function submit_blog() {
	if($("headline").value != "") {
		document.blog_form.submit();
	} else {
		alert("Du har inte skrivit en rubrik");
	}

}
function submit_presentation() {
	document.presentation_form.submit();
}