// JavaScript Document

$.fn.fauxColumns = function(){
			
	var maxHeight = 0;
	var innerHeight = 0;

	this.each(function(){
		if (this.offsetHeight > maxHeight) maxHeight = this.offsetHeight;
	});
	
	this.each(function(){
		this.style.height = maxHeight + 'px';
		if (this.offsetHeight > maxHeight) {
			this.style.height = (maxHeight - (this.offsetHeight - maxHeight)) + 'px';
		}
	});
	
};

//$("div.column").fauxColumns();

function global_functions(){
	
	$(document).find('.vcardlink').click(function(){
		location.href='http://feeds.technorati.com/contacts/'+escape(location.href);
		//window.open('http://feeds.technorati.com/contacts/'+escape(document.location), 'vcard');
		return false;
	});
	
	$(document).find('img.polaroidAlignRight')
	.wrap('<div class="polaroidWrapperRight"></div>')
	.wrap('<div class="polaroidInner"></div>')
	.before('<span class="polaroidTopLeft"></span>')
	.before('<span class="polaroidBottomRight"></span>');
	
	$(document).find('img.polaroidAlignLeft')
	.wrap('<div class="polaroidWrapperLeft"></div>')
	.wrap('<div class="polaroidInner"></div>')
	.before('<span class="polaroidTopLeft"></span>')
	.before('<span class="polaroidBottomRight"></span>');
	
	// Search field clear and reset
	$('#xu_site_search').focus(function(){
		$(this).get(0).value = '';
	}).blur(function(){
		if($(this).get(0).value == ''){
			$(this).get(0).value = 'Search pages';
		}
	});
	
	// instead of hiding things with a css class, this function does it.
	// the reason being Contribute will hide the content in editing mode via CSS,
	// but Contribute doesn't run JavaScript in editing mode.
	$(document).find('.hide').hide();
	
	// new toggle div function. Just add the 'toggle' class, and
	// add set the href value to #id_of_div_to_toggle" to the link just like you would link to an anchor.
	// example: <a href="#id_of_div" class="toggle">click to toggle</a>
	$(document).find('.toggle').click(function(){
		var el = this.getAttribute("href");
		$(el).toggle();
		$("div.column").fauxColumns();
		return false;
	});
	
	// Stripe table rows
	$('.tableStripe tr').mouseover(function(){
		$(this).addClass('tableRowHover');
	}).mouseout(function(){
		$(this).removeClass('tableRowHover');
	});
	$('.tableStripe tr:even').addClass('altRow');
	
	// pop-up window. Requires a link to have the class 'popup' assigned to it.
	// example: <li><a href="/directions/" class="popup" rel="800:600">Directions</a></li>
	$(document).find('a.popup').click(function(){
		var rel = this.getAttribute("rel");
		if(rel == ''){
			window.open(this.href,'newWindow');
		} else {
			//var re = /[^0-9]/;
			var rel_split = rel.split(':');
			var w = rel_split[0];
			var h = rel_split[1];
			var leftPos=(screen.width-w)/2;
			var topPos=(screen.height-h)/2;
			window.open(this.href,'newWindow','width='+w+',height='+h+',left='+leftPos+',top='+topPos+',scrollbars=no,resizable=no,statusbar=no,menubar=no,toolbar=no');
		}
		return false;
	});
}

/* these are a page specific functions, so to reduce the amount 
of $(document).finds these are called seperately to on the needed pages */
function blog_functions() {
	$(document).find('a.toggleBlogPreview').click(function(){
		var rel = this.getAttribute('rel');
		var rel_split = rel.split(':');
		var preview = rel_split[0];
		var url = rel_split[1];
		//$(document).find('.hide:visible').hide();
		$(preview).load('blogs/'+url, function(){
			$(document).find('a.closeBlogPreview').click(function(){
				$(this.parentNode).hide();	
				return false;
			});
		});
		$(preview).fadeIn('slow', function(){
			$('#contentBottom').ScrollTo(1000);							
		});
		return false;
	});	
}
function fast_facts(){
	$('#ff_selector').change(function(){
		var val = $(this).get(0).value;	
		$(document).find('.ffTopX').hide();
		$('#ff'+val).fadeIn('slow');		
	});
}

/* ------
* Script based on: http://www.bigbold.com/snippets/posts/show/1068
* re-written by Brian Litzinger to use the jQuery library
* cookie functions from: http://www.quirksmode.org/js/cookies.html

This is bloated and needs cleaned up... 
------- */

function tabs_internal(){

	$("div.tabContent").hide();
	$("div#tab1").show();	
	$("li#tabLink1").addClass('active');			
	
	$("li#tabLink1 a").click(function(){
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab1").show();
		$("li#tabLink1").addClass('active');
		return false;
	});
	$("li#tabLink2 a").click(function(){
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab2").show();
		$("li#tabLink2").addClass('active');
		return false;
	});
}

/*
var tabs_index = function(){
	
}
*/
	
