	
	Event.observe(
		window, 'load', function() {
			if($('mainphoto')){
				initRating();	
				var thumbnails = $$('.upload-images img.thumb');
				showUpload(thumbnails[0]);
			}
		}
	);
	
	function initRating(){
		$('rating-slider').style.width = startWidth + 'px';
		var items = $('rating-imgcontainer').getElementsByTagName('img');
		for (var i=0; i<items.length; i++) {
			items[i].onmouseover = starHoverOver;
			items[i].onmouseout = starHoverOut;
			items[i].onclick = starClick;
		}
	}
	
	function starHoverOver(){
		var elmntIndex;
		var items = this.parentNode.getElementsByTagName('img');
		for (var i=0; i<items.length; i++) {
			if(items[i]==this) elmntIndex = i;
		}
		if(!hasClicked) $('rating-slider').style.width = ((elmntIndex+1)*17) + 'px';
	}

	function starHoverOut(){
		if(!hasClicked) $('rating-slider').style.width = startWidth + 'px';
	}

	function starClick(){
		if(hasClicked){
			$('rating-ratings').update("<strong>"+strClickOnce+"</strong>");
			return;
		}
		var elmntIndex;
		var items = this.parentNode.getElementsByTagName('img');
		hasClicked = true;
		$('rating-imgcontainer').className='rating-inactive';
		for (var i=0; i<items.length; i++) {
			if(items[i]==this) elmntIndex = i;
		}
		new Ajax.Request(ajaxURL+'?record='+record+'&rating='+(elmntIndex+1), {
				method: 'get',
				onSuccess: function(transport) {
					handleAjaxResult(transport);
				}
		});
	}

	function handleAjaxResult(r){
		var succesValue = r.responseXML.documentElement.getAttribute("value");
		var newRating = r.responseXML.documentElement.getAttribute("newrating");
		var attHasclicked = r.responseXML.documentElement.getAttribute("hasclicked");
		if(succesValue == "true" && attHasclicked=="0"){
			$('rating-slider').style.width = newRating + 'px';
			$('rating-ratings').update("<strong>"+strRatingThanks+"</strong>");
		}
		else{
			$('rating-slider').style.width = startWidth + 'px';
			$('rating-ratings').update("<strong>"+strClickOnce+"</strong>");
		}
	}

	function showUpload(oImg){
		var newWidth,newHeight;
		var imgPreloader = new Image();
		var mainImg = $('mainphoto');
		mainImg.remove();
		var newImg = new Element('img',{'id':'mainphoto'});
		$('main-uploadimage').appendChild(newImg);
		imgPreloader.onload = function(){
			$('mainphoto').src=this.src;
			//landscape: width:345px; height:231px;
			//portrait: width:223px; height:333px;
			if(this.width>this.height){  // LANDSCAPE
				newHeight = this.height/(this.width/347);
				$('mainphoto').height = newHeight;
				$('mainphoto').style.marginTop = (333-newHeight)/2 + 'px';
			}
			else{ // PORTRAIT
				newWidth = this.width/(this.height/333);
				$('mainphoto').width = newWidth;
			}
			$('mainphoto').onclick = function(){
				popWindow(this.src,'upload',1024,768,'resizable=1,scrollbars=1')
			}
		}
		imgPreloader.src = oImg.src;
	}
