var is_ipad = navigator.userAgent.match(/iPad/i) != null;
var is_iphone = navigator.userAgent.match(/iPhone/i) != null;

jQuery(document).ready(function() {


	// Set menu active state
	PageFunctions.set_nav_active_state(location.pathname);


	jQuery('.slideshow_link').click(function(e) {

		// Prevent default action
		e.preventDefault();

		var carousel_caption = jQuery(this).parents('.carousel_parent').find('.carousel_caption');
		var image_caption    = jQuery(this).parent('li').find('.image_caption');

		var image_link       = jQuery(this).parents('.carousel_parent').find('.image_link');
		var this_link        = jQuery(this).parent('li').find('.this_link');

		// Show loading
		carousel_caption.html('Loading...');

		// Update image
		jQuery('#slideshow_main_img').attr('src', jQuery(this).attr('href'));

		jQuery('#slideshow_main_img').load(function() {
 			carousel_caption.html(image_caption.html());
 			image_link.attr('href', this_link.html());
 		});

		jQuery('.slideshow_link').parent('LI').removeClass('active');
		jQuery(this).parent('LI').addClass('active');

	});


	// Hover function main nav
	jQuery('#header UL#main_nav LI').hover(
		function() {
			jQuery(this).find('.main_nav_dropdown').show();
			jQuery(this).addClass('hover');
		},
		function() {
			jQuery(this).find('.main_nav_dropdown').hide();
			jQuery(this).removeClass('hover');
		}
	);

	// Hover function header tabs
	jQuery('#header DIV.header_tab').hover(
		function() {
			jQuery(this).find('.header_tab_dropdown').show();
			jQuery(this).addClass('hover');
		},
		function() {
			jQuery(this).find('.header_tab_dropdown').hide();
			jQuery(this).removeClass('hover');
		}
	);

	// Hover function for image slideshow
	jQuery('.image_slideshow .carousel_container UL.carousel_inside LI').hover(
		function ()	{
			jQuery(this).addClass('hover');
		},
		function () {
			jQuery(this).removeClass('hover');
		}
	);


	// Hover function fixed footer center drawer
	jQuery('#ff_center_drawer UL LI').hover(
		function() {
			jQuery(this).addClass('hover');
		},
		function() {
			jQuery(this).removeClass('hover');
		}
	);




	var ff_center_drawer_timeout = false;
	var ff_left_drawer_timeout   = false;

	// Click function to open/close center drawer
	jQuery('#ff_center_link').click(function() {

		this_link = jQuery(this);

		if (this_link.hasClass('disabled')) {
			return false;
		}


		clearTimeout(ff_center_drawer_timeout);

		this_link.addClass('disabled');

		if (jQuery('#ff_center_drawer').is(':visible')) {
			jQuery('#ff_center_drawer').slideUp(function() {
				this_link.removeClass('disabled');
			});
			jQuery('#ff_center').removeClass('active');
			jQuery('#ff_center_link_arrow').removeClass('active');
		} else {
			if (jQuery('#ff_left_drawer').is(':visible')) {
				jQuery('#ff_left_drawer').slideUp();
			}
			jQuery('#ff_center_drawer').slideToggle(function() {
				this_link.removeClass('disabled');
			});
			jQuery('#ff_center').addClass('active');
			jQuery('#ff_center_link_arrow').addClass('active');
		}
		return false;
	});

	jQuery('#ff_center_wrapper').mouseleave(function() {
		clearTimeout(ff_center_drawer_timeout);
		ff_center_drawer_timeout = setTimeout("jQuery('#ff_center_drawer').slideUp();jQuery('#ff_center').removeClass('active')", 2000);
	});



	// Click function to open/close left drawer

	jQuery('#ff_share').click(function() {

		this_link = jQuery(this);

		if (this_link.hasClass('disabled')) {
			return false;
		}

		clearTimeout(ff_left_drawer_timeout);

		this_link.addClass('disabled');

		if (jQuery('#ff_center_drawer').is(':visible')) {
			jQuery('#ff_center_drawer').slideUp();
			jQuery('#ff_center').removeClass('active');
		}
		jQuery('#ff_left_drawer').slideToggle(function() {
			this_link.removeClass('disabled');
		});
		return false;
	});

	jQuery('#ff_left_wrapper').mouseleave(function() {
		clearTimeout(ff_left_drawer_timeout);
		ff_left_drawer_timeout = setTimeout("jQuery('#ff_left_drawer').slideUp();",2000);
	});






	// Show / hide dropdown
	jQuery('.select_group INPUT').click(function() {

		var target = jQuery(this).parent('FIELDSET').find('UL');

		if (target.is(':hidden')) {
			jQuery('.select_menu:visible').slideUp('fast');
			target.slideDown('fast');
		} else {
			target.slideUp('fast');
		}
	});

	// Hide dropdown/copy value to parent
	jQuery('.select_group UL LI').click(function() {

		var fieldset = jQuery(this).parents('FIELDSET');

		if (fieldset.hasClass('link')) {
			window.location.href = jQuery(this).find('A').attr('href');
		} else {
			// Copy value to input field
			fieldset.find('INPUT').attr('value', jQuery(this).html());

			// If address exits, copy to address/daddr field
			if (jQuery('#address').length) {
				jQuery('#address').attr('value', jQuery(this).attr('address'));
			}

			// Hide menu
			jQuery(this).parent('UL').slideUp('fast');
		}
	});


	// screen image changer
	jQuery('#slideshow_nav A').click(function() {

		// Stop cycling through
		if (typeof(interval_id) != 'undefined') {
			clearInterval(interval_id);
		}

		// Don't do anything if it's currently visible
		if (jQuery(this).hasClass('active')) return false;

		// Determine curent position
		var pos = jQuery(this).attr('position');

		// Change the slide
		change_slide(pos);

		return false;
	});

	// Show/hide default values for text inputs
	jQuery('.default_text').each(set_default_text);

	// Cycle through slideshow
	var interval_id = setInterval('slide_cycle()', 6000);

	// Bind text size adjustment functions
	jQuery('#font_large').click(function() {
		jQuery('.font_size_select').find('A').removeClass('active');
		jQuery(this).addClass('active');
		jQuery('#content').removeClass('text_size_larger');
		jQuery('#content').addClass('text_size_large');
	});
	jQuery('#font_larger').click(function() {
		jQuery('.font_size_select').find('A').removeClass('active');
		jQuery(this).addClass('active');
		jQuery('#content').removeClass('text_size_large');
		jQuery('#content').addClass('text_size_larger');
	});
	jQuery('#font_normal').click(function() {
		jQuery('.font_size_select').find('A').removeClass('active');
		jQuery(this).addClass('active');
		jQuery('#content').removeClass('text_size_large');
		jQuery('#content').removeClass('text_size_larger');
	});


	/**
	 * Function to show/hide tabbed sections
	 */
	jQuery('.tab_link').click(function() {

		// Determine position
		var pos = jQuery(this).attr('position');

		// Hide all others, show clicked
		jQuery('.tab_content').hide();
		jQuery('#tab_content_'+pos).show();

		// Update active class
		jQuery('UL.tabs > LI').removeClass('active');
		jQuery(this).parent('LI').addClass('active');

		return false;
	});


	/**
	 * Function to show/hide home nav sections
	 */
	jQuery('.home_nav_link').click(function() {

		// Determine position
		var pos = jQuery(this).attr('position');

		// Hide all others, show clicked
		jQuery('.home_nav_content').hide();
		jQuery('#home_nav_content_'+pos).show();

		jQuery('#home_nav_content_'+pos).find('.carousel_parent').show();

		// new for home page nav (used as tab)
		jQuery('UL.home-content-nav LI A').removeClass('active');
		jQuery(this).addClass('active');

		return false;
	});




	/**
	 * Function to show/hide course steps sections
	 */
	jQuery('.step_link').click(function() {

		// Determine position
		var pos = jQuery(this).attr('position');

		// Hide all others, show clicked
		jQuery('.course_steps_detail').hide();
		jQuery('#step_content_'+pos).show();

		// Update active class
		jQuery('UL.courses_steps LI > A').removeClass('active');
		jQuery(this).addClass('active');

		return false;
	});





	// Bind form submit to all links with class "submit_button"
	jQuery('.submit_button').click(function() {
		if (jQuery(this).hasClass('lecole_submit')) {
			lecole_submit(jQuery(this));
		} else if (jQuery(this).hasClass('alumni_submit')) {
			alumni_submit(jQuery(this));
		} else {
			jQuery(this).parents('FORM').submit();
		}
		//return false;
	});


	// Add last-child rules to menu, as ie6-8 ignores this in style sheet
	jQuery('UL.nav LI:last-child A').css('border-bottom', 'none');
	//jQuery('.video_info:nth-child(3)').css("margin-right", 0);

	jQuery('UL.select_menu LI:last-child').css('border-bottom', 'none');


	// Show video
	/*
	jQuery('.video_img_link').click(function() {

			// Use this var 'video_link' to load selected video
			var video_link  = jQuery(this).attr('href');

			var video 		= jQuery(this).parents('.video_info');
			var video_id	= video.attr('video_id');

			var player		= jQuery('.video_wrapper');



			//console.log(video_id);

			player.find('H2').html(video.find('H6').html());
			player.find('P').html(video.find('.description').html());

//			player.find('.video_placeholder').html(video_id);

			//console.log('myKdp');
			//console.log(myKdp);

			jQuery('html,body').animate({scrollTop: 200}, 300);

			//console.log(myKdp);

			myKdp.insertMedia("-1", video_id, 'true');
//			myKdp.addJsListener("initMedia","kdpDoOnLoadVideo");



	});
	*/


	if (!is_ipad && !is_iphone) {
		jQuery('#fixed_footer').delay(500).slideDown('slow');
	}


	jQuery(".home_happening_row_wrapper").mouseover(function(){

		var studyLinks = jQuery(this).find(".study-links");

		if(studyLinks.find("a").length == 1){
			studyLinks.css("top",50);
		}

		studyLinks.css('display','block');
	});

	jQuery(".home_happening_row_wrapper").mouseout(function(){
		jQuery(this).find(".study-links").css('display','none');
	});


});
/* End jQuery(document).ready() */



