// General Javascript/jQuery for Quick Order Portal 'Freestyle'
//--------------------------------------------------------------
$(document).ready(function() {
	addFooterContactDetails("BTC Group, BTC House, 9 Millington Road, Hayes, Middx, UB3 4AZ<br />Telephone: 0845 612 3600, Fax: 0208 587 3350<br />");
	addLoginFunctionality();
	addSearchFunctionality();
	//removeHomeText();
	
	$("body.basket .checkout-link").before('<h3 style="display:inline;font-weight:bold;margin:0 8px 0 32px;">Complete Your Order:</h3>');

	$('#top-search .inner-box').append("<a href='/my-account.html' style='position: absolute; top: 7px; left: 620px; color: white; text-decoration: none;'>| My Account</a>");

	
	// Add content to the front page
	//--------------------------------
	if(isHomePage()) {
		
		// Add the top bar to all the pages
		$('body.category #inner-wrapper').prepend("<div id='topBar' style='width: 958px; height: 120px;'></div>");
	
		//$('body.home #tab-links').append("<a href='http://www.btctest.quickorder.uk.com/categories/name/bags/category_id/1'><img alt='' src='static/Image/left2.jpg' /></a>");
		//$('body.home #tab-links').append("<a href='http://www.btctest.quickorder.uk.com/categories/name/clocks/category_id/660'><img alt='' src='static/Image/left3.jpg' /></a>");
		//$('body.home #tab-links').prepend("<a href='http://www.btctest.quickorder.uk.com/categories/name/eco/category_id/6' id='topLeftLink'><img alt='' src='static/Image/left1.jpg' /></a>");

		// Linked images on the left of the page (with hover animation).
		//$('#topLeftLink').hover(
			//function() {
				//$('#topLeftLink img').attr('src', 'static/Image/left1On.jpg')

			//},
			//function() {
				//$('#topLeftLink img').attr('src', 'static/Image/left1.jpg')

			//}

		//);

		// Images down the right of the front page (including Live Person Chat).
		//-----------------------------------------------------------------------
		//$('#secondary-links').html("<a href='http://server.iad.liveperson.net/hc/64533636/?cmd=file&file=visitorWantsToChat&site=64533636&byhref=1&imageUrl=http://images.liveperson.com/lp/64533636' target='_blank'><img alt='' src='/static/Image/liveChat.jpg' /></a>");
		//$('#secondary-links').append("<a href='/categories/name/pens/category_id/14'><img alt='' src='static/Image/right1.jpg' /></a>");
		//$('#secondary-links').append("<a href='http://www.btctest.quickorder.uk.com/categories/name/keyrings/category_id/9'><img alt='' src='static/Image/right2.jpg' /></a>");
		//$('#secondary-links').append("<a href='http://www.btctest.quickorder.uk.com/categories/name/stress/category_id/165' style='margin-top: 340px;'><img alt='' src='static/Image/right3.jpg' /></a>");
	//	$('#secondary-links').append("<a href='#'><img alt='' src='static/Image/right4.jpg' /></a>");

		// Fading banners on the front page.
		bannerFade();

	}
	else $('#secondary-links').html(''); // Hide right hand image column on non-front pages.
	
	if(isProductPage()) {
		$('.info-wrap').append("<div style='float: left; clear: both; margin-top: 20px;'><a href='http://server.iad.liveperson.net/hc/64533636/?cmd=file&amp;file=visitorWantsToChat&amp;site=64533636&amp;byhref=1&amp;imageUrl=http://images.liveperson.com/lp/64533636' target='_blank'><img src='/static/Image/LivePerson%20Detail%20Page.jpg' alt='' /></a></div>");

	}

	if(isCheckoutPage()) {
		$('#primary').append("<img src='/static/Image/filetypes.png' style='position: absolute; top: 1115px; right: 200px;' />");

	}

	// Add custom banners to category pages
	// processCatBanner();

	// Add live chat to checkout page.
	//	if(isCheckoutPage()) addPrimaryExtra("<img alt='' src='/static/Image/liveChat.jpg' />");

	// Hide credit card logos on basket (basket) page
	if(queryPageType() == 'basket') $('#logos').css('display', 'none');

	$('#a-z-list').prepend('<h2>Fast Find - For All Your Promotional Products</h2>');

});

// Add HTML to an extra division in #primary
//--------------------------------------------------------------
function addPrimaryExtra(htmlExtra) {
	$('#primary').prepend("<div id='primaryExtra'>" + htmlExtra + '</div>');

}

// Category names/images
//--------------------------------------------------------------
var categoryNames = [];
var categoryImages = [];

categoryNames[0] = 'bags';
categoryNames[1] = 'clocks';
categoryNames[2] = 'clothing';
categoryNames[3] = 'computer-items';
categoryNames[4] = 'desk';
categoryNames[5] = 'eco';
categoryNames[6] = 'keyrings';
categoryNames[7] = 'misc';
categoryNames[8] = 'mousemats';
categoryNames[9] = 'mugs';
categoryNames[10] = 'paper';
categoryNames[11] = 'pens';
categoryNames[12] = 'travel-leisure';
categoryNames[13] = 'umbrellas';

categoryImages[0] = 'bags.gif';
categoryImages[1] = 'clocks.gif';
categoryImages[2] = 'clothing.gif';
categoryImages[3] = 'computer.gif';
categoryImages[4] = 'desk.gif';
categoryImages[5] = 'eco.gif';
categoryImages[6] = 'keyrings.gif';
categoryImages[7] = 'miscellaneous.gif';
categoryImages[8] = 'mouseMats.gif';
categoryImages[9] = 'mugs.gif';
categoryImages[10] = 'paper.gif';
categoryImages[11] = 'pens.gif';
categoryImages[12] = 'travel.gif';
categoryImages[13] = 'umbrellas.gif';

