jQuery(document).ready(function($) {	
	//if (document.getElementById('tab_content')) {
	if ($('#tab_links')) {
		$('#tab_content ul').hide();

		$('#tab_content ul:first').fadeIn('slow');
		// a tab_links li a for a's
		$('#tab_links li').each(function(index) {
			$(this).removeClass('selected');
			$(this).bind('click', function(event) {
				
				$('#tab_content ul').fadeOut('slow');
				$('#tabs_' + $(this).text().toLowerCase().replace(' ','')).fadeIn('slow');
			});
		});
/*
		var tabs = document.getElementById('tab_links').children;
		$('#tabs_' + tabs[0].innerHTML.toLowerCase()).fadeIn('slow');

		for (i = 0; i < tabs.length; i++) {
			$(tabs[i]).bind('click', function(event) {
				$('#tab_content ul').fadeOut('slow');
				$('#tabs_' + this.innerHTML.toLowerCase()).fadeIn('slow');
			});
		}
*/
	}
	$('#acc').accordion({
		animated: [{height: 'toggle', duration: 'normal'}],
		autoHeight: false
	});

	if ($('p').hasClass('short_info')) {
		$('.more_info').hide();

		$('.short_info a').bind('click', function(event) {
			$('.short_info').hide();
			$('.more_info').show();
		});
	}

	$('a[rel*=facebox]').facebox() 
	$('input.jsdisable').attr('disabled', 'disabled');
	$('#loading').hide();
	
	if ($('.motion').find('img')) {
		setTimeout('motionone()', 5000);
	}
	if ($('.youtube').find('img')) {
		setTimeout('fadeshot()', 5000);
	}
	
	$('.polaroid').each(function (i) {
		$(this).after('<p class="polaroid_title" style="width: ' + $(this).attr('width') + 'px;">' + $(this).attr('title') + '</p>');
	});
	
	$('.hovertitle img').bind('mouseover', function() {
		$('.hovertitle_title').html($(this).attr('title'));
	});
	$('.hovertitle_b img').bind('mouseover', function() {
		$('.hovertitle_title_b').html($(this).attr('title'));
	});
	
});

function movie(code, div, provider) {
	if (!provider) {
		provider = 'video';
	}

	var play = new SWFObject('/elements/flash/player.swf', 'ply', 640, 380, "9", "#000000");
	play.addParam('allowfullscreen', 'true');
	play.addParam('allowscriptaccess', 'always');
//	play.addParam('flashvars', 'file=/videos/play/' + code + '&type=flv&image=/videos/image/' + code);

	if (provider == 'youtube') {
		play.addVariable('file', code);
		code = code.split('/');
		gt = code[0];
		code = code[5];
		play.addVariable('image', '/videos/image/' + gt + '/' + code + '.jpg');
	} else {
		play.addVariable('file', '/videos/play/' + code);
		play.addVariable('image', '/videos/image/' + code.replace('flv', 'png'));
	}
	play.addVariable('provider', provider);

	play.write(div);
}

function player(url, div, width, height) {
	if (!width) width = 640;
	if (!height)  height = 380;
	if (!div) div = 'video';

	var player = new SWFObject(url, 'ply', width, height, "9", '#000000');

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

	player.write(div);
}

function doimages() {
	gt = $('#gamertag').val();
	if (!gt) { return; }
	$('#avatarbody').attr('src', 'http://avatar.xboxlive.com/avatar/' + gt + '/avatar-body.png');
	$('#avatarlarge').attr('src', 'http://avatar.xboxlive.com/avatar/' + gt + '/avatarpic-l.png');
	$('#avatarsmall').attr('src', 'http://avatar.xboxlive.com/avatar/' + gt + '/avatarpic-s.png');
	$('#mgc').attr('src', 'http://card.mygamercard.net/' + gt + '.png');

	$('#profile_control .right img').attr('src', 'http://avatar.xboxlive.com/avatar/' + gt + '/avatarpic-s.png');
}

function checkgt() { check('gamertag'); }
function checkemail() { check('email_address'); }

