// Functions used outside of Jquery
// 	-  called from within Flash
// 		Need to pass in video_path as well
//		Switch image_viewer to use this same method, to avoid hard coding vars in flash

function video_player(selectedID){
	if (selectedID == undefined) {
		selectedID = 0;
		} 
	GB_show('Video Player', 'popup/videoplayer/?id=' + selectedID, 645, 946, 'video_player');
}

function general_alert(text){
	GB_show('Alert', 'popup/alert/' + text, 200, 400, 'general_alert');
}

function site_login(target_url){
	GB_show('Login', 'popup/login/?target=' + escape(target_url), 410, 710, '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;
        });
    });
} 

// Jquery based JS used on document.ready
$(document).ready(function(){
	 
	/********************************
	Greybox Popups
	********************************/
	$("a.confirm_leave").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  GB_show(t,this.href,406,710,'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*=transformersgame.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,406,710,'confirm_leave');
	  return false;
	});
	

	// close buttons or links for ALL popups
	$(".popup_close").click(function(){
		parent.parent.GB_hide();
	});

	/*****************************************************************
	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_pause").click(function(){
		videoPause(); //videoPause() is defined in jquery.greybox.js
		return false;
	});
	$("a.video_kill").click(function(){
		killVideo(); //videoPause() is defined in jquery.greybox.js
		return false;
	});
	
	$("a.image_viewer").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  GB_show(t,this.href,490,810,'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"
	});
	

	// 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();
	});
	

	// fancy scroll to top on #top link!
	$("p.top a").click(function(){
		$("html").animate({scrollTop: 0}, "slow");
		return false;
	});

	/*****************************************************************
	BLOG / NEWS list posts
	fancy animation when user selects article link (or click on LI).
		- Do not apply to .permalinks, we use those for direct links
			to a news/ blog post
	*****************************************************************/
/*
	$("#platforms a").click(function() {
		// switch class="current" for navigation item
		$("#posts li,#platforms li").removeClass("current");
		$(this).parent("h2").parent("li").addClass("current");
		// asign value of the link target
		var thisTarget = $(this).attr("rel");
		// show target block, hide other block
		$(".selected").hide("slow");
		$(thisTarget).show("slow").addClass("selected");
		return false;
	});
*/
	/*$("#posts li p").click(function() {
		// switch class="current" for navigation item
		$("#posts li").removeClass("current");
		$(this).parent("li").addClass("current");
		// asign value of the link target
		var thisTarget = $(this).siblings("h2").children("a").attr("rel");
		// show target block, hide other block
		$(".selected").hide("slow");
		$(thisTarget).show("slow").addClass("selected");
		return false;
	});*/
	
	// hover on P tags
    $("#posts li p").hover(function () {
		$(this).css({'cursor' : 'pointer'});
      }, function () {
		$(this).css({'cursor' : 'default'});
      }
    );
	
	/*****************************************************************
	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;
	});


});


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);
	}
}
	
$(document).ready(function(){
	$('.toggle_radio:radio').change(function() {  
		highlight_avatar($(this).attr('id'));
	});
});

