var current_popup = false;

var userAgent = navigator.userAgent.toLowerCase();
$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());

$(document).ready(function() {
//	$('ul.tabs a').corner('round top 10px');
});

function toggleStar(el, form_id, field_id) {
	if (el.className == 'star star_on') {
		$('#'+field_id).val('false');
		el.className = 'star';
	} else {
		$('#'+field_id).val('true');
		el.className = 'star star_on';
	}
	$('#'+form_id)[0].onsubmit();
}

function showSlider(parent_id, form_id, field_id, v, context) {
	var parent = $('#'+parent_id);
	var track = parent.find('div.slider:first');
	var handle = parent.find('div.handle:first');
	var info_wrap = parent.find('div.sliderinfo:first');
	var info = info_wrap.find('div.info:first');
	var arrow = info_wrap.find('div.arrow:first');
	arrow.css('background-position', ((v + 10) - ((v / 15) * 3)) + 'px top');
	info.html(sliderStatus(v, context));
	parent.effect("highlight", {color: "#f6e161"}, 2000);
	track.slider({
		value: v,
		min: 0,
		max: 100,
		step: 20,
		slide: function(e, ui){
			var value = ui.value;
			arrow.css('background-position', ((value + 10) - ((value / 15) * 3)) + 'px top');
			info.html(sliderStatus(value, context));
		},
		change: function(e, ui){
			var value = ui.value;
			arrow.css('background-position', ((value + 10) - ((value / 15) * 3)) + 'px top');
			info.html(sliderStatus(value, context));
			$('#'+field_id).val(value);
			$('#'+form_id)[0].onsubmit();
		}
	});
}

function sliderStatus(value, context){
	if(context == null){
		context = 'registration'
	}
	arr = new Array();
	arr['registration'] = new Array()
  arr['registration'][0] = 'I do not know this person.<br />Never seen or spoken too.';
  arr['registration'][20] = 'This person looks familiar.<br />Although we never spoke or met each other.';
  arr['registration'][40] = 'We know each other.<br />Occassionally we speak or meet each other.';
  arr['registration'][60] = 'We know each other quite well.<br />We\'ve helped each other a few times.';
  arr['registration'][80] = 'We know each other very well.<br />We work(ed) together on a regular basis.';
  arr['registration'][100] = 'We know each other very, very well.<br />We work(ed) together on a daily basis.';

	arr['talk'] = new Array()
  arr['talk'][0] = 'I want to remove my rating.';
  arr['talk'][20] = 'This was not my kind of session.';
  arr['talk'][40] = 'I barely made it through.';
  arr['talk'][60] = 'It was ok.';
  arr['talk'][80] = 'This was a great session, i really enjoyed it.';
  arr['talk'][100] = 'This was by far the best session i ever attended.';

	return arr[context][value]
}

function subsubtab(a, id) {
	var el = $('#'+id);
	var about = $('#subsubtabs_about');
	var experience = $('#subsubtabs_experience');
	var contact = $('#subsubtabs_contact');
	var introduce = $('#subsubtabs_introduce');
	var custom_fields = $('#subsubtabs_custom_fields');
	
	about.css('display', 'none');
	experience.css('display', 'none');
	contact.css('display', 'none');
	introduce.css('display', 'none');
	if (custom_fields) {
	  custom_fields.css('display', 'none');
  }
	
	el.css('display', 'block');
	$('#subsubtabs_link1').attr('class', '');
	$('#subsubtabs_link2').attr('class', '');
	$('#subsubtabs_link3').attr('class', '');
	$('#subsubtabs_link4').attr('class', '');
	if ($('#subsubtabs_link5')) {
	  $('#subsubtabs_link5').attr('class', '');
  }
	
	a.className = 'active';
}

function popup(id) {
	el = $('#'+id);
	if (el) {
		// get dimensions & offset
		var viewport = vp();
		var offset = os();
		var el_width = el.width();
		var el_height = el.height();
		var offsetLeft = ((viewport[0] - el_width) / 2) - 140;
		var offsetTop = ((viewport[1] - el_height) / 2) + offset[1];
		offsetTop = (offsetTop < 0) ? 0 : offsetTop;
		
		// position element
		el.css({
			'top': Math.round(offsetTop) + 'px',
			'left': Math.round(offsetLeft) + 'px',
			'position': 'absolute',
			'z-index': '100'
		});
		
		// show overlay & element
		showOverlay();
		if (!el.hasClass('popup')) {
			el.addClass('popup');
		}
		el.appendTo('body');
		current_popup = el;
		el.show();
		
		// set focus to first input
		var elements = el.find('input[type="text"], input[type="password"], textarea');
		elements.each(function(){
			if ((!$(this).attr('type') || $(this).attr('type') != 'hidden')) {
				$(this).focus();
				return false;
			}
		});
	}
}

function showOverlay() {
	if (typeof document.body.style.maxHeight === "undefined") {
		$('html').css('overflow', 'hidden');
	}
	$('body').prepend('<div id="overlay"></div>');
	var overlay = $('#overlay');
	var height = body_height();
	overlay.css('height', height+'px');
	detectMac() ? overlay.addClass('overlay_mac') : overlay.addClass('overlay');
	overlay.click(function(event){
		hideOverlay();
	});
	document.onkeydown = function (e) { 	
		keycode = (e == null) ? event.keyCode : keycode = e.which;
		if (keycode == 27) {
			hideOverlay();
		}
	};
}

function hideOverlay() {
	var overlay = $('#overlay');
	if (overlay) {
		if (current_popup) {
			current_popup.hide();
			current_popup = false;
		}
		overlay.remove();
		$('html').css('overflow', 'auto');
	}
}

function detectMac() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1) {
		return true;
	}
}

function vp() {
	var x, y;
	
	if (self.innerHeight) {
		x = self.innerWidth;
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) {
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	return [x, y];
}

function os() {
	var x, y;
	
	if (self.pageYOffset) {
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	} else if (document.body) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	return [x, y];
}

function body_height() {
	if (document.documentElement.clientHeight > document.documentElement.scrollHeight) {
		return document.documentElement.clientHeight;
	} else {
		return document.documentElement.scrollHeight;
	}
}