var dd1_txt;
var dd2_txt;
var dd3_txt;
var dd4_txt;
var dd5_txt;

$(document).ready(function(){

$("#keyword_sel").change(function(){
    if($("#keywords").is(":visible")== true){
        $("#keywords").hide();
    }else{
        $("#keywords").show();
    }
});

$("#keyword_desel").change(function(){

        $("#keywords").hide();
   
});

$(".repeating_region_7").hover(function(){
    $(this).find(".member_desc").slideDown("slow");
}
, function(){
    $(this).find(".member_desc").slideUp("slow");
});

$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'light_square'});

$('#news_archive').selectmenu();

$('#cs_filter').selectmenu({
icons: [
			{find: '.hm_icon'},
			{find: '.mt_icon'},
			{find: '.sfs_icon'},
			{find: '.hfs_icon'},
			{find: '.ss_icon'},
			{find: '.im_icon'},
			{find: '.cc_icon'}
		]
});

dd1_txt = $("#dd1 .dropdown_txt").html();
dd2_txt = $("#dd2 .dropdown_txt").html();
dd3_txt = $("#dd3 .dropdown_txt").html();
dd4_txt = $("#dd4 .dropdown_txt").html();
dd5_txt = $("#dd5 .dropdown_txt").html();


// dropdown text

$("#dd1 li").hover(
	function(){
	$("#dd1 .dropdown_txt").html($("span", this).html());
	},
	function(){
	$("#dd1 .dropdown_txt").html(dd1_txt);
	}
);

$("#dd2 li").hover(
	function(){
	$("#dd2 .dropdown_txt").html($("span", this).html());
	},
	function(){
	$("#dd2 .dropdown_txt").html(dd2_txt);
	}
);

$("#dd3 li").hover(
	function(){
	$("#dd3 .dropdown_txt").html($("span", this).html());
	},
	function(){
	$("#dd3 .dropdown_txt").html(dd3_txt);
	}
);

$("#dd4 li").hover(
	function(){
	$("#dd4 .dropdown_txt").html($("span", this).html());
	},
	function(){
	$("#dd4 .dropdown_txt").html(dd4_txt);
	}
);

$("#dd5 li").hover(
	function(){
	$("#dd5 .dropdown_txt").html($("span", this).html());
	},
	function(){
	$("#dd5 .dropdown_txt").html(dd5_txt);
	}
);

// dropdown

$("#nav>ul>li").hover(
	function(){
	
	if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version <= 8){
		$(".dropdown, .dropdown2", this).show();
	}
	else{
		$(".dropdown, .dropdown2", this).fadeIn("fast");
	}
	
	},

	function(){
	
		if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version <= 8){
		$(".dropdown, .dropdown2", this).hide();
		}
		else{
		$(".dropdown, .dropdown2", this).fadeOut("fast");
		}
		
	}
);

$('.dropdown').each(function(index){
    var height = $(this).height();
	$(".dropdown_txt", this).css("min-height", (height - 51)+"px");
});

$('.dropdown2').each(function(index){
    var height = $(this).height();
	$(".dropdown_txt", this).css("min-height", (height - 51)+"px");
});

// location selector

$("#select_location_button").click(function(){

	if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version <= 8){
		$("#select_location").show();
	}
	else{
		$("#select_location").fadeIn("fast");
	}
	
});

$("#select_location").mouseleave(function(){

	if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version <= 8){
	$(this).hide();
	}
	else{
	$(this).fadeOut("fast");
	}
	
});

$("#select_location area, #select_location a").hover(

	function(){
	
	var map_class = $(this).attr("class");
	var map_image = '/images/map_'+map_class+'.png';
	
	$("#select_location_left img").attr("src", map_image);
	$("#select_location_right ."+map_class).addClass('active');
	
	},

	function(){
	
	$("#select_location_left img").attr("src", "/images/map-none_selected.png");
	$("#select_location_right a").removeClass('active');
	
	}

);

// news loader

$(".news_item a").click(function(){

	var parent = $(this).parent();
	var grandparent = (parent[0].nodeName == 'H3') ? $(this).parent("h3").parent("div").parent("div") : $(this).parent("div").parent("div") ;

	var news_date = $(".date", grandparent).html();
	var news_title = $("h3 a", grandparent).html();
	var news_copy = $(".news_item_copy", grandparent).html();
	var news_image = $(".news_item_left img", grandparent).attr("src");
	news_image = news_image.replace("thumbs/", "");
	var article_complete = 	'<span class="date">'+news_date+'</span><h2>'+news_title+'</h2><p><img src="'+news_image+'" alt="" width="200" />'+news_copy+'</p>';
	$("#active_news_item").html(article_complete);
	$(".left_col .news_item, .left_col .media_item").removeClass('active');
	$(grandparent).addClass('active');
	window.scrollTo(0, 0);
	//$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'light_rounded'});

});

