jQuery.preloadImages = function(){for(var i = 0; i<arguments.length; i++){jQuery("<img>").attr("src", arguments[i]);}}

/*for use in youtube video*/
var params = {allowScriptAccess:"always",wmode: "transparent"};
var atts = {id:"jsvideoplayer"};

/***setup the various js features for all pages***/
function initPage(pPage) {
	switch(pPage) {
		case 'home':
			updateNavi('home');	
			break;
		case 'localhoops':
		case 1:
			updateNavi('localhoops');	
			updateSectionTitle('Local Hoops');
			break;			
		case 'allstar':
		case 2:		
			updateNavi('allstar');	
			updateSectionTitle('All Star');
			break;			
		case 'nba':
		case 3:		
			updateNavi('nba');	
			updateSectionTitle('NBA');
			break;			
		case 'performancezone':
		case 4:		
			updateNavi('performancezone');	
			updateSectionTitle('Performance Zone');
			break;			
		case 'asia':
		case 5:		
			updateNavi('asia');	
			updateSectionTitle('Asia');
			break;			
		case 'bling':
		case 6:		
			updateNavi('bling');	
			break;			
		case 'news':
		case 7:
			updateNavi('news');	
			updateSectionTitle('News');
			break;			
		case 'eventscalendar':
		case 8:		
			updateNavi('eventscalendar');	
			updateSectionTitle('EVENTS CALENDAR');
			break;	
		case 'gallery':
		case 9:
			updateNavi('gallery');	
			break;	
		case 'basketballacademy':
		case 10:		
			updateNavi('basketballacademy');	
			updateSectionTitle('BASKETBALL ACADEMY');
			break;				
	}
	
	initMailingListForm();
	
	//enable rollover effect for all images within the page
	$("img:.rollover").hover(function() { this.src = this.src.replace("_off", "_on");}, function() { this.src = this.src.replace("_on", "_off"); });
}


/***set navigation button style for the current page***/
function updateNavi(pPage) {
	var pageNav = $('#nav_'+pPage+' img');
	var imgSrc = $(pageNav).attr('src').replace("_off", "_on");

	$(pageNav).removeClass('rollover');
	$(pageNav).attr('src',imgSrc);
}

/**update section title***/
function updateSectionTitle(pTitle) {
	$('#section_title').html(pTitle);
}


/***setup validation for mailing list form***/
function initMailingListForm() {
	if($("#mailingForm").length > 0) {
		$(function() {
			$("#mailingForm").validate({
				errorContainer: $("#formWarning"),
				errorLabelContainer: "#formWarning",								 
				rules: {
					emailAaddress: {required: true,email: true}
				},
				messages: {
					emailAaddress: "Please fill in a valid email address"
				},
				submitHandler: function(form) {
					submitMailingForm();
				}		
			});	
		});			
	}	
}

/***process the mailing list submission***/
function submitMailingForm() {
	var enteredEmail = $("input#emailAaddress").val();
	enteredEmail = enteredEmail.replace(" ","%20");

	var dataString = 'email=' + enteredEmail;
	$.ajax({
		type: "POST",
		url: "mailing-list-process.php",
		data: dataString,
		success: function() {
		   $('#formWarning').css({'display':'block'});
		   $('#formWarning').text('Your email has been added to our mailing list').fadeIn('slow').animate({opacity: 1.0}, 3000).fadeOut('slow', function() {$('#formWarning').text('');});
		   document.mailingForm.reset();
		}
	});
	return false;	
}


