/* Gatlin Namespace */

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

		Gatlin.menuNavigation();
		Gatlin.bannerRotate();
		Gatlin.flashContent();
		Gatlin.partnerLocatorDialog();
		Gatlin.setSignupAction();
		Gatlin.quickSearch();
		Gatlin.toggleLanguageList();
		Gatlin.toggleSearchText();
		Gatlin.prepareForms();
		Gatlin.enableTabs();

		//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(); });

		//PSlider
		//Gatlin.providerSlider();
		// Note: Tab init moved to course detail view due to ui script dependency
	}, // end init()

	bannerRotate: function () {
		var banner = $('.banner-wide');
		if (banner.length) banner.cycle();
	},

	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);
			}
		});
	},

	enableTabs: function() {
		var $tabs = $('div.tabs-block').tabs();
		if ($tabs.length) $('ul.ui-tabs-nav > li', $tabs).addClass('ui-corner-all');
	},

	flashContent: function() {
		// 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.value-video').flash({
			hasVersion: '9.0.0',
			params: { allowfullscreen: 'false', allowscriptaccess: 'sameDomain', quality: 'high', wmode: 'transparent', flashVars: 'videoFilename=/media/video/gi_intro_en.flv' },
			swf: '/media/video/gi_player_home.swf',
			height: 232,
			width: 570
		});

	}, // end flashContent()

	menuNavigation: function() {
		var menu = $('#menu-navigation');
		if (!menu.length) return;
		menu.load('/catalogue/listing', function() {
			$(this).gimenu({ backLink: false, crumbDefaultText: 'Choose a path:' });
		});
	},

	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()

	prepareForms: function() {
		// indicate required fields
		$('input.required, select.required, textarea.required').before('<span class="required">*</span>');
	},

	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

	quickSearch: function()	{
		var search = $('form.quick_search input.search');
		if (!search.length) return;
		search.autocomplete('/catalogue/auto_complete', {
			autoFill: false,
			max: 15,
			minChars: 3,
			selectFirst: true,
			scrollHeight: 220,
			width: 260
		});
	},

	//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();
});
