$(function() {
	$(
		'#login, #password'
	).focus(function() {
		this.defaultValue = this.defaultValue || this.value;
		if (this.defaultValue == this.value) this.value = '';
	}).blur(function() {
		if (!this.value) this.value = this.defaultValue;
	});
});

$(function() {
	$('div.badlogin').map(function(){
		alert('Ошибка авторизации: неправильно указан логин или пароль.')
	})
});

$(function(){
	if(window['forumPostHandler']) forumPostHandler();
});

$(function(){
	var removePlayer = function(){ $(this).prev().andSelf().remove() };
	$('#addPlayerToMyGroup + dl').find('dd').click(removePlayer)
	$('#addPlayerToMyGroup').click(function(){
		$(this).prev('select').find('option:selected[value != "--"]').map(function(){
			if($('#addPlayerToMyGroup').next('dl').find('dt input[value=' + $(this).val() + ']').length > 0) return;
			$('<dt>').append($('<input>').attr({type: 'hidden', name: 'players[]'}).val($(this).val())).append($('<a>').attr({href: '/personal/' + $(this).text() + '/', _target: 'blank'}).text($(this).text())).appendTo('#addPlayerToMyGroup + dl')
			$('<dd>').text('удалить').click(removePlayer).appendTo('#addPlayerToMyGroup + dl')
		})
	})
});

$.each(
	{
		'div#audio div.player'     : {swf : '/i/mp3player.swf',      width: 526, height: 53, bgcolor: '#000000'},
		'div#audio div.player-mini': {swf : '/i/mp3player-mini.swf', width: 325, height: 39, bgcolor: '#4D4D4D'}
	}, function(path, settings) {
		$(path).each(function(){
			var div = $(this);
			var id = div.attr('player:id');
			var so = new SWFObject(settings.swf, 'player' + id, settings.width, settings.height, 6, settings.bgcolor);

			$.each(['id', 'title', 'mp3', 'group', 'deletez', 'contest'], function(i, varName) {
				so.addVariable(varName, div.attr('player:' + varName));
			});

			so.addParam('allowScriptAccess', 'always');
			so.addParam('menu',  'false');

			so.write($(this).attr('id'));
		})
	}
);

$('#deleteidea').click(function(){
	return window.confirm("Вы действительно хотите удалить идею????");
});

function urbanSoundAction(_do){
	if(window.confirm('Вы уверены ?')){
		$.post("/group.action", {action: _do.action, id: _do.id}, function(data){
			if(data.res == 1) window.location.href = window.location.href.replace(/#.+$/,'')
		}, "json")
	}
}

function callExternalInterface(p) {
	$("object, embed").each(function(i){
		try{
			this.goHome(p);
		} catch(e){}
	})
}

$(function() {
	openVeil = function(href, imgs, rel) {
		$('body').addClass('veil');
		if(imgs.length > 0) {
			loadImage(imgs[rel]);
			createLinks(rel, imgs); 
		} else {
			loadImage(href);
		}
		
		return false;
	}

	createLinks = function(cInx, imgs) {
		cInx = parseInt(cInx);
		if(cInx - 1 >= 0) {
			$("#navlinks a:first").css("display", "inline");
			$("#navlinks span:first").css("display", "none");
			$("#navlinks a:first").attr('href', imgs[cInx - 1]);
			$("#navlinks a:first").attr('rel', cInx - 1);
		} else {
			$("#navlinks a:first").css("display", "none");
			$("#navlinks span:first").css("display", "inline");
		}

		if(cInx + 1 < imgs.length) {
			$("#navlinks a:last").css("display", "inline");
			$("#navlinks span:last").css("display", "none");

			$("#navlinks a:last").attr('href', imgs[cInx + 1]);
			$("#navlinks a:last").attr('rel', cInx + 1);
		} else {
			$("#navlinks a:last").css("display", "none");
			$("#navlinks span:last").css("display", "inline");

		}
	}

	loadImage = function(href) {
		var img = $('<img>');
		img.attr({src: href}).load(function() {
			var height   = img[0].height;
			var topspace = (600 - height)/2;
			img.css('margin-top', topspace);
			$('#popupframe').empty().append(img);
		})
	}

	 $("#navlinks a").click(function() {
		loadImage(this.href);
		cInx =  $(this).attr('rel');
		var imgs = $('body.userphotos a.popup, body.calendar a.popup');
		createLinks(cInx, imgs); 
		return false;
	});
	
	$('body.userphotos a.popup, body.city-photos a.popup, body.calendar a.popup').click(function() {
		$('body.userphotos form select').toggle();
		var imgs = $('body.userphotos a.popup, body.calendar a.popup');
		if(this.rel != null) {
		  rel = this.rel - 1;
		} else {
			rel = null;
		}
		return openVeil(this.href, imgs, rel);
	});

	$("#closeVeil").click(function(){
		$('body.userphotos form select').toggle();
		$("body").removeClass("veil");
		return false;
	});

});

$(function() {
	$('div.statistic div.void a').click(function(){
		var _self_ = this;
		$.post($(this).attr('action'), {action: 'vote', id: $(this).attr('id'), cid: $(this).attr('cid')}, function(data){
			if(data.res == 1) {
				var rate = $(_self_).parents('div.statistic').find('div:first span')
				rate.text(parseInt(rate.text()) + 1)
				$(_self_).fadeOut('normal')
			}
		}, "json")
		$(this).unbind("click").click(function(){ return false })
		return false;
	})
});

$('div#video div.play').each(function(){
	var div = $(this);
	var id = div.attr('play:id');
	var size = new Array(425, 344);
	if($("body").attr("class") == "root" || $("body").attr("class").indexOf("dt-news") > 0) {
		size = new Array(281, 232);
	}
	var so = new SWFObject("http://www.youtube.com/v/"+id+"&hl=en&fs=1&", 'video', size[0], size[1], 6, "#000000");

	so.addParam('allowscriptaccess', 'always');
	so.addParam('allowfullscreen',  'true');

	so.write($(this).attr('id'));
});

$(function() {
	if($("body").attr("class") == "root") {
		var bannerName = $('#proactivePromo').attr('name');
		var so = new SWFObject("/i/su-"+bannerName+".swf", 'proactivePromo', '396', '158', 6, "#000000");
		so.addParam('allowscriptaccess', 'always');
		so.addParam('allowfullscreen',  'true');
		so.addParam('wmode', 'transparent');
		so.write("proactivePromo");
	}
});

