var isDOM=(document.getElementById ? true : false);
var isIE4=((document.all && !isDOM) ? true : false);
var isNS4=(document.layers ? true : false);
var curSelVal="";
var startPos=0;
var endPos=0;
function getRef(id){
	if(isDOM)return document.getElementById(id);
	if(isIE4)return document.all[id];
	if(isNS4)return document.layers[id];
}
function getSty(id){
	if(isDOM)return document.getElementById(id).style;
	if(isIE4)return document.all[id].style;
	if(isNS4)return document.layers[id];
}
function enterHTML(str){
	var sel=document.selection;
	if(sel!=null){
		var rng=sel.createRange();
		if(rng!=null && rng.htmlText!='')rng.text=str;
	}
}
function textaction(tag){
	if(document.selection){
		// internet explorer etc.
		document.form1.dagTekst.focus();
		var str = document.selection.createRange().text;
		if(str.substring(0,3) == "["+tag+"]"){
			str = str.substring(3,(str.length - 4))
			enterHTML(str)
		}else{
			if(tag == "url") enterHTML("[" + tag + '=' + str + "]"+str+"[/"+tag+"]");
			else enterHTML("["+tag+"]"+str+"[/"+tag+"]");
		}
	} else if(window.getSelection){
		// firefox etc.
		var oldString = curSelVal;
		var newString = "";
		var finalString = "";
		var len = curSelVal.length;
		if(len > 0 && document.form1.dagTekst.value.length > 0){
			var firstPart = oldString.substring(0, startPos);
			var lastPart = oldString.substring(endPos, len);
			var i;
			for(i=startPos;i<endPos;i++){
				newString += oldString[i];
			}

			if(tag == "url") finalString = '[' + tag + '=' + newString + ']' + newString + '[/' + tag + ']';
			else finalString = '[' + tag + ']' + newString + '[/' + tag + ']';

			document.form1.dagTekst.value = firstPart + finalString + lastPart;
			curSelVal = "";
		}else{
			curSelVal = "";
		}
	}
}
function storeCursorPos(oTextarea){
	if(document.selection){
		// internet explorer etc.
		if(oTextarea.createTextRange)oTextarea.cursorPos = document.selection.createRange().duplicate();
	} else if(window.getSelection){
		// firefox etc.
		curSelVal = document.form1.dagTekst.value;
		startPos = oTextarea.selectionStart;
		endPos = oTextarea.selectionEnd;
		if(startPos == endPos) curSelVal = "";
	}
}
function insertAtCursor(oTextarea,sText){
	if(oTextarea.createTextRange && oTextarea.cursorPos){
		var oCursorPos = oTextarea.cursorPos;
		oCursorPos.text = sText;
		document.form1.dagTekst.focus();
	}else{
		oTextarea.value += sText;
		document.form1.dagTekst.focus();
	}
}
function textadd(str){
	document.form1.dagTekst.focus();
	document.form1.dagTekst.value += str
}
function alertSize(strDomain){
/*	var myWidth = 1024;
	if(typeof(window.innerWidth) == 'number'){
		//Non-IE
		myWidth = window.innerWidth;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		//IE 6+ 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		//IE 4
		myWidth = document.body.clientWidth;
	}

	// Check of het window groot genoeg is
	if(myWidth < 980)ResWarning(strDomain);*/
}
function ResWarning(strDomain){
	document.write('<div id="SizeWarning"><br/>')
	document.write('<table border="0" cellpadding="0" cellspacing="0" class="table_sizewarning"><tr>')
	document.write('<td class="box_type1_top_left"></td>')
	document.write('<td class="box_type1_top_middle"></td>')
	document.write('<td class="box_type1_top_right"></td>')
	document.write('</tr><tr><td class="box_type1_left_middle"></td><td class="warning">')

	if(strDomain == "en_"){
		document.write('<b>Warning!</b><br/>')
		document.write('myDiary is best viewed with a 1024x786 screen resolution.<br/><br/>')
		document.write('<a href="/help/setting_the_correct_screen_resolution.asp">more info</a><br/>')
	}else{
		document.write('<b>Let op!</b><br/>')
		document.write('myDiary kan het best bekeken worden met een resolutie van 1024x786.<br/><br/>')
		document.write('<a href="/help/de_juiste_scherm_resolutie_instellen.asp">meer informatie</a><br/>')
	}

	document.write('</td><td class="box_type1_right_middle"></td>')
	document.write('</tr><tr>')
	document.write('<td class="box_type1_bottom_left"></td>')
	document.write('<td class="box_type1_bottom_middle"></td>')
	document.write('<td class="box_type1_bottom_right"></td>')
	document.write('</tr></table></div>')
}
// tonen van filter opties
function toggle(object){
	if(document.getElementById){
		if(document.getElementById(object).style.visibility == 'visible')
			document.getElementById(object).style.visibility = 'hidden';
		else
			document.getElementById(object).style.visibility = 'visible';
	}else if(document.layers && document.layers[object] != null){
		if(document.layers[object].visibility == 'visible' || document.layers[object].visibility == 'show')
			document.layers[object].visibility = 'hidden';
		else
			document.layers[object].visibility = 'visible';
	}else if(document.all){
		if(document.all[object].style.visibility == 'visible')
			document.all[object].style.visibility = 'hidden';
		else
			document.all[object].style.visibility = 'visible';
	}
	return false;
}
function imgResizer(max_width,lang){
	$('img.resize').each(function(){
		$(this).css("border","none");
		$(this).css("max-width",max_width+'px');
		var width=$(this).width();
		var height=$(this).height();
		if(width > max_width){
			var ratio=(height / width);
			var new_width=max_width;
			var new_height=(new_width * ratio);
			$(this).height(new_height).width(new_width);
			$(this).hover(function(){
				if(lang == 'nl')$(this).attr("title","klik voor originele grootte");
				else $(this).attr("title","click for original size");
				$(this).css("cursor","pointer");
			});
		}
		var imgsrc=$(this).attr("src");
		$(this).wrap("<a href='" + imgsrc + "' target='_blank'></a>");
	});
}
function FontResize(n){
	var t=$('#jsFontSize');
	var f=t.css('font-size');
	t.css('font-size',(parseFloat(f)+n)+f.slice(-2));
}
function TogglePreview(){
	var input=$('#inputScreen');
	var preview=$('#previewScreen');
	if(input.is(':visible')){
		input.hide();
		preview.show().html('<img src="/images/loader.gif" alt="loading.." class="loader"/>');
		var s=$("textarea[name='dagTekst']").val().replace(new RegExp("\\n", "g"),"\r\n");
		$.post("preview.asp",{title:$("input[name='dagTitel']").val(),text:s},function(data){
			preview.html(data);
			$('#proStyles input,#proStyles select').each(function(){$(this).change()});
		});
		$('#btnPreview').val('terug naar schrijven');
	}else{
		preview.hide();
		input.show();
		$('#btnPreview').val('preview');
	}
}
function InitPreviews(){
	var s=$('#proStyles');
	if(s.length > 0){
		s.find('select[name="fontFamily"]').change(function(){
		  $('#previewScreen').css('font-family',$(this).find('option:selected').attr('title'));
		});
		s.find('select[name="fontSize"]').change(function(){
		  $('#previewScreen').css('font-size',$(this).find('option:selected').val()+'px');
		});
		s.find('select[name="fontAlign"]').change(function(){
		  $('#previewScreen').css('text-align',$(this).find('option:selected').attr('title'));
		});
		s.find('input[name="fontColor"]').change(function(){
		  $('#previewScreen').css('color',$(this).val());
		});
		s.find('input[name="fontColorTitle"]').change(function(){
		  $('#previewScreen h2').css('color',$(this).val());
		});
		s.find('input[name="bgColor"]').change(function(){
		  $('#previewScreen').css('background-color',$(this).val());
		});
	}
}
$(document).ready(function(){
	$('#zoeken input:text').click(function(){
		if(!$(this).hasClass('watermark')){
			$(this).val('');
			$(this).addClass('watermark').css('color','#000');
		}
	});
    InitPreviews();
});
