/**
 * @author stevegolba
 */

$(document).ready(function() {
	var siteTabID = $("input[name='sc']").val();
	var siteCategoryID = $("input[name='category']").val();
	var itemID = $("input[name='id']").val();
	//alert('siteTabID=' + siteTabID + ', siteCategoryID=' + siteCategoryID + ', itemID=' + itemID);
	
	// If on page or area:
	// 		Login (siteTabID == 1) but not Home page (siteCategoryID == -101)
	// 		Returning Customers Login (siteTabID == 13) but not Contact page (siteCategoryID == -113)
	// 		Shopping Cart (siteTabID == 3)
	// 		Checkout (siteTabID == 4)
	// 		My Account (siteTabID == 6 or siteTabID && siteCategoryID are empty)
	if (
		(siteTabID == '1' && siteCategoryID != '-101') || 
		(siteTabID == '13' && siteCategoryID != '-113') || 
		siteTabID == '3' || 
		siteTabID == '4' || 
		siteTabID == '6' || 
		(!siteTabID && !siteCategoryID)
	) {
		// 'Undo' OptConnect theme's 'page setup' styles
		$("div#main").css('padding', '10px');
		$("table#carttable").parentsUntil("tbody").parentsUntil('td').css('width', '938px');
	}
	
	// Check if on item detail page, or page or area listed above
	//if (itemID || (siteTabID == '1' && siteCategoryID != '-101') || siteTabID == '3' || siteTabID == '4' || siteTabID == '6') {
	if (itemID) {
		// Display breadcrumbs
		$("#breadcrumbs").css('display','inline-block');
		$("#breadcrumbs").parent("div.clearingfix").css('display', 'block');
	}
	
	// Set 'Solutions' link to go to 'Solutions > Go Wireless' page.
	$("a.tabbutton[title='Solutions']").attr('href', '/s.nl/c.1118754/sc.9/category.178/.f');
	
	// Set 'About Us' link to go to 'About Us > Our Company' page.
	$("a.tabbutton[title='About Us']").attr('href', '/s.nl/c.1118754/sc.12/category.213/.f');
	
	// Set 'About Us' link to go to 'About Us > Our Company' page.
	$("a.tabbutton[title='News']").attr('href', '/s.nl/c.1118754/sc.11/category.284/.f');
	
	// Remove margin-top on div ID 'div__body'
	$("div#div__body").css('margin-top', '0');
	
	// Remove extra cellpadding on particular tables
	var divs = $("div.category_header").parentsUntil("table[cellpadding='2']");
	if (divs.length) {
		$(divs[divs.length - 1]).parent().attr('cellpadding', '0');
	}
	
	divs = $("div.category_header").find("table[cellpadding='5']");
	if (divs.length) {
		$(divs[0]).attr('cellpadding', '0');
	}
	
	divs = $("div.main_content").parentsUntil("table[cellpadding='5']");
	if (divs.length) {
		$(divs[divs.length - 1]).parent().attr('cellpadding', '0');
	}
	
	// If on My Account page
	if (!siteTabID && !siteCategoryID) {
		// Adjust width of 'My Account' Billing/Orders/Support box
		$("div[id^='servercontent'][id$='_splits']").css('width', '938px'); // Looking for div with id 'servercontentneg519_splits'
	}
	
	MM_preloadImages(
		"/c.1118754/site/images/toporangearea.jpg",
		"/c.1118754/site/images/toporangeareadark.jpg",
		"/c.1118754/site/images/toporangeareahilite.jpg"
	);
});

function getSubcategoryMenuHTML(categoryLabel, companyID) {
	var html = '';
	
	if (!categoryLabel || !companyID) {
		return html;
	}
	
	var subcatAry = [];
	if (categoryLabel.toLowerCase() == 'solutions') {
		subcatAry = [
			{
				'name':		'Go Wireless',
				'catID':	'9',
				'subcatID': '178'
			},
			{
				'name':		'Wireless for ATMs',
				'catID':	'9',
				'subcatID':	'121'
			}/*,
			{
				'name':		'Kiosks',
				'catID':	'9',
				'subcatID':	'122'
			}
			*/
		];
	} else if (categoryLabel.toLowerCase() == 'accessories') {
		subcatAry = [
			{
				'name':		'Amplifiers',
				'catID':	'10',
				'subcatID':	'125'
			},
			{
				'name':		'Antennas',
				'catID':	'10',
				'subcatID':	'123'
			}, 
			{
				'name':		'Power Supplies',
				'catID':	'10',
				'subcatID':	'124'
			}
		];
	} else if (categoryLabel.toLowerCase() == 'about us') {
		subcatAry = [
			{
				'name':		'Our Company',
				'catID':	'12',
				'subcatID':	'213'
			},
			{
				'name':		'Values',
				'catID':	'12',
				'subcatID':	'214'
			}, 
			{
				'name':		'Testimonials',
				'catID':	'12',
				'subcatID':	'215'
			},
			{
				'name':		'Careers',
				'catID':	'12',
				'subcatID':	'216'
			}
		];
	} 
	if (subcatAry.length) {
		var listItemsHTML = '';
		var catID = '';
		for (var i = 0; i < subcatAry.length; i++) {
			listItemsHTML += '<li>';
			listItemsHTML += '<a href="http://shopping.netsuite.com/s.nl/c.' + companyID + '/sc.' + subcatAry[i]['catID'] + '/category.' + subcatAry[i]['subcatID'] + '/.f">' + subcatAry[i]['name'] + '</a>';
			listItemsHTML += '</li>';
			if (!catID) {
				catID = subcatAry[i]['catID'];
			}
		}
		html += '<ul' + (catID ? ' id="cat' + catID + '-sublist" onmouseenter="showCategorySubList(\'' + catID + '\');" onmouseleave="hideCategorySubList(\'' + catID + '\');"' : '') + '>' + listItemsHTML + '</ul>';
	}
	
	return html;
}

function showCategorySubList(catID) {
	if (catID) {
		$("ul#cat" + catID + "-sublist").css('left', 'auto');
	}
}

function hideCategorySubList(catID) {
	if (catID) {
		$("ul#cat" + catID + "-sublist").css('left', '-999em');
	}
}

function getCategoryHeaderImgSrc(categoryInternalID) {
	var imgSrc = '';
	
	if (categoryInternalID == '-111') { // News
		imgSrc = '/core/media/media.nl?c=1118754&id=5769&h=552857e83d50a7a9eea1';
	} else if (categoryInternalID == '-113') { // Contact
		imgSrc = '/core/media/media.nl?c=1118754&id=5768&h=d6f4dbfeef054a9e74e4';
	} else if (categoryInternalID == '-114') { // Support
		imgSrc = '/core/media/media.nl?c=1118754&id=5771&h=c47f4c38602be8c5d9db';
	}
	
	return imgSrc;
}