var gtok = 0;
var eaok = 0;
function check(what) {
	var gt = $('#' + what).val();

	$('#' + what + '_resp').fadeOut('slow', function() {
		$('#' + what + '_resp').html('');
		$('#' + what + '_resp').removeClass('user_warning');
		$('#' + what + '_resp').removeClass('user_notice');

		if (gt) {
			jQuery.ajax({
				url:		'/profile/new/check/?check=' + what + '&' + what + '=' + gt,
				success:	function(data) {
					if (result) {
						$('#' + what + '_resp').html('Your ' + what.replace('_', ' ') + ' is valid');
						$('#' + what + '_resp').addClass('user_notice');

						if (what == 'gamertag') {
							doimages();
							gtok = 1;
						} else {
							eaok = 1;
						}

						if (gtok && eaok) {
							$('input.jsdisable').attr('disabled', '');
						}
					} else {
						if (message != 'offline') {
							$('#' + what + '_resp').html('Your ' + what.replace('_', ' ') + ' is ' + message);
						} else {
							$('#' + what + '_resp').html('Data Resources are offline, please try again later');
						}
						$('#' + what + '_resp').addClass('user_warning');

						if (what == 'gamertag') {
							gtok = 0;
						} else {
							eaok = 0;
						}
					}
				},
				dataType:	'script'
			});
		} else {
			$('#' + what + '_resp').html('You Must provide your ' + what.replace('_', ' ') + '');
			$('#' + what + '_resp').addClass('user_warning');
			if (what == 'gamertag') {
				gtok = 0;
			} else {
				eaok = 0;
			}
		}
		$('#' + what + '_resp').fadeIn('slow');
	});
}

/* video page screenshot slide up slide down */
var pos = 0;
var dir = 1;
function motionone() {
	$('.motion').find('img').each(function() {
		if (dir == 1) {
			$(this).animate({
				top: '-=' + $(this).parent().height() + 'px'
			}, 5000);
		} else {
			$(this).animate({
				top: '+=' + $(this).parent().height() + 'px'
			}, 5000);
		}
	});
	pos += dir;
	if (pos == 2 && dir == 1) {
		dir = -1;
	}
	if (pos == 0 && dir == -1) {
		dir = 1;
	}
	setTimeout('motionone()', 7500);
}
/* youtube page fade shots */
var x = 0;
var fadeshotgo = false;
var fadeshotstop = false;
function fadeshot() {
	if (fadeshotstop == true) {
		clearTimeout(fadeshotgo);
		fadeshotgo = false;
		fadeshotstop = false;
		return;
	}
	// make first visible img fade out
	x += 1;
	$('.youtube').each(function() {
		$(this).children('img:visible').first().fadeOut('slow', function() {
			if (x == 3) {
				$('.youtube').each(function() {
					$(this).children('img:hidden').first().fadeIn('fast', function() {
						$('.youtube').each(function() {
							$(this).children('img:hidden').fadeIn('fast');
						});
					});
				});
				x = 0;
			}
		});
	});
	// works for one youtube not multiple
//	$('.youtube img:visible').first().fadeOut('slow', function() {
		// how many left
//		x += 1;
//		if (x == 3) {
//			$('.youtube img:hidden').first().fadeIn('fast', function() {
//				$('.youtube img:hidden').fadeIn('fast');
//			});
//			x = 0;
//		}
//	});

	if (!fadeshotgo) {
		fadeshotgo = setInterval('fadeshot()', 5000);
	}
}
function youtube_dsp(code, thetitle) {
	if (!$('#dialogplayer').size()) {
		$('<div id="dialogplayer"></div>').prependTo('body');
	}
	$('#dialogplayer').dialog({
		draggable: false,
		height: 422,
		modal: true,
		resizable: false,
		title: thetitle,
		width: 670,
		
		show: 'blind',
		hide: 'blind',
		
		open: function(event, ui) {
			fadeshotstop = true;
			player('http://youtube.com/v/' + code, 'dialogplayer');
		},
		close: function(event, ui) {
			fadeshotstop = false;
			fadeshot();
			$('#dialogplayer').remove();
		}
	});
}
function video_dsp(url, thetitle) {
	if (!$('#dialogplayer').size()) {
		$('<div id="dialogplayer"></div>').prependTo('body');
	}
	$('#dialogplayer').dialog({
		draggable: false,
		height: 422,
		modal: true,
		resizable: false,
		title: thetitle,
		width: 670,
		
		show: 'blind',
		hide: 'blind',
		
		open: function(event, ui) {
//			player('http://360gaming.net/v/' + code, 'dialogplayer');
//			movie(code, 'dialogplayer');
			player(url, 'dialogplayer');
		},
		close: function(event, ui) {
			$('#dialogplayer').remove();
		}
	});
}

// uploadify
function createupload(el, buttontext, dir) {
	$("#" + el).uploadify({
		uploader			: "/elements/js/jquery_plugins/uploadify/uploadify.swf",
		script				: dir,
		folder				: "irrelvant",
		multi				: false,
		auto				: true,
		sizeLimit			: 45 * 1024 * 1024,
		cancelImg			: "/elements/js/jquery_plugins/uploadify/cancel.png",
		onComplete			: function (e, q, f, r) { filedone(r); },
		buttonText			: buttontext
	});
}

