function showImage(obj, id) {
	var prevAnchor = document.getElementsByClassName('actImg');
	if(prevAnchor.length>0) {
		prevAnchor[0].className = 'aImg';
	}
	obj.className = 'actImg';
	
	imageId = id;
	
	loadComments();
	var newImage = obj.firstChild.src.replace('th', 'big');
	var originalImage= obj.firstChild.src.replace('th', 'original');
	var bigImage = $('big');
	var img = bigImage.getElementsByTagName('img')[0];
	var anchor = bigImage.getElementsByTagName('a')[0];
	anchor.href=originalImage;
	anchor.style.display='none';
	$('loader').style.display='block';
	
	img.src=newImage;
	img.onload = function() {
		imgLoaded(this);
	}
	
	
	$('sendtofriend').href = '/sendphoto/'+id;
}

function imgLoaded(obj) {
	$('loader').style.display='none';
	obj.parentNode.style.display='inline';
}

function originalImage() {}

function calculator() {
	var url = '/templates/calculator.php';
	var w = 650;
	var h = 375;
	var left = (screen.width - w) / 2;
	var top = (screen.height - h) / 2;
	var	opt = 'toolbar=no,menubar=no,width='+w+',height='+h+',scrollbars=yes,location=no,titlebar=yes,directories=no,screenX='+top+',screenY='+left+',top='+top+',left='+left;
	var win = window.open(url, '', opt);
}

function postcomment(id) {
	if(typeof(id)!='undefined') {
		var url = '/postcomment?cam_id='+id;
	}else {
		var url = '/postcomment?img_id='+imageId;
	}
	var w = 460;
	var h = 420;
	var left = (screen.width - w) / 2;
	var top = (screen.height - h) / 2;
	var	opt = 'toolbar=no,menubar=no,width='+w+',height='+h+',scrollbars=yes,location=no,titlebar=yes,directories=no,screenX='+top+',screenY='+left+',top='+top+',left='+left;
	var win = window.open(url, '', opt);
}


var comments = [];

function showComments(commentsData) {
	var num = commentsData.length;
	var place = $('all-comments');
	place.innerHTML = '';

	var noComments = $('no-comments');
	noComments.style.display = 'none';
	
	if(num>0) {
		for(var i=0; i<num; i++) {

			var div = document.createElement('div');
			div.className = 'comment';
			
			var h4 = document.createElement('h4');
			h4.innerHTML = commentsData[i]['title']+' <span>('+commentsData[i]['time']+')</span>';
			
			var p = document.createElement('p');
			p.innerHTML = commentsData[i]['short_desc'];

			div.appendChild(h4);
			div.appendChild(p);
			place.appendChild(div);
		}
	}else {
		noComments.style.display = 'block';
	}

	
}
function syncRatings(imageRating, numVotes) {
    counter = 1;
    $A($('stars').getElementsByTagName('img')).each(function(img){
        if(counter<=Math.round(imageRating)) {
            img.src = '/images/css/star.gif';
        } else {
            img.src = '/images/css/star-empty.gif';
        }
        if(counter==5) {
            throw $break;
        }
        counter++;
    });
    $('actual_rating').innerHTML = imageRating;
    $('increment_rating').innerHTML = numVotes;
}
function loadComments() {
//	if(typeof(comments[imageId])!='undefined') {
		//showComments(comments[imageId]);
//	}else {
		new Ajax.Request(
			'/inc/ajax/ajaxResponse.php?do=getComments',
			{
				method: 'POST',
				parameters:
				$H({
					'img_id': imageId
				}).toQueryString(),
				onComplete: function(responseText) {
					json = eval(responseText.getResponseHeader('X-JSON'));
					commentsData = json.commentsData;
                    imageRating = json.imageRating;
                    numVotes = json.numVotes;
                    syncRatings(imageRating, numVotes);

					showComments(commentsData);
					comments[imageId] = commentsData;
				}
			}
		);
//	}
}


function switchPlace(obj) {
	top.location.href='/places/'+obj.value;
}
function isIe() {
    agent = navigator.userAgent.toLowerCase();
    return agent.indexOf("msie") != -1 && agent.indexOf("opera") == -1;
}
function sethomepage_help() {
    document.getElementById('dragme').style.display = 'block';
    Event.stopObserving(document, "click", hide_dragme);
    setTimeout('Event.observe(document, "click", hide_dragme)');
}
function hide_dragme() {
    $("dragme").hide();
}
function abuse() {
    new Ajax.Request('/inc/ajax/ajaxResponse.php', {
        'method': 'POST',
        'parameters': $H({'image_id': imageId, 'do': 'report_abuse'}).toQueryString(),
        'onComplete': function(response) {
           $('flagme').innerHTML = 'Thank you! Your report was sent to site administrator.';
           $('flagme').addClassName('flagged');
        }
    });
}
