JPGMAG.comments={
	preview:function(){
		if($j('#commentTextarea').val()>''){
			$j('#commentInspire').html('Previewing your comment:');
			if(!$j('#commentPreview').length){
				var a=($j('#haValue').val()=='1')?'<a id="aa" href="/people/'+$j('#unValue').val()+'"><img src="http://c0177201.cdn.cloudfiles.rackspacecloud.com/'+$j('#uValue').val()+'.jpg" alt="'+$j('#dnValue').val()+'" width="23" height="23"/></a>':'';
				var d=new Date(),m=['January','February','March','April','May','June','July','August','September','October','November','December'];
				var bp='<p id="bylinePreview"><strong><a href="/people/'+$j('#unValue').val()+'">'+$j('#dnValue').val()+'</a></strong> said <span>('+d.getDate()+' '+m[d.getMonth()]+' '+d.getFullYear()+')</span>:';
				var pp='<p id="commentPreview" class="cmTxt"></p>';
				$j('#commentTextarea').before(a+bp+pp);
			}
			var cmt=JPGMAG.lb2br($j('#commentTextarea').val());
			do {cmt=cmt.replace('<br/><br/><br/>','<br/><br/>');}while(cmt.search('<br/><br/><br/>')>-1);
			$j('#commentPreview').html(cmt);
		}else{
			$j('#commentInspire').html('What did this photo inspire in you?');
			$j('#commentFormlet>div #aa,#commentFormlet>div #bylinePreview, #commentFormlet>div #commentPreview').remove();
		}
	},
	toggle:function(){
		var c='';
		if($j('#nCi').val()>0){c+='<input type="checkbox" name="commentcount" value=""'+(JPGMAG.cookie.get('hideCommentComments')?'':' checked="checked"')+' id="commentCheck" class="checkToggle"/><label for="commentCheck">'+$j('#nCi').val()+' comment'+($j('#nCi').val()!=1?'s':'')+'</label> ';}
		if($j('#nPi').val()>0){c+='<input type="checkbox" name="propcount" value=""'+(JPGMAG.cookie.get('hidePropComments')?'':' checked="checked"')+' id="propCheck" class="checkToggle"/><label for="propCheck">'+$j('#nPi').val()+' prop'+($j('#nPi').val()!=1?'s':'')+'</label> ';}
		if($j('#nLi').val()>0){c+='<input type="checkbox" name="linkcount" value=""'+(JPGMAG.cookie.get('hideLinkComments')?'':' checked="checked"')+' id="linkCheck" class="checkToggle"/><label for="linkCheck">'+$j('#nLi').val()+' link'+($j('#nLi').val()!=1?'s':'')+'</label> ';}
		if(c>''){
			$j('#commentscount').append(' <span id="commentToggles"'+(JPGMAG.cookie.get('hideComments')?' class="dim"':'')+'>'+c+'</span>');
			$j('#commentTotal').click(function(){
				if($j('#commentscount').hasClass('closed')){
					JPGMAG.cookie.set('hideComments','true',new Date(1999));
				}else{
					JPGMAG.cookie.set('hideComments','true',(new Date()).addYear());
				}
				$j('#commentscount').toggleClass('closed').toggleClass('open');
				$j('#commentToggles,#commentList').toggle();
			});
			$j('.checkToggle').click(JPGMAG.comments.dimmer);
		}
	},
	dimmer:function(){
		var c=$j(this).attr('id').replace('Check','');
		var cc=c.substr(0,1).toUpperCase()+c.substr(1,c.length);
		if($j(this).is(':checked')){
			$j('#commentList .'+c).show();
			JPGMAG.cookie.set('hide'+cc+'Comments','true',new Date(1999));
		}else{
			$j('#commentList .'+c).hide();
			JPGMAG.cookie.set('hide'+cc+'Comments','true',(new Date()).addYear());
		}
	},
	tabs:function(){
		var x=document.uniqueID&&document.compatMode&&!window.XMLHttpRequest&&document.execCommand;
		try{if(!!x){x("BackgroundImageCache",false,true)}}catch(e){};
		$j('#formcluster .commentsTab').click(function(){
			if($j(this).parent('fieldset').hasClass('on')){
				$j(this).parent('fieldset').removeClass('on');
			}else{
				$j('#formcluster fieldset').removeClass('on');
				$j(this).parent('fieldset').addClass('on');
				$j(this).next('.miniform').find(':input:visible:enabled:first').focus();
			}
		});
	},
	noempties:function(){
		var c=($j(this).is('#cSub'))?$j('#commentTextarea'):($j(this).is('#pSub'))?$j('#propInput'):($j(this).is('#lsub'))?$j('#linkInput'):null;
		var t=($j(this).is('#cSub'))?'comment':($j(this).is('#pSub'))?'prop':null;
		if(c && c.val().length<3 && !$j(this).is('#lSub')){
			alert('Your '+t+' is too short. Bet you meant to say something more!');
			return false;
		}else if(c && c.val().length<11 && $j(this).is('#lSub')){
			alert('Check that link again, it seems too short.');
			return false;
		}
	}
}

