/* Gatlin Namespace */

Gatlin = {
	init: function() {
		// open some links in a new window
		$('a[rel="external"]').attr('target', '_blank');

		Gatlin.flashContent();
		Gatlin.partnerLocatorDialog();
		Gatlin.setSignupAction();
		Gatlin.toggleLanguageList();
		Gatlin.toggleSearchText();
		//Set landing tabs if home page or course listing
		$('body#landing,body#list_courses').each(function(){ Gatlin.setLandingTabs(); });
		$('#jump_category select').change(function(){ window.location = "/catalogue/category/" + $(this).val(); });
		Gatlin.prepForms();
		//PSlider
		//Gatlin.providerSlider();
		// Note: Tab init moved to course detail view due to ui script dependency
	}, // end init()
	providerSlider: function() {
		//create slider container
		$('div#provider-select ul.p_results').after('<div class="pr_scroll"></div>');
		//create slider
		var itemsLength = $('div#provider-select ul.p_results li').length;
		var container = $('div#provider-select');
		var ul = $('ul.p_results',container)
		var p_slider = $('div.p_results_slider', container).slider({
			orientation: "vertical",
			min: 0,
			max: itemsLength,
			handle: 'div.pr_scroll',
			slide: function(event, ui){
				ul.css('margin-top', ui.value * -1);
			}
		});
	},
	
	flashContent: function() {
		// get language variable for multi-lingual content
		var lang = $("html").attr('lang');
		// add globe logo
		var size = 65;
		$('body:not(.admin,.partner) div#header h1').prepend('<span class="logo_globe"></span>').children(':first').flash({
			hasVersion: '9.0.0',
			height: size,
			params: { allowfullscreen: 'false', allowscriptaccess: 'sameDomain', quality: 'high', wmode: 'transparent' },
			swf: '/media/common/globe_gg.swf',
			width: size
		});
		//Add Header Loop
		
		$('body:not(.admin,.partner) div#header h1').after('<span id="header_promo_loop"></span>');
		$('span#header_promo_loop').flash({
			hasVersion: '9.0.0',
			height: 39,
			params: { allowfullscreen: 'false', allowscriptaccess: 'sameDomain', quality: 'high', wmode: 'transparent' },
			swf: '/media/common/header_loop.swf',
			width: 470
		});
		//Landing Video
		$('body#landing div#callouts div.vid-wrap').empty().flash({
			hasVersion: '9.0.0',
			params: { allowfullscreen: 'false', allowscriptaccess: 'sameDomain', quality: 'high', wmode: 'transparent',flashVars: 'videoFilename=/media/video/gi_intro_'+lang+'.flv'},
			swf: '/media/video/gi_player_home.swf',
			height: 316,
			width: 570
		});
		
	}, // end flashContent()

	partnerLocatorDialog: function() {
		$('a[href="/locator"]').each(function() {
			$(this).click(function(event) {
				// keep the links from going anywhere
				event.preventDefault();

				// create lightbox and inject flash content
				$('<div></div>')
				.dialog({
					close: function() { $(this).remove(); },
					dialogClass: 'partner-locator-dialog',
					draggable: false,
					height: 500,
					modal: true,
					resizable: false,
					title: Gatlin.translations.get('site.locate_partners'),
					width: 780
				})
				.flash({
					hasVersion: '8.0.0',
					height: 450,
					params: { allowfullscreen: 'false', allowscriptaccess: 'sameDomain', quality: 'high', wmode: 'transparent' },
					swf: '/media/partner_locator/partner_locator.swf',
					width: 775
				});
			})
		})
	}, // end partnerLocatorDialog()

	setSignupAction: function(){
		$('a.signup_go').each(function() {
			$(this).click(function(event) {
				// keep the links from going anywhere
				event.preventDefault();

				// create lightbox
				$('<div><img src="/media/navigation/loading-large.gif" /></div>').dialog({
					close: function() { $(this).remove(); },
					dialogClass: 'signup-dialog',
					draggable: false,
					height: 485,
					modal: true,
					resizable: false,
					title: $('span.title', $(this).closest('li')).text(),
					width: 780
				})
				.html(Gatlin.translations.get('callouts.signup_text'));
			})
		})

	},// end setSignupAction

	toggleLanguageList: function() {
		$('a.lang_link:parent').hover(
			function () { $('#language-list').show("fast"); },
			//hide menu on mouseleave of list
			function () { $('#language-list').mouseleave(function(){$('#language-list').hide()});}
		);
	},

	toggleSearchText: function() {

		$('input.title-attribute-as-label')
		.focus(function() {
			// Make sure the title attribute is defined, if it isn't already
			if (!this.title.length) this.title = this.value;

			// If the value attribute equals the title attribute, clear out the value
			if (this.value == this.title)
				this.value = "";

			$(this).addClass('has-user-entered-text');
		})
		.blur(function() {
			// If there's no text in there, reset the field back to the
			// default text and remove the "has-user-entered-text" class
			if (!this.value.length) {
				this.value = this.title;
				$(this).removeClass('has-user-entered-text');
			}
		});

	}, // end toggleSearchText()

	// Landing Page Category Tabs
	setLandingTabs: function(){
		$('div.mc_tabs').tabs();
		var selected = $('div.mc_tabs').tabs('option', 'selected');

		// first load selected content with images
		var panel_id = $('div.mc_tabs ul.tabs_nav li:eq('+selected+') a').attr('href');
		Gatlin.retrievePanel(panel_id);

		// now set click event to load selected panels with images
		$('div.mc_tabs ul.tabs_nav li a').click(function(){ Gatlin.retrievePanel($(this).attr('href')) });
	}, // end setLandingTabs

	// AJAX Content Requests for Tab Panels
	retrievePanel: function(panel_id){
		var category_tag = panel_id.replace('#panel_','');
		if(category_tag != 'who'){
		$('div'+panel_id).load("/catalogue/category_panel/"+category_tag);
		}
	}, // end retreive panel

	//Misc form prep
	prepForms: function()	{
	//prep quicksearch input
	if ($("form#quick_search").length > 0) {
	  $("form#quick_search input#qs_keywords").click(function(){
	  $("form#quick_search input#qs_keywords").val("");
	  $("form#quick_search input#qs_keywords").css("text-align","left");
	  $("form#quick_search input#qs_keywords").unbind("click",this);
	  });
	 //check for form required fields and add helper icons
	 if($("input.aria-required-true").length > 0) {
	 //TODO finish $JJC$
	 }
	 //Add Autosuggest to Keyword Search
	 if($("input#qs_keywords").length > 0){
		$("input#qs_keywords").autocomplete("/catalogue/auto_complete",{
		width: 260,
		selectFirst: true,
		max: 15,
		minChars: 3,
		autoFill: false,
		scrollHeight: 220
		})
	 }
	}
	}, //end prepForms()
	//Provder Selection
	set_pcountry: function(country) {
		$.get("/locator/select/" + country, function(data) { $('div#provider-select').replaceWith(data); });
	},
	//FreeReg Validation
	checkFreeReg: function(){
		var req_fields = $("form#free_signup input[type='text']");
		var complete = true;
		for(var i=0; i < req_fields.length; i++) {
		if(req_fields[i].value==''){
			complete = false	
		}
		}
		if(!complete){
			alert('All of the fields are required to register for your free course.  Please complete the remaining fields and try again');
		}
		return complete;	
		},//end freereg validation
	
	translations: {
		cache: {},
		get: function(item) {
			if (this.cache[item] === undefined) {
				this.cache[item] = $.ajax({ async: false, dataType: 'text', url: '/language/translate/' + item }).responseText;
			}

			return this.cache[item];
		}
	}

}


$(document).ready(function() {
    Gatlin.init();
});
