$(document).ready(function(){

// hack to correct the fixed position footer bar.
//stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
{
	 $("#strap_wrapper").css("position", "static");
	 $('ul.portal_nav li a').click(function(evt){
		if($(this).hasClass('nolink')){
			evt.preventDefault();	
		}
	});
};


	
	$(".formWrapper form .datepicker").datepicker();
	
	$(".formWrapper form").submit(function(evt){
		var submit_flag = true;
		$(".red").remove();
		$("input[type!=radio][type!=checkbox].dsRequiredText, select.dsRequiredSelect, textarea.dsRequiredTextArea", $(this)).each(function(){
			var parent_item = $(this).parent("p");				
			$(this).removeClass("needed");
			if($(this).val() == ''){
				$(this).addClass("needed").after("<em class='red'> - Required</em>");
				submit_flag = false;
			}
		});	
		
		$("input[type=radio].dsRequiredRadio, input[type=checkbox].dsRequiredRadio", $(this)).each(function(){
			var parent_item = $(this).parents("p");					
			if($(".red", parent_item).length == 0){
				var item_name = $(this).attr("name");
				var filled = false;	
				if($("input[name=" + item_name + "]:checked", parent_item).length > 0){
					filled = true;		
				}
				if(filled == false){
					parent_item.append("<em class='red'> - Required</em>");
				}			
			}
		});
								
		if (submit_flag == false){
			evt.preventDefault();	
			location.hash = "form_top";
			$("div.formWrapper").prepend("<h3 class=\"error\">Please check all required fields</h3>");
		}
	});
	
	$('div.languages').mouseover(function(){
		$('div.languages div').show();
		$('div.languages').mouseleave(function(){
			$('div.languages div').hide();
		});
	});
	
	// Main nav
	if($('ul.main_nav li a[href="'+window.location.pathname+'"]').length){
		$item = $('ul.main_nav li a[href="'+window.location.pathname+'"]');
		if($item.parent().hasClass('parent')){
			$item.parent().addClass('active');				
		} else {
			$item.parent().parent().parent().addClass('active');	
		}
	} else {
		if (typeof(targetURL) != 'undefined') {
			var sec_id = targetURL.substring(1, targetURL.length-1);
			$('ul.main_nav li a[class='+sec_id+']').parent().addClass('active');
		} 
	}
	
	// Tabs
	$('div.tab_display:eq(0)').show();
	$('ul.tabs li a').click(function(evt){

		// Toggle tab content
		tab_id = $(this).attr('href').substr(1);
		$('div.tab_display').hide();
		$('div[id='+tab_id+']').show();
		evt.preventDefault();
		
		// Toggle active tab
		$('ul.tabs li a').removeClass('active');
		$(this).addClass('active');

	});
	
	// Course Schedule, Overview toggle
	$('table.default_table tr td.active').hide();
	$('table.default_table tr td.overview a').click(function(evt){
		$(this).parent().parent().addClass('active');
		$(this).parent().parent().next().children('td').show();
		if ($(this).text() == 'View Overview') {
			$(this).text('Close Overview');
			$(this).addClass('close');
			$(this).parent().parent().next().show();
		} else {
			$(this).text('View Overview');
			$(this).removeClass('close');
			$(this).parent().parent().removeClass('active');
			$(this).parent().parent().next().hide();
		}
		evt.preventDefault();
	});
	$('table.default_table tr td.active a.close').click(function(evt){
		$(this).parent().parent().parent().prev().removeClass('active');
		$(this).parent().parent().hide();
		if ($('table.default_table tr td.overview a').text() == 'Close Overview') {
			$('table.default_table tr td.overview a').text('View Overview');
			$('table.default_table tr td.overview a').removeClass('close');
		} 
		evt.preventDefault();
	});
	
	$('div.tabs_content tr').each(function() {
        $(this).children().eq(0).children().wrapAll('<div class="tabs_col_wrapper"><div class="tabs_col_shadow"><div class="tabs_col">');
        $(this).children().eq(0).addClass('first');
        $(this).children().eq(1).children().wrapAll('<div class="tabs_col_wrapper"><div class="tabs_col_shadow"><div class="tabs_col">');
        $(this).children().eq(2).addClass('last');
    });
	
	// Instructor pop-over
	$('div.instructor_pop_wrapper').hide();
	$('a.instructor_pop_trigger').click(function(evt){
		evt.preventDefault();
		$('#course_schedule div.instructor_pop_wrapper').hide();
		$(this).siblings('div').show();
	});
    $('div.instructor_pop_wrapper a.close').click(function(evt){
		evt.preventDefault();

        $(this).parent().parent().hide();
    });
	
	// Portal flyout
	
	$('ul.portal_nav li a').mouseenter(function(){
		var flyout_id = $(this).parent().attr('class');
		if($('div.header div.flyout[id='+flyout_id+']').length){
			var offset = $(this).offset();
			$('div.header div.flyout[id='+flyout_id+']').show().css('left',parseInt(offset.left) + 'px');
		}
		$('ul.portal_nav li a').mouseleave(function(){
			$('div.header div.flyout[id='+flyout_id+']').mouseenter(function(){
				$('div.header div.flyout[id='+flyout_id+']').show();
				$('div.header div.flyout').mouseleave(function(){
					$('div.header div.flyout').hide();
				});
			});
			$('div.header div.flyout').hide();
		});
	});
	$('div.header div.flyout a.close').click(function(evt){
		$('div.header div.flyout').hide();
		evt.preventDefault();
	});
	
	// Strap flyouts
	$('ul.strap_nav li a:not(ul.strap_nav li div a, ul.strap_nav li.partners a)').click(function(evt){
		var flyout_id = $(this).attr('rel');
		$('ul.strap_nav li div:not(ul.strap_nav li.partners div)').hide();
		$('div[id='+flyout_id+']').show();
		evt.preventDefault();
	});
	$('a.strap_link, div.strap_search a.close').click(function(evt){
		$(this).parent().hide();
		evt.preventDefault();
	});
	$('ul.strap_nav li.partners a:not(ul.strap_nav li.partners div a)').click(function(evt){
		$('div.strap_partners_wrapper').toggle();
		evt.preventDefault();
	});
	$('div.strap_partners a.close').click(function(evt){
		$('div.strap_partners_wrapper').hide();
		evt.preventDefault();
	});

	$("#strap_wrapper form, #courseSortForm").delegate("select", "change", function(){
		parseStrapResults($(this), false);
	});
	
	$("#strap_wrapper select.country, #courseSortForm select.country").each(function(){		
		parseStrapResults($(this), true);							
	});	
	
	

	$("form.cal_filter").delegate("select", "change", function(){
		parseStrapResults($(this), false);
	});
	
	$("form.cal_filter select.country").each(function(){		
		parseStrapResults($(this), true);
	});	

    transText();
	
	
	$('div.news p.active a').click(function(evt){
		evt.preventDefault();
		$('div.news div').toggle();
	});
	
	$('div.news a.close, div.news a.panel_close').click(function(evt){
		evt.preventDefault();
		$('div.news div').hide();
	});
	
	$(".input").focus(function(evt){
		if($(this).attr("title") == $(this).val()){
			$(this).val("");	
		}
	});
	
	$(".input").focusout(function(evt){
		if($(this).val().length == 0){
			$(this).val($(this).attr("title"));	
		}
	});
			
	$("#feedback").click(function(evt){
		evt.preventDefault();
		$("#feedback_flyout_wrapper").show().css('margin-top', '-' + $("#feedback_flyout_wrapper").height() + 'px');
		return false;
	});
	
	$("#feedback_flyout_wrapper a.close").click(function(evt){
		evt.preventDefault();
		$("#feedback_flyout_wrapper").hide();
	});
	
	$("#top_link").click(function(evt){
		evt.preventDefault();
		$(window).scrollTop(0);
	});
	
	
});