// Process category banner addition
//--------------------------------------------------------------
function processCatBanner() {
	if(!isCategoryPage()) return;

	// Tokenise class string, save to array
	var classString = $('body').attr('class');
	var classTokens = classString.split(' ');

	// Get second class, i.e. the category name
	for(var i = 0; i < categoryNames.length; i++) {
		if(categoryNames[i] == classTokens[1]) 	{
			var bannerImage = "<div class='bannerImage'><img src='/static/Image/banner/" + categoryImages[i] + "' /></div>"
			$('body.category #topBar').html(bannerImage);
			return;

		}

	}

	$('body.category #topBar').html("<div class='bannerImage'><img src='/static/Image/banner/blank.png' /></div>");

}

// Are we on the home page?
//--------------------------------------------------------------
function isHomePage() {
	if(queryPageType() == 'home') return true;
	return false;

}

// Are we on a category page?
//--------------------------------------------------------------
function isCategoryPage() {
	if(queryPageType() == 'category') return true;
	return false;

}

// Are we on a subcategory page?
//--------------------------------------------------------------
function isSubCategoryPage() {
	if(queryPageType() == 'subCategory') return true;
	return false;

}

// Are we on a product page?
//--------------------------------------------------------------
function isProductPage() {
	if(queryPageType() == 'product') return true;
	return false;

}

// Are we on the checkout page?
//--------------------------------------------------------------
function isCheckoutPage() {
	if(queryPageType() == 'checkout') return true;
	return false;

}

// Are we on the basket page?
//--------------------------------------------------------------
function isBasketPage() {
	if(queryPageType() == 'basket') return true;
	return false;

}

// Find out what kind of page we are on
//--------------------------------------------------------------
function queryPageType() {
	var bodyElement = $('body');

	if(bodyElement.hasClass('home')) return 'home';
	else if(bodyElement.hasClass('basket')) return 'basket'
	else if(bodyElement.hasClass('product')) return 'product';
	else if(bodyElement.hasClass('checkout')) return 'checkout';
	else if(bodyElement.hasClass('basket')) return 'basket';
	else if(bodyElement.hasClass('product')) return 'product';
	else if(bodyElement.hasClass('advanced-search')) return 'advancedSearch'
	else if(bodyElement.hasClass('page')) return 'page';

	var classString = $('body').attr('class');
	var classTokens = classString.split(' ');
	var numClasses = classTokens.length;

	// Category or subcategory?
	if(numClasses < 3) {
		if(bodyElement.hasClass('category')) return 'category';

	}
	else if(bodyElement.hasClass('category')) return 'subCategory';

	// For some reason QOP hasn't put a class on the body tag.
	// Try and find out more by checking page content.
	if($('#checkout').length) return 'checkout';

	return false;

}

// Add footer contact division, populate with HTML.
//--------------------------------------------------
function addFooterContactDetails(contactHTML) {
	$('#footer #meta').prepend("<div id='contactDetails'></div>");
	$('#footer #contactDetails').prepend(contactHTML);

}

// Login box
//--------------------------------------------------------------
function addLoginFunctionality() {
	var usernameText = 'username';
	var passwordText = 'pass';
	$('#secondary #usr').attr('value', usernameText);
	$('#secondary #pwd').attr('value', passwordText);

	// Remove username text on focus
	$('#secondary #usr').focus(function(event) {
		if($('#secondary #usr').attr('value') == usernameText) $(this).attr('value', '');

	});

	// Show username text on blur
	$('#secondary #usr').blur(function(event) {
		if($('#secondary #usr').attr('value') == '') $(this).attr('value', usernameText);

	});

	// Remove password text on focus
	$('#secondary #pwd').focus(function(event) {
		if($('#secondary #pwd').attr('value') == passwordText) $(this).attr('value', '');

	});

	// Show password text on blur
	$('#secondary #pwd').blur(function(event) {
		if($('#secondary #pwd').attr('value') == '') $(this).attr('value', passwordText);

	});

}

// Search
//--------------------------------------------------------------
function addSearchFunctionality() {
	var searchKeywordText = 'Search';
	$('#top-search input#keywords').attr('value', searchKeywordText); // Search box default text

	// Remove search text on focus
	$('#keywords').focus(function(event) {
		if($(this).attr('value') == searchKeywordText) $(this).attr('value', '');

	});

	// Add search text on blur
	$('#keywords').blur(function(event) {
		if($(this).attr('value') == '') $(this).attr('value', searchKeywordText);

	});

	$('#top-search input.submit').attr('value', 'Go'); // Login button text	

}

// Inner Fade jQuery Plugin
//--------------------------------------------------------------
function bannerFade() {
	if(!isHomePage()) return;

	$('#bannerFade ul').innerfade({
		animationtype: 'fade',
		speed: 1000,
		timeout: 6000,
		type: 'sequence',
		containerheight: 'auto'

	});
	
	$('#topBannerFade ul').innerfade({
		animationtype: 'fade',
		speed: 1000,
		timeout: 6000,
		type: 'sequence',
		containerheight: 'auto'

	});

}

// Reset heading text
//--------------------------------------------------------------
//function removeHomeText() {
//	$('#header h1 a').text('');
//}