/*--- IE6 hover ---*/
function ieHover(h_list, h_class){
	if($.browser.msie && $.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		$(h_list).live('mouseover', function(){
			$(this).addClass(h_class);
		}).live('mouseout', function(){
			$(this).removeClass(h_class);
		});
	}
}
/*--- fade gallery ---*/
function fadeGall(){
	/*var wait_time = 10000; // in ms
	var change_speed = 1000; // in ms
	var _hold = $('#fade-gallery');
	if(_hold.length){
		var _t;
		var _f = true;
		var _list = _hold.find('div.gallery-holder > ul > li');
		var _btn = $('<ul class="switcher"></ul>');
		_hold.append(_btn);
		_list.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = _btn.find('a');
		var _a = Math.floor(Math.random()*_list.length);
		if(_a == -1) _a = 0;
		
		if($.browser.msie) _list.removeClass('active').hide().eq(_a).addClass('active').show();
		else _list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		_btn.eq(_a).addClass('active');
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		_hold.mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_t) clearTimeout(_t);
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, wait_time);
			}
		});
		if(_f && wait_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, wait_time);
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				if($.browser.msie){
					_list.eq(_a).removeClass('active').hide();
					_list.eq(_ind).addClass('active').show();
				}
				else{
					_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
					_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
				}
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_a = _ind;
			}
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, wait_time);
			}
		}
	}*/
}
/*--- login animation ---*/
function loginAnim(){
	$('div.login-holder').each(function(){
		var _hold = $(this);
		var _btn = _hold.find('a.btn-login');
		var _box = _hold.find('div.login');
		var s_box = _box.children('div.hold');
		var _t;
		if(_hold.hasClass('opened')){
			_box.show();
			s_box.css('marginTop', 0); 
		}
		else{
			_box.hide();
			s_box.css('marginTop', -s_box.outerHeight()); 
		}
		_btn.click(function(){
			if(_t) clearTimeout(_t);
			if(_hold.hasClass('opened')){
				_hold.removeClass('opened');
				s_box.stop().animate({marginTop: - s_box.outerHeight()}, 400, function(){
					_box.hide();
				});
			}
			else{
				_hold.addClass('opened');
				if(_box.is(':hidden')){
					_box.show();
					s_box.css('margin-top', -s_box.outerHeight());
				}
				s_box.stop().animate({marginTop: 0}, 400);
			}
			return false;
		});
		_hold.mouseenter(function(){
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			if(_t) clearTimeout(_t);
			_t = setTimeout(function(){
				if(_hold.hasClass('opened')){
					_hold.removeClass('opened');
					s_box.stop().animate({marginTop: - s_box.outerHeight()}, 400, function(){
						_box.hide();
					});
				}
			}, 1000);
		});
		$(document).mousedown(function(e){
			e = e || event;
			var t = e.target || e.srcElement;
			t = $(t);
			if(t.parents('div.login-holder').length == 0 && _hold.hasClass('opened')){
				_hold.removeClass('opened');
				s_box.stop().animate({marginTop: - s_box.outerHeight()}, 400, function(){
					_box.hide();
				});
			}
		});
	});	
}
/*---- clear inputs function ---*/
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		if(!this.val) this.val = this.value;
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}
$(document).ready(function(){
	ieHover('.certification-form .submit-button');
	fadeGall();
	loginAnim();
	clearInputs();
	forcelogins();
});

/* Code added by Andy */

function openLogin(login_id) {
	var _hold = $('#'+login_id);
	var _btn = _hold.find('a.btn-login');
	var _box = _hold.find('div.login');
	var s_box = _box.children('div.hold');
	var _t;	
	_hold.addClass('opened');
	if(_box.is(':hidden')){
		_box.show();
		s_box.css('margin-top', -s_box.outerHeight());
	}
	s_box.stop().animate({marginTop: 0}, 400);
}

function forcelogins() {	
	if(force_supplier==1) {
		openLogin('supplier_login');
	}
	if(force_member==1){
		openLogin('member_login');
	}
	//
}

var force_supplier=0
var force_member=0

 var myWidth = 0
 var myHeight = 0
 
 function URLEncode(unencodedValue )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = unencodedValue;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

// view port
function getSize() {
	 if(typeof(window.innerWidth) == 'number') {
	  //Non-IE
	  myWidth = window.innerWidth;
	  myHeight = window.innerHeight;
	 }else if(document.documentElement &&
	  (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
	  //IE 6+ in 'standards compliant mode'
	  myWidth = document.documentElement.clientWidth;
	  myHeight = document.documentElement.clientHeight;
	 } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
	  //IE 4 compatible
	  myWidth = document.body.clientWidth;
	  myHeight = document.body.clientHeight;
	 }
	 //alert(myWidth+':'+myHeight);
	 //return [myWidth, myHeight];
}

function Infomaxim_sendView(type,id,campaign,opt2,opt3) {
	if (!document.all&&!document.layers&&!document.getElementById) //stop old browser breaking this?
	return
	getSize()
	//jQuery the data to analytics collector
	$('#infomaxim_analytics').load('/includes/hit.asp?t='+URLEncode(type)+'&i='+id+'&w='+screen.width+'&h='+screen.height+'&c='+screen.colorDepth+'&aw='+myWidth+'&ah='+myHeight+'&ca='+campaign+'&ref='+URLEncode(document.referrer)+'&url='+URLEncode(location.href));
}

function Infomaxim_sendClick(url,type) {
	$('#infomaxim_analytics').load('/includes/hit.asp?click=1&url='+URLEncode(url)+'&t='+type);
}
 
$(document).ready(function(){
    //Requires jQuery 1.3
    //Explanation:-  http://permanentinkdesign.com/articles/google-analytics-jquery-and-file-downloads/
    //InfomaximAnalytics();

    // The slashes are to ensure the period is in the url, the $ is to make sure it is the end of the url, the i is to make it case insensitive.
	filetypes = /\.doc$|\.xls$|\.exe$|\.zip$|\.pdf$|\.mp3$|\.psd$/i;
	$("a").live("click", function(){
		// Track mailto links
		if ($(this).attr("href").match(/^mailto\:/i)) {
			var url = $(this).attr("href").replace(/^mailto\:/i, "")
			Infomaxim_sendClick(url,'mailto')
		}
		// Track external links
		else if (location.host != this.host.replace(/\:80$/i, "")) {
			var url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "")
			Infomaxim_sendClick(url,'ext')
		}
		// Track downloads (links with a given extension)
		else if ($(this).attr("href").match(filetypes)) {
			// The URL needs to be changed for each site this is applied to.
			var host = location.host.replace(/\./, "\\.")
			var pattern = new RegExp('^(http\:\/\/)*(www\.)*('+host+')*\/','i')
			var url = $(this).attr("href").replace(pattern, "")
			Infomaxim_sendClick(url,'download')
		}
	});
});
