/* ui fixes... speed things up before (document).ready ~cgerikh */
jQuery('div a[href*="admin/clientssummary.php"]').parent().hide();

$(document).ready(function() {

	// sitenav
	//$('#main-nav-menu > li, #main-nav-icons > li').nav();


	/* **************************************
	override/hack up the breadcrumbs ~cgerikh
	************************************** */
	// combine homepage & client area in to just home
	$("#breadcrumb a:contains('Portal Home')").hide();
	$("#breadcrumb a:contains('Client Area')").html("Home");
	
	// rename items
	$("#breadcrumb a:contains('My Products & Services')").html("Services Overview");
	$("#breadcrumb a:contains('Product Details')").html("Service Details");
	$("#breadcrumb a:contains('My Domains')").html("Domains");
	$("#breadcrumb a:contains('My Details')").html("User Profile");
	$("#breadcrumb a:contains('Change Security Question')").html("Security Question");
	$("#breadcrumb a:contains('My Invoices')").html("Invoices");
	$("#breadcrumb a:contains('Add New Contact')").html("Add User");
	$("#breadcrumb a:contains('Manage Contacts')").html("Manage Users");
	$("#breadcrumb a:contains('Mass Payment')").html("Pay Bill");

	// fix 2 pages that have the same crumb but different page titles (fix based on H2)
	if($("h1:contains('Submit Ticket')").size() > 0) {
		$("#breadcrumb a:contains('Submit Ticket')").html("Submit Ticket");
	}
	if($("h1:contains('Create Billing Inquiry')").size() > 0) {
		$("#breadcrumb a:contains('Submit Ticket')").html("Create Billing Inquiry");
	}
	if($("h1:contains('Contact Us')").size() > 0) {
		$("#breadcrumb a:contains('Submit Ticket')").html("Contact Us");
	}
	// fix trailing crumb/remove arrow
	$("#breadcrumb a:last-child").css("background","none");

	/* **************************************
	product legend labels ~cgerikh
	************************************** */

	$(".data .clientareatableactive").text("A");	
	$(".data .clientareatablepending").text("P");	
	$(".data .clientareatablesuspended").text("S");	
	$(".data .clientareatableterminated").text("T");	

	/* **************************************
	ui fixes ~cgerikh
	************************************** */
	// remove dashbaord widget trailing border
	$("article .dgrid").css("border-bottom","none");
	//$(".grid_12 h2").wrap('<div class="dash-hbg"><div class="dash-ht"><div class="dash-hb"></div></div></div>');
	$(".grid_12 h2").wrap('<div class="dash-hbg2"><div class="dash-ht2"></div></div>');
	$(".grid_13 .grid_12").append('<div class="dash-hb2"></div>');
	
	$("p:contains('Powered by')").hide();

	//$('div a[href*="admin/clientssummary.php"]').parent().hide();
	$('div a[href*="admin/clientssummary.php"]').parent().addClass("admin-box").show();

	// Menu Mods
	if ($('#controlpanel-menu-list li').size() == 0) {
		$('#controlpanel-menu-list').hide();
		$('#controlpanel-menu').hide();
	} else {
		$('#tools-menu-blank').hide();
	};
	if ($('#tools-menu-power').length) {
		$('#tools-menu-blank').hide();
	};
	
	// Init Dashobard Widgets
	if (typeof(is_dashboard) != 'undefined') {
		WELCOME.init();
		DASH.init();
	};
	
	// Init Other Page Hacks
	OTHER.init();
	jQuery('body').append("<script type=\"text/javascript\" language=\"javascript\" src=\"https://trackalyzer.com/formalyze_init.js\"></script>")

});

// Give Modernizr.load a string, an object, or an array of strings and objects
Modernizr.load([
	{
		test : Modernizr.mq(),
		nope : '/templates/mylink/_js/libs/respond.min.js'
	},
	{
		test : Modernizr.rgba,
		nope : ['/templates/mylink/_js/mylibs/ie-fallback.js']
	}
]);



// Tabs
function doTab(ix, hdrName, cntName) {
	ix = ix-1;
	if (ix < 0) ix = 0;

	var tabHeader = document.getElementById(hdrName);
	var tabContent = document.getElementById(cntName);

	var onTab = 0;
	for (var i=0; i<tabHeader.childNodes.length; i++) {
		if (!tabHeader.childNodes[i].tagName || !tabContent.childNodes[i]) {
			continue;
		}

		if (onTab == ix) {
			tabHeader.childNodes[i].className = "active";
			tabContent.childNodes[i].className = "tab-src";
		} else {
			tabHeader.childNodes[i].className = "";
			tabContent.childNodes[i].className = "tab-src hidden";
		}

		onTab++;
	}
}

