// General Javascript/jQuery for Quick Order Portal 'Freestyle'
//--------------------------------------------------------------
$(document).ready(function() {
	addLoginFunctionality();
	addSearchFunctionality();

	$('#category-nav').prepend("<img alt='' src='/static/Image/giftCats.png'/>"); // Add a category-nav header
//	$('#tertiary').append("<a href='http://www.networkcatalogue.co.uk/view/catalogues/spectrum2009/' id='virtualCatHead' style='display: block; margin: 3px 0 2px;' target='_blank'><img alt='' src='/static/Image/virtualCat.png' style='display: block; margin: 0; padding: 0;' /></a>");
//	$('#tertiary').append("<a href='http://www.networkcatalogue.co.uk/view/catalogues/spectrum2009/' style='display: block; margin: 0; padding: 0;' target='_blank'><img alt='' src='/static/Image/catCover.png' /></a>");

	// Inject content from an external file
	$('#footer #bottom-links').load('/static/aToZ.htm #aToZ');

	fixAdvancedSearch();
	switchGlobalProductText();
//	myLogoFormMove();
	$('body.my-logos #technologo-image').append("<div style='clear: left; float: left; margin-top: 10px;'><a href='/sso/QuickOrder?action=jump&to=last_prod' style='float: left; clear: left;'><img alt='View Products With My Logo' src='/static/Image/viewProductsBig.png' /></a></div>");
	$('body.my-logos #user-gallery .inner-box form').prepend("<h2 style='float: left; clear: both; font-size: 18px; margin-bottom: 10px; width: 100%;'>Upload Logo</h2>");

	var checkLoginText = $('body.my-logos p.msg').not('#technologo-image').html();
	if("You must be logged in to add images to your gallery." == checkLoginText) {
		$('body.my-logos p.msg').not('#technologo-image').append("<br />Please <a href='/login.html' style='float: none; clear: none;'>log in to continue.</a>");

	}


	addFooterHeightAdjust();

});

// Move global product text from the top of the product details to the bottom
//-----------------------------------------------------------------------------
function myLogoFormMove() {
	var myLogoForm = $('body.my-logos #user-gallery .inner-box').children('form');
	$('body.my-logos #user-gallery form').hide();
	$('body.my-logos #user-gallery .inner-box').prepend(myLogoForm.html());

}

// Move global product text from the top of the product details to the bottom
//-----------------------------------------------------------------------------
function switchGlobalProductText() {
	var productText = $('#product .info-wrap .body p').parent().html();
	$('#product .info-wrap .body p').parent().hide();
	$('#product .info-wrap').append(productText);

}

// Remove distracting search options from advanced
// search results page.
//--------------------------------------------------
function fixAdvancedSearch() {
	if($('#search-results').length) $('#search').hide();
	$('#search-results').prepend("<div>For further results, <a id='refineSearch' href='/search.html'>Refine Your Search</a></div>");

	$('#refineSearch').click(function(event) {
		event.preventDefault();
		$('#search').fadeIn();
		$(this).hide();

	});

}

// Adjust footer height
//--------------------------------------------------
function addFooterHeightAdjust() {
	var heights = new Array(3);
	heights[0] = $('#primary').height();
	heights[1] = $('#secondary').height();
	heights[2] = $('#tertiary').height();
	heights.sort(sortNumber);

	// Set all heights to maximum found
	$('#primary').height(heights[2]);
	$('#secondary').height(heights[2]);
	$('#tertiary').height(heights[2]);

	// Set footer position
	$('#footer').css('position', 'absolute');
	$('#footer').css('top', (heights[2] + 130) + 'px'); // Height + header height

}

function sortNumber(a,b) { return a - b; }

// Login box
//--------------------------------------------------------------
function addLoginFunctionality() {
	$('#secondary #login li.text input#usr').attr('value', 'username'); // Username input field default text
	$('#secondary #login li.text input#pwd').attr('value', 'password'); // Password input field default text
	if($('#secondary #login li.text input#pwd').attr('value') == 'username')
	$("#secondary #login li.text input#usr").focus(function(event) { $(this).attr('value', ''); }); // Remove username text on focus
	$("#secondary #login li.text input#pwd").focus(function(event) { $(this).attr('value', ''); }); // Remove password text on focus

}

// 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', ''); // Login button text	

}