/**
 * Ecole submit function
 */
function lecole_submit(submit_link) {

	var form = submit_link.parents('FORM');
	var email = form.find('INPUT[name=email]');
	var email_confirm = form.find('INPUT[name=email_confirm]');

	email.removeClass('error');
	email_confirm.removeClass('error');

	if (email.val() === '' || email.val() === 'Email address') {
		jQuery('#submit_error').html('Please enter your email address');
		email.addClass('error');
		return false;
	} else if (email_confirm.val() === '' || email_confirm.val() === 'Confirm email address') {
		jQuery('#submit_error').html('Please confirm your email address');
		email_confirm.addClass('error');
		return false;
	} else if (email.val() != email_confirm.val()) {
		jQuery('#submit_error').html('Email addresses must match');
		email.addClass('error');
		email_confirm.addClass('error');
		return false;
	} else {
		// Submit via ajax
		jQuery.post(
             document.location.href,
             form.serialize(),
             function () {
				// Redirect regardless of success/fail
				window.location.href = "/confirmation/lecole/";
             }
        );
        return false;
		//submit_link.parents('FORM').submit();
	}
	return false;
}



/**
 * Alumni submit function
 */
function alumni_submit(submit_link) {

	var form        = submit_link.parents('FORM');
	var fciUsername = form.find('INPUT[name=fciUsername]');
	var fciPassword = form.find('INPUT[name=fciPassword]');

	fciUsername.removeClass('error');
	fciPassword.removeClass('error');

	if (fciUsername.val() === '' || fciUsername.val() === 'User Name') {
		jQuery('#submit_error').html('Please enter your username');
		fciUsername.addClass('error');
		return false;
	} else if (fciPassword.val() === '' || fciPassword.val() === 'Password') {
		jQuery('#submit_error').html('Please enter your password');
		fciPassword.addClass('error');
		return false;
	} else {
		submit_link.parents('FORM').submit();
		//return false;
	}
	return false;
}



