$(document).ready(function(){

	// add larger thumbnail preview when hovering thumbs 
	$(".thumb").hover(function() {
		
		// projekte brauchen positionierung
		$(this).find('img').addClass("projekte").stop()
		
		// remove class of previously hovered thumbs
		$(".thumb").find('img').removeClass("hover");
		
		// add class for hovered img
		$(this).find('img').addClass("hover").stop()
		
		  // animate (zoom-in)
			.animate({
				marginTop: '0px', 
				marginLeft: '0px', 
				top: '-5%', 
				left: '-5%', 
				width: '110%',
				height: '110%'
			}, 200);
		
		} , function() {
			
		// animate (zoom-out)
		$(this).find('img').stop().animate({
				marginTop: '0', 
				marginLeft: '0',
				top: '0', 
				left: '0', 
				width: '100%', 
				height: '100%'
			}, 400);
		
		});
	
});

function jCaroussel(icon) {
	
	var iconPath = determinePath(icon.src);
	var root = determinePath(iconPath);
	var updateUrl = root + "/addon/jCaroussel.php";
	
	$.post(updateUrl, {
		key: 'jCaroussel',
		value: icon.id },
		function(msg) {
			if(msg) alert(msg);
		}
	);
}

function blank(obj) {
	obj.target = '_blank';
}

function changeTo(img) {
	document.getElementById("bild").src = img.src;
}

/* editor */

function containerOf(icon) {
	divs = icon.parentNode.getElementsByTagName("div");
	for(i=0; i < divs.length; i++) {
		if(divs[i].className == 'text') {
			return divs[i];
		}
	}
}

function next(icon) {
	icons = icon.parentNode.getElementsByTagName("img");
	for(i=0; i < icons.length; i++) {
		if(icon == icons[i]) {
			return icons[i+1];
		}
	}
}

function previous(icon) {
	icons = icon.parentNode.getElementsByTagName("img");
	for(i=0; i < icons.length; i++) {
		if(icon == icons[i]) {
			return icons[i-1];
		}
	}
}

function edit(icon) {
	icon.style.display = 'none';
	next(icon).style.display = 'inline';
	$(containerOf(icon)).fresheditor('edit', true);
	containerOf(icon).innerHTML = containerOf(icon).innerHTML.replace(">www.",">http://www.");
}

function determinePath(path) {
	pathEnd = path.lastIndexOf('/');
	return path.substr(0,pathEnd);
}

function save(icon) {
	icon.style.display = 'none';
	document.body.style.cursor = 'wait';
	
	iconPath = determinePath(icon.src);
	root = determinePath(iconPath);
	updateUrl = root + "/admin/update.php";
	
	$(containerOf(icon)).fresheditor("save",
		function (id, parsedHtml) {
			$.post(updateUrl, {
				pid: document.body.id,
				section: id,
				content: parsedHtml},
				function(msg) {
					if(msg.indexOf('CONTENTEDITABLE_ERROR: update') > -1 &&
						 msg.indexOf('MYSQL_ERROR (') > -1)  {
						alert(msg);
					} else {
						containerOf(icon).innerHTML = msg;
					}
					icon.style.display = '';
					document.body.style.cursor = '';
					previous(icon).style.display = '';
				}
			);
		}
	);
	
  $(containerOf(icon)).fresheditor('edit', false);
  
}
