//thief.
var thumb_preview = function() {	

	thumb_preview.loading=0;
	thumb_preview.loaded = new Object();
	$('a.t_pane').each(function(i,e) {
		thumb_preview.loaded[$(e).attr('rel')] = $(e).children().attr('src');
	});
	
	$('#sidebar a.t_p').not('.bound').hover(function(e) {
		r = $(this).attr('rel');
		$(this).addClass('bound');
		thumb_preview.active = true;
		thumb_preview.loading = r;
		thumb_preview.load_el = this;
		
		if (typeof (thumb_preview.loaded[r]) == 'undefined') {

			$.post('/ajax_assorted.php', {'id':r,'intent':'thumb'}, function(resp) {
				
				if (resp.status=='success') {
					if (resp.id==thumb_preview.loading && thumb_preview.active) {
					thumb_preview.loaded[resp.id] = resp.thumbnail;
					var img = new Image();
					$(img).attr('src', resp.thumbnail).load(function() {
						$(thumb_preview.load_el).closest('div').find('a.t_pane img').attr('src',resp.thumbnail);
						//$('#t_preview').html('<img src="'+ resp.thumbnail +'" alt="Thumbnail preview" /><br /><span class="title">'+ resp.title +'<span class="addon">'+resp.addon+'</span></span>');
					});
					}
				} else {
					$('#t_preview').remove();
				}
			}, 'json');
		} else {
			//due to html meddling parents() selector is useless...
			$(thumb_preview.load_el).closest('div').find('a.t_pane img').attr('src',thumb_preview.loaded[r]);
		}

				
    },
	function(){

    });	
	
	$('#sidebar a.t_p').not('.bound').parents('ul').hover(function(e) {

		thumb_preview.orig = $(this).parent().find('a.t_pane').attr('rel');
				
    },
	function(){
		thumb_preview.active = false;
		$(thumb_preview.load_el).closest('div').find('a.t_pane img').attr('src',thumb_preview.loaded[thumb_preview.orig]);

    });	

	
};


