int = '';
obj = null;

$(document).ready(function() {

	//Jquery for expanding file menus
	var button_state = new Array(); //1 on, 0 off
	
	
	$('.file_header').each(function(i) {
		$(this).next().slideUp();
		button_state[i] = 0;
	});
	
	
	$('.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');
			}
		});
	});
	
	
	
	

	
	$('#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);
	});
	
	
	
	//INITIALIZE SLIDESHOW AS HIDDEN	
	$("div#slideshow").hide();
	
	//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();
	
	$.ajax({
		type: "GET",
		url: "./draw/banner.draw.php",
		data: "p=" + p,
		success: function(msg) {
			if (msg.length > 2) {
				$("div#slideshow").show();
				$("div#main").css("position", "relative").css("top", "0px").css("padding-bottom", "0");
				var slide = new Array();
				var slide = msg.split(';');
				$("#slideshow").append(slide[1]);
				if (slide[0] == 0) {
					$('#slideshow').cycle({
						fx: 'fade',
						speed: 1000,
						timeout: 7000
					});
				}
				else if (slide[0] == 1) {
					$('#slideshow').cycle({
						fx: 'scrollDown',
						speed: 1000,
						timeout: 7000
					});
				}
				else if (slide[0] == 2) {
					$('#slideshow').cycle({
						fx: 'turnUp',
						speed: 1000,
						timeout: 7000
					});
				}
				else if (slide[0] == 3) {
					$('#slideshow').cycle({
						fx: 'fadeZoom',
						speed: 1000,
						timeout: 7000
					});
				}
			}
			else {
				$("div#slideshow").hide();
			}
		}
	});
	

	
	
	
	$('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();
	});
	
});

//--------------- 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_player() {
	$("div.popup").html('');
	var oid = $(obj).attr('oid');
	var offset = $(obj).offset();
	var offset2 = $(".wroster").offset();
	
	if (parseInt(offset2.top) > parseInt(offset.top) - 160) {
		offset.top = parseInt(offset2.top) + 163;
	}
	
	$('.popup').css({
		'position': 'absolute',
		'top': (parseInt(offset.top) - 160) + '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;
}
