// Are we using WebKit here?
// This method is recommended by Apple themselves.

var tempStr = navigator.userAgent.toLowerCase();
var pos = tempStr.indexOf("applewebkit/");
var isAppleWebkit = (pos != -1);		
if (isAppleWebkit) { addShadowForWebKit(); }

// This function adds a null shadow to text on WebKit-based browsers which leads
// to better rendered, thinner typography. It is totally harmless CSS, but we
// lead it dynamically as it does not belong to a CSS 2 style sheet and would
// make the CSS 2 document invalid.

function addShadowForWebKit() {
	var cssLink = document.createElement("link")
	cssLink.href = "css-webkit.css";
	cssLink.rel = "stylesheet";
	cssLink.type = "text/css";
	cssLink.media = 'screen';
	document.getElementsByTagName("head")[0].appendChild(cssLink);
}


var effectdur = 1;
var currentBalloon = null;
var firstAppear = false;
var focusedElement = false;
balloonImages = new Array("art/str-globeyellow.png","art/str-globered.png","art/str-globeorange.png", "art/str-globeyellow.png", "");
balloonText = new Array("", "", "","", "");

var activeTab;
var activeTabControl;

function allPagesSetup() 
{
	if (weAreUsingIe != '6') setUpBallonDisplay();
}

function setupFormHighlight()
{
	var focusControls = $$('input', 'textarea', 'select');
	focusControls.each(function(item) {
		var myParent = item.up();
		if (myParent.hasClassName('ctrlHolder')) {
			item.onfocus = function() {return onFocusHighlight(myParent, item)};
			item.onblur = function() {return onFocusHighlight(myParent, item)};
		}
	});
	
	var optionControls = $$('option');
	optionControls.each(function(item) {
		item.onclick = function() {
		var myParent = item.up();
		if (myParent.hasClassName('selectInput')) {
			myParent.focus();
			if (!myParent.up().hasClassName('focused')) myParent.up().toggleClassName('focused');
		}
		}
	});
	
	$$('.sideBar').each(function(item) { item.toggle();});

	if ((weAreUsingIe == 'No') || (weAreUsingIe == '7Plus')) {
		$$('.noJS').each(function(item) { item.toggle();});
		$$('div.button').each(function(item) { 
			item.style.display = "block";
			item.onclick = function () {
				var myForm = item.up('form')
				document.myForm.submit();
			};
		});
	}
	
	window.onfocus = function() {
		if (focusedElement && !isAppleWebkit) {
			focusedElement = false;
			var theCntrl = $('manuform').getElementsByClassName('focused');
			
			if (theCntrl.length > 0) {
			
				var theField = theCntrl[0].getElementsBySelector('input','textarea' ,'select');
	
				theCntrl[0].toggleClassName('focused');
	
				new Effect.Morph(theCntrl[0], {style:'background:#fffbed; border: 2px solid #fee07e;', duration: 0.35});
				
				new Effect.Appear($(theField[0].id+'Info'), {delay: 0.15, afterFinish: function() {focusedElement = true}});
			}
		}
	};
	
	window.onblur = function() {
		if (focusedElement && !isAppleWebkit) {
			focusedElement = false;
			var theCntrl = $('manuform').getElementsByClassName('focused');
			
			if (theCntrl.length > 0) {
				var theField = theCntrl[0].getElementsBySelector('input','textarea', 'select');
	
				theCntrl[0].toggleClassName('focused');
	
				new Effect.Morph(theCntrl[0], {style:'background:#272626; border: 2px solid #272626;', duration: 0.1});
				new Effect.Fade($(theField[0].id+'Info'), {delay: 0.1, afterFinish: function() {focusedElement = false}});
			}
		}
	}
	
	activeTab = $('firstTabField');
	activeTabControl = $('firstTab');
	theImage1 = new Image();
	theImage1.src = "art/str-tabFirstInActive.png";
	theImage2 = new Image();
	theImage2.src = "art/str-tabSecondActive.png";
	theImage3 = new Image();
	theImage3.src = "art/str-tabThirdActive.png";
	
	theImage4 = new Image();
	theImage4.src = "art/str-tabFirstOver.png";
	theImage5 = new Image();
	theImage5.src = "art/str-tabSecondOver.png";
	theImage6 = new Image();
	theImage6.src = "art/str-tabThirdOver.png";
			
	var tabControls = $('tabsList').getElementsBySelector('li');

		tabControls.each(function(item) {
			item.onclick = function() {
				if ($(item.id+'Field') != activeTab) {
					//new Effect.Fade(activeTab, {duration:0.5, queue:'end'});
					activeTab.hide();
					activeTabControl.toggleClassName('active');
					activeTab = $(item.id+'Field');
					activeTabControl = item;
					//new Effect.Appear($(item.id+'Field'), {queue: 'end'});
					$(item.id+'Field').show();
					activeTabControl.toggleClassName('active');
				}
			}
		});
	
}

function onFocusHighlight(elm, field)
{
	elm.toggleClassName('focused');
	
	if (elm.hasClassName('focused')) {
		new Effect.Morph(elm, {style:'background:#fffbed; border: 2px solid #fee07e;', duration: 0.35, afterFinish: function() {cleanUp(); }});
		if ((weAreUsingIe == 'No') || (weAreUsingIe == '7Plus'))
			new Effect.Appear($(field.id+'Info'), {delay: 0.1, afterFinish: function() {focusedElement = true}});
		else $(field.id+'Info').show();
	}
	else {
		new Effect.Morph(elm, {style:'background:#272626; border: 2px solid #272626;', duration: 0.1});
		if ((weAreUsingIe == 'No') || (weAreUsingIe == '7Plus'))
			new Effect.Fade($(field.id+'Info'),{duration: 0.1, afterFinish: function() {focusedElement = false}});
		else $(field.id+'Info').hide();
	}
	
}

