$(document).ready(function(){

	/* login */
	$('#login').click(function(e){
		e.preventDefault();
		if($(this).hasClass('active')){
			$(this).removeClass('active');
			$('#login-box').css({'right':'-999999em'});
			$('.header').css({'overflow':'hidden'});
		}else{
			$(this).addClass('active');
			$('#login-box').css({'right':'157px'});
			$('.header').css({'overflow':'visible'});
		}
	});

	/* home scroller */
	var $scroller_a = $('#scroller-menu ul li a');
	//$scroller_a.click(function(event){event.preventDefault();});
	$('#scroller-panes .hidden').hide();
	$('#scroller-menu ul li:first-child a').addClass('selected');

	//if hovered, show related content
	$scroller_a.hover(function(){
		$scroller_a.removeClass('selected');
		$(this).addClass('selected');

		var url = $(this).attr('rel');
		
		$('#scroller-panes ul li:visible').hide();
		$(url).show();
	}, function(){
		//do fady thing here maybe?
		//or not
	});


	/* social tabs */
	var $social_a = $('#social-tabs ul li a');
	$social_a.click(function(event){event.preventDefault();});
	$social_a.hover(function(){
		$('#social-window ul li.hidden').hide();

		$social_a.removeClass('active');
		$(this).addClass('active');

		var url = $(this).attr('href');

		$('#social-window ul li:visible').hide();
		$(url).show();
	});
	
	/* Join open thing */

	//join hover animation
	$('#join-pull').hover(function(){
		$(this).animate({'padding-top':'42px'}, {duration: 300, queue: false, easing: 'swing'});
	}, function(){
		$(this).animate({'padding-top':'22px'}, {duration: 300, queue: false, easing: 'swing'});
	});
	//join click toggle
	$('#join-pull').click(function(e){
		e.preventDefault();
		
		if($(this).hasClass('open')){
			$(this).removeClass('open');
			$('#inner-join-bar').fadeOut('1000', function(){
				$('#join-bar').animate(
				{
					height: '0'
				},
				{
					duration: 800,
					queue: false
				});
			});
		}else{
			$(this).addClass('open');
			$('#join-bar').animate({
				'height': '23em'
			},
			{
				duration: 800,
				queue: false,
				complete: function() {
					$('#inner-join-bar').fadeIn('1000');
				}
			});
		}
	});
	
	
	/* date picker */
	$('.datepicker').datepicker({ changeYear: true, changeMonth: true,  yearRange: '1990:2020', dateFormat: 'dd/mm/yy'});
		
	/* closing thing */
	var $h = $('.to-close').height()+'px';
	
	$('.has-close .close').click(function(){
		var ths = $(this).parent('.has-close');
		
		if($('.to-close', ths).children().is(':hidden')){
			$('.to-close', ths).animate({
				'height' : $h
			},{
				duration: 1000,
				queue: false,
				complete: function(){
					$('.to-close', ths).children().fadeIn();
				}
			});
		}else{
			$('.to-close', ths).animate({
				'height' : '0px'
			},{
				duration: 1000,
				queue: false,
				complete: function(){
					$('.to-close', ths).children().hide();
				}
			});
		}
	});
	
	/* Focus all forms with the parent hover style */
	$('input, textarea').focus(function(){
		$(this).parent('li').addClass('focussed');
	});
	
	$('input, textarea').blur(function(){
		$(this).parent('li').removeClass('focussed');
	});
	
	
	/*basic tips*/
	$('.tip').prepend('<div class="nub"></div>');
	$('.tip').not('.fixed').hide();
	$('input, textarea').focus(function(){
		var p = $(this).parent();
		var ph = p.height()+10;
		ph + 'px';
		
		$('.tip', p).not('.form-inline-tip').css({'margin-top':-ph});
		$('.form-inline-tip', p).css({'margin-top':'-10px'});
		
		$('.tip', p).fadeIn();
	});
	$('input, textarea').blur(function(){
		$('.tip').fadeOut();
	});
		
	/*big tips*/
	//tools overlay
	$('.big-tip').hide();
	$('.more-tip[rel], .account-more-tip[rel]').overlay({
		left: '25%',
		top: '10%',
		mask: {
			color: '#eee',
			loadSpeed: 200,
			opacity: 0
		}
	});
	
	
	//this REALLY needs fixing
	// $('.big-tip-close').click(function(e){
	//     var target = e.originalTarget || e.srcElement;
	//     $(target).parent().getOverlay().close();
	// $('#someId').data('overlay').close();
	// });
	
	// $('.big-tip-close').click(function(e){
	// 	e.preventDefault();
	// 	$(this).data('overlay').close();
	// });
	
	

	//Field Clearing
	$('.input-clear').live('click', function () {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});
	$('.input-clear').live('blur', function () {
		if (this.value === '') {
			this.value = this.defaultValue;
		}
	});

	//Search Field Clearing
	$('.search-input').live('click', function () {
		if (this.value == 'Search') {
			this.value = '';
		}
	}).live('blur', function () {
		if (this.value === '') {
			this.value = 'Search';
		}
	});
	
	/* Gallery initialisation */
	
	$("a[rel=gallery]").fancybox({
		'titlePosition'	: 'inside'
	});
	

	/* Tiny MCE init */
		
	$('textarea.tiny').tinymce({
		// Location of TinyMCE script
		script_url : base_url + 'assets/html/js/tiny_mce/tiny_mce.js',

		// General options
		theme : "advanced",
		skin : "scd",
	  	theme_advanced_toolbar_location : "top",
		theme_advanced_buttons1 : "bold,italic,underline,|,bullist,numlist,|,cleanup",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : ""
	});
	
	
	/* submit Job form */
	
	$('#submit-job-form').validate();
});

