/*
-------------------------------------------------
*
*	Project:	Red Bull Open Ice 2010
*	File:		functions.js
*
*
-------------------------------------------------
*/

// + Ajax Init
// Create a new XMLHttpRequest object to talk to the Web server
var loopStart = false;
var xmlHttp = false;

try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(e2) {
    xmlHttp = false;
  }
}
if(!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

// Main Nav RollOver
var nav_id = new Array(
	'#home',
	'#news',
	'#schedule',
	'#rules',
	'#tourney',
	'#sign_up',
	'#contact'
);

function over(id) {
	callRoll(id, 'url(../images/main_nav_over.jpg) no-repeat 6px 2px', '000');
}
function out(id) {
	if(!$(id).is('.current')) {
		callRoll(id, 'transparent', 'fff');
	}
}
function callRoll(id, bg, color) {
	$(id).css('background',bg);
	a = id + ' a';
	$(a).css({'border-top':'1px solid #' + color, 'color':'#' + color});
	if($(id).is(':last-child')) {
		$(a).css({
			'border-top':'1px solid #' + color,
			'border-bottom':'1px solid #' + color,
			'color':'#' + color
		});
	}
	for(i = 0; i < nav_id.length; i++) {
		if(nav_id[i] == id) {
			if(nav_id[i-1] == current) {
				$(a).css('border-top', '1px solid #000');	
			}
			next_a = nav_id[i+1] + ' a';
			if(nav_id[i+1] == current) {
				$(next_a).css('border-top', '1px solid #000');
			} else {
				$(next_a).css('border-top', '1px solid #' + color);
			}
		}
	}
}
// End Main Nav

// Home Page Comments

var commentSection = { 'event' : 'closed', 'news' : 'closed' };

$(document).ready(function() { docReady(); });
function docReady() {
	event_count = $('.event_comments').length;
	e_heights = new Array();
	for(var i = 0; i < event_count; i++) {
		var kids = $('#event_comments_' + i).children();
		e_heights.push($('#event_comments_' + i + '>.e_comment_block:first').height() + 32);
		if(kids.length > 0) {
			$('#event_comments_' + i).css({ 'height' : e_heights[i] });
		}
	}
	
	news_count = $('.news_comments').length;
	n_heights = new Array();
	for(var j = 0; j < news_count; j++) {
		var news_kids = $('#news_comments_' + j).children();
		n_heights.push($('#news_comments_' + j + '>.n_comment_block:first').height() + 32);
		if(news_kids.length > 0) {
			$('#news_comments_' + j).css({ 'height' : n_heights[j] });
		}
	}
}
function toggleComments(section, id) {
	selector = '#'+ section +'_comments_'+ id;
	if(commentSection[section] == 'closed') {
		$(selector).css({'height' : 'auto', 'overflow' : 'visible'});
		commentSection[section] = 'open';
	} else {
		if(section == 'event') {
			$(selector).css({'height' : e_heights[id], 'overflow' : 'hidden'});
		} else {
			$(selector).css({'height' : n_heights[id], 'overflow' : 'hidden'});
		}
		commentSection[section] = 'closed';
	}
}

// Comment Forms
var replace_id = '';
function submitComment(num, table, size, isStory) {
	var form = document.getElementById(table +'_add_comments_'+ num);
	var data = Array(
		'comment='+ form.add_comment.value +'&'+
		'author='+ form.author.value +'&'+
		'id='+ form.id.value +'&'+
		'size='+ size +'&'+
		'is_story='+ isStory +'&'+
		'table='+ table
	);
	
	var url = '../components/submit_comment.php?'+ data;
	xmlHttp.open('GET', url, true);
	xmlHttp.onreadystatechange = beginSubmit;
	xmlHttp.send(null);
	if(table == 'news') {
		if(isStory == 'false') {
			replace_id = 'news_replace';
		} else {
			replace_id = 'news_story_replace';
		}
	} else {
		replace_id = 'events_replace';
	}
	shrink(table, form.id.value);
}
function beginSubmit() {
	if(xmlHttp.readyState == 4) {
		document.getElementById(replace_id).innerHTML = xmlHttp.responseText;
		docReady();
	}
}
function grow(section, id) {
	textarea = '#'+ section +'_add_comment_'+ id;
	post = '#'+ section +'_post_'+ id;
	//$('#'+ section + '_author_' + id).show('slow');
	$('#'+ section + '_author_' + id).animate({'height' : '12px', 'display' : 'block'});
	$(textarea).animate({'height' : '100px'}, 500);
	$(post).animate({'top' : '88px'}, 500);
}
function shrink(section, id) {
	textarea = '#'+ section +'_add_comment_'+ id;
	post = '#'+ section +'_post_'+ id;
	//$('#'+ section + '_author_' + id).hide('slow');
	$('#'+ section + '_author_' + id).animate({'height' : '0', 'display' : 'none'});
	$(textarea).animate({'height' : '12px'}, 500);
	$('.post').animate({'top' : '0px'}, 500);
}

// Waiver Drop Down
var isClosed = true;
function dropWaiver() {
	if(isClosed) {
		children = $('#waiver_dropdown').children();
		li_total = $('.dropdown_item').height() * (children.length - 1);
		dropHeight = $('#waiver_dropdown').height() + li_total + (5 * children.length);
		$('#waiver_dropdown').animate({ 'height' : dropHeight }, 200);
		isClosed = false;
	} else {
		$('#waiver_dropdown').animate({ 'height' : '14px'}, 200);
		isClosed = true;
	}
}
function downloadWaiver(pdf) {
	//alert('downloading');
	document.location.href = pdf;
}
function sendTo(url, bool) {
	if(bool == '') {
		document.location.href = url;
	} else if(bool == 'blank') {
		window.open(url, "_blank");
	}
}

// Share this
function shareThis(url) {
	document.location.href = url;
}

function teamOver(id) {
	$('#team-players'+id).animate({ opacity:0 }, 100, function() { $(this).css({ display:'block' }), $(this).animate({ opacity:1 }, 500) });
}
function teamOut(id) {
	$('#team-players'+id).animate({ opacity:0 }, 500, function() { $(this).css({ display:'none' }), $(this).animate({ opacity:1 }, 100) });
}

function showImg(arrayId) {
	var randomNumber = Math.random() * 1000;
	$.get('components/get-image.php?id='+ arrayId +'&rand='+ randomNumber, function(data) { showReturn(data); });
}

function reveal() {
	$('#bubble').animate({ width : '710px', left : '30px' });
	$('.snippet-txt').fadeOut('fast');
	$('.full-txt').fadeIn('fast');
	$('.close-bubble').fadeIn('fast');
}
function hide() {
	$('#bubble').animate({ width : '140px', left : '610px' });
	$('.full-txt').fadeOut('fast');
	$('.snippet-txt').fadeIn('fast');
	$('.close-bubble').fadeOut('fast');
}
function showReturn(img) {
	var dim = img.split('**');
	
	//$('#overlay').css({ display : 'block' });
	$('#overlay').fadeIn('slow');
	$('#center-img').html(dim[0]);
	$('#center-img').width(Number(dim[1]));
	
}

$(document).ready(function() { 
	$('#overlay').click(function() { 
		$('#center-img').html('');
		$(this).fadeOut('slow');
		//$(this).css({ display : 'none' });
	}) 
});

function thumbPrev() {
	var currentInfo = $('#image-slide-box > img:first').attr('id');
	var current = currentInfo.split('-');
	var randomNumber = Math.random() * 1000;
	$.get('components/update-thumbs.php?leftId='+ current[2] +'&direction=prev&rand='+ randomNumber, function(data) { updateThumbs(data) });
}
function thumbNext() {
	var currentInfo = $('#image-slide-box > img:last').attr('id');
	var current = currentInfo.split('-');
	var randomNumber = Math.random() * 1000;
	$.get('components/update-thumbs.php?rightId='+ current[2] +'&direction=next&rand='+ randomNumber, function(data) { updateThumbs(data) });
}
function updateThumbs(html) {
	$('#image-slide-box').html(html);
}
function lrgPrev() {
	
}
function lrgNext() {
	
}