function profile_html(html) {
	var form_ = $('.profile_body ._form');
	form_.html(html);
	
	$('form select[name=category_id]', form_).change(function(){
		if(this.selectedIndex)
			$('input[name=category_name]', this.form).val(this.options[this.selectedIndex].text);
		else
			$('input[name=category_name]', this.form).val('');
	}).change();
	
	$('form', form_).smoothForm({
		processing: $('<span>Processing data...</span>'),
		success: function(html) {
			$('.profile_body ._form').fadeOut('fast', function(){
				profile_html(html);
			});
		}
	});
	
	form_.fadeIn();
}

function profile_click() {
	$('.profile_body').slideDown('slow');
	
	window.location.hash = this.href.replace(/^.*(profile\/\w+)\.php$/, '$1');
	
	$(this).addClass('sel').siblings('a').removeClass('sel');
	var cnt_ = 0;
	var html_ = false;
	var draw_ = function(){
		if(++cnt_ < 2)
			return;
		profile_html(html_);
	};
	$('.profile_body ._form').fadeOut('fast', draw_);
	$.get(this.href, function(html){ html_ = html; draw_(); })
	
	return false;
}

function profile_init() {
	$('.profile_body ._menu a').click(profile_click);
	
	$('.profile_hdr ._edit a').click(function(){
		$('.profile_body').slideToggle('slow');
		return false;
	});
	$('.profile_hdr ._links a, .profile_hdr ._user a').click(function(){
		var url = this.href;
		if($('.profile_body:visible').length > 0)
			url += (url.match(/\?/) ? '&' : '?') + 'edit=1' + window.location.hash;
		window.location = url;
		return false;
	});
	
	$('.profile_photo_box ._actions a').click(profile_click);
	$('.profile_album_box ._actions a').click(profile_click);
	$('.profile_video_box ._actions a').click(profile_click);
	
	if(window.location.hash) {
		var url = window.location.hash.substring(1, window.location.hash.length) + '.php';
		$('.profile_body ._menu a[href$='+url+']').click();
	}
}

function media_init() {
	$('.media_sel .media_nav ._prev').click(function(){
		var i = $('.media_sel .media_nav ._page').index($('.media_sel .media_nav ._sel')) - 1;
		var i = $('.media_sel .media_nav ._page').eq(i).click();
		return false;
	});
	$('.media_sel .media_nav ._next').click(function(){
		var i = $('.media_sel .media_nav ._page').index($('.media_sel .media_nav ._sel')) + 1;
		var i = $('.media_sel .media_nav ._page').eq(i).click();
		return false;
	});
	$('.media_sel .media_nav ._page').click(function(){
		var i = $('.media_sel .media_nav ._page').index(this);
		$('.media_sel .media_nav ._page').removeClass('_sel');
		$(this).addClass('_sel');
		var t = $('.media_sel .media_thumbs');
		$('.media_sel .media_thumbs ._box').hide();
		var b = $('.media_sel .media_thumbs ._box');
		for(var c = 0; c < 4; c++)
			b.eq(4 * i + c).hide().fadeIn();
		//$('.media_sel .media_thumbs').scrollTop($('._box', t).eq(i * 4).offset().top - t.offset().top - $('._box', t).css('margin-top').replace(/\D+/, ''));
		return false;
	});
	$('.media_sel .media_nav ._sel').click();
}

