var $=jQuery.noConflict();

$(document).ready(function() {
// custom functions
$.fn.hasAncestor = function(a) {
	return this.filter(function() {
		return !!$(this).closest(a).length;
	});
};
$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};
$.fn.cycle.transitions.scrollVertUp = function($cont, $slides, opts) {
	$cont.css('overflow','hidden');
	opts.before.push(function(curr, next, opts, fwd) {
		$.fn.cycle.commonReset(curr,next,opts);
		opts.cssBefore.top = fwd ? (next.cycleH-1) : (1-next.cycleH);
		opts.animOut.top = fwd ? -curr.cycleH : curr.cycleH;
	});
	opts.cssFirst = { top: 0 };
	opts.cssBefore= { left: 0 };
	opts.animIn   = { top: 0 };
	opts.animOut  = { left: 0 }; 
};

// highlight nav items
$(function(){
	var f = document.location.pathname;
	var n = f.length;
	var s = f.split(/\//);
	var o = s[1];
	if (n<2) {
		$('#logo').addClass('on');
	} else {
		$('#nav a[href$="' + f + '"]').addClass('on');
	}
	$('#nav>li>a[href$="/' + o + '/"]').addClass('on');
});

// dropdown menus
$('#nav li ul').each(function () {
	$(this).parent().eq(0).hover(function () {
		$(this).addClass('on');
		$('ul:eq(0)', this).css('z-index','901').slideDown(200, 'swing');
	}, function () {
		$(this).removeClass('on');
		$('ul:eq(0)', this).stop(true,true).css('z-index','900').slideUp(400, 'swing');
	});
});

// fix subnav hover in ie
$('#nav ul a').bind({
    mouseenter: function() {
       $(this).css('backgroundColor','#E3E3E3');
    },
    mouseleave: function() {
       $(this).css('backgroundColor','#EDEDED');
    }
});

// animated icon
function iconloop () {
	$('#icon').animate({ backgroundPosition: '0px -110px' }, 0).delay(200).animate({ backgroundPosition: '0px 0px' }, 0).delay(100).animate({ backgroundPosition: '0px -220px' }, 0).delay(100).animate({ backgroundPosition: '0px -330px' }, 0).delay(150).animate({ backgroundPosition: '0px -220px' }, 0).delay(100).animate({ backgroundPosition: '0px -110px' }, 0).delay(200).animate({ backgroundPosition: '0px 0px' }, 0);
	t = setTimeout( function() { iconloop() }, 2000);
}
$('#icon').hover(function () {
	iconloop();
}, function () {
	clearTimeout(t);
	$('#icon').stop(true, true).delay(100).animate({ backgroundPosition: '0px 0px' }, 0);
});


// slideshow
if ($('#hshow').length > 0) {
	$('#hslide .txt').css({opacity:'0.9'});
	$('#hslide').cycle({
		fx: 'scrollVertUp',
		animIn: 'scrollUp',
		animOut: 'scrollDown',
		startingSlide: 2,
		speed: 500,
		sync: 1,
		easing: 'jswing',
		timeout: 35000,
		delay: 24000,
		pause: 1,
		next: '#hshow .down',
		prev: '#hshow .up',
		cleartype: true,
		cleartypeNoBg: true,
		pager: '#hnav',
		pagerAnchorBuilder: function(i, slide) { return '#hnav li:eq('+ i +') a'; }
	});
	
	//slideshow nav
	$('#hshow .ar').css('opacity', '0.00').css('display', 'block');
	$('#hshow').bind({
		mouseenter: function() {
		$('#hshow .ar').stop().animate({ opacity: '0.60' }, 500);
		$('#hshow .ar').bind({
				mouseenter: function() {$(this).stop().animate({ opacity: '0.80' }, 500); },
				mouseleave: function() {$(this).stop().animate({ opacity: '0.50' }, 500); },
				mousedown: function() {$(this).stop().animate({ opacity: '1.00' }, 100); }
			});
		},
		mouseleave: function() {
		$('#hshow .ar').stop().animate({ opacity: '0.00' }, 500);
		}
	});
	$('#hnav a, .ar').click( function() {
		$('#hslide').cycle('resume');
	});
	$("#hnav").hover( function () {
		$('#hslide').cycle('pause');
	}, function () {
		$('#hslide').cycle('resume');
	});
	
	// alert
	$('#halert').css('opacity', '0.00').css('display', 'block');
	$('#halert').pause(2000).animate({ opacity: '0.60' }, 500).pause(5000).fadeOut(500);
}


// search
$('#sbar').focus(function() {
	$('#search').css('background-position','0px -1px');
	$(this).css('color','#666666');
	if (this.value == this.defaultValue){ this.value = ''; }
	if(this.value != this.defaultValue){ this.select(); }
});
$('#sbar').blur(function() {
	$('#search').css('background-position','0px -48px');
	$(this).css('color','#527D97');
});

// login and register
$('.lgn').click( function() {
	$('#reg').css('display','none');
	$('#lgn').css('display','block');
	$('#logi span').animate({ top: '-10px' }, 500);
});
$('.reg').click( function() {
	$('#lgn').css('display','none');
	$('#reg').css('display','block');
	$('#logi span').animate({ top: '-10px' }, 500);
});
$('#lup').click( function() {
	$('#logi span').animate({ top: '-210px' }, 500);
});

//note
$('#notecan').cycle({
	fx: 'fade',
	random: 1,
	speed: 500,
	sync:1,
	easing: 'jswing',
	timeout: 10000,
	delay: 5000,
	pause: 1,
	next: '#note .d',
	prev: '#note .u',
	cleartype: true,
	cleartypeNoBg: true
});
$('#note .nar').css('opacity', '0.00').css('display', 'block');
$('#concan').bind({
    mouseenter: function() {
       $('#note .nar').stop().animate({ opacity: '0.30' }, 500);
       $('#note .nar').bind({
            mouseenter: function() {$(this).stop().animate({ opacity: '0.70' }, 500); },
            mouseleave: function() {$(this).stop().animate({ opacity: '0.30' }, 500); },
			mousedown: function() {$(this).stop().animate({ opacity: '1.00' }, 100); }
        });
    },
    mouseleave: function() {
       $('#note .nar').stop().delay(200).animate({ opacity: '0.00' }, 500);
    }
});

// remove br in application form for radios/checkboxes
if ($('#apform').length > 0) {
	$('.nbr br').remove();
}

// splash
if ($('#splash')[0]) {
if (document.referrer.search(/cambridge-intl.com/) == -1) {
$.fancybox(
	$("#splash").html(), 
	{
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayColor' : '#000000',
		'width' : 800,
		'height' : 380,
		'autoDimensions' : false
	}
);
$('.fbc').click( function() {
	$.fancybox.close();
});
}
}

//header slideshow
if ($('#sshow')[0]) {
$('.caption').css('opacity', '0.80');
$('#sshow').cycle({
    fx: 'fade',
    speed: 1000,
    easing: 'easeInOutExpo',
    timeout: 6000,
    delay: 1000,
    pause: 1,
    cleartype: true,
    cleartypeNoBg: true,
    pager: '.page',
    pagerAnchorBuilder: function(index, el) { return '<a href="javascript:void(0);" title="goto slide"></a>'; },
    before: function() {
        $('.caption').animate({height:'0px',paddingTop:'0px',paddingBottom:'0px'}, 100, 'easeInOutExpo');
    },
    after: function() {
        $('.caption').html(this.alt).animate({height:'20',paddingTop:'10px',paddingBottom:'10px'},  500, 'easeInOutExpo');
    }
});
}

});
