var viewportwidth;
var viewportheight;


//strips out console log calls when console is not active, to stop js errors
window.console||(console={log:function(){}});
setIEgetElementsByClassName();

function setIESafeCSS( obj, thisStyle ) //thisStyle would be a string
{
	//alert( BrowserDetect.OS + ' - ' + BrowserDetect.browser + ' - ' +  BrowserDetect.version );
	
	if( BrowserDetect.browser == 'Explorer'   &&   BrowserDetect.version <= 7 ) //setAttribute not supported by ie 7 and below
	{
		thisStyle = thisStyle.split(' ').join('');
		var styleNameArr = thisStyle.split(';');
		var styleValArr  = [];
		for( var i = 0; i < styleNameArr.length; i++ )
		{
			var styleArr = styleNameArr[i].split(':');
			var name = styleArr[0];
			var val  = styleArr[1];
			
			if( name != ''  &&  name != undefined  &&  val != ''  &&  val != undefined )
			{
				obj.style[ name ] = val;
			}
		}
	}
	else
	{
		obj.setAttribute('style', obj.getAttribute('style') + '; ' + thisStyle + '; ' );
	}
}



//allows IE to use getElementsByClassName(). Other modern browsers have it built in
function setIEgetElementsByClassName()
{
	if (document.getElementsByClassName == undefined) {
		document.getElementsByClassName = function(className)
		{
			var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
			var allElements = document.getElementsByTagName("*");
			var results = [];
	
			var element;
			for (var i = 0; (element = allElements[i]) != null; i++) {
				var elementClass = element.className;
				if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
					results.push(element);
			}
	
			return results;
		}
	}
}



function setViewortDims()
{
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	
	if (typeof window.innerWidth != 'undefined')
	{
	  viewportwidth = window.innerWidth,
	  viewportheight = window.innerHeight
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	else if (typeof document.documentElement != 'undefined'
	 && typeof document.documentElement.clientWidth !=
	 'undefined' && document.documentElement.clientWidth != 0)
	{
	   viewportwidth = document.documentElement.clientWidth,
	   viewportheight = document.documentElement.clientHeight
	}
	
	// older versions of IE
	
	else
	{
	   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	   viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	//console.log('Your viewport width is '+viewportwidth+'x'+viewportheight);
}



//JUST USED FOR VICKY DAVIES
function removeHiddenDivs()
{
	
	var hiddenDivGroup = $('.hidden-div');
	hiddenDivGroup.remove();
}

function getIsHomeAndPageURL()
{
	var introQueryStr = 'intro=false';
	var openCollStr = 'openColl=true';
	var hasFunction = 'function=';
	//didn’t like getPageURL() for some reason on the checkout pages. Wasn’t getting full url, but this works. Strange.
	var fullURL = window.location.href;
	var urlArr = fullURL.split('index.asp');
	
	var includesIndex = false;
	var isHome = false;
	var showIntro = false;
	var openColl = false;
	var pageURL = '';
	
	if( urlArr.length > 1 )
	{
		includesIndex = true;
		pageURL = urlArr[1];
	}
	
	if( includesIndex   &&   pageURL == ''   ||   			 //if index.asp is in name
		includesIndex   &&   pageURL.indexOf( introQueryStr ) != -1   ||   //if intro is showing
		includesIndex   &&   pageURL.indexOf( openCollStr ) != -1   ||     //if open Collection nav is set by intro page
		fullURL.indexOf( '.asp' ) == -1 )	  	  			 //if just web root
	{
		isHome = true;
	}
	
	
	
	if( isHome   &&   includesIndex   &&   pageURL.indexOf( introQueryStr ) == -1   ||   
		fullURL.indexOf( '.asp' ) == -1 ) //if just web root
	{
		showIntro = true;
	}
	
	if( includesIndex   &&   pageURL.indexOf( openCollStr ) != -1 )
	{
		openColl = true;
	}
	
	return [ isHome, pageURL, showIntro, openColl ];
}

//JUST USED FOR VICKY DAVIES



function getPageURL()
{
	var fullURL = getFullURL();
	var urlArr = fullURL.split('/');
	var pageURL = urlArr[ urlArr.length-1 ];
	
	return pageURL;
}


function getFullURL()
{
	var fullURL = window.location.href;
	
	return fullURL;
}



function concatHTMLCollection( htmlCol1, htmlCol2 )
{
	var arr = [];
	
	for( var i = 0; i < htmlCol1.length; i++ )
	{
		arr.push( htmlCol1[i] );
	}
	
	for( var j = 0; j < htmlCol2.length; j++ )
	{
		arr.push( htmlCol2[j] );
	}
	
	return arr;
}


function writeToPage( str )
{
	<!--
	document.open();
	document.write( str );
	document.close();
	//-->
}


function hideElementByID( id )
{
	var thisEl = document.getElementById( id );
	thisEl.style.visibility = 'hidden';
	thisEl.style.width = '0px';
	thisEl.style.height = '0px';
	thisEl.style.top = '0px';
	thisEl.style.left = '0px';
	thisEl.style.position = 'absolute';
}


function include(filename)
{
	var head = document.getElementsByTagName('head')[0];
	
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	
	head.appendChild(script)
}


function writeJSToPage( str )
{
	var thisTag = '<script src="' + str + '" type="text/javascript" language="javascript" ></script>';
	writeToPage( thisTag );
}


function writeCSSToPage( str )
{
	var thisTag = '<link href="' + str + '" rel="stylesheet" type="text/css" />';
	writeToPage( thisTag );
}


function getHTMLText( html )
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent||tmp.innerText;
}


//stops errors from showing
function turnOffErrors()
{
	window.onerror = noError;
}

function noError(){return true;}