/**
 * Function Show/hide default values for text inputs
 */
function set_default_text() {
    var default_value = this.value;
    jQuery(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            jQuery(this).addClass('active');
        }
    });
    jQuery(this).blur(function() {
        if(this.value == '') {
            this.value = default_value;
            jQuery(this).removeClass('active');
        }
    });
}


/**
 * Function to cycle through slides
 */
function slide_cycle() {

	// Get position of currently visible slide
	var pos = parseInt(jQuery('#slideshow_nav LI A.active').attr('position'));

	// If less than total slide count increment by 1, otherwise reset to 1
	if (pos < jQuery('#slideshow_nav LI A').size()) {
		pos += 1;
	} else {
		pos = 1;
	}

	// Run the change_slide function
	change_slide(pos);
}


/**
 * Function to change slide and active states
 */
function change_slide(pos) {

	// Remove all hover classes
	jQuery('#slideshow_nav A.active').removeClass('active');

	// Add hover class to link
	jQuery('#slideshow_nav A[position="'+pos+'"]').addClass('active');

	// Show this slide if not already visible
	if ((slide = jQuery('#slideshow #slideshow_slide_'+pos))) {

		jQuery('#slideshow LI:visible').fadeOut();
		jQuery(slide).fadeIn();
	}
}


var HomeFlash = {

	loaded : function() {
		jQuery('BODY').addClass('loaded');
	},

	remove: function() {
		jQuery('#home_slideshow').hide();
	}

};