JPGMAG.flag={
	reasons:{
		story:{"copyright_violation":"A copyright violation","advertising":"Just advertising"},
		photo:{"copyright_violation":"A copyright violation","digital_enlargement":"A digital enlargement","sexually_explicit":"Sexually explicit","not_a_photo":"Not a photo"}
	},
	pop:function(){
	    var link='/about/';if(JPGMAG.flag.type=='photo'){link+='photos'}else{link+='stories'}
		var html='<div id="flagWindow"><div id="flagPopBox" class="flagPop"><div class="flagLeft"> </div><div id="flagform"><div class="flagRight"><form accept-charset="UTF-8" action="/actions/flag.php" method="post" id="frmflagstory"><h2>Eep! Is there a problem with this '+JPGMAG.flag.type+'?</h2><p>We strive to be as open and inclusive as possible. Please read our most recent <a class="lightblue" href="'+link+'">guidelines</a> before reporting. If you think this '+JPGMAG.flag.type+' is a serious violation, use this form to let us know.</p><p><label for="reasonSelect">Yes, this '+JPGMAG.flag.type+' is...</label> <select id="reasonSelect" name="reason"><option value="">Please select</option>';
		for(key in JPGMAG.flag.reasons[JPGMAG.flag.type]){html+='<option value="'+key+'">'+JPGMAG.flag.reasons[JPGMAG.flag.type][key]+'</option>';}
		html+='</select></p><p><label for="commentsTextarea">Briefly explain:</label><br/><textarea id="commentsTextarea" name="description"/></p><p class="submit"><input type="submit" value="Send it"/> or <input type="button" id="flagCancel" value="Cancel"/></p></form></div></div>';
		if($j.browser.msie&&$j.browser.version<7){window.scrollTo(0,0);$j('body,html').css('overflow','hidden').height('100%');}
		$j('#content').append(html);
		$j('#flagform form').submit(JPGMAG.flag.submit);
		$j('#flagCancel').click(JPGMAG.flag.close);
		$j('#flagWindow').fadeIn('fast');
		return false;
	},
	submit:function(){
		var al='';
		if($j('#flagform option:selected').val()==''){al='Please select the type of problem you are reporting.';}
		if($j('#flagform textarea').val()==''){al+=(al>'')?' Also, p':'P';al+='lease briefly explain the problem for us.';}
		if(al>''){alert(al);return false;}
		$j('#flagform :input').attr('disabled','disabled');
		if($j('#flagform .submit span').length<1){$j('#frmflagstory .submit').prepend('<span></span>');}
		$j('#flagit .submit span').html('<img width="200" height="16" src="/images/TransparentLoadingBar.gif" alt="" />');
		$j.ajax(
			{
				data:
				{
					element_type:JPGMAG.flag.type,
					element_id:$j('#flagformd input[name="element_id"]').val(),
					reason:$j('#flagform option:selected').val(),
					description:$j('#flagform textarea').val(),
					key:$j('#flagformd input[name="key"]').val()
				},
				error:JPGMAG.flag.handleFailure,
				success:JPGMAG.flag.handleSuccess,
				type:'POST',
				url:'/actions/flag.php'
			}
		);
		return false;
	},
	handleFailure:function(){
		JPGMAG.flag.failed++;
		$j('#flagform :input').attr('disabled','');
		$j('#flagform .submit span').html('Ack! '+((JPGMAG.flag.failed>1)?'Okay. You can contact <a href="/about/contact.html">JPG Support</a> to report this problem.':'That didn&rsquo;t work.<br/>Would you mind trying that again?'));
	},
	handleSuccess:function(d){
		if(d!='1'){JPGMAG.flag.handleFailure();}
		else{
			var html='<div id="flagformfeedback"><div class="flagRight"><h2>Thanks for letting us know!</h2><p>Rest assured, we take your concerns seriously. We\'ll look into it and take any necessary action.</p><input type="button" id="flagAllDone" value="I feel better"/></div></div>';
			$j('#flagPopBox').append(html);
			$j('#flagAllDone').click(JPGMAG.flag.close);
			$j('#flagform').slideUp('normal');
			$j('#flagformfeedback').slideDown('normal');
		}
	},
	close:function(){$j('#flagWindow').fadeOut('fast',function(){$j('#flagCancel,#flagform form,#flagAllDone').unbind();$j('#flagWindow').empty().remove();if($j.browser.msie&&$j.browser.version<7){$j('body,html').css('overflow','auto').height('auto');}});return false;},
	failed:0
}

$j(function(){
	if($j('#commentscount').length){JPGMAG.comments.toggle();}
	if($j('#commentblock').length){JPGMAG.comments.tabs();}
	if($j('#commentFormlet').length){
		$j('#cSub').before('<input type="button" name="isCommentPreview" id="previewButton" value="Preview"/>');
		$j('#previewButton').click(JPGMAG.comments.preview);
	}
	$j('#cSub,#pSub,#lSub').click(JPGMAG.comments.noempties);
	if($j('#flagformd input[name="element_type"]').length){JPGMAG.flag.type=$j('#flagformd input[name="element_type"]').val();}
	$j('.flag a').click(JPGMAG.flag.pop);
});