if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
  }
}

var settings = {
	ckeditor_height: 140,
	ckeditor_width: 420,
	add_icons_to_content_buttons: true,
	auto_alt_tables: true
};

function showHideOtherBox() {
	$("select option:selected").each(function () {
		if($(this).hasClass('other')){
			$(this).parent().parent().find('input').slideDown("fast");		
		}
		else{
			$(this).parent().parent().find('input').slideUp("fast");
		}
	});
}

function updateFormDisplay(animate) {
	
	
	//get the selected option in the form
	selected = "";
	$("#service_required select option:selected").each(function () {
		selected = $(this).val();
	});
	
	//set to show array
	to_show = new Array("empty");

	//then run the showing function, which is also passed an array, switch which runs shows
	switch(selected){
		//case "translation":
		case "1":
			to_show = new Array("nature_of_text","number_of_words","source_language","target_language","expected_turnaround","additional_information");
		break;
		//case "interpreting":
		case "2":
			to_show = new Array("nature_of_meeting","source_language","target_language","duration_of_booking","additional_information");
		break;
		//case "transcription" :
		case "3" :
			to_show = new Array("nature_of_audio","duration","format","source_language","target_language","expected_turnaround","additional_information");
		break;
		//case "transcription_and_or_translation" :
		case "4" :
			to_show = new Array("nature_of_audio","duration","format","source_language","target_language","expected_turnaround","additional_information");
		break;
		//case "proof_reading" :
		case "5" :
			to_show = new Array("nature_of_text","number_of_words","source_language","target_language","expected_turnaround","additional_information");
		break;
		//case "voice_over" :
		case "6" :
			to_show = new Array("number_of_voiceover_artists","languages","voice_type","duration_of_session","want_them_to_arrange_studio","translation_before_recording","additional_information");
		break;
		//case "translation_and_or_voice_over" :
		case "7" :
			to_show = new Array("number_of_voiceover_artists","languages","voice_type","duration_of_session","want_them_to_arrange_studio","translation_before_recording","additional_information");
		break;
	}
	
	//hide all in an array
	var form_rows_to_hide = new Array("number_of_voiceover_artists","format","voice_languages","voice_type","duration_of_session","want_them_to_arrange_studio","translation_before_recording","nature_of_text","number_of_words","source_language","target_language","expected_turnaround","additional_information","nature_of_meeting","duration_of_booking","nature_of_audio","duration","last");
	
	for (z in to_show){
		//to_show[y]
		form_rows_to_hide.splice(form_rows_to_hide.indexOf(to_show[z]), 1);
	}
	 
	for (x in form_rows_to_hide){
		//form_rows_to_hide[x];
		if(animate){
			$('#' + form_rows_to_hide[x]).slideUp('slow');
		}else{
			$('#' + form_rows_to_hide[x]).hide();
		}
	}
	

	if(selected == "6" || selected == "7"){
		$('#number_of_voiceover_artists').show();
	}else{
		$('#number_of_voiceover_artists').hide();
	}
	
	//then show the nessiasry forms
	showFormRows(to_show);
	//reset array
	to_show = Array("empty");
	
}

function showFormRows(rows_to_show){
	for (y in rows_to_show){
		$('#' + rows_to_show[y]).slideDown("slow");
	}
}

function setToolTipPosition(mouse, hovered_over_object){
	
	$('#under-construction-tooltip').removeClass('menu-tip');
	
	toolTipX = (mouse.pageX - (142 / 2) );
	toolTipY = (mouse.pageY - 70 );
	
	
	toolTipY = hovered_over_object.offset().top + ((hovered_over_object.height() / 2) - 30);
	toolTipX = hovered_over_object.offset().left + (hovered_over_object.width()  - 20);
	
	
	if(hovered_over_object.attr('id') == "case-studies"){
		$('#under-construction-tooltip').addClass('menu-tip');
		toolTipY = hovered_over_object.offset().top - 50;
		toolTipX = hovered_over_object.offset().left + ((hovered_over_object.width() - 142)/2) ;
	}
	
	$('#under-construction-tooltip').css({'left' : toolTipX, 'top' : toolTipY});
	
	
}

function showUnderConstruction(){
	$('#under-construction-tooltip').animate({
	    opacity: 1,
		height: 56,
		width: 140
	  }, 350
	);
}

function hideUnderConstruction(first_time){
	if(first_time){
		$('#under-construction-tooltip').hide();
	}
	
	
	$('#under-construction-tooltip').animate({
	    opacity: 0,
		height: 0,
		width: 140
	  }, 350, function() {
	    // Animation complete.
	});
}

