$(document).ready(function(){
	
	// Test for Trebuchet Font
	// Surprisingly, some custom corporate OSX installs do not have this "web safe" font.
	font.setup();
	var font_installed = font.isInstalled('Trebuchet MS');
	if (font_installed == false) {
		$('body').css('font-family', 'TrebuchetMSRegular, Arial, sans-serif'); // load Trebuchet with @font-face
	}
	
	// make all links relative
	// setup_relative_links();
	
	// Dropdown hover setup
	setup_dropdown_hover();
	
	// Setup tabs if found on the page
	// Pass fade speed as variable
	if ($('#tabs').size() > 0) {
		setup_tabs("slow");
	}
	
	// Setup home banners
	if ($('body#home').size() > 0) {
		setup_home_banner("normal");
	}	
	
	// setup lightbox listeners
	$("a[rel='portfolio'], a.lightbox").colorbox();
	$(".video-lightbox").parent().colorbox({iframe:true, innerWidth:425, innerHeight:344});
	$("#why-edline .testimonial-video a[rel='video']").colorbox({iframe:true, innerWidth:425, innerHeight:344});
	
	// check callouts for plain text
	$('div.callout a').each(function(){
		find_img = $(this).find('img');

		if (find_img.length == 0) {
			var content = $(this).html();
			$(this).parent().html(content).find('a').remove();
		}
	});
	
	// Dynamically change # of attendees
	$('#attendees input').live('blur', function(){
		update_attendee_data();
	});
	
	// Setup side nav backgrounds for long titles
	// that cause wrapping
	if ($('#sub-navigation').size() > 0){
		setup_sidenav();
	}
	
	if ($('form#freeform').size() > 0) {
		
		// style forms
		style_forms();
		
		// change action to corpstaging.edline.com
		//change_form_action('http://corpstaging.edline.com');
	}
	
	// Fix EE Search bug
	$('#search form').attr('action', '/');
	
	// Setup banner for landing pages if custom one is present
	if ($('#p #interior-banner-image img').size() > 0) {
		setup_landing_page_banner()
	}
	
	// Setup portfolio slider
	if ($('div#portfolio-pieces').size() > 0) {
		setup_portfolio_slider();
	}
	
	//IE 7 fixes
	if (($.browser.msie) && (parseInt(jQuery.browser.version) == 7)) {
		ie7_fixes();
	}
	
	var checkbox = document.createElement("input");
	checkbox.type = "checkbox";
	checkbox.name = "verifycheckbox";
	checkbox.value= "1";
	
	var div = document.getElementById("no-robot-checkbox");
	div.appendChild(checkbox);
	
	checkbox.checked = false;
	
});

function setup_portfolio_slider(){
	
	// hide overflow
	$('#content').css('overflow','hidden');
	
	var slider = $('#portfolio-pieces');
	var num_pieces = $('div.portfolio-piece').length;
	var frame_width = 747;
	var slider_width = ((Math.floor(num_pieces/9)) + 1) * frame_width;
	var max_left = (slider_width - frame_width) * (-1);
	slider.width(slider_width);
	slider.css('position','relative').css('left', 0);
	$('.previous a').fadeOut('fast');
	
	// click next
	$('.next a').click(function(){
		var current = slider.css('left');
		current = parseInt(current);
		
		//alert('cur='+current+' max='+max_left);
		
		// make sure we're not on the last frame
		if (current > max_left) { 
			
			var new_left = current - frame_width;
			slider.animate({
				left:new_left
			}, 1000, function(){
				// check for last frame
				if (new_left == max_left) {
					$('.next a').fadeOut();
				}
			});
		
		} 
		
		$('.previous a').fadeIn();
		
		
		return false;
	});
	
	// click prev
	$('.previous a').click(function(){
		var current = slider.css('left');
		current = parseInt(current);
		
		//alert('cur='+current+' max='+max_left);
		
		// make sure we're not on the last frame
		if (current != 0) { 
			
			var new_left = current + frame_width;
			slider.animate({
				left:new_left
			}, 1000, function(){
				// check for last frame
				if (new_left == 0) {
					$('.previous a').fadeOut();
				}
			});
		
		} 
		
		$('.next a').fadeIn();
		
		
		return false;
	});	
}

function setup_relative_links(){
	
	$('a').each(function(){
		var href = $(this).attr('href');
		if (href) {
			var hostname = window.location.protocol + "//" + window.location.host;
			var new_href = href.replace(hostname, '');
			$(this).attr('href', new_href);	
		}

	});
}

function setup_dropdown_hover(){

	

	// Main nav drop downs
	$('#navigation ul li.top').hover(function(){
		var dd = $(this).find('ul.dropdown');
			
			$('ul.dropdown.started').slideUp(400);
		
			
			dd.stop(true,true).addClass('started').slideDown(400, function(){
				$(this).addClass('down');
				
			});			

	}, function(){
		var dd = $(this).find('ul.dropdown.started');

			dd.slideUp(400, function(){
				$(this).removeClass('down');
				$(this).removeClass('started');
			});		
	
	});
	
	// choose correct bg image based on height of element
	var hover_link = $('ul.dropdown li a');
	
	hover_link.each(function(){
		if ($(this).text().length > 24
		) {
			$(this).addClass('tall');
		}
	});

}

