/** US Solar javascript
 * @author I-Tul Design & Software, Inc.
 */
var tab_time = 6000; // in ms
var chat_time = 20; // seconds until chat window pops up
var tab_timer = null;
var $ = jQuery;
function selectTab(index, restart) {
	if(restart != undefined && restart) {
		resetNextTabTimer();
	}
	else {
		if(tab_timer != null) {
			window.clearTimeout(tab_timer);
			tab_timer = null;
		}
	}
	$.each($(".tab_box"), function(idx, node) {
		if(node.style.display != 'none') {
			$(node).fadeOut(400, function() {$("#tab_box_"+index).fadeIn();});
		}
	});
	$.each($(".tab_title"), function(idx, node) {
		$(node).removeClass("active");
	});
	$("#tab_title_"+index).addClass('active');
}
function nextTab() {
	var active_idx = 1;
	$.each($(".tab_title"), function(idx, node) {
		var cName = node.getAttribute("class");
		if(cName == null) cName = node.getAttribute("className");
		if(cName.indexOf("active") >= 0) {
			active_idx = parseInt(node.id.replace("tab_title_", "")) + 1;
		}
	});
	if(!document.getElementById("tab_title_"+active_idx)) {
		active_idx = 1;
	}
	selectTab(active_idx, true);
	resetNextTabTimer();
}
$(document).ready(function() {
	if(document.getElementById("tab_title_1")) {
		resetNextTabTimer();
		$(".video_box a").fancybox();
	}
	$("a[rel='modal']").fancybox();
	initChatHelper();
});
function resetNextTabTimer() {
	if(tab_timer != null) window.clearTimeout(tab_timer);
	tab_timer = window.setTimeout(nextTab, tab_time);	
}
function initChatHelper() {
	if(getCookie('ignorechathelper') != 'true') {
		var timer = window.setTimeout(showChatHelper, chat_time * 1000);
	}
}
function showChatHelper() {
	$.get("chatOnline.php", {}, function(response) {
		if(parseInt(response) == 1) {
			$("#showChatBox").fancybox({
				'onClosed': closedChatHelper,
				'overlayShow': false,
				'height': 240,
				'width': 500
			}).trigger('click');
		}
	});
}
function closedChatHelper() {
	setCookie('ignorechathelper', 'true', 1);
}
function getCookie(c_name) {
	if (document.cookie.length>0)
	{
	c_start=document.cookie.indexOf(c_name + "=");
	if (c_start!=-1)
	{
	c_start=c_start + c_name.length+1;
	c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
	}
	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
