//===== Script Initialization ======
$(document).ready(function(){

	$('div#products a.criteria').click(selectProduct);
	$('div#categories a.criteria').click(selectCategory);
	
	$(window).resize(checkOverlaySize);
	$(window).scroll(checkOverlayPosition);

	$('div#faqOverlay').click(closeFaq);
	$('a#closeButton').click(closeFaq);
	$('a#linkButton').click(getLink);
	
	getCategories();
	if(previousFaq > -1) getFaq(previousFaq);
});



//===== Select Product =====
var selectProduct = function() {
	var selectedProduct = $(this).parent();
	if(selectedProduct.hasClass("off")){
		selectedProduct.siblings().removeClass("on");
		selectedProduct.siblings().addClass("off");
		selectedProduct.removeClass("off");
		selectedProduct.addClass("on");
				
		getCategories();
	}

}

//===== Select Category =====
var selectCategory= function() {
	var selectedCategory = $(this).parent();
	if(selectedCategory.hasClass("off")){
		selectedCategory.removeClass("off");
		selectedCategory.addClass("on");
	}else if(selectedCategory.hasClass("on")){
		selectedCategory.removeClass("on");
		selectedCategory.addClass("off");
	}

	getResults();
}

//===== Select FAQ =====
var selectFaq= function() {
	var faqId = $(this).attr('id');
	previousFaq = faqId.substr(1, faqId.length - 1);
	getFaq(previousFaq);
}

//===== Get Results =====
function getResults(){
	$.ajax({
		method: "get",
		url: "./faqResults.php",
		data: {
			"products[]": selectedProducts(),
			"categories[]": selectedCategories()
		},
		dataType: "html",
		beforeSend: getResultsBefore, 
		complete: getResultsComplete, 
		success: getResultsSuccess
	});
}

var getResultsBefore = function(){
	loadCount(1);
}

var getResultsComplete = function(){
	loadCount(-1);
}

var getResultsSuccess = function(html, textStatus){

	$('div#faqContent').html(html);
	$('a.faq').click(selectFaq);

	checkContentHeight();
}

//===== Get Categories =====
var previousCategories = new Array();
function getCategories(){

	$.ajax({
		method: "get",
		url: "./faqCategories.php",
		data: {"products[]": selectedProducts()},
		dataType: "json",
		beforeSend: getCategoriesBefore, 
		complete: getCategoriesComplete, 
		success: getCategoriesSuccess
	});
}

var getCategoriesBefore = function(){
	previousCategories = selectedCategories();
	$('div#categories div').removeClass("on");
	$('div#categories div').removeClass("off");
	$('div#categories div').addClass("disabled");
	loadCount(1);
}

var getCategoriesComplete = function(){
	loadCount(-1);
}

var getCategoriesSuccess = function(jsonObj, textStatus){
	$('div#categories div').removeClass("on");
	$('div#categories div').removeClass("off");
	$('div#categories div').addClass("disabled");
	var categoryIds = jsonObj.categories;
	var categoryDomId;
	for (var i in categoryIds){
		categoryDomId = 'div#c' + categoryIds[i];
		$(categoryDomId).removeClass("disabled");
		
		if(previousCategory(categoryIds[i])){
			$(categoryDomId).addClass("on");
		}else{
			$(categoryDomId).addClass("off");
		}
	}

	getResults();
}

function previousCategory(id){
    for (var i in previousCategories) {
		if(previousCategories[i] == id) return true;
    }
    return false;
}


//===== Get FAQ =====
function getFaq(faqId){
	$.ajax({
		method: "get",
		url: "./faqResults.php",
		data: {"id": faqId},
		dataType: "html",
		beforeSend: getFaqBefore, 
		complete: getFaqComplete, 
		success: getFaqSuccess
	});
}

var getFaqBefore = function(){
	loadCount(1);
}

var getFaqComplete = function(){
	loadCount(-1);
}

var getFaqSuccess = function(html, textStatus){
	$('div#faqInfo').html(html);

	$('div#faqCriteriaContainer').hide();
	$('div#faqEntryContainer').show();

	$('div#faqOverlay').show();
	checkOverlaySize();
	checkOverlayPosition();
}

var closeFaq = function(){
	$('#linkText').hide();
	$('div#faqOverlay').hide();
	$('div#faqEntryContainer').hide();
	$('div#faqCriteriaContainer').show();
	checkContentHeight();
}

//===== Read Current State =====
function selectedProducts(){
	var products = new Array();
	$('div#products div.on').each(function(i){
		var productId = $(this).attr('id');
		productId = productId.substr(1, productId.length - 1);
		products.push(productId);
	});
	return products;
}

function selectedCategories(){
	var categories = new Array();
	$('div#categories div.on').each(function(i){
		
		var categoryId = $(this).attr('id');
		categoryId = categoryId.substr(1, categoryId.length - 1);
		categories.push(categoryId);

	});
	return categories;
}

var getLink = function(){

	var urlBase = "http://www.orbitbaby.com/en/support/faq/index.php?id=" + previousFaq;

	var productQuery = "";
	var products = selectedProducts();
	for (var i in products){
		productQuery += "&products[]=" + products[i];
	}

	var categoryQuery = "";	
	var categories = selectedCategories();
	for (var i in categories){
		categoryQuery += "&categories[]=" + categories[i];
	}
	
	$('#linkText').show();
	$('#linkText').attr("value", urlBase + productQuery + categoryQuery);
	
}

//===== Auto Positioning Functions =====

var checkContentHeight = function(){
	$('div#faqContent').height('');
	if($('div#faqContent').height() < $('div#faqNav').height()){
		$('div#faqContent').height($('div#faqNav').height());
	}
}

var checkOverlaySize = function(){
	$('div#faqOverlay').width($(window).width() + "px");
	$('div#faqOverlay').height($(window).height() + "px");
}

var checkOverlayPosition = function(){
	$('div#faqOverlay').css("top", $(document).scrollTop() + "px");
	$('div#faqOverlay').css("left", $(document).scrollLeft() + "px");
}

//===== Loading Icon =====
var dataLoading = 0;
function loadCount(amount){
	dataLoading += amount;
	if(dataLoading <= 0){
		dataLoading = 0;
		$('img#loadingIcon').hide();
	}else{
		$('img#loadingIcon').show();
	}
}

//===== menu spacing ======
	function equalMenu(){
		arrayParents = $('ul.select ul.sub').map(function() {
			return this.id;
		}).get();
		numParents = $('ul.select ul.sub').size();
		for (i=0;i<=numParents;i++){
			numChild = $('#' + arrayParents[i] + ' > li').size();
			childWidth = 710 / numChild - 40;
			$('#' + arrayParents[i] + ' > li').css('width',childWidth);
		}
	};