function setup_sidenav(){
	
	// Side nav styling
	$('#sub-navigation ul li a:first').css('border-top', 'none');
	$('#sub-navigation ul li a:last, #sub-navigation ul li.sub-level-0:last').css('border-bottom', 'none');	
	
	// choose correct bg image based on length of link text
	var active_link = $('#sub-navigation ul li.here.sub-level-0 a:first, #sub-navigation ul li.parent-here a:first');
	
	active_link.each(function(){
		link_text = $(this).text();
		if (link_text.length < 20) {
			active_link.css('background-position', '0px -60px');
			active_link.css('padding', '17px 20px');		
		} else if (link_text == 'Facility Management Solutions') {
			$(this).css('letter-spacing','-1px');
		}
	});
	
}

function setup_tabs(speed){
	
	// Hide all tab content except for the first
	$('#tabs .tab').not('.tab:first').hide();
	$('#tab-menu ul li a:first').addClass('active');
	
	// Setup triggers
	$('#tab-menu ul li a').click(function(){
		
		// Remove active class from current
		$('#tab-menu ul li a.active').removeClass('active');
		
		// Add active class to clicked tab trigger
		$(this).addClass('active');
		
		// Get index of the clicked tab
		var clicked_tab_index = $(this).parent('li').index();
		var current_tab = $('#tabs .tab:visible');
		var current_tab_index = current_tab.index();		

		// Check if user clicked same tab first, 
		if (clicked_tab_index != current_tab_index){
			// Fade out current tab
			current_tab.fadeOut(speed, function(){
				// Fade in clicked tab
				$('#tabs .tab:eq('+clicked_tab_index+')').fadeIn(speed);
			});
		}
		
	});
	
}

function setup_home_banner(speed){
	
	// Hide all tab content except for the first
	$('#banner .tab').not('.tab:first').hide();
	//$('#banner .tab a.view-demo').not('a.view-demo:first').hide();	
	$('#banner-nav ul li a:first').addClass('active');
	
	
	// Setup triggers
	$('#banner-nav ul li a').click(function(){
		
		// Remove active class from current
		$('#banner-nav ul li a.active').removeClass('active');

		var tab_class = $(this).attr('class');
		var tab_bg_div = $('#interior-banner-image');
		
		// Add active class to clicked tab trigger
		$(this).addClass('active');
		
		// Get index of the clicked tab
		var clicked_tab_index = $(this).parent('li').index();
		var current_tab = $('#banner .tab:visible');
		var current_tab_index = current_tab.index();		
		var demo_button = $('#banner .tab.'+tab_class+' a.view-demo.');
	
		
		// Check if user clicked same tab first, 
		if (tab_bg_div.attr('class') != tab_class){
			// Fade out current tab

			
			tab_bg_div.fadeOut(function(){
				current_tab.fadeOut(); 
				
				$('#banner .tab:eq('+clicked_tab_index+')').fadeIn(speed);
				// Fade in clicked tab
				tab_bg_div.attr('class','').addClass(tab_class).fadeIn(speed);

			});
		}
		
	});
	
}

function update_attendees(){

	var attendee_count = $('#attendee_count').val();
	
	$('#attendees').slideUp('slow', function(){

		$(this).empty();

		for(var i=1; i<=attendee_count; i++) {
			var attendee_inputs = '<h4>Attendee #'+i+'</h4>'+
				'<table>'+
					'<tr>'+
						'<td>'+
							'<p>Name:</p></td><td><input type="text" title="" class="attendee_name"/>'+
						'</td>'+
						'<td>'+
							'<p>Email:</p></td><td><input type="text" title="" class="attendee_email"/>'+
						'</td>'+
					'</tr>'+
					'<tr>'+
						'<td>'+
							'<p>Title:</p></td><td><input type="text" class="attendee_title"/>'+
						'</td>'+
						'<td>'+
							'<p>Phone:</p></td><td><input type="text" class="attendee_phone"/>'+
						'</td>'+
					'</tr>'+
				'</table>'+
				'<hr/>';
		
			$(this).append(attendee_inputs);
		}		
		
		$(this).slideDown('slow')
	});
				
}

function update_attendee_data(){
	
	var attendee_input = $('input[name="attendees"]');
	var attendee_data = '';
	var i = 1;
	
	$('#attendees table').each(function(){
		
		attendee_name = $(this).find('input.attendee_name').val();
		attendee_email = $(this).find('input.attendee_email').val();
		attendee_title = $(this).find('input.attendee_title').val();
		attendee_phone = $(this).find('input.attendee_phone').val();						
		
		attendee_data = attendee_data + 'Attendee #'+i+': ' + attendee_name + ', ' + attendee_title + ', ' + attendee_email + ', '+attendee_phone + ' \n';
		
		attendee_input.val(attendee_data);
		
		i++;
	});	
	
}

function style_forms(){
	// Make the last cell of each row text-align:left
	$('.form table tr').each(function(){
		$(this).find('td:last').css('text-align', 'left');
	});
}

function change_form_action(url){
	var current_url = $('form#freeform').attr('action');
	
	if (current_url == '//') {
		$('form#freeform').attr('action', url);	
	}
}

function setup_landing_page_banner(){
	var banner_img = $('#p #interior-banner-image img').attr('src');
	$('#p #interior-banner-image').css('background-image', 'url('+ banner_img +')');	
}

function ie7_fixes(){
	$('.submit-button').css('text-indent','0').attr('value',' ');
}