function parseStrapResults(changed_item, init){
	var $parent = changed_item.parent();
	if(changed_item.hasClass("country") && !init){
		$("select.state option", $parent).remove();
		var $items = $("#strap_wrapper input.strap_state[data-country=" + changed_item.val() + "]");
		if($items.length > 0){
			$("select.state", $parent).show().prev('label').show();
			$items.each(function(index){
               	$("select.state", $parent).append($('<option></option>').text($(this).attr("data-text")).val($(this).val()));
			});
		}else {
			$("select.state", $parent).hide().prev('label').hide();
		}
	}
	
	if(changed_item.hasClass("city") == false){
		
			$("select.city option", $parent).remove();
			
			var $sel_country = $("select.country option:selected", $parent).val();
			var $sel_state = null;
			if($("select.state:visible", $parent).length > 0 || init == true){
				$sel_state = $("select.state option:selected", $parent).val();
			}
			
			var $items = '';
			if($sel_state == null){
				$items = $("#strap_wrapper input.strap_cities[data-country=" + $sel_country + "]");
			} else {
				$items = $("#strap_wrapper input.strap_cities[data-country=" + $sel_country + "][data-state=" + $sel_state + "]");
			}
			
			if($items.length > 0){
				$("select.city", $parent).show().prev().show();
				$items.each(function(index){
					$("select.city", $parent).append($('<option></option>').text($(this).attr("data-text")).val($(this).val()));
				});
			}else {
				$("select.city", $parent).hide().prev().hide();
			}	
					
	}	
}

function transText(){
	var toLang = $("#language_selector p.active a").attr("data-lan");
    var transDiv = $('body');

    if(toLang!='en'){
        transDiv.translate(toLang);
    }
}