function homepage_init() {
	$('#media_overview ._tab_btn').click(function(){
		var colors = [ '#94020e', '#70009b', '#1a5702' ];
		var i = $('#media_overview ._tab_btn').index(this);
		var me = this;
		$('#media_overview ._tab_btn').each(function(){
			var bg = $(this).css('background-image');
			if(this == me) {
				$(this).css({
					color: '#fff',
					backgroundImage: bg.replace(/_off\.png/, '_on.png'),
					backgroundColor: colors[i]
				});
			} else {
				$(this).css({
					color: '#555',
					backgroundImage: bg.replace(/_on\.png/, '_off.png'),
					backgroundColor: '#000'
				});
			}
			$(this)
				.data('bg', $(this).css('background-image'))
				.data('fg', $(this).css('color'));
		})
		$('#media_overview ._tab_body').hide().eq(i).fadeIn();
		return false;
	}).mouseover(function(){
		$(this).css({
			color: '#fff',
			backgroundImage: $(this).data('bg').replace(/_off\.png/, '_on.png')
		});
	}).mouseout(function(){
		$(this).css({
			color: $(this).data('fg'),
			backgroundImage: $(this).data('bg')
		});
	}).eq(0).click();
	
	$('#media_overview ._media_box').each(function(){
		var txt = $('._body a', this).text();
		var url = $('._body a', this).attr('href');
		var a = $('<div style="background:#000; opacity:0.5; filter:alpha(opacity=50); position:absolute; z-index:1; left:0; top:4px; width:216px; height:135px;"></div>' +
			'<a class="fbshare" href="http://www.facebook.com/sharer.php?u='+escape(url)+'&t='+escape(txt)+'" style="position:absolute; z-index:2; left:10px; top:100px;"><span class="_btn"><span class="_txt">Share</span></span></a>' +
			'<a class="retweet" href="http://twitter.com/home?status='+escape(txt+'\n'+url)+'" style="position:absolute; z-index:2; right:10px; top:100px;"><span>&#9851;&nbsp;Retweet</span></a>' +
			'<a class="_det" href="'+url+'"><span></span></a>')
		a.hide().appendTo(this);
		var t = null;
		$(this)
			.mouseover(function(){ if(t) window.clearTimeout(t); t = null; a.fadeIn(); })
			.mouseout(function(){ t = window.setTimeout(function(){ a.fadeOut(); }, 100); });
	});
	
	if(window.h_fn && h_fn.length) {
		var h_fn_i = 0;
		var h_fn_t = null;
		var h_fn_show = function(){
			var img = new Image();
			$(img).load(function(){
				$('#header_box ._img').fadeOut(function(){
					$('#header_box ._img').css('background-image', 'url(' + h_fn[h_fn_i].pic + ')');
					$('#header_box ._line1').html(h_fn[h_fn_i].ttl);
					$('#header_box ._line2').html(h_fn[h_fn_i].bdy);
					$('#header_box ._img').fadeIn();
					$('#header_box ._cont').data('url', h_fn[h_fn_i].url).css('cursor', h_fn[h_fn_i].url != '' ? 'pointer' : 'auto')
					if(h_fn.length > 1) {
						h_fn_t = window.setTimeout(function(){
							$('#header_box ._btn_right a').click();
						}, 6000);
					}
				});
			});
			img.src = h_fn[h_fn_i].pic;
		};
		$('#header_box ._img').hide();
		h_fn_show();
		$('#header_box ._btn_left a').click(function(){
			window.clearTimeout(h_fn_t);
			if(--h_fn_i < 0)
				h_fn_i = h_fn.length - 1;
			h_fn_show();
			return false;
		});
		$('#header_box ._btn_right a').click(function(){
			window.clearTimeout(h_fn_t);
			if(++h_fn_i >= h_fn.length)
				h_fn_i = 0;
			h_fn_show();
			return false;
		});
		$('#header_box ._cont').click(function(){
			var url = $(this).data('url');
			if(url != '')
				location.href = url;
		});
	}
}

function popup_init() {
	var popPars = { arrowKey:false, rel:'nofollow' };
	$(document).bind('cbox_complete', function(){
		$('#cboxLoadedContent a[rel=popup]').colorbox(popPars);
		$('#cboxLoadedContent form').smoothForm({
			success: function(html){
				var par = {};
				for(var i in popPars)
					par[i] = popPars[i];
				par.html = html;
				$.colorbox(par);
			}
		});
		$('#cboxLoadedContent input:first').focus();
		$('#cboxLoadedContent .message ._ttl a').click(function(){
			var a = $(this);
			a.parent().siblings('._body').slideToggle('normal', function(){
				if(a.hasClass('_unseen'))
					$.get(a.attr('href'));
				a.removeClass('_unseen');
			});
			return false;
		});
		$('#cboxLoadedContent .message a._del').click(function(e){
			return confirm('Permanently delete this message?');
		});
	});
	$('a[rel=popup]').colorbox(popPars);
}

$(function() {
	$('.pngfix').ifixpng();
	$('.pngfix_str').ifixpng();
	$('.tabcontrol').tabs();
	
	$('#top_links a').mouseover(function(){
		var src = $('img', this).attr('src');
		$(this).data('img_src', src);
		$('img', this).attr('src', src.replace('_off', '_on'));
	}).mouseout(function(){
		$('img', this).attr('src', $(this).data('img_src'));
	});
	
	$('.news_title, .news_toggle').click(function(){
		var art = $(this).parents('.news_article');
		$('.short', art).toggle();
		$('.long', art).slideToggle();
		
		return false;
	});
	
	profile_init();
	
	media_init();
	
	$('select#url1, select#url2').change(function(){
		var url = location.href.replace(/([&\?])y=\d+/, '');
		location = url + (url.match(/\?/) ? '&' : '?') + 'y=' + this.options[this.selectedIndex].value.replace(/^.*sYear=(\d+).*$/, '$1');
	});
	
	homepage_init();
	
	popup_init();
});

