//
// Creates an xml/http object for ajax functions to use
//
function getXmlHttpObject() {
	var objXmlHttp = null;

	try {
		// Firefox, Opera 8.0+, Safari
		objXmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			objXmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {
			try {
				objXmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	return objXmlHttp;
}

function greyScreen(blnOn) {
	// Displays/hides the grey screen
	if ($('div#divGreyScreen').length == 0) {
		$('body').append('<div id="divGreyScreen" style="opacity: 0.6; display:none; position: absolute; background: #000000; height: 100%; width: 100%; top: 0; left: 0; z-index:1;"></div>');
	}

	if (blnOn) {
		$('div#divGreyScreen').css('display', 'block');
	} else {
		$('div#divGreyScreen').css('display', 'none');
	}
}

function showHelp() {
	window.open('/help/');

}

//
// Hides the navigation area
//
function hideNav() {
	// Check to see if we're on the set live edit page
	var objSetImage = document.getElementById('divImage');

	if (objSetImage) {
		objSetImage.style.width = '695px';
	}

	document.getElementById('leftNavControl').src = '/img/show_nav.gif';
	document.getElementById('leftNavControl').onclick = showNav;
	document.getElementById('leftNavControl').onmouseover = new Function("Tip('Show navigation pane')");
	document.getElementById('leftNav').className = 'leftNavHidden';
}


//
// Unhides the navigation area
//
function showNav() {
	var objSetImage = document.getElementById('divImage');

	if (objSetImage) {
		objSetImage.style.width = '460px';
	}

	document.getElementById('leftNavControl').src = '/img/hide_nav.gif';
	document.getElementById('leftNavControl').onclick = hideNav;
	document.getElementById('leftNavControl').onmouseover = new Function("Tip('Hide navigation pane')");
	document.getElementById('leftNav').className = 'leftNavVisible';
}


//
// Displays account selector (below order wizard image)
//
function changeAccount() {
	objXmlHttp = getXmlHttpObject();

	if (objXmlHttp == null) {return;}

	objXmlHttp.onreadystatechange = function() {
		if (objXmlHttp.readyState == 4) {
			document.getElementById("nav_account_selector").innerHTML = objXmlHttp.responseText;
		}
	}

	objXmlHttp.open('GET', '/ajax/get_account_selector.php', true);
	objXmlHttp.send(null);
}

jQuery.fn.centre = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

// Applies the jquery CSS fix
$(document).ready(function(){

	var aj,strPreviousSearch;
	aj = strPreviousSearch = null;

	$("*").not("font,h1,h2,h3,h4,h5,h6,strong").addClass("common_font");

	var showSelector = function() {

		if($('div#divAccountSearchResults').length == 0) {
			// Create the search results div
			$('body').append("<div id=\"divAccountSearchResults\" style=\"display: none\"  class=\"ajaxSelector\"><p>No results found for your search</p><ul></ul></div>");


			$('#divAccountSearchResults ul li').live('click', function() {
				var idA = $(this).data('id');
				$('form#accountSelector').append('<input type="hidden" name="idAccountSelector" value="' + idA + '" />').submit();
			});

			$('#txtSearchAccounts').bind('blur', function(){
				if (!$('#divAccountSearchResults').data('hover')) {
					$('#divAccountSearchResults').css('display', 'none');
				}
			});

			$('#divAccountSearchResults').hover(
				function() {$.data(this, 'hover', true);},
				function() {$.data(this, 'hover', false);}).data('hover', false);

		}


		$('div#divAccountSearchResults').css('display', 'block');
		var oOff = $('#txtSearchAccounts').offset();
		var intDivLeft = oOff.left;
		var intDivTop = oOff.top + $('#txtSearchAccounts').outerHeight();
		$('div#divAccountSearchResults').width(185);
		$('div#divAccountSearchResults').offset({top: intDivTop, left: intDivLeft});
	}

	if($('img#imgSearchAccounts').length > 0) {

		$("<link/>", {rel: "stylesheet",type: "text/css",href: "/css/account_search_box.css"}).appendTo("head");

		var strCSS = '/css/account_search_box.css';

		/*$.ajax({
          url: strCSS,
          dataType: 'css',
          success: function(){
                $('<link rel="stylesheet" type="text/css" href="'+strCSS+'" />').appendTo("head");
            }
		});*/

		$('<link>').appendTo($('head')).attr({type: 'text/css', rel:'stylesheet'}).attr('href', strCSS);



		$('img#imgSearchAccounts').click(
			function(){
				$('div#nav_account_selector').html('<input type="text" id="txtSearchAccounts" autocomplete="off" style="width:183px" />');
				$('input#txtSearchAccounts').focus().bind('keyup', function(){
					var strValue = $('input#txtSearchAccounts').val();
					if (strValue.length > 2) {
						// Do the search

						if (aj != null) {
							aj.abort();
						}

						aj = $.post('/api/accounts/search/', {strSearch: strValue}, function(data){
							showSelector();

							if (data.status == 'ok') {
								if (data.accounts.length > 0) {
									$('#divAccountSearchResults').css('display', 'block');
									$('#divAccountSearchResults ul').css('display', 'block');
									$('#divAccountSearchResults p').css('display', 'none');
									$('#divAccountSearchResults ul').empty();
									$(data.accounts).each(function(i,item){
										$('#divAccountSearchResults ul').append("<li>" + item.name +  "</li>");
										$('#divAccountSearchResults ul li:last').data('id', item.id);
									});
								} else {
									$('#divAccountSearchResults').css('display', 'none');
								}
							}
						});
					}
				});
			}
		);
	}


	if ($('a#lnkWizardButton').length == 1) {

		$('body').data('wizard_load_in_progress', false);

		$('a#lnkWizardButton').on('dblclick', function(e){e.preventDefault(); return(false)});
		$('a#lnkWizardButton').on('click', function(e){
			if (!$('body').data('wizard_load_in_progress')) {
				$('body').data('wizard_load_in_progress', true);
				greyScreen(true);

				$('#divLoadingScreen').css('display', 'block').centre();

				document.location = '/order-wizard/select-template/page-1/';

				e.preventDefault();
				return(false);
			}
		});

		var sForm = '<div id="divLoadingScreen" style="position: absolute; z-index: 2; width: 90px; height: 95px; border: 1px solid black; background: #fff; padding: 20px; display: none;">\n\
			<center>\n\
			<img src="/img/icons/loading.gif"><br />Please Wait</center>\n\
			</center></div>';
		$('body').append(sForm);

	}



});
