/**
 * All In One JavaScript file
 * includes the following js files:
 *	- cookie.js
 *	- textsize.js
 *	- trust.js
 *	- enlarge.js
 *	- iglossary.js
 *	- navigation.js
 */

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * cookie.js
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
var Cookie = {
	set: function (name, value, seconds) {
		if (seconds) {
			d = new Date();
			d.setTime(d.getTime() + (seconds * 1000));
			expiry = '; expires=' + d.toGMTString();
		}
		else
			expiry = '';
		document.cookie = name + "=" + value + expiry + "; path=/";
	},
	get: function (name) {
		nameEQ = name + "=";
		ca = document.cookie.split(';');
		for (i = 0; i < ca.length; i++){
			c = ca[i];
			while (c.charAt(0) == ' ')
				c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length, c.length);
		}
		return null
	},
	unset: function (name) {
		Cookie.set(name, '', -1);
	}
}

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * textsize.js
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function changeTextSize(className) {
	$('access').getElementsByClassName(className)[0].addClassName('accessOn');
	if (className == 'small') {
		document.getElementsByTagName('body')[0].style.fontSize = 100 + '%';
		$('access').getElementsByClassName('medium')[0].removeClassName('accessOn');
		$('access').getElementsByClassName('large')[0].removeClassName('accessOn');
		Cookie.set('textSize', className, 31556926);
	}
	else if (className == 'medium') {
		document.getElementsByTagName('body')[0].style.fontSize = 115 + '%';
		$('access').getElementsByClassName('small')[0].removeClassName('accessOn');
		$('access').getElementsByClassName('large')[0].removeClassName('accessOn');
		Cookie.set('textSize', className, 31556926);
	}
	else if (className == 'large') {
		document.getElementsByTagName('body')[0].style.fontSize = 130 + '%';
		$('access').getElementsByClassName('small')[0].removeClassName('accessOn');
		$('access').getElementsByClassName('medium')[0].removeClassName('accessOn');
		Cookie.set('textSize', className, 31556926);
	}
}
function textSizeLinks() {
	var textControls;
	textControls = '<div id="access">';
	textControls += snippet('misctextsize');
	textControls += '<a class="small" href="#" onclick="changeTextSize(\'small\');return false;">A</a>';
	textControls += '<a class="medium" href="#" onclick="changeTextSize(\'medium\');return false;">A</a>';
	textControls += '<a class="large" href="#" onclick="changeTextSize(\'large\');return false;">A</a>';
	textControls += '</div>';
	$('textSize').innerHTML = textControls;
}

document.observe('dom:loaded', function(){
	// write out the text size links if the textSize div is present
	if ($('textSize')) {
		textSizeLinks();
		if (!Cookie.get('textSize')) {
			changeTextSize('small');
			Cookie.set('textSize', 'small', 31556926);
		}
		else {
			changeTextSize(Cookie.get('textSize'));
		}
	}
});

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * trust.js
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function trustBoxOK () {

	new Effect.Fade($('trust'));
	new Effect.Appear($('audience'));
	
	Cookie.set('trust', 'dismissed', 86400);

	return false;
}
function audienceOK () {

	new Effect.Fade($('trustFilterNoise'));
	new Effect.Fade($('trustFilter'));
	new Effect.Fade($('audience'));
	
	// set cookie here if javascript is enabled
	// set cookie so window isn't displayed again for 1 day
	Cookie.set('audience', 'dismissed', 86400);
	
	return false;
}

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * enlarge.js
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function closeEnlarge() {
	$('filter').hide();
	$('enlarge').hide();
}
	
var enlargeHTML = '';

function enlarge(imgSrc, imgWidth, imgHeight, imgTitle, ImgTxt, ImgCategory, ImgID) {
	dcsMultiTrack('DCS.dcsuri', imgSrc, 'WT.cusImageCategory',ImgCategory,'WT.cusImage',imgTitle.replace("'", ''),'WT.cusLogOn',loggedIn ? '1' : '0','WT.cusEngage','Engagement','WT.cusEPoint','2');
	enlargeHTML = '<div id="imageInfo" style="width:' + imgWidth + 'px;margin-top:'+Math.max(document.documentElement.scrollTop,20)+'px">';
	enlargeHTML += '<a href="#"  class="close" onclick="closeEnlarge(); return false;"><img src="/html/images/close.gif" alt="'+snippet('miscpopupclose')+'" /></a>';
	enlargeHTML += '<img src="' + imgSrc + '" width="' + imgWidth + '" height="' + imgHeight +'" /><br />';
	enlargeHTML += '<span>';
	if (loggedIn != true) {
		enlargeHTML += '<input class="register" name="" type="button" value="'+snippet('imageregister')+'" onclick="dcsMultiTrack(\'DCS.dcsuri\', \''+imgSrc+'\', \'WT.cusImageCategory\', \''+ImgCategory+'\', \'WT.cusImage\', \''+imgTitle.replace("'", '')+'\'); location=\'/scripts/pages/'+snippet('LangID')+'/_registration/login.php\';" />';
	}
	else {
		enlargeHTML += '<input class="register" name="" type="button" value="'+snippet('imagedownload')+'" onclick="dcsMultiTrack(\'DCS.dcsuri\', \''+imgSrc+'\', \'WT.cusImageCategory\', \''+ImgCategory+'\', \'WT.cusImage\', \''+imgTitle.replace("'", '')+'\', \'WT.cusEngage\',\'Engagement\',\'WT.cusEPoint\',\'3\'); downloadImage(\'' + ImgCategory + '\', \'' + ImgID + '\');" />';
	}
	enlargeHTML += '</span>';
	enlargeHTML += '<h3>' + imgTitle + '</h3>';
	enlargeHTML += '<p>' + ImgTxt + '</p>';
	enlargeHTML += '</div>';
	$('filter').show();
	$('enlarge').innerHTML = enlargeHTML;
	$('enlarge').show();
}
	
function downloadImage(ImgCategory, ImgID) {
	window.open('/scripts/pages/'+snippet('LangID')+'/auth/download_image.php?c=' + ImgCategory + '&i=' + ImgID + '');
}


document.observe('dom:loaded', function(){
	$('contain').insert({before: '<div id="filter"></div><div id="enlarge"></div>'});
	$('filter').hide();
	$('enlarge').hide();
});

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * iglossary.js
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
document.observe('dom:loaded', function(){
	$$('a.iglossary').each(function(element){
		new Tip(element, $(element.href.substring((element.href.indexOf('#') + 1))).innerHTML, {duration: 0.3, effect: "appear", title: element.innerHTML});
	});
});

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * navigation.js
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function suckerFish(target) {
	var navigation_root = document.getElementById(target);
	var items = navigation_root.getElementsByTagName('li');
	for (i=0; i < items.length; i++) {
		var item = items[i];
		if (item.nodeName == 'LI') {
			item.onmouseover = function() {
				this.className += " over";
			}
			item.onmouseout = function() {
				this.className = this.className.replace("over", "");
			}
		}
	}
}

// set the focus on the search query field
document.observe('dom:loaded', function(){
	suckerFish('nav');
	suckerFish('nav2');

	$$('#nav2 li.last ul')[0].observe('mouseover', function(){
		$('query').focus();
	});
});

//used to get snippets of text for use in javascript files.
function snippet(term) {
	return snippetArr[term];
}

document.write('<script type="text/javascript" src="/html/json/general.json"><\/script>');
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