/***setup navigations and controls for the photo album gallery***/
function init_albums() {
	//apply settings to the first album
	$('#album_container').find('.album_title:eq(0)').addClass('current');
	$('#album_container').find('.album:eq(0)').show();
	
	$('.album_title').click(function () {  
		//hide all albums
		$('#album_container').find('.album_title').removeClass('current');
		$('#album_container').find('.album').slideUp('slow');
									  
		//show the selected album
		$(this).next().slideDown('slow',function(){$(this).find('.page').show();});
		$(this).addClass('current');

         return false;  
     });  	

	//count the number of albums in the page and setup the gallery
	var totalAlbum = $("#album_container .album_photo").size();
	$("#album_container").find(".album_photo").each(function() {
		var tmpAlbumID = $(this).attr('id');													 
		$("a[rel='"+tmpAlbumID+"']").colorbox();
	});
	
	$('.album_control a').click(function () {  	
		var albumID = $(this).parents().find('.album_photo').attr('id');
		$('#'+albumID+'_control a').removeClass('current');
		
		var selectedIndex = $('#'+albumID+'_control a').index(this);
		$('#'+albumID+' .page').hide();
		$('#'+albumID).find('.page:eq('+selectedIndex+')').show();
		$(this).addClass('current');
		
		return false;
	});
}


/***update the article page background***/
function adjustArticleHeight() {
		var h =  $(document).height() - 485;
		$('#article_background').height(h);
}

/***update the gallery page background***/
function adjustGalleryHeight() {
		//var h = $('#site_top_container_gallery').outerHeight()-295;
		var h =  $(document).height() - 485;
		$('#article_background').height(h);
}


/***update the bling page background***/
function adjustBlingHeight() {
		//var h = $('#site_top_container_bling').outerHeight()-295;
		var h =  $(document).height() - 485;
		$('#article_background').height(h);
}


/***setup the bling item rollover effect***/
function initBlingItem() {
	$('.item_image a').mouseover(function (e) {  	
		var win = $(this).parent().find('.detail');
		if ($(win).is(":hidden")) {
			var mx = e.pageX;
			var my = e.pageY;
			var m = $(this).parent().offset();
			relativeX = mx - 100;
			var relativeY = (my - m.top) + m.top;
			$(win).css({'left':relativeX});
			$(win).css({'top':relativeY});
			$(win).show();
		}
	});	

	$('.item_image a').mouseout(function () {  	
		$(this).parent().find('.detail').hide();
	});	
}


/***load the basketball culture***/
function loadBasketballCulture() {
	var xmlLocation	= "basketball-culture.php";
	var slideCount = 0;

	$.ajax({
		type: "GET",
		url: xmlLocation,
		dataType: "xml",
		success: function(xml) {	
			$(xml).find('slide').each(function(){
				var imagePath = $(this).find('image').text();
				var imageAltText = $(this).find('alttext').text();
				var slideURL = $(this).find('url').text();
				$('<div class="content"></div>').html('<a href="'+slideURL+'" target="_blank"><img src="'+imagePath+'" alt="'+imageAltText+'"></a>').appendTo('#rotate_content');
				slideCount += 1;
			});
			
			if (slideCount > 0) {
				var tmpChildContent = "";	
				for (i=1;i<=slideCount;i++) {
					if ((i%2)==0) {
						tmpChildContent += "<li class=\"even\"><a href=\"#\">"+i+"</a></li>";		
					}
					else {
						tmpChildContent += "<li class=\"odd\"><a href=\"#\">"+i+"</a></li>";		
					}
				}
				$('<ul></ul>').html(tmpChildContent).appendTo('#rotate_control');

				$('#rotate_control li').click(function()
				{	
					$('#rotating_container .content').hide();
					var selectedIndex = $('#rotate_control ul li').index(this);
					$('#rotate_content').find('div:eq('+selectedIndex+')').fadeIn(2000).show();
					return false;
				});	
			}
		}
	});
}

/***update the current video that it is playing***/
function updateCurrentVideo(pVideoID, pVideoTitle) {
	ytplayer = document.getElementById("jsvideoplayer");
	$('#currentVideo object').attr('data','http://www.youtube.com/v/'+pVideoID+'?enablejsapi=1&playerapiid=ytplayer');
	$('#currentVideo .videoTitle').text(pVideoTitle);
	if (ytplayer) {
		setTimeout(function(){ytplayer.playVideo();},1500);
	}	
}

/***open the contact us page***/
function showContactUs() {
	tb_show('','contact-us.php?TB_iframe=true&height=283&width=400');	
}