/*
PAGE-ONLINE v2.0 JavaScript
Written by: Rogier van Onna
Date: 07-03-2009
Date modified: 07-03-2009
*/

var emoticons = 0; /* Aantal emoticons gebruik in bericht */
var slide_interval; /* Snelheid van diashow (fotoboek) */
var sliding = false; /* Diashow actief ja/nee (fotoboek) */
var website_url = 'http://www.berkieborduurcreaties.nl/';
var adminBoxDrag = false;

function fullSize(source) {
	var img = new Image(); 
	img.src = source; 
	var src_url = website_url + '/fullsize_photo.php?src=' + img.src;
	var pLeft = (screen.width-img.width)/2;
	var pTop = (screen.height-img.height)/2;
	window.open(src_url,'FullSize','left=' + pLeft + ',top=' + pTop + 'scrollbars=0,resizable=0,toolbars=0,location=0,menubar=0,status=0');
}


/* formulier-veldwaardes bepalen */
function setInputValue(element, val) {

	/* Tekstvelden */
	if(	element.type == "textarea" || 
		element.type == "text" ||
		element.type == "password") {
		element.value = val;
	}
	
	/* Checkbox */
	if(element.type == "checkbox") {
		element.checked = true;
	}
	
	/* Selectiebox / Opties */
	if(element.length) {
		for(var i = 0; i < element.length; i++) {
			if(element[i].value == val) {
				if(element.type == "select-one") {
					element[i].selected = true;
				} else if(element[i].type == "radio") {
					element[i].checked = true;
				}
			}
		}
	}
	
}

/* CSS class bepalen */
function setClassName(element, classname) {
	
	element.className = classname;
	/* element.setAttribute("class", classname); */
	
}

/* SubMenu openen */
function openMenu(menu) {
	document.getElementById(menu).style.visibility = 'visible';
}
/* SubMenu sluiten */
function closeMenu(menu) {
	document.getElementById(menu).style.visibility = 'hidden';
}

/* Tag toevoegen in textarea (geselecteerde tekst)
function textarea_tag(tag) {
	if((tag != 'none') || (tag != '')) {
		var selected_text = document.selection.createRange();
		var selected_str = selected_text.text;
		if(selected_str != ''){ selected_text.text = '<' + tag + '>' + selected_str + '</' + tag + '>'; } else { alert('U dient een tekst te selecteren!'); }
	}
}	
*/	

/* Emoticon toevoegen aan textarea */
function emoticon(input) {	
	if(emoticons<9) {
		document.message_form.message.value += input + ' ';
		document.message_form.message.focus();
		emoticons++;
	}
}

function showDiv(div) {
	if(document.getElementById(div)!=null) {
		if(document.getElementById(div).style.visibility=='hidden') {
			document.getElementById(div).style.visibility = 'visible';
			document.getElementById(div).style.position = 'static';
		} else {
			document.getElementById(div).style.visibility = 'hidden';
			document.getElementById(div).style.position = 'absolute';
			document.getElementById(div).style.left	= '0';
			document.getElementById(div).style.top = '-800';
		}
	}
}
		
/* Fotoboek functies */
function slide(delay) { 
	clearInterval(slide_interval); 
	slide_interval = setInterval("photo_select('next')",delay); 
	document.getElementById('slideshow_status').innerHTML = '(slideshow uit)';
	sliding = true;
}

function stopslide() { 
	clearInterval(slide_interval); 
	document.getElementById('slideshow_status').innerHTML = '(slideshow aan)';
	sliding = false;
} 
	
function photo_select(action) {
	var image1 = document.getElementById('photo1');
	var n = image1.value;
	
	if(action=='next') 		{ n++; }
	if(action=='previous') 	{ if(n==0) { n = PHOTOS.length-1; } else { n--; }}
	if(action=='first') 	{ n = 0; }
	if(action=='last') 		{ n = PHOTOS.length-1; }
	if(n>=PHOTOS.length) 	{ n = 0; }
		
	image1.value = n;
				
	image1.src = PHOTOS[n][0];
	/* alert('width: ' + PHOTOS[n][3] + ' height: ' + PHOTOS[n][4]); */
	if(PHOTOS[n][3] != '')
		image1.width = PHOTOS[n][3];
	if(PHOTOS[n][4] != '')
		image1.height = PHOTOS[n][4];
	
	if(document.getElementById('photonr'))
		document.getElementById('photonr').innerHTML = (n+1) + ' van ' + PHOTOS.length;
	if(document.getElementById('photoinfo'))
		document.getElementById('photoinfo').innerHTML = PHOTOS[n][2] + '&nbsp;-&nbsp;' + PHOTOS[n][1];
}

