// Functions used outside of Jquery
// -most called from within flash
function video_player(){
	GB_show('Video Player', this.href, 928, 565, 'video_player');
}
function general_alert(text){
	GB_show('Alert', 'popup/alert/' + text, 200, 400, 'general_alert');
}
// Loads a div with ajax content, and makes pagination links call this function
function loadPiece(href,divName) {
    $(divName).load(href, {}, function(){
        var divPaginationLinks = divName+" .pagination a";
        $(divPaginationLinks).click(function() {
            var thisHref = $(this).attr("href");
            loadPiece(thisHref,divName);
            return false;
        });
    });
}
// fom SBERRY for use in the forums:
var selected_avatar;
function set_selected_avatar(id) {
	selected_avatar = id;
}
function highlight_avatar(id) {
	if (selected_avatar != id) {
		if (selected_avatar != undefined) {
			$('#'+selected_avatar).parents('.avatar_item').removeClass('avatar_item_selected');
		}
		$('#'+id).parents('.avatar_item').addClass('avatar_item_selected');
		set_selected_avatar(id);
	}
}
/************************************************
Jquery based JS used on document.ready
************************************************/
$(document).ready(function(){

	// disable coming_soon links
	$('.coming_soon').click(function() {
		return false;
	});

	// fancy scroll to top on #top link!
	// No workie in WEBKIT
	$("p.top a").click(function(){
		$("html").animate({scrollTop: 0}, "slow");
		return false;
	});

	/********************************
	Greybox Popups
	********************************/
	$("a.confirm_leave").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  GB_show(t,this.href,774,441,'confirm_leave');
	  return false;
	});

	// greybox for links that are entered into the news admin, where HTML is NOT permitted
	// ignore links that contain wolfenstein.com, so as to not invoke greybox on internal links.
	$('a.postlink').not('a.postlink[href*=blurgame.com]').click(function(){
	 var t = this.title || $(this).text() || this.href;
	 var post_link_href = BASE_URL_GB+"popup/confirmleave/?url="+encodeURIComponent(this.href);
	 GB_show(t,post_link_href,774,441,'confirm_leave');
	  return false;
	});

	$("a.image_viewer_link").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  GB_show(t,this.href,774,441,'image_viewer');
	  return false;
	 // sample URL to send to image_viewer:
	 // popup/imageviewer/?img=http://foo.com/bar.jpg&amp;dl_path=http://foo.com/bar.zip"
	});

	// close buttons or links for ALL popups
	$(".popup_close").click(function(){
		parent.parent.GB_hide();
	});

	// prevent requests for #
	$(".close").click(function(){
		return false;
	});

	/* USED on the videos page to popup som mofo videos */
	$("a.video_player").click(function(){
		GB_show('Video Player', this.href, 928, 565, 'video_player');
		return false;
	 // sample URL to send to video_player:
	 // popup/videoplayer/?url=http://cdn.djhero.com/djhero/media/191/noisa_mix_medium.flv&title=E3 Gameplay"

	});

	/*****************************************************************
	These functions are primarily run from within Flash
	these are HTML versions, used for testing
		- add <a href="#" class="foo_bar"> for simulation
	*****************************************************************/
	/*
	$("a.video_player").click(function(){
		video_player();
		return false;
	}); */

	$("a.video_player_akamai").click(function(){
		video_player_akamai();
		return false;
	});
	$("a.video_pause").click(function(){
		videoPause(); //videoPause() is defined within video player SWFs
		return false;
	});
	$("a.video_kill").click(function(){
		killVideo();
		return false;
	});

	// replace target="_blank"
	$("[rel='external']").click(function(){
		this.target = "_blank";
	});

	// hide gamertag inputs until platform is checked
	$("label.gamer_tag, label.gamer_tag + input.small").hide();

	$(".platforms input[type='checkbox']").click(function() {
		$(this).siblings("label.gamer_tag,input.small").toggle();
	});

	/*****************************************************************
	PAGINATION
		- values are LANG specific, so we first get the LANG specific
			value.
	*****************************************************************/
	var page_jump_lang = $("#page_jump").val();
	$("#page_jump").focus(function(){
		if ($(this).val() == page_jump_lang) {
			$(this).val("");
		}
	});
	$("#page_jump").blur(function(){
		if ($(this).val() == '') {
			$(this).val(page_jump_lang);
		}
	});

	/*****************************************************************
	WALLPAPER ROLLOVERS
	*****************************************************************/
	$("li.wallpaper_item").hover(function(){
		$(this).children(".rollover").show();
	}, function(){
		$(this).children(".rollover").hide();
	});

	$(".wallpaper .preview").click(function(){
		GB_show(this.href,this.href,600,800,'wallpaper_preview');
		return false;
	});

	/*****************************************************************
	LOCKED CONTENT ALERTS (media/screenshots/)
	*****************************************************************/
	$("li.locked_content").hover(function(){
		$(this).children(".locked_alert").show();
	}, function(){
		$(this).children(".locked_alert").hide();
	});

	$("a.locked_link").click(function(){
		return false;
	});

	/*****************************************************************
	BETA REDEMPTION /beta/ Marketing info on index page
	*****************************************************************/
	$(".set_lang_wrap a").click(function(){
		$(".hide input").attr("disabled", "disabled");
	});
	// remove disabled attribute if specific countries have been selected
	$(".set_lang_wrap a#gb,.set_lang_wrap a#fr,.set_lang_wrap a#de,.set_lang_wrap a#at,.set_lang_wrap a#ch,.set_lang_wrap a#es,.set_lang_wrap a#it,.set_lang_wrap a#no,.set_lang_wrap a#fi,.set_lang_wrap a#is,.set_lang_wrap a#dk,.set_lang_wrap a#se").click(function(){
		$(".hide input").attr("disabled","");
		return false;
	});
	$(".set_lang_wrap a").click(function(){
		$(".error").remove();
		$(".set_lang_wrap li").removeClass("flag_selected");
		$(this).parent("li").addClass("flag_selected");
		var selected_set_lang = $(this).attr("href");
		var selected_country = $(this).attr("id");
		// populate hidden inputs
		$("#set_lang").val(selected_set_lang);
		$("#collect_country").val(selected_country);
		// display retailer selection
		$("p.subheader_2").show("fast");
		$("ul.collect_beta").parent("div").hide("fast");
		var thisTarget = $(this).val();
		$("div."+selected_country).show("fast");
		$("input[type=radio]").attr("checked", "");
		return false;
	});
	/*****************************************************************
	EXISTING ACCOUNT LOGIN ON REGISTRATION PAGE
	*****************************************************************/
	$('#register_existing_update').click(function(){
		$.post('register/account/ajax', { email: $('#email').val(), password: $('#password').val() }, function(r){
			if (r == 'true')
			{
				// Do a phpbb login
				$('#register_existing').attr('action', 'board/ucp.php?mode=login');
				$('#email').attr('name', 'username'); // Email field must be 'username'
			}

			$('#register_existing').submit();

		}, 'text');
		return false;
	});
	// fom SBERRY for use in the forums:
	$('.toggle_radio:radio').change(function() {
		highlight_avatar($(this).attr('id'));
	});
});

// use to track shit in google
function recordOutboundLink(category, action, label) {
	try {
		var pageTracker = _gat._getTracker("UA-4618468-14");
		pageTracker._trackEvent(category, action, label);
	} catch(err){}
}