$(function() {
thumb_preview();
$('.toggleBar .infoTag').add('.toggleBar .toggleSwitch').click(function(e) {

	witchs = $(this).parent().find('.toggleSwitch');
	if($(witchs).hasClass('active')) {
		$(witchs).removeClass('active');
		$(witchs).parent().nextAll('.toggleContent').toggle();
	} else {
		$(witchs).addClass('active');
		$(witchs).parent().nextAll('.toggleContent').toggle();
	}

});

window.animateVidside = true;
window.animateVidsideCached = false;

function playVidside () {
	if (window.animateVidside) {
		var sauce = $('#videoSidebar div a img').attr('src').split('/');
		var lastsauce = sauce.pop();
		if (lastsauce=='default.jpg') {
			next='1.jpg';
			if(window.animateVidsideCached!=true) { $.get(sauce.join('/') + '/1.jpg'); }
		} else if (lastsauce=='1.jpg') {
			next='2.jpg';
			if(window.animateVidsideCached!=true) { $.get(sauce.join('/') + '/2.jpg'); }
		} else if (lastsauce=='2.jpg') {
			next='3.jpg';
			if(window.animateVidsideCached!=true) { $.get(sauce.join('/') + '/3.jpg'); }
		} else if (lastsauce=='3.jpg') {
			next='default.jpg';
			if (window.animateVidsideCached!=true) { window.animateVidsideCached = true; }
		}
		
		sauce.push(next);
		$('#videoSidebar div a img').attr('src', sauce.join('/'));
		setTimeout(playVidside, 1400); 
	}
}

//setTimeout(playVidside, 1400);






if ($('body').hasClass('watch-video')) {



window.animateVidpaneOne = true;
window.animateVidpaneOneCached = false;

function playVidsideOne () {
	var sauce = $('.vidbox1 div a img').attr('src').split('/');
	var lastsauce = sauce.pop();
	if (lastsauce=='default.jpg') {
		next='1.jpg';
		if(window.animateVidpaneOneCached!=true) { $.get(sauce.join('/') + '/1.jpg'); }
	} else if (lastsauce=='1.jpg') {
		next='2.jpg';
		if(window.animateVidpaneOneCached!=true) { $.get(sauce.join('/') + '/2.jpg'); }
	} else if (lastsauce=='2.jpg') {
		next='3.jpg';
		if(window.animateVidpaneOneCached!=true) { $.get(sauce.join('/') + '/3.jpg'); }
	} else if (lastsauce=='3.jpg') {
		next='default.jpg';
		if (window.animateVidpaneOneCached!=true) { window.animateVidpaneOneCached = true; }
	}
	
	sauce.push(next);
	$('.vidbox1 div a img').attr('src', sauce.join('/'));
	setTimeout(playVidsideOne, 1400); 
}

setTimeout(playVidsideOne, 1400);

window.animateVidpaneTwo = true;
window.animateVidpaneTwoCached = false;

function playVidsideTwo () {
	var sauce = $('.vidbox2 div a img').attr('src').split('/');
	var lastsauce = sauce.pop();
	if (lastsauce=='default.jpg') {
		next='1.jpg';
		if(window.animateVidpaneTwoCached!=true) { $.get(sauce.join('/') + '/1.jpg'); }
	} else if (lastsauce=='1.jpg') {
		next='2.jpg';
		if(window.animateVidpaneTwoCached!=true) { $.get(sauce.join('/') + '/2.jpg'); }
	} else if (lastsauce=='2.jpg') {
		next='3.jpg';
		if(window.animateVidpaneTwoCached!=true) { $.get(sauce.join('/') + '/3.jpg'); }
	} else if (lastsauce=='3.jpg') {
		next='default.jpg';
		if (window.animateVidpaneTwoCached!=true) { window.animateVidpaneTwoCached = true; }
	}
	
	sauce.push(next);
	$('.vidbox2 div a img').attr('src', sauce.join('/'));
	setTimeout(playVidsideTwo, 1400); 
}

setTimeout(playVidsideTwo, 1400);




}


$('#videoSidebar div a img').mouseover(function(e) {
	//window.animateVidside = true;
})

$('#videoSidebar div a img').mouseout(function(e) {
	//window.animateVidside = false;
})



$('#login-form').submit(function(e) {
	e.preventDefault();

	$.post("/ajax_login.php", { 'u': $('#userbox').val(), 'p':$('#passbox').val(), 'h':2 },
	function (data) {
		if (data.valid) {
			
			if(data.location=='stay') {
				if (window.location.pathname=='/details.php') {
					window.location.reload();
				} else {				
					$('#loginBox').fadeOut(500, function() {
						$('#loginBox').addClass('active').html(data.html).fadeIn(500, function() {
							$('#top-promotion-bnr, #top-promotion-side').fadeOut(500, function() {
								$('#top-promotion-bnr, #top-promotion-side').remove();
							});
							$('body').removeClass('guest').addClass('member' + (data.premium?' ajaxfade premium':''));
							$('#new_header').animate({'height':'125px'}, 500);
						});
						
					});
				}
			} else if (data.location=='url') {
				window.location = data.url;
			}
			
		} else {
			$('#registerLink').hide();
			$('#usernameArea').addClass('error').html(data.message);
		}
	}, "json");
	
});


	$('#reviewBox a.next').click(function(e) {
		e.preventDefault();
		if ($('#reviewBox .active').hasClass('last')) {
			$('#reviewBox .active').hide().removeClass('active');
			$('#reviewBox .first').show().addClass('active');
		} else {
			$('#reviewBox .active').hide().removeClass('active').next().show().addClass('active');
		}
	});
	
	
	$('body.members-maps-edit #envPanel input[type=checkbox]').click(function(e) {
		
		if (this.id=='env_17') {
		
			if ($('#'+this.id).attr('checked')==true) {
				$('#envPanel input[type=checkbox]').not('#env_17').removeAttr('checked').attr('disabled', 'disabled');
			} else {
				$('#envPanel input[type=checkbox]').not('#env_17').removeAttr('disabled');
			}
		} else {
			if ($('#'+this.id).attr('checked')==true) {
				$('#env_17').removeAttr('checked').attr('disabled', 'disabled');
			} else if ($('#envPanel input[type=checkbox]:checked').not('#env_17').length==0) {
				$('#env_17').removeAttr('disabled');
			}			
		}

	});
	
	if ($('#envPanel input[type=checkbox]:checked').not('#env_17').length>0) {
		$('#env_17').removeAttr('checked').attr('disabled', 'disabled');
	} else if ($('#env_17:checked').length>0) {
		$('#envPanel input[type=checkbox]').not('#env_17').removeAttr('checked').attr('disabled', 'disabled');
	}
	
	
	$('div#profileMessage span#nominate-button').not('#nominate-button.disabled').click(function(e) {
		if (confirm('Nominate this user to become a critic?')) {
		$.post('/ajax_assorted.php', {'intent':'nominate','user':$('#avatarBox').attr('rel')}, function(resp) {
		if (resp.status=='success') {
				alert(resp.message);
				$('div#profileMessage span#nominate-button').addClass('disabled').unbind('click');
			} else {
				alert(resp.message);
			}
		}, 'json');
	
		}
	});
	$('div#profileMessage span#nominate-button.disabled.more-reviews').click(function(e) {
		alert('This user has not contributed enough to be nominated yet.');
	});
	$('div#profileMessage span#nominate-button.disabled.needs-critic').click(function(e) {
		alert('You must be a critic to nominate other members.');
	});
	$('div#profileMessage span#nominate-button.disabled.is-critic').click(function(e) {
		alert('This user is already a critic.');
	});
	$('div#profileMessage span#nominate-button.disabled.nominated').click(function(e) {
		alert('You have already nominated this user.');
	});
	$('div#profileMessage span#nominate-button.disabled.needs-member').click(function(e) {
		alert('You must be a logged-in member of L4Dmaps to use this feature.');
	});
	
	$('div#profileMessage span#report-button').not('.disabled').click(function(e) {
	
		if (confirm('Report this user for foul activity?')) {
		$.post('/ajax_assorted.php', {'intent':'report','user':$('#avatarBox').attr('rel')}, function(resp) {
		if (resp.status=='success') {
				alert(resp.message);
				$('div#profileMessage span#report-button').addClass('disabled').unbind('click');
			} else {
				alert(resp.message);
			}
		}, 'json');
		}
	});
	$('div#profileMessage span#report-button.disabled.needs-member').click(function(e) {
		alert('You must be a logged-in member of L4Dmaps to use this feature.');
	});
	$('div#profileMessage span#report-button.disabled.reported').click(function(e) {
		alert('You have already reported this user.');
	});
	
	// gentags chartags | chosentags | create add remove 
	tags = new Object();
	tags.chosen = new Object();
	tags.count = $('#tag-count').text();
	
	$('#chosentags option').each(function(i,e) {
		tags.chosen[$(e).text()] = $(e).text();
	});

	
	$('div.tagwidget #create').click(function(e) {
		e.preventDefault();
	
		if (typeof (tags.chosen[$('#createbox').val()]) == 'undefined') {

			if (tags.count<10) {
				tags.chosen[$('#createbox').val()] = $('#createbox').val();

				$('#chosentags').append('<option>' + strip($('#createbox').val()) + '</option>');
				$('#createbox').val('');

			} else {
				alert('You can only use 10 tags');
				return false;
			}

		} else {
			alert('Tag has already been used');
		}
		return false;
	});
	

	$('div.tagwidget #add').click(function(e) {
		e.preventDefault();

		tags.add();
		return false;
	});
	
	
	$('div.tagwidget #remove').click(function(e) {
		e.preventDefault();
		tags.remove();
		return false;
	});
	
	$('div.tagwidget #chartags option, div.tagwidget #gentags option').dblclick(function(e) {
		e.preventDefault();
		tags.add();
		return false;
	});
	
	$('div.tagwidget #chosentags').dblclick(function(e) {
		e.preventDefault();
		tags.remove();
		return false;
	});
	
	$('div.tagwidget #myform').submit(function(e) {
		$('div.tagwidget #chosentags option').each(function(i,e) {
			$('div.tagwidget #myform').append('<input type="hidden" name="finaltags[]" value="'+$(e).text()+'" />');
		});
	});
	
	tags.add = function () {
		$('div.tagwidget #chartags option:selected, div.tagwidget #gentags option:selected').each(function(i,e) {
			if (typeof (tags.chosen[$('#createbox').val()]) == 'undefined') {
				if (tags.count<10 && $(e).attr('class')!='fixed') {
					$(e).addClass('selected').removeAttr('selected');
					var type = $(e).parent().attr('id');
					var ident = $(e).attr('id');

					$('#chosentags').append('<option class="'+type+'" id="'+ident+'">' + strip($(e).text()) + '</option>');
					$('#tag-count').text(++tags.count);
				} else {
					alert('You can only use 10 tags');
					$('div.tagwidget option').removeAttr('selected');
					return false;
				}
			}
		});
	}
	
	tags.remove = function () {

		$('div.tagwidget #chosentags option:selected').each(function(i,e) {
			delete tags.chosen[$(e).text()];
			if ($(e).attr('id')!='') {
				console.log('removing' + $(e).attr('id'));
				$('#' + $(e).attr('id')).removeClass('selected');
			}
			$(e).remove();

			$('#tag-count').text(--tags.count);
		});
	}
	
	
	window.rec_changing=false;
	$('#rec-more').click(function(e) {

		if (!window.rec_changing) {
			window.rec_changing=true;
			$.post('/ajax_assorted.php', {'intent':'recommended'}, function(resp) {
				$('#recommended-block').fadeOut(500, function() {
					$('#recommended-block').html(resp);
					$('#recommended-block').fadeIn(500, function() {
						window.rec_changing=false;
					});
				});
				
			}, 'html');
		}
	});
	
	$('#filter_toggle').click(function(e) {
		
		if ($(this).hasClass('open')) {
			$(this).removeClass('open').addClass('closed');
			$('.filterOptions').slideUp(500);
		} else {
			$(this).removeClass('closed').addClass('open');
			$('.filterOptions').slideDown(500);
		}
		
		
	});
	
});

function strip(html) {
	var tmp = document.createElement("DIV");
	tmp.innerHTML = html;
	return tmp.textContent||tmp.innerText;
}	
	