/* Wachtwoord functies */
function calculatePasswordStrength(password) {
	var strength = 0;
	var length = password.length;
	
	if (length > 6) { strength++; }
	if (length > 8) { strength++; } 
	
	if (password.match(/[a-z]/i)) { strength++; } // Kleine letters
	if (password.match(/[a-z][A-Z]|[A-Z][a-z]/)) { strength++; } // Hoofd en kleine letters
	if (password.match(/[0-9]/g)) { strength++; } // Nummers
	if (password.match(/[!,@,#,$,%,^,*,?,_,~,-]/g)) { strength++; } // Speciale tekens
	
	return strength;
}

function getPasswordStrength(password) {
	var img1 = new Image(); 
	var img2 = new Image(); 
	var img3 = new Image(); 
	img1.src = website_url + '/admin/img/icon_accept.gif'; 
	img2.src = website_url + '/admin/img/icon_avarage.gif'; 
	img3.src = website_url + '/admin/img/icon_warning.gif'; 
		
	var password_strength = calculatePasswordStrength(password);
	var label = document.getElementById('password_strength');
	
	if(password_strength == 5 || password_strength == 6) {
		label.innerHTML = '<img src="' + website_url + '/admin/img/icon_accept.gif">&nbsp;Wachtwoordsterkte:&nbsp;Goed';
	} else if(password_strength == 4 || password_strength == 3) {
		label.innerHTML = '<img src="' + website_url + '/admin/img/icon_avarage.gif">&nbsp;Wachtwoordsterkte:&nbsp;Gemiddeld';
	} else {
		label.innerHTML = '<img src="' + website_url + '/admin/img/icon_warning.gif">&nbsp;Wachtwoordsterkte:&nbsp;Slecht';
	}
	
}

function checkRepeatPassword(password) {
	
	if(password == document.forms.password_form.new_password.value) {
		document.getElementById('repeat_password').innerHTML = '<img src="' + website_url + '/admin/img/icon_accept.gif">';
	}
	
}

function Copy2Clipboard(input) {
  	input.select()
  	if(window.clipboardData) { 
	  	var r=clipboardData.setData('Text',input.value); return 1; 
  	}
 	else return 0
}

function showPreview(photo, img_w, img_h) {
	
	// Gooi oude element weg
	if (document.getElementById('preview') != null) {
		document.body.removeChild(document.getElementById('preview'));
	}
	
	// Variable definiëren
	var divTag = document.createElement("div");
	var imgTag = document.createElement("img");
	var brTag = document.createElement("br");
	var aTag = document.createElement("a");
	
	// Hyperlink maken om venster te sluiten
	aTag.href = 'javascript:hidePreview()';
	aTag.innerHTML = 'venster&nbsp;sluiten';
	
	// Image tag maken voor fotoweergave
	imgTag.src = photo;
	
	if(img_w != null) { imgTag.width = img_w; }
	if(img_h != null) { imgTag.height = img_h; }

	// Schermgrootte bepalen
	var height = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
	var width = (window.innerWidth) ? window.innerWidth : document.body.clientWidth;
	
	// Afmetingen bepalen
	var rw,rh,r;
	if(imgTag.width > width || imgTag.height > height) { 
		rw = imgTag.width/width; 
		rh = imgTag.height/height;
		if(rw>=rh) { r=rw; } else { r=rh; }
		imgTag.width = (imgTag.width / r)-100;
		imgTag.height = (imgTag.height / r)-100;
	}
	
	// Scrollafstand bepalen
	var scrolledX, scrolledY;
	if( self.pageYOffset ) {
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
	} else if( document.body ) {
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	} 
	
	// Venter aanmaken
 	divTag.style.left = (scrolledX + (width / 2)) - (imgTag.width / 2);
  	divTag.style.top = (scrolledY + (height / 2)) - ((imgTag.height / 2) + 50); 
  	divTag.style.position = 'absolute'; 
	divTag.id = 'preview';
	divTag.className = 'preview';
	divTag.appendChild(imgTag);
	divTag.appendChild(brTag);
	divTag.appendChild(aTag);
	document.body.appendChild(divTag);
	
	// Zorgen dat je het venster kunt slepen
	Drag.init(document.getElementById("preview"));

}

function hidePreview() {
	document.body.removeChild(document.getElementById('preview'));
}

function setAdminboxPosition() {
	var adminbox = document.getElementById('adminBox');
	adminbox.style.top = readCookie('cookie_adminbox_top');
	adminbox.style.left = readCookie('cookie_adminbox_left');
}

function saveAdminboxPosition() {
	var adminbox = document.getElementById('adminBox');
	//alert('top: '+adminbox.style.top+' left:'+adminbox.style.left);
	createCookie('cookie_adminbox_top',adminbox.style.top,365);
	createCookie('cookie_adminbox_left',adminbox.style.left,365);
}

function dragAdminOptions() {
	
	// Zorgen dat je het venster kunt slepen
	if(!adminBoxDrag) {
		var adminbox = document.getElementById('adminBox');
		adminbox.style.cursor = 'move';
		Drag.init(document.getElementById('adminBox'));	
		adminBoxDrag = true;
	}
	
}

function showAdminOptions() {
	document.getElementById('adminBox').style.visibility = 'visible';
	Effect.SlideDown('adminBox');
}

function hideAdminOptions() {
	document.getElementById('adminBox').style.visibility = 'hidden';
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	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);
}
