int = '';
obj = null;

$(document).ready(function() {


	Galleria.loadTheme('./include/gallery/themes/classic/galleria.classic.min.js');
	$('#JQgallery').galleria({
        width:710,
        height:500
    });
    
	//Jquery for expanding file menus
	var button_state = new Array(); //1 on, 0 off
	
	$('.stat_button li').live('click', function(){
		$('.teamstats').hide();
		var attr = $(this).attr('val');
		$('.'+attr).show();
		$('.stat_goalie').show();
		$('.stat_button').removeClass('stat_active');
		$(this).addClass('stat_active');		
	});
	
	$('.file_header').each(function(i) {
		$(this).next().slideUp();
		button_state[i] = 0;
	});
	
	$('#banner_nav a').live('click', function(){
		//$('#bann_item img').hide(50);
		$('#banner_nav a').removeClass('active');
		$(this).addClass('active');
		var p = $(this).attr('new_id');
		$('#bann_item img').attr('src', './userfile/media/'+$(this).attr('img'));
		//$('#bann_item img').show(500);
		$.ajax({
			type: "POST",
			url: "./request/get_banner.request.php",
			data: "ID=" + p,
			success: function(msg) {
				$('#bann_item a').attr('href', msg);
			}
		});
		//$('#bann_item').html('<img style="height: 235px; width: 441px;" src="./style/whsbanner_09_4.jpg"/>');
	});
	
	$('#banner_items').cycle({
		fx: 'fade',
		speed: 'slow',
		after: function(idx, slide, opts) {
			$('#banner_title').html($(slide).children(0).attr('alt'));
			
		},
		pager: '#banner_nav',
		
		pagerAnchorBuilder: function(idx, slide) {
			return '#banner_nav li:eq(' + idx + ') a';
		}
	});
	
	
	$('.file_header').each(function(i) {
		var next = $(this).next();
		$(this).click( function() {
			
			if (button_state[i] == 0) {
				$(next).slideDown();
				button_state[i] = 1;
				$(this).addClass('border_bottom');
				$(this).next().next().addClass('border_bottom');
				
			} else {
				$(next).slideUp();
				button_state[i] = 0;
				$(this).removeClass('border_bottom');
				$(this).next().next().removeClass('border_bottom');
			}
		});
	});
	

	//$('#banner_items li').css('background-color', '#1b222c');
	
	$('img.image_banner').css('width', '435px');
	$('img.image_banner').css('height', '238px');

	$('#ddnav').superfish();
	$('a.lightbox').lightBox(); // Select all links with lightbox class
	$('div.rosterrow').hover(function() {
		window.clearInterval(int);
		int = '';
		obj = $(this);
		int = window.setInterval('fetch_player()', 500);
	}, function() {
		clear_player(this);
	});
	
	
	//LEFT NAV MENU
	var $_GET = getUrlVars();
	$('ul[parent=' + $_GET['lp'] + ']').show();
	$('ul[parent=' + $_GET['lp'] + ']').parent('li').prev('li.link').children('span.expand').html('-');
	
	$('span.expand').click(function() {
		var childNav = $(this).attr('child');
		if ($('ul[parent=' + childNav + ']').is(':hidden')) {
			$('span.expand').html('+');
			$(this).html('-');
			$('ul.childnav').slideUp();
			$('ul[parent=' + childNav + ']').slideDown();
		}
	});
	
	var p = $_GET['p'];
	if (!p) 
		p = 2;
	var op = $_GET['op'];
	$("ul#s" + p).show();
	$("ul#s" + op).show();

	
	
	
	$('a.eventtoday').click(function() {
		$.ajax({
			type: "GET",
			url: "./include/dayevent.php",
			data: "d=" + $(this).attr("name"),
			success: function(msg) {
				$("div.eventlist").html(msg);
			}
		});
		
	});
	
	//-------- NEWS ARCHIVE --------*/
	$('.newsArchive select[name=newsYear]').change(function() {
		fetch_newsMonthOptions();
	});
	
	$('.newsArchive select[name=newsMonth]').change(function() {
		fetch_newsArchive();
	});
	
	//-- EVENTS ARCHIVE --
	$('.eventsArchive select[name=eventsYear]').change(function() {
		fetch_eventsMonthOptions();
	});
	
	$('.eventsArchive select[name=eventsMonth]').change(function() {
		fetch_eventsArchive();
	});
	
	
	//$( "#calendar" ).datepicker();

	
});

//--------------- FUNCTIONS ------------------------//

function fetch_newsMonthOptions() {
	var year = $('.newsArchive select[name=newsYear]').val();

	$.ajax({
		type: "POST",
		url: "./request/fetch_newsMonth.request.php",
		data: 'year=' + year,
		success: function(msg) {
			$('.newsArchive select[name=newsMonth]').html(msg);
			fetch_newsArchive();
			//alert(msg);
		}
	});
}

function fetch_newsArchive() {
	var year = $('.newsArchive select[name=newsYear]').val();
	var month = $('.newsArchive select[name=newsMonth]').val();
	
	$.ajax({
		type: "POST",
		url: "./request/fetch_news.request.php",
		data: 'year=' + year + '&month=' + month,
		success: function(msg) {
			$("#newsArchive").html(msg);
		}
	});
}

function fetch_eventsMonthOptions() {
	var year = $('.eventsArchive select[name=eventsYear]').val();

	$.ajax({
		type: "POST",
		url: "./request/fetch_eventsMonth.request.php",
		data: 'year=' + year,
		success: function(msg) {
			$('.eventsArchive select[name=eventsMonth]').html(msg);
			fetch_eventsArchive();
			//alert(msg);
		}
	});
}

function fetch_eventsArchive() {
	var year = $('.eventsArchive select[name=eventsYear]').val();
	var month = $('.eventsArchive select[name=eventsMonth]').val();
	
	$.ajax({
		type: "POST",
		url: "./request/fetch_events.request.php",
		data: 'year=' + year + '&month=' + month,
		success: function(msg) {
			$("#eventsArchive").html(msg);
		}
	});
}

function fetch_player() {
	$("div.popup").html('');
	var oid = $(obj).attr('oid');
	var offset = $(obj).offset();
	var offset2 = $(".wroster").offset();
	
	
	$('.popup').css({
		'position': 'absolute',
		'top': (parseInt(offset.top)) + 'px',
		'left': (parseInt(offset.left) + 100) + 'px'
	
	});
	$('.popup').fadeIn();
	
	$.ajax({
		type: "GET",
		url: "./include/playerpopup.php",
		data: "oid=" + oid,
		success: function(msg) {
			$("div.popup").html(msg);
		}
	});
	
	window.clearInterval(int);
	int = '';
}

function clear_player(obj) {
	$('.popup').fadeOut();
	window.clearInterval(int);
	int = '';
}

function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for (var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;

}

