$(document).ready(function(){

$(function() {
    if ($('.switcher').length) {
        for (var i = 2, l = $('.panel').length; i <= l; i++) {
            $('.switcher').append($('<li><a href="#">' + i + '</a></li>'));
        }
        $('.switcher').data('i', 1);

        $('.switcher').click(function(e) {
            e.preventDefault();
            if (!$(e.target).is('a') || $(this).data('i') == $(e.target).text()) {
                return;
            }
            $(this).data('i', $(e.target).text());
            $('.switcher .active').removeClass('active');
            $(e.target).parent('li').addClass('active');

            $('.panel:not(.hide)').fadeOut(400, function() {
                $('.panel').addClass('hide');
                $('.panel' + $(e.target).text()).fadeIn().removeClass('hide');
            });
        });

        setInterval(function() {
            if (window.doRotate) {
                var next = parseInt($('.switcher').data('i'), 10);
                next += 1;
                if (next > $('.switcher li').length) {
                    next = 1;
                }
                $(".switcher a:contains('" + next + "')").click();
            }
        }, 5000);

        window.doRotate = true;
        $('.gallery-holder').hover(function() {
            window.doRotate = false;
        }, function() {
            window.doRotate = true;
        });
    }

	$('.a-target-blank a').attr('target', '_blank');

	// Clear input fields on focus:
	$('input.js_clear').focus(function() {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	})

	// Restore input fields on blur:
	.blur(function() {
		if (this.value == '') {
			this.value = this.defaultValue;
		}
	});

  $('.placeholder').focus(function() {
    if ($(this).attr('title')) {
      $(this).data('placeholder', $(this).attr('title'));
      $(this).attr('title', '');
    }

    if ($(this).data('placeholder') === this.value) {
    	this.value = '';
    }
  }).blur(function() {
    if (this.value === '') {
      this.value = $(this).data('placeholder');
    }
  });

	// Tab functionality for log-in panel
	$('#login-tabs a').click(function(e) {
		e.preventDefault();
		if ($(this).attr('id') === 'jobSeekerTab') {
			var userType = 'jobseeker';
		} else {
			var userType = 'employer';
		}
		wookie('user', userType, { expires: 999, path: '/' });

		if (!$(this).hasClass('active')) {
			wookie('activeTab', $(this).attr('id'), { expires: 999, path: '/' });
			$('#login-tabs a, #login-tabs li').removeClass('active');
			$(this).addClass('active').parent('li').addClass('active');
		}
	});

  var activeTab = wookie('activeTab');
  if (activeTab) {
    var ele = $('#' + activeTab);
    if (ele.length) {
    	ele.trigger('click');
    }
  }

	// Change search radius with drop-down menu and submit

	$("#radius_dropdown").change(function() {
		var r = $(this).val();
		$("#r").val(r);
		$("#jobSearch").submit();
	});

	// Scroll the calendar up and down with the scroll buttons

	var cal_offset = 0;

	$("#jobfair_cal .up").click(function() {
		if (cal_offset > 0) {
			cal_offset -= 270;
			$("#scroll_calendar").animate({'margin-top':'-'+cal_offset+'px'});
		}
	})

	$("#jobfair_cal .down").click(function() {
		if (cal_offset < $("#scroll_calendar table").outerHeight()-300) {
			cal_offset += 270;
			$("#scroll_calendar").animate({'margin-top':'-'+cal_offset+'px'});
		}
	});

  // Rotate the hirenow section pics on page reload
  if ($('.rotate1')) {
    var images = ['image2.png', 'hirenow1.png', 'hirenow2.png', 'hirenow3.png'];
    $('.rotate1').attr('src', '/images/template/' + images[Math.floor(Math.random() * images.length)]);
  }

  // Rotate the gethired section pics on page reload
  if ($('.rotate2')) {
    var images = ['image3.png', 'gethired1.png', 'gethired2.png', 'gethired3.png'];
    $('.rotate2').attr('src', '/images/template/' + images[Math.floor(Math.random() * images.length)]);
  }
});

}); // end of doc.ready

