// JavaScript Document
var myWindow;
var proWindow;
function openWindow(i,w,h) {
	if (w == null || w.length==0) { w=300; }
	if (h == null || h.length==0) { h=300; }
	target = "/site/imgViewer.cfm?i="+i;
	ww=eval(w + 100); hh=eval(h + 100);
	myWindow = window.open(target,"myWindow","width="+(ww)+",height="+(hh)+",scrollbars=0,resizable=1");
	myWindow.focus(); myWindow.closed == false;
}
function viewImage(i,w,h) {
	if ( typeof(myWindow) == "undefined") { openWindow(i,w,h); } 
	else { myWindow.close(); openWindow(i,w,h);	}
}
function addClass(e,c){if(!$(e).hasClassName(c)){$(e).addClassName(c)}}
function remClass(e,c){if($(e).hasClassName(c)){$(e).removeClassName(c)}}
function dd(e){
	var p=$(e).ancestors()[2];
	var d=document;
	function cl(){remClass(p,'on');d.onclick=null;}
	function sh(){d.onclick=cl;}
	if(p.hasClassName('on')){cl();}else{
		addClass(p,'on');
		setTimeout(sh, 0);
        try {var u=(p.getElementsByTagName("UL")[1]), c=u.childNodes, w=u.clientWidth, e=u, x=0;}
        catch (ig){}
	}
	return false;	
}
function resetCommentForm(){
	new Ajax.Request('/assets/shared/captcha.cfm',{
		method: 'get',
		parameters: {ajax:true},
		onSuccess:function(response,json){
			$('comment').value='';
			$('captchaAnswer').value='';
			$('captchaHash').value=json.captchahash;
			$('captchaQuestion').update(json.captchaquestion);
			$('commentInput').scrollTo();
		}
	})		
}
function addItemComment(){
	var result = valid_itemcomment.validate();
	var ncm = Try.these( function() { return $('commentMessage') }) || false;
	if(result){
		new Ajax.Request('/items/_addComment.cfm',{
			method:'get',
			parameters: { itemid:$F('itemid'), userid:$F('userid'), comment:$F('comment'), captchaHash:$F('captchaHash'), captchaAnswer:$F('captchaAnswer'), email_me:$F('email_me')},
			onSuccess: function(response,json){
				if(json.status == 0){
					$('flashComment').update(json.message);
	       			$('flashComment').show();
	                setFlashBad();
				} else {
					$('flashComment').hide();
					var commentDiv = Builder.node('div',{id:'comment'+json.id,className:'comment',style:'display:none;'},[
						Builder.node('div',{className:'whoby'},[
							Builder.node('div',{className:'right'},[
								Builder.node('a',{href:'#',style:'float:left;',onclick:'deleteComment('+json.id+',\'comment'+json.id+'\',\'userdelete\');return false'},'Delete'),
								Builder.node('div',{id:'dd'},[
									Builder.node('ul',{className:'dd'},[
										Builder.node('li',[
											Builder.node('a',{onclick:'return dd(this);',href:'#',className:'trigger'},'Flag'),
											Builder.node('ul',[
												Builder.node('li',[
													Builder.node('a',{href:'#',onclick:'reportComment('+json.id+',\'comment'+json.id+'\',\'offensive\');return false'},'Offensive'),
													Builder.node('a',{href:'#',onclick:'reportComment('+json.id+',\'comment'+json.id+'\',\'notrelevant\');return false'},'Not Relevant'),
													Builder.node('a',{href:'#',onclick:'reportComment('+json.id+',\'comment'+json.id+'\',\'spam\');return false'},'SPAM')
												])
											])
										])
									])
								])
							]),
							Builder.node('div',{className:'left'},[
								'By ',
								Builder.node('a',{href:'http://my.office-humour.co.uk/people/'+json.username+'/'},json.username),
								' ',
								json.timestamp
							])
						]),
						Builder.node('div',{className:'text'},json.comment)
					]);
					resetCommentForm();
					$('itemComments').appendChild(commentDiv);
					// $('comment'+json.id).scrollTo();
					Effect.Appear(commentDiv.id);
	                setFlashGood();
					$('flashComment').show();
					$('flashComment').update('Comment added. Thanks!');
					Effect.Fade('flashComment',{delay:5,duration:1});
					commentCount = commentCount+1;
					if(commentCount!=0 && ncm){
						$('commentMessage').update('Add a <a href="##" onclick="resetCommentForm(); return false">new comment</a>.');
					}
				}
			},
			onFailure: function(response){alert("Sorry\n\nSomething broke. Please try again later.");}
		});	
	}
}
function deleteComment(id,e,reason) {
	var ans = confirm("Are you sure you want to delete this comment?");
	if (ans){
		new Ajax.Request('/items/_reportComment.cfm',{
			method:'get',parameters:{id:id,reason:reason,r: Math.random()},
			onSuccess: function(response,json){
				if(json == 1){
					$(e).immediateDescendants()[0].update("Removed");
					$(e).immediateDescendants()[1].update("<p><strong>Your comment has been removed.</strong></p>");
					Effect.Fade(e,{delay:2,duration:1});
					commentCount = commentCount - 1;
					if(commentCount==0){
						$('commentMessage').update('There aren\'t any comments yet for this item. Be the first to add one.');
						resetCommentForm();
					}
				} else {
					alert("Sorry\n\nSomething broke. Please try again later.");
				}
			},
			onFailure: function(response){alert("Sorry\n\nSomething broke. Please try again later.");}
		});
	}
}
function reportComment(id,e,reason) {
	new Ajax.Request('/items/_reportComment.cfm',{
		method:'get',parameters:{id:id,reason:reason,r: Math.random()},
		onSuccess: function(response,json){
			if(json == 1){
				$(e).immediateDescendants()[0].update("Reported");
				$(e).immediateDescendants()[1].update("<p><strong>The comment has been removed pending review.</strong></p>");
			} else {
				alert("Sorry\n\nSomething broke. Please try again later.");
			}
		},
		onFailure: function(response){alert("Sorry\n\nSomething broke. Please try again later.");}
	});
}
var fia=0;
var fir=0;
function addFavourite(itemid,userid){
	if(fia==1){return false}
	new Ajax.Request('/items/_addFavourite.cfm', {
		method: 'get',
		parameters: {
			itemid: itemid,
			userid: userid,
			r: Math.random()
		},
		onSuccess: function(response, json){
			if (json == 1) {
				fia = 1;
				$('favmessage').update("Saved!");
				$('favcount').update(favouriteCount + 1);
				new Effect.Highlight('favmessage',{duration:1.5,startcolor:'#EEF8E4'});
			}
			else {
				alert("Sorry\n\nSomething broke. Please try again later.");
			}
		},
		onFailure: function(response){
			alert("Sorry\n\nSomething broke. Please try again later.");
		}
	});
}
function removeFavourite(itemid,userid){
	if(fir==1){return false}
	var ans = confirm("Are you sure you want to remove this as a favourite?");
	if (ans) {
		new Ajax.Request('/items/_deleteFavourite.cfm', {
			method: 'get',
			parameters: {
				itemid: itemid,
				userid: userid,
				r: Math.random()
			},
			onSuccess: function(response, json){
				if (json == 1) {
					fir=1;
					$('favmessage').update("Removed");
					new Effect.Highlight('favmessage',{duration:1.5,startcolor:'#EEF8E4'});
				}
				else {
					alert("Sorry\n\nSomething broke. Please try again later.");
				}
			},
			onFailure: function(response){alert("Sorry\n\nSomething broke. Please try again later.");}
		});
	}else{
		return false
	}
}
function flagItem(id,userid,reason){
	new Ajax.Request('/items/_flagitem.cfm',
	{
		method:'get',
		parameters: { id:id, userid:userid, reason:reason, r:Math.random() },
		onSuccess: function(response,json){
			if(json == 1){
				$('flagitem').update('<p>You have flagged this item and it is currently pending review.</p>')
			} else {
				alert("Sorry\n\nSomething broke. Please try again later.");
			}
		},
		onFailure: function(response){}
	});	
}
