/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
*/
jQuery.easing['jswing']=jQuery.easing['swing'];jQuery.extend(jQuery.easing,{def:'easeOutQuad',swing:function(x,t,b,c,d){return jQuery.easing[jQuery.easing.def](x,t,b,c,d);},easeInQuad:function(x,t,b,c,d){return c*(t/=d)*t+b;},easeOutQuad:function(x,t,b,c,d){return-c*(t/=d)*(t-2)+b;},easeInOutQuad:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t+b;return-c/2*((--t)*(t-2)-1)+b;},easeInCubic:function(x,t,b,c,d){return c*(t/=d)*t*t+b;},easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b;},easeInOutCubic:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t+b;return c/2*((t-=2)*t*t+2)+b;},easeInQuart:function(x,t,b,c,d){return c*(t/=d)*t*t*t+b;},easeOutQuart:function(x,t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b;},easeInOutQuart:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t+b;return-c/2*((t-=2)*t*t*t-2)+b;},easeInQuint:function(x,t,b,c,d){return c*(t/=d)*t*t*t*t+b;},easeOutQuint:function(x,t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b;},easeInOutQuint:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t*t+b;return c/2*((t-=2)*t*t*t*t+2)+b;},easeInSine:function(x,t,b,c,d){return-c*Math.cos(t/d*(Math.PI/2))+c+b;},easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b;},easeInOutSine:function(x,t,b,c,d){return-c/2*(Math.cos(Math.PI*t/d)-1)+b;},easeInExpo:function(x,t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b;},easeOutExpo:function(x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b;},easeInOutExpo:function(x,t,b,c,d){if(t==0)return b;if(t==d)return b+c;if((t/=d/2)<1)return c/2*Math.pow(2,10*(t-1))+b;return c/2*(-Math.pow(2,-10*--t)+2)+b;},easeInCirc:function(x,t,b,c,d){return-c*(Math.sqrt(1-(t/=d)*t)-1)+b;},easeOutCirc:function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b;},easeInOutCirc:function(x,t,b,c,d){if((t/=d/2)<1)return-c/2*(Math.sqrt(1-t*t)-1)+b;return c/2*(Math.sqrt(1-(t-=2)*t)+1)+b;},easeInElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4;}else{var s=p/(2*Math.PI)*Math.asin(c/a);}return-(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;},easeOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4;}else{var s=p/(2*Math.PI)*Math.asin(c/a);}return a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b;},easeInOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d/2)==2)return b+c;if(!p)p=d*(.3*1.5);if(a<Math.abs(c)){a=c;var s=p/4;}else{var s=p/(2*Math.PI)*Math.asin(c/a);}if(t<1)return-.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p)*.5+c+b;},easeInBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*(t/=d)*t*((s+1)*t-s)+b;},easeOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b;},easeInOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;if((t/=d/2)<1)return c/2*(t*t*(((s*=(1.525))+1)*t-s))+b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t+s)+2)+b;},easeInBounce:function(x,t,b,c,d){return c-jQuery.easing.easeOutBounce(x,d-t,0,c,d)+b;},easeOutBounce:function(x,t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b;}else if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b;}else if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b;}else{return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b;}},easeInOutBounce:function(x,t,b,c,d){if(t<d/2)return jQuery.easing.easeInBounce(x,t*2,0,c,d)*.5+b;return jQuery.easing.easeOutBounce(x,t*2-d,0,c,d)*.5+c*.5+b;}});

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		},
		ui : {
			showcase : function(){
				var $showcase = $('#showcase');
				var $slider = $showcase.find('div.slides');
				
				var width = $slider.find('div.slide:first').width();
				var count = $slider.find('div.slide').length;
				
				var transition = 1000;
				var time = 7500;
				var current = 1;
				
				if(count < 2) return false;
				
				var code = '<ul class="index"><li><a href="#prev" class="step prev">Previous</a></li>';
				for(i = 0; i < count; i++) code += '<li><a href="#'+(i+1)+'"'+(i == 0 ? ' class="active"' : '')+'>'+(i+1)+'</a></li>';
				code += '<li><a href="#next" class="step next">Next</a></li></ul>';
				
				$showcase.append(code);
				
				var $links = $showcase.find('ul.index li a');
				
				$links.click(function(){
					if($(this).is('.active') || $slider.is(':animated')){
						return false;
					} else if($(this).is('.step')){
						if($(this).is('.next')) var target = current + 1;
						if($(this).is('.prev')) var target = current - 1;
						
						if(target > count) target = 1;
						if(target < 1) target = count;
						
						$links.eq(target).trigger('click');
						
					} else {
						$(this).addClass('active').parent().siblings('li').find('a.active').removeClass('active');
						var num = parseInt($(this).html());
						
						$slider.animate({marginLeft: '-'+(width*(num-1))+'px'},transition,'easeInOutQuart',function(){
							current = num;
						});
					}
					return false;
				});
				
				showcaseGo = function(){
					$showcase.find('ul.index li a.next').trigger('click');
				}
				
				showcaseInt = setInterval('showcaseGo()',time);
				
				$showcase.bind('mouseenter',function(){
					clearInterval(showcaseInt);
				}).bind('mouseleave',function(){
					showcaseInt = setInterval('showcaseGo()',time);
				});
			},
			nav : function(){
				var $root = $('ul.nav-b');

				$root.find('li').each(function(){
					if($(this).find('> ul').length > 0){
						$(this).find('> a').after('<span>&raquo;</span>');
						$(this).find('> span').click(function(){
							$(this).parent().toggleClass('active');
						});
					} else {
						//$(this).find('> a').after('<span class="empty">&raquo;</span>');
					}
				});

				var path = location.pathname.substring(1);
				var pageIndex = path.indexOf('/Pages');
				if (pageIndex > 0)
				{
					path = path.substring(0, pageIndex);
				}
				$('ul.nav-b a[href*="' + path + '"]').parent().css('class', 'active');
			}
		},
		fixes : {
			hovers : function(){
				if(!$.browser.msie || parseInt($.browser.version) != 6) return false;
				$('ul.nav-c > li').bind('mouseenter mouseleave',function(){
					$(this).find('> ul').toggleClass('on')
				});
			}
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.ui.showcase();
	Engine.ui.nav();
	Engine.fixes.hovers();
	
});