function tabs_index(){

	total_slides = 6;
	show_timer = 7000; 
	show_count = show_timer/1000;
	cookie = readCookie('rememberTab');
	$("div.tabContent").hide();
	$("div#tab1").show();	
	$("li#tabLink1").addClass('active');			
	
	if(cookie == null){
		start_tabs(1, total_slides, show_timer);
		document.getElementById('rememberTab').checked = false;
	} else {
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab"+cookie).show();
		$("li#tabLink"+cookie).addClass('active');
		document.getElementById('rememberTab').checked = false;
		write_current_tab(cookie);
	}
	
	$("li#tabLink1 a").click(function(){
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab1").show();
		$("li#tabLink1").addClass('active');
		document.getElementById('rememberTab').value = 1;	
		document.getElementById('rememberTab').checked = false;	
		write_current_tab(1);
		stop_all();
	});
	$("li#tabLink2 a").click(function(){
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab2").show();
		$("li#tabLink2").addClass('active');
		document.getElementById('rememberTab').value = 2;
		document.getElementById('rememberTab').checked = false;
		write_current_tab(2);
		stop_all();
	});
	$("li#tabLink3 a").click(function(){
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab3").show();
		$("li#tabLink3").addClass('active');
		document.getElementById('rememberTab').value = 3;
		document.getElementById('rememberTab').checked = false;
		write_current_tab(3);
		stop_all();
	});
	$("li#tabLink4 a").click(function(){
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab4").show();
		$("li#tabLink4").addClass('active');
		document.getElementById('rememberTab').value = 4;
		document.getElementById('rememberTab').checked = false;
		write_current_tab(4);
		stop_all();
	});
	$("li#tabLink5 a").click(function(){
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab5").show();
		$("li#tabLink5").addClass('active');
		document.getElementById('rememberTab').value = 5;
		document.getElementById('rememberTab').checked = false;
		write_current_tab(5);
		stop_all();
	});
	$("li#tabLink6 a").click(function(){
		$("div.tabContent:visible").hide();
		$("li.tabLink").removeClass('active');
		$("div#tab6").show();
		$("li#tabLink6").addClass('active');
		document.getElementById('rememberTab').value = 6;
		document.getElementById('rememberTab').checked = false;
		write_current_tab(6);
		stop_all();
	});	
	$("#rememberTab").click(function(){
		textValue = document.getElementById('rememberTab').value;
		createCookie('rememberTab', textValue, 365);
		set_current_tab(textValue);
		stop_all();
	});		
}
	
function write_current_tab(frame){
	newCookie = readCookie('rememberTab');
	tabHTML = $("li#tabLink"+frame+" a").html();
	currentTab = $("li#tabLink"+newCookie+" a").html();
	if(cookie != frame && cookie != null){
		newHTML = "Make <b>"+ tabHTML +"</b> my default tab. My default tab is currently <b>"+ currentTab +"</b>.";
	} else if(cookie != null) {
		newHTML = "Your default tab is currently <b>"+ currentTab +"</b>.";
	} else {
		newHTML = "Make <b>"+ tabHTML +"</b> my default tab.";
	}
	$("div#settings span").html(newHTML);
}

function set_current_tab(frame){
	newCookie = readCookie('rememberTab');
	currentTab = $("li#tabLink"+newCookie+" a").html();
	newHTML = "Your default tab is now set to <b>"+ currentTab +"</b>.";
	$("div#settings span").html(newHTML);
}
	
function start_tabs(start_frame, end_frame, delay) {
	interval1 = setTimeout(switch_tabs(start_frame,start_frame,end_frame, delay), delay);
	count_down(show_count);
}

function timer(frame, start_frame, end_frame, delay){
	interval2 = setTimeout(switch_tabs(frame, start_frame, end_frame, delay), delay);
}
						
function switch_tabs(frame, start_frame, end_frame, delay) {
	write_current_tab(frame);
	return (function() {	
		$('div#tab'+frame).hide();
		$('li#tabLink'+frame).removeClass('active');
		if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
		$('div#tab'+frame).show();
		$('li#tabLink'+frame).addClass('active');
		document.getElementById('rememberTab').checked = false;
		document.getElementById('rememberTab').value = frame;
		timer(frame, start_frame, end_frame, delay);
		count_down(show_count);
	})
}  

function count_down(count){
	count--;
	$("div#tabCounter").html(count);
	interval3 = setTimeout("count_down("+count+")", 1000);
	if(count == 0){
		count = show_count;
		clearTimeout(interval3);
	}
}

function stop_all() {
	$("div#tabCounter").hide();
	clearTimeout(interval1);
	clearTimeout(interval2);
	clearTimeout(interval3);
	cookie = readCookie('rememberTab');
	return false;
}

function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name){
	createCookie(name,"",-1);
}


/* homepage brand image slideshow */
function start_brand(start_frame, end_frame, delay) {
	interval4 = setTimeout(switch_brands(start_frame,start_frame,end_frame, delay), delay);
}
						
function switch_brands(frame, start_frame, end_frame, delay) {
	return (function() {	
		$('div#brandImage'+frame).fadeOut("slow");
		if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
		$('div#brandImage'+frame).fadeIn("slow");
		setTimeout(switch_brands(frame, start_frame, end_frame, delay), delay);
	})
}  

