function setRating(rating){
	var ratingNode = $('Rating');
	var ratingContent = $('RatingContent');
	var previousHeight = ratingContent.getHeight();
	
	new Ajax.Request('ajax/rating.php',{
		'method': 'get',
		'parameters':{
			'id':Website.currentLinkId,
			'rating':rating
		},
		'onLoading': function(){
 			ratingContent.update('<div class="ajaxLoader"></div>');
		},
		'onSuccess': function(request){
			ratingContent.update(request.transport.responseText);
			
			var height = ratingNode.getHeight(); 
			var marginTop = parseInt(ratingNode.getStyle('margin-top'))-height+previousHeight;
			
			ratingNode.setStyle({'marginTop':marginTop+'px'});
			
			$$('#RatingContent form')[0].getElements().each(function(element){
				console.log(element);
				if ( element.name != 'email' && element.name != 'message' ) return;
				
				console.log('fixing');
				
				element.defaultValue = element.value;
				
				element.onfocus = function(){
					if ( this.defaultValue ) this.value = '';
				};
				
				element.onblur = function(){
					if ( this.value == '' ) this.value = this.defaultValue;
					else this.defaultValue = '';
				};
			});
		}
	});
}

function closeRatingBox(){
	var ratingNode = $('Rating');
	ratingNode.update('');
	ratingNode.setStyle({'marginTop':''});
}

function sendRatingComment(){
	var ratingNode = $('Rating');
	var ratingContent = $('RatingContent');
	
	ratingNode.setStyle({'marginTop':''});
	
	$$('#RatingContent form')[0].request({
		'onLoading': function(){
			ratingContent.update('<div class="ajaxLoader"></div>');
		},
		'onSuccess': function(request){
			ratingContent.update(request.transport.responseText);
		}
	})
	
	return false;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function print_r(obj,hideValue){ 
     var objString = '<ul>';
     
     for( var varName in obj ) {
     	objString += '<li>'+ varName +'('+ typeof obj[varName] +')';
     	
     	if ( typeof obj[varName] == 'object' ) {
     		if ( hideValue ) continue;
     		objString += print_r(obj[varName],true);
     	} else {
     		objString += ':'+ escape(obj[varName]);
     	}
     }
     objString += '</ul>';
     return objString;
}

/*has a negative effect on input-field's focus...in IE (whenever deselect is called, the currentinput field, loses its focus)*/
function deselect(){
	if ( document.selection ) document.selection.empty();
	else if ( window.getSelection ) window.getSelection().removeAllRanges();
}

function rdebug(obj){
	debug(print_r(obj));
}

function debug(value){
	document.getElementById('debugOut').innerHTML += '<br>'+value;
}

function stopEvent(event){
	if ( event.stopPropagation ) { event.stopPropagation(); }
	else { event.cancelBubble = true; }
}

function execScripts(node){
	if ( node.tagName == 'SCRIPT' ){
		try{
			eval(node.text);
		} catch( e ){
			if ( window.console ) window.console.log(e);
		}
	} else {
		var child = node.firstChild;
		while ( child  ) {
			execScripts(child);
			child = child.nextSibling;
		}
	}
};

function remoteRequest(method,url,successHandler){
	var request;
	if ( window.XMLHttpRequest) request = new XMLHttpRequest();
	else request = new ActiveXObject("Microsoft.XMLHTTP");
	
	request.open(method,url,true);
	
	if ( successHandler ) {
		request.onreadystatechange = function() {
			if ( request.readyState == 4 && request.status == 200 ) {
				successHandler(request);
			}
		};
	}
	
	request.send();
};

function lightbox(link,event){
	var background = document.createElement('div');
	var lightbox = document.createElement('div');
	var closeButton = document.createElement('div');
	
	var closeHandler = function(){
		background.parentNode.removeChild(background);
		lightbox.parentNode.removeChild(lightbox);
		
		document.body.onkeydown = null;
	};
	
	document.body.onkeydown = function(event){
		if ( !event ) event = window.event;
		
		switch( event.keyCode ) {
			case 27:
				closeHandler();
				break;
		}
	};
	
	background.className = 'lightbox-background';
	closeButton.className = 'close';
	lightbox.className = 'lightbox loading';
	
	closeButton.onclick = closeHandler;
	background.onclick = closeHandler;
	document.body.appendChild(background);
	lightbox.appendChild(closeButton);
	document.getElementById('Content').appendChild(lightbox);

	remoteRequest('GET',link,function() {
		if ( request.readyState == 4 && request.status == 200 ) {
			lightbox.className = 'lightbox';
			lightbox.innerHTML = request.responseText;
			
			execScripts(lightbox);
			Calculator.initInstances();
			
			lightbox.appendChild(closeButton);
		}
	});
	
	return false;
};