function cleanUp()
{
	var toClean = $('manuform').getElementsByClassName('ctrlHolder');
	toClean.each(function(item) {
		if (!item.hasClassName('focused')) {
			item.setStyle({background:'#272626', border: '2px solid #272626'});
		}
	});
}

function setUpBallonDisplay() {
	if ((weAreUsingIe == 'No') || (weAreUsingIe == '7Plus')) {
	
		if (weAreUsingIe == '7Plus') effectdur = 0.25;
		
		new Effect.Appear($('brandsBar'));

		var brandList = $('brandsBar').getElementsBySelector("ul");
		var brandLinks = brandList[0].getElementsBySelector("a");
		
		brandLinks.each(function(barItem,count) {
			if (!barItem.hasClassName('current')) {
				
				balloonText[count] = barItem.title.replace(" - ","</b>");
				barItem.setAttribute("title", "");
				
				barItem.onmouseover = function()
				{
					return displayBalloon(barItem,count);
				}
									
				Event.observe(barItem, 'mouseout', function(e) { 
					if (!Position.within(barItem, Event.pointerX(e), Event.pointerY(e))) { 
						var queue = Effect.Queues.get('balloons');
						queue.each(function(e) { e.cancel() });
						var balloon = document.getElementById("balloon");
						new Effect.Fade(balloon, {duration: effectdur , queue: {position: 'end', scope: 'balloons'}}); 
						currentBalloon = null;
					} 
				});
			}
		});
			
		var divElt = document.createElement('div');
		divElt.setAttribute("id", "balloon");
		divElt.style.display = "none";
		document.getElementsByTagName("body")[0].appendChild(divElt);
	}
}

function displayBalloon(anDIV, count)
{	
	if ( (anDIV) == currentBalloon ) return false;
	if (currentBalloon == null) { firstAppear = true } else { firstAppear = false };
	
	if (currentBalloon == null) {
		currentBalloon = anDIV;
		new Effect.Move(anDIV, {x:0, y:0, duration: 0.0, queue: {position: 'end', scope: 'balloons'}, afterFinish: function() {assembleBalloon(anDIV, count);}});
	}
  return false;
}

function assembleBalloon(anDIV, count) {
	if (weAreUsingIe == '7Plus')
		var objX = findPosX(anDIV) - document.documentElement.clientWidth + anDIV.getWidth();
	else 
		var objX = findPosX(anDIV);
	var objY = findPosY(anDIV);

	var balloon = document.getElementById("balloon");
	if ( balloon )
	{
		if ( balloon.childNodes )
		{
			while ( balloon.childNodes.length > 0 )
			{
				balloon.removeChild(balloon.childNodes[0]);
			}
		}
		var brandElt = document.createElement('img');
		brandElt.setAttribute("id","branding");
		brandElt.setAttribute("src",balloonImages[count]);
		balloon.appendChild(brandElt);
		var newElt = document.createElement('div');
		newElt.setAttribute("id","ballooncontents");
		balloon.appendChild(newElt);
		var contentObj = document.getElementById("ballooncontents");
		myContents = balloonText[count];	
		var myContentHolder = $('ballooncontents');
		myContentHolder.innerHTML = "<p><b>" + myContents + "</p>";
		balloon.style.top = (objY+15) + 'px'; balloon.style.left = (objX-210) + 'px';    
		new Effect.Appear(balloon, {duration: effectdur, queue: {position: 'end', scope: 'balloons'}});
	}
}

function findPosX(obj)
{
	var obj = obj;    
	var curleft = 0;
	if (obj.offsetParent)
    {
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
        }
        if ( obj != null )
			curleft += obj.offsetLeft;
    }
    else if (obj.x)
		curleft += obj.x;
	    return curleft;
}

// This is a support function that finds the Y position of elements.

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
    {
		while (obj.offsetParent)
        {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
        if ( obj != null )
            curtop += obj.offsetTop;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function minHeightToAvoidFlickr(theElement)
{	
	var theHeightToSet = (Element.getHeight(theElement) + 'px');
	$('formWrapper').style.minHeight = theHeightToSet ;
}

// This function removes tooltips for IE6 + JS to work around a browser bug.

function removeTips() {

for(i=0;i<document.images.length;i++) {
document.images[i].setAttribute("alt", "");
	}
	
	for(i=0;i<document.links.length;i++) {
document.links[i].setAttribute("title", "");
	}
}

function preloadBg() {

	var count = 0;
	
	for (count=0; count < 5;count++) {
		theImage = new Image();
		theImage.src = balloonImages[count];
	}
	theImage.src = "art/str-bubble.png";
}

Effect.BackgroundScroll = Class.create();
Object.extend(Object.extend(Effect.BackgroundScroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element.currentBackgroundPosition)
      this.element.currentBackgroundPosition = [0, 0];
    this.offsets = arguments[1];
    this.start(arguments[2] || {});
  },
  setup: function() {
    this.current = this.element.currentBackgroundPosition;
    this.delta = [
      this.offsets[0] - this.current[0],
      this.offsets[1] - this.current[1]];
  },
  update: function(position) {
    this.element.currentBackgroundPosition = [
      Math.round(this.current[0] + (this.delta[0]*position)),
      Math.round(this.current[1] + (this.delta[1]*position)) ];
    this.element.style.backgroundPosition = 
      this.element.currentBackgroundPosition[0] + 'px ' +
      this.element.currentBackgroundPosition[1] + 'px';
  }
});