// media loader

$(".media_item a").click(function(){

	var parent = $(this).parent();
	var grandparent = (parent[0].nodeName == 'H3') ? $(this).parent("h3").parent("div").parent("div") : $(this).parent("div").parent("div") ;

	var news_date = $(".date", grandparent).html();
	var news_title = $("h3 a", grandparent).html();
	var news_copy = $(".media_item_copy", grandparent).html();
	var media_link = $(".media_item_link", grandparent).html();
	var article_complete = 	'<span class="date">'+news_date+'</span><h2>'+news_title+'</h2>'+media_link+'<p>'+news_copy+'</p>';
	$("#active_news_item").html(article_complete);
	$(".left_col .news_item, .left_col .media_item").removeClass('active');
	$(grandparent).addClass('active');
	window.scrollTo(0, 0);
	$(".pp_pic_holder").remove();
	$(".pp_overlay").remove();
	$(".ppt").remove();
	$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'light_square'});

});


$("#add_employment_item").click(function(){

// var content = $("#employment_item").html();
var new_id = $(".employment_item").length;

// alert(new_id);

var employment_from_month = $("#employment_from_0").html();
var employment_from_year = $("#employment_from_year").html();
var employment_to_month = $("#employment_to_0").html();
var employment_to_year = $("#employment_to_year").html();

var content = '<div id="employment_item" class="employment_item">'
	+'<div class="form_row">'
	+'<div class="form_row_left"><label for="job_title_'+new_id+'">Job title</label></div>'
	+'<div class="form_row_right"><input type="text" name="job_title['+new_id+']" id="job_title_'+new_id+'" value="" /></div>'
	+'</div>'
	+'<div class="form_row">'
	+'<div class="form_row_left"><label for="employer_'+new_id+'">Employer</label></div>'
	+'<div class="form_row_right"><input type="text" name="employer['+new_id+']" id="employer_'+new_id+'" value="" /></div>'
	+'</div>'
	+'<div class="form_row">'
	+'<div class="form_row_left"><label for="employment_from_'+new_id+'">From</label></div>'
	+'<div class="form_row_right">'
	+'<select name="employment_from_month['+new_id+']" id="employment_from_'+new_id+'">'
	+employment_from_month
	+'</select>'
	+'<select name="employment_from_year['+new_id+']">'
	+employment_from_year
	+'</select>'
	+'</div>'
	+'</div>'
	+'<div class="form_row">'
	+'<div class="form_row_left"><label for="employment_to_'+new_id+'">To</label></div>'
	+'<div class="form_row_right">'
	+'<select name="employment_to_month['+new_id+']" id="employment_to_'+new_id+'">'
	+employment_to_month
	+'</select>'
	+'<select name="employment_to_year['+new_id+']">'
	+employment_to_year
	+'</select>'
	+'</div>'
	+'</div>'
	+'<div class="form_row">'
	+'<div class="form_row_left"><label for="duties_'+new_id+'">Main Duties</label></div>'
	+'<div class="form_row_right"><textarea name="duties['+new_id+']" id="duties_'+new_id+'" rows="7" cols="28"></textarea></div>'
	+'</div>'
	+'</div>';

$("#employment_container").append(content);

});


});

RegExp.escape = function(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

// browser detect

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

function validateAlert(){
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				var chkd = 0;

				if(document.getElementById('name').value == ""){
					alert("You have not entered your name");
					document.getElementById('name').focus();
					return false;
				}
				if(!filter.test(document.getElementById('email').value)){
					alert("You have not entered your email address");
					document.getElementById('email').focus();
					return false;
				}
				for(i=0; i<document.alertForm.elements.length; i++){
					if(document.alertForm.elements[i].type == "checkbox"){
						if(document.alertForm.elements[i].checked == true){
							chkd = chkd + 1;
						}

					}
				}
				if(chkd == 0){
					alert("You have not selected any areas of interest");
					return false;
				}
				answer = confirm("Confirm the selections made are correct");
				if(answer){
					document.alertForm.submit();
				}
				else{
					return false;
				}
			}

BrowserDetect.init();