function check_if_tooltip_needs_to_be_hidden(){
	if(hide_tooltip){
		hideUnderConstruction();
	}
}

function show_viewable_flags() {
	var viewport_height = $(window).height() + $(window).scrollTop();
	$(".flag_link").each(function(){
		if(viewport_height > $(this).offset().top){
			if( $(this).children('.a_little_flag').attr('src') == "/img/clear.gif"  ){
				var image_link = $(this).attr('rel');		
				$(this).children('.a_little_flag').attr({src : image_link });
				$(this).removeClass('flag_link');
			}
		}
	});
}

var maps = {
	
	initialise: function(){
		
		
		var myLatlng = new google.maps.LatLng($('#lat').val(), $('#lng').val());
		    var myOptions = {
		      zoom: 14,
		      center: myLatlng,
		      mapTypeId: google.maps.MapTypeId.ROADMAP
		    }
		
		    var map = new google.maps.Map(document.getElementById("map"), myOptions);
		
		    var contentString = $('#popup_content').html();
		
		    var infowindow = new google.maps.InfoWindow({
		        content: contentString
		    });
		
		    var marker = new google.maps.Marker({
		        position: myLatlng,
		        map: map
		    });
		    google.maps.event.addListener(marker, 'click', function() {
		      infowindow.open(map,marker);
		    });
		
		
	}
	
};


$(document).ready(function(){
	
	if( $('#page404').length > 0 ){
		document.title = "404 Not Found"
		$('.main-title').html("");
		$('.main-title').hide();
		$('#menu ul li').each(function(){
			$(this).removeAttr('id');
		});
	}
	
	show_coming_soon = false;
	
	if(show_coming_soon){
		if( $.browser.msie && $.browser.version == 6 ) {
			setInterval( "check_if_tooltip_needs_to_be_hidden()", 3000 );
		}else{
			setInterval( "check_if_tooltip_needs_to_be_hidden()", 3000 );
		}
	}
	
	hideUnderConstruction(true);
	hide_tooltip = true;
	
	//animate menu fade
	$('#menu li a',this).hover(function() {
		$(this).find(".menu-image").stop().animate({ 
			"opacity": 0 
		},0, "easeOutSine");
	},
		function(){
			$(this).find(".menu-image").stop().animate({ 
				"opacity": 1 
			},750, "easeOutQuad");
		}
	);
	
	//animate new badge
	new_badge_right = 9;
	new_badge_top = 4;
	
	$('#menu li a',this).hover(function() {
		
		if($(this).parent().is('#on')){
			new_badge_right = 9;
			new_badge_top = 4;
		}else{
			new_badge_right = -5;
			new_badge_top = 8;
		}
		
		$(this).find(".new").stop().animate({ 
			"right": 9,
			"top": 4
		},250, "easeOutSine");
	},
		function(){
			$(this).find(".new").stop().animate({ 
				"right": new_badge_right,
				"top": new_badge_top
			},250, "easeOutQuad");
		}
	);
	
	//dynamic request form
	updateFormDisplay(false);
	showHideOtherBox();
	
   	$('#service_required select').bind({
	    change: function() {
			updateFormDisplay(true);
	    },
		keyup: function() {
			updateFormDisplay(true);
	    }
	});
	
	$('select').bind({
	    change: function() {
			showHideOtherBox(true);
	    },
		keyup: function() {
			showHideOtherBox(true);
	    }
	});
	
	
	$('.under-construction',this).bind({
	    mousemove: function(e) {
			hide_tooltip = false;
			setToolTipPosition(e,$(this));
	    },
		mouseover: function() {
			$('#under-construction-tooltip').clearQueue();
			hide_tooltip = false;
			showUnderConstruction();
		},
		mouseout: function() {
			setTimeout(function(){
				hide_tooltip = true;
			}, 1000);
		},
		click: function() {
			return false;
		}
	});
	
	$('.delete').click(function(){
	  var answer = confirm('Are you sure you want to delete?');
	  return answer;
	});
	

	
	$("input, textarea").focus(
	 	function(){
			if(this.value == this.defaultValue){
				this.select();
			}
		}
	)
	
	input_fields = $('.input-text');
	if(input_fields.length > 0){
		input_fields[0].focus();
	}
	
	
	
	show_viewable_flags();
	$(window).scroll(function () { 
		show_viewable_flags();
	});
	



	// feedback
	$('.a_secion').hide();
	
	$('.nice_form input[type=text]').addClass('input_text');
	$('.nice_form input[type=password]').addClass('input_text');
	$('.nice_form input[type=button]').addClass('input_button');
	$('.nice_form input[type=submit]').addClass('input_button submit_button');
	$('.nice_form input[type=checkbox]').addClass('input_checkbox');		
	
	
	$('#which_service').change(function(){
		var selected_section = $(this).val();
		var slide_speed = 0;
		
		//selected_section
		$('.a_secion').each(function(index) {
			if($(this).attr('id') == (selected_section + '_section')){
				$(this).delay(333).slideDown();
				$('#service_error').hide();
			}else{
				$(this).slideUp(333);
			}
		});
	})
	
	var selected_section = $('#which_service').val();
	$('.a_secion').each(function(index) {
		if($(this).attr('id') == (selected_section + '_section')){
			$(this).delay(333).slideDown();
			$('#service_error').show();
		}else{
			$(this).slideUp(333);
		}
	});
	
	
	$('#request-form').submit(function(){
		var to_return = true;
		if($('#which_service').val() == ""){
			$('#service_error').show();
			var to_return = false;
		}
		
		if($('#your_name').val() == ""){
			$('#name_error').show();
			var to_return = false;
		}else{
			$('#name_error').hide();
		}
		
		if($('#email').val() == ""){
			$('#email_error').show();
			var to_return = false;
		}else{
			$('#email_error').hide();
		}
		
		if($('#job').val() == ""){
			$('#job_error').show();
			var to_return = false;
		}else{
			$('#job_error').hide();
		}
		
		return to_return;
	});
	
	
	//add tel if iphone
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		var tel = "tel:+442072537700";
		$('#tel_box a').attr('href', tel);
	}
	
	
	
	if( $('.do_ckeditor').length > 0 ){
		var ck_config = {
		    toolbar:
		    [
				['Bold', 'Italic', 'Bullet'],
				['Copy', 'Paste', 'BulletedList'],
				['Link','Unlink'],
		        ['ShowBlocks','Source'],
		        ['Maximize']
		    ],
			skin: 'kama'
		};
		ck_config.removePlugins = 'elementspath';		
		ck_config.toolbarCanCollapse = false;
		ck_config.resize_enabled = false;
		ck_config.width = settings.ckeditor_width;
		ck_config.height = settings.ckeditor_height;
		$('.do_ckeditor').ckeditor(ck_config);
		$('.ie_ckeditor_warning').hide();
	}
	

	
});