var Cookie = {

	create: function(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
	}

	, read: function(name, def) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return def;
	}

	, erase: function(name) {
			createCookie(name,"",-1);
	}

};



var PageFunctions = {

	showHomeIntro : function(path) {

		var cookie_name = 'intro';

		if ((Cookie.read(cookie_name) != '1')
			&& swfobject.hasFlashPlayerVersion('9')
			&& !is_ipad
			&& !is_iphone
			) {
			Cookie.create(cookie_name, '1', 90);
			document.location.href = path;
		}

	},


	// Set current section active state on main menu
	set_nav_active_state : function(path) {

		var end 		= 0;
		var segments 	= path.split('/');
		var menu;
		var selectorId;

		var subsection = segments[1] + "/" + segments[2];

		switch(subsection){
			case 'courses/ny':
				selectorId = 'newyorkcourses_link';
				break;
			case 'courses/ca':
				selectorId = 'cacourses_link';
				break;
		}

		if( segments[1] == 'explore' ) selectorId = 'explore_link';

		if( typeof selectorId != 'undefined' ){
			menu = jQuery("#"+selectorId);
		}else{
			menu 		= jQuery("A[href='/"+segments[1]+"']");
		}

		if (segments[2] === "fci_community_login") {
			jQuery('#sub_nav_fci').addClass('active');
		} else if (menu) {
			menu.parent('LI').addClass('active');
		}


		/*
		// Extract first URL segment
		end = path.length;
		if (path.indexOf("/",1) !== -1) {
			end = (path.indexOf("/", 1) - 1);
		}
		segments[1] = path.substr(1, end);

		// Extract second URL segment
		path = location.pathname.substr(end + 2);
		end = path.length;
		if (path.indexOf("/", 1) !== -1 ){
			end = path.indexOf("/", 1);
		}
		segments[2] = path.substr(0, end);

		if (segments[2] === "fci_community_login") {
			jQuery('#sub_nav_fci').addClass('active');
			return;
		}
		*/

		/*

		switch (segments[1]) {
			case 'courses':
				jQuery('#main_nav_courses').addClass('active');
				return;
				break;
			case 'apply':
				jQuery('#main_nav_apply').addClass('active');
				return;
				break;
			case 'explore':
				jQuery('#main_nav_explore').addClass('active');
				return;
				break;
			case 'careers':
				jQuery('#main_nav_alumni').addClass('active');
				return;
				break;
			case 'fci_consulting_group':
				jQuery('#sub_nav_consulting').addClass('active');
				return;
				break;
			case 'catering_events':
				jQuery('#sub_nav_events').addClass('active');
				return;
				break;
			case 'lecole_restaurant':
				jQuery('#sub_nav_lecole').addClass('active');
				return;
				break;
			default:
				break;
		}

		// Nothing matched, try to find link

		var menu = jQuery("A[href='"+location.pathname+"']");

		console.log(menu);

		// Nothing matched, try to find link
		if (menu) menu.parent('LI').addClass('active');

		*/


	}

};





