// General Javascript/jQuery for Quick Order Portal 'Freestyle'
//--------------------------------------------------------------
/* NOTES regarding QOP Javascript enhancements
//--------------------------------------------------------------
 This Javascript is provided as is, there is no support for
 any the supplied code below.
 
 The code requires the inclusion of jQuery before this code
 is loaded.
 
 The banner fading functionality requires both jQuery, then
 the jQuery innerfade plugin.

 This file contains some useful functions including
 - Automatic smart inclusion of helper text in the login and
   search input fields. E.g. 'search' or 'login'.
 - Functions to aid Javascript that needs to target a specific
   QOP page. E.g. just the home page, or every page excluding
   the basket or checkout pages.
 - Addition of enhancing links in the mini-baskets that can
   be included as part of the design.
 - Cross-fading image slideshow functionality i.e. an
   imitation flash banner. This is both more compatible,
   generally smoother and easier to maintain.
 - Some general housekeeping e.g. the ability to switch
   global product text on the product details page and
   the addition of 'in stock' text where necessary.
 - Javascript content addition helper functions.
   E.g. extraFooterContent and extraPrimaryContent variables
   can both be edited. If present they allow the addition
   of new page content post-load.
 - Automatic footer placement in response to large product
   lists etc. This should only be used as a last resort if
   you are finding the CSS tricky.
--------------------------------------------------------------*/

// The following functions are called on page load
//----------------------------------------------------
$(document).ready(function() {
	bannerFade();
//	switchGlobalProductText();

	// Add a random header in the primary division of each
	// page. Could be used for rotating advertisements.
//	addRandomImage(5);
	
	// Makes a link open in new window.
	// The example applies this to the header link.
//	setNewPage('#header h1 a');

	// Add flash at the top of the primary division.
	// Change flash file path as required.
//	addFlash('test.swf');

	// Reposition footer depending on page height
//	addFooterHeightAdjust();

});

// 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('category')) return 'category';
	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';
	else if($('#product').length) return 'product';
	return false;

}

// Inner Fade jQuery Plugin
//----------------------------------------------------
function bannerFade() {
//	if(!isHomePage()) return; // Only on home page

	$('#bannerFade ul').innerfade({
		animationtype: 'fade',
		speed: 1500,
		timeout: 6000,
		type: 'sequence',
		containerheight: 'auto'

	});

}

// Set a link to open in a new window
//--------------------------------------------------------------
function setNewPage(linkCSS) {
	$(linkCSS).attr('target', '_new');	

}

// Move global product text from the top of the product details to below it
//-----------------------------------------------------------------------------
function switchGlobalProductText() {
	if(!isProductPage()) return;

	var productText = $('#product .info-wrap .body p').parent().html();
	$('#product .info-wrap .body p').parent().hide();
	$('#product .info-wrap').append(productText);

}