$(window).load(function(){
	

	if($('#flashContent').length > 0){
		var randomnumber = Math.floor(Math.random()*10000000)
		var flashvars = {};
		var params = {};
		var attributes = {scale:"noScale"};
		attributes.id = "Language Identifier";
		swfobject.embedSWF("../swf/PreLoader.swf","flashContent", "640", "2010", "10.0.0", false, flashvars, params, attributes);
	}
	
	//google maps
	if( $('#map').length > 0 ){
		maps.initialise();		
	}
	
	$('#refer_a_friend_wrapper').height($('body').height()).fadeTo(0,0.5);
	
	$('#refer_a_friend_wrapper').toggle(false);
	$('#refer_a_friend_holder').toggle(false);
	
	$('#refer_a_friend_holder').css({
		"top":($(window).height()/2)-(($('#refer_a_friend_holder').height()+50+8)/2),
		"left":($(window).width()/2)-(($('#refer_a_friend_holder').width()+50+8)/2)
	});
	// the above includes borders and padding
	
	$(window).resize(function(){
		$('#refer_a_friend_holder').css({
			"top":($(window).height()/2)-(($('#refer_a_friend_holder').height()+50+8)/2),
			"left":($(window).width()/2)-(($('#refer_a_friend_holder').width()+50+8)/2)
		});
			// the above includes borders and padding
	});
	
	$('#close_box, #refer_a_friend_link, #refer_a_friend_wrapper').click(function(){
		$('#refer_a_friend_wrapper').toggle();
		$('#refer_a_friend_holder').toggle();
		return false;
	});
	
	
	$('#refer_a_friend').submit(function(){
		$('#loading_animation').fadeIn(0);
		$('#refer_a_friend').find('.message').text('&nbsp;')
		$.ajax({
			type: 'POST',
			url: "/includes/email_a_friend.php",
			data: $("#refer_a_friend").serialize(),
			success:function(response){
				$('#loading_animation').fadeIn(0).delay(200).fadeOut(0);
				$('#refer_a_friend').find('.return_message').text(response).addClass('message');
				if(response=="Your email has been sent")
				{
					$('#refer_a_friend').find('.return_message').removeClass('message');
					$('#refer_a_friend').find('.submit_button').val('Refer another');
				}
			}
		});
		
		return false;
	});
	
});