WELCOME = {
	'init': function() {
		$('#welcome-tod').html(this.setTODGreeting());
		$('#welcome-renew').html(this.setRenewMessage());
	},
	'setTODGreeting': function() {
		var d = new Date();
		var hour = d.getHours();
		if (hour < 12) {
			return 'morning';
		} else if (hour > 17) {
			return 'evening';
		} else {
			return 'afternoon';
		}
	},
	'setRenewMessage': function() {
		var count = $('#wdgt-renew table tbody tr').size();
		if (count > 2) {
			return ' You have <a href=\"clientarea.php?action=domains\">' + (count -1) + '</a> Domain Registrations ready for renewal. ';
		} else if (count > 1) {
			return ' You have <a href=\"clientarea.php?action=domains\">1</a> Domain Registration ready for renewal. ';
		} else {
			return '';
		};
	}
	
}

DASH = {
	
	'init': function() {
		DASH.products.init();
		DASH.blog.init();
		DASH.widgets.init();
	},
	
	'products': {
		'init': function() {
			$.ajax({
				url: "/billing/products.php?function=products",
				dataType: "xml",
				//context: document.body,
				success: function(data){
					DASH.products.xml = data;
					DASH.products.render();
					DASH.products.checkEmpty();
				}
			});
		},
		'xml': null,
		'isColocation': false,
		'render': function() {
			var row = 0;
			var shown = false;
			$.each(this.getProducts(), function(index, value){
				if ($(value).find('groupname').text().toLowerCase().trim() == 'colocation' || $(value).find('name').text().toLowerCase().trim() == 'colocation services') {
					DASH.products.isColocation = true;
					DASH.widgets.alerts();
				} else {
					//console.log($(value).find('groupname'));
				}
				if ($(value).find('status').text().toLowerCase() == 'active' && row < 3) {
					row++;
					$('#prod-table').append('<tr><td style="padding: 12px 0 0 10px;">' + $(value).find('name').text() + '</td><td style="text-align: center;">' + DASH.products.getButton($(value).find('id').text()) + '</td></tr>');
				} else if ($(value).find('status').text().toLowerCase() == 'active' && row == 3) {
					row++;
					$('#prod-table').append('<tr><td colspan="2" style="text-align:right;padding:8px 14px;"><a href="/billing/clientarea.php?action=products">View All</a></td></tr>');
					shown = true;
				} else {
					// console.log($(value));
				};
			});
		},
		'checkEmpty': function() {
			if ($('#prod-table tbody tr').length == 0) {
				$('#wdgt-prods section').replaceWith('<section>No Active Products or Services</section>');
			};
		},
		'getProducts': function() {
			return $(this.xml).find('product');
		},
		'getButton': function(id) {
			return '<form method="post" action="/billing/clientarea.php?action=productdetails"><input type="hidden" name="id" value="' + id + '"><input class="button" type="submit" value="Details" alt="Details"></form>';
		}
	},
	
	'blog': {
		'init': function() {
			$.ajax({
				url: "/feed/",
				dataType: "xml",
				success: function(data){
					DASH.blog.xml = data;
					DASH.blog.items = $(DASH.blog.xml).find('item');
					DASH.blog.render();
				}
			});
		},
		'xml': null,
		'items': null,
		'render': function() {
			var todaytotal = 0;
			$.each(this.items, function(index, value){
				var today = new Date();
				var pubdate = $(this).find('pubDate').text();
				if (typeof(pubdate) != 'undefined') {
					pubdate = new Date(pubdate);
					if (!UTIL.date.isToday(pubdate)) {
						return false;
					} else {
						todaytotal++;
					};
				};
			});
			if (todaytotal > 1) {
				$('#welcome-blog').html('There are <a href="/company/blog/" target="_blank">' + todaytotal + '</a> new blog entries today.');
			} else if (todaytotal == 1) {
				$('#welcome-blog').html('There is <a href="/company/blog/" target="_blank">1</a> new blog entry today.');
			};
		}
	},
	
	'widgets': {
		'init': function() {
			this.alerts();
			this.serviceLogins();
		},
		'alerts': function() {
			if (DASH.products.isColocation == true) {
				//$('#wdgt-alerts').show()
			};
		},
		'serviceLogins': function() {
			$('#no-hosting-prods').closest('article').hide();
		}
	}
}

UTIL = {
	'today': new Date(),
	'date': {
		'isToday': function(date) {
			if (date.getDate() !== UTIL.today.getDate()) {
				return false;
			} else if (date.getMonth() !== UTIL.today.getMonth()) {
				return false
			} else if (date.getYear() !== UTIL.today.getYear()) {
				return false
			};
			return true
		},
		'isThisWeek': function(date) {
			
		}
	}
}

OTHER = {
	'init': function() {
		this.ProdDetails();
	},
	'ProdDetails': function() {
		$('#moduleclientarea').find('.button:odd').hide();
		$('#moduleclientarea').show();
	}
}

if(typeof(String.prototype.trim) === "undefined") {
	String.prototype.trim = function() {
		return String(this).replace(/^\s+|\s+$/g, '');
	};
}