function search() {
	window.location.href = '/search/search?q=' + escape($("#f-search").val()) + '&site=default_collection&client=rebrand_frontend&output=xml_no_dtd&proxystylesheet=rebrand_frontend';
}

$(document).ready(function() {
	$("#searchButton").click(function() {
		search();
	});
	
	//set form submition for esl login form on /continuing_education/languages/Pages/login.aspx
	$("#idButton").click(function() {
		jQuery().SharePointFormSubmit(
		  {
				'element':'#eslForm', 
				'frmMethod':'post', 
				'frmAction': 'http://rdc.vitalenglish.com/ExternalLogin.aspx',
				'debug':true
		  })
		  return false;
	});
        
	//set form submition for CE course search form
	$("#ceCourseSearchButton").click(function() {
		jQuery().SharePointFormSubmit(
		  {
				'element':'#courseSearchDiv',
				'frmMethod':'post', 
				'frmAction': 'http://test.rdc.ab.ca/continuingeducation/courseSearch.php',
				'debug':true
		  })
		  return false;
	});

	$("#f-search").keydown(function(e) {
		e = e || window.event;
		var code = e.keyCode || e.which;	
		switch (code) {
			case 13: { search(); return false; }
		}
		return true;
	});
});
	
	
/*!
 * SharePointFormSubmit
 *
 * 2009 Alex Dinnouti
 * licensed under GPL licenses.
 * 
 * http://code.google.com/p/jquerysharepointform/
 * 
 * Date: 2009-09-17
 * Revision: 2
 *
 * *
 * Allow to send a form within a form in sharepoint
 * 
 * parameters:
 * 	element - the element that contains the form
 * 	frmMethod - post / get
 * 	frmAction - url
 *  frmTarget optional,  default = '_self' -  '_blank' / '_parent' / '_self' / '_top' / window name
 *  debug - true/false
 
 * in case you need a custom submit button
 *	sbmCustom - true/false
 *	sbmID - submit ID
 *	sbmName - submit Name
 *	sbmValue - submit Value
 
 * in case you need to add form ID and Name	
 *  frmID - form id - optional
 *  frmName - form name - optional
 
 * see a example at:
 * http://code.google.com/p/jquerysharepointform/
 
 * for Convio fans, replace the submit with hidden
*/

(function($){
	$.fn.SharePointFormSubmit = function(arr) {
	
		var frmMethod = arr['frmMethod'];
		var frmAction = arr['frmAction'];
		var element = arr['element'];
		
		frmTarget = (arr['frmTarget'] === undefined) ? '_self' : arr['frmTarget'] ; 
		frmID = (arr['frmID'] === undefined) ? '' : arr['frmID'] ;
		frmName = (arr['frmName'] === undefined) ? '' : arr['frmName'] ;
		
		debug = ((arr['debug'] === undefined) || (arr['debug'] == false)) ? 'none' :  'block' ;
		
		
		var f = $('<form></form>');
			f.attr('method', frmMethod);
			f.attr('action', frmAction);
			f.attr('target', frmTarget);
			f.attr('id', frmID);
			f.attr('name',frmName);
			f.css('display', debug);
		
		$(element).find('select, textarea, :input:not(:checkbox)').each(function(){
			e = $('<input>');
			e.attr('type', $(this).attr('type'));
			e.attr('id', $(this).attr('id'));
			e.attr('name', $(this).attr('id')); //BM: had to change the name to ID because SP is stripping out the name attribute from the for elements for some reason.
			e.attr('value', $(this).val());
			e.attr('checked', $(this).attr('checked'));
			e.attr('multiple', $(this).attr('multiple'));
			f.append(e);
		});
		
		// seems IE have a bug in the type=checkbox it insert value=on instead of checked='checked'
		$(element).find('input:checkbox').each(function(){
			if ($(this).attr('checked'))
			{
				chkboxChecked = 'checked';
			} else {
				chkboxChecked = '';
			};

			e = '<input type="checkbox" id="'+ $(this).attr("id") +'" name="'+ $(this).attr("name") +'" checked="'+ chkboxChecked  +'">';
			f.append(e);
		});


		// in case you need a custom submit buttom, Convio.com does.
		if (arr['sbmCustom']){
			sbmt = $('<submit>');
			sbmt.attr('type', 'submit');
			sbmt.attr('id', arr['sbmID']);
			sbmt.attr('name', arr['sbmName']);
			sbmt.attr('value', arr['sbmValue']);
			f.append(sbmt);			
		}
	
		// submit the form
		// add the form to the document between </form> and </html>
		// if you put a breakpoint before the submit you will be able to see it
		var h = $('body').append(f);
		// $(sbmt).trigger('click');
		f.submit();
		
		return false; //BM: added this return becasue the SP page form was being submitted whenever the fake form button was clicked.
	};
})(jQuery);
