$(document).ready(function() {

  $("ul.sf-menu").superfish({
    delay:   100,
    speed:   'fast'
  });

  $('#applicantsPerPage').live('change', function(){
    location.href = '?quantity=' + $(this).val();
  });

  // adds hooks to the first and last list items
  $("ul li:first-child").addClass("first-item");
  $("ul li:last-child").addClass("last-item");
  $("ol li:first-child").addClass("first-item");
  //$("ol li:last-child").addClass("last-item");

  $('#article-content ul li').prepend('<span>&rsaquo;</span> ');
  
  // Application Form: set the Terms and Conditions link to have the href including the scholarship_id
  $("input[name='application[scholarship_id]']").click( function(event) {
    var href = $('#regional-scholarship-tc').attr('href');
    $('#regional-scholarship-tc').attr('href', href.substr(0,href.lastIndexOf('=') + 1) + $(this).val());
  });
  
  function disableForm() {
    $('#application-edit input').attr('readonly', 'readonly').attr('disabled', 'disabled').css({'background-color': "#F0F0F0"});
    $('#application-edit textarea').attr('readonly', 'readonly').attr('disabled', 'disabled').css({'background-color': "#F0F0F0"});
    $('#application-edit select').attr('readonly', 'readonly').attr('disabled', 'disabled');
    $('.btn-update').hide();
    $('#application-info').hide();
    $('#application-info-sent').show();
    $('#btn-application-submit-bottom').hide();
    $('.application-edit .edit-link').hide();
    $('#application-edit dd').css({'color': "#666"});
    $('#application-edit input[rel!=""]').each(function(){
      if($(this).val() == $(this).attr('rel')) {
          $(this).val('');
      }
    });
    $('#application-edit textarea[rel!=""]').each(function(){
      if($(this).val() == $(this).attr('rel')) {
        $(this).val('');
      }
    });
  }

  // remove default content from fields with emptyMe class
  $('.empty-me').each(function() {
    $(this).bind({
      click: function() {
        $(this).val('');
      }
    });
  });

  $(".login-btn").bind("click", function() {

    $.fancybox.showActivity();

    $.ajax({
      type		: "GET",
      cache	  : false,
      //url     : $(this).attr('action'),
      url     : '/guard/login',
      data		: { },
      success: function(data) {
        $.fancybox(data);
      }
    });

    return false;
  });

  $("#shareByEmail").bind("click", function() {

    $.fancybox.showActivity();

    $.ajax({
      type		: "GET",
      cache	  : false,
      //url     : $(this).attr('action'),
      url     : '/contender/shareByEmail',
      data		: { },
      success: function(data) {
        $.fancybox(data);
      }
    });

    return false;
  });

  $(".fancybox").fancybox({
		titlePosition : 'over',
		overlayOpacity : '0.8',
    	overlayColor : '#000'
		});

  $(".youtube-video").click(function() {
    $.fancybox({
        'padding'		: 0,
        'overlayOpacity' : '0.8',
        'overlayColor' : '#000',
        'autoScale'		: false,
        'transitionIn'	: 'none',
        'transitionOut'	: 'none',
        'title'			: this.title,
        'titlePosition' : 'over',
        'width'		: 680,
        'height'		: 495,
        'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
        'type'			: 'swf',
        'swf'			: {
             'wmode'		: 'transparent',
          'allowfullscreen'	: 'true'
        }
      });

    return false;
  });

  $('.error').each(function(){
    $('#application_'+$(this).attr('rel')).addClass('input-error');
  });

/* My Application Form */

  $(".address-postcode-finder").fancybox({
    titlePosition : 'over',
		overlayOpacity : '0.8',
    overlayColor : '#000',
    width: 870,
    height: 425
  });


  $('#application-edit .section').hide();
  $('#application-nav .active').each(function(){
    id = $('#application-nav .active a').attr('href').split('#')[1];
    $('#application-edit #'+id).show();
  });

  $('#application-nav .active-and-checked').each(function(){
    id = $('#application-nav .active-and-checked a').attr('href').split('#')[1];
    $('#application-edit #'+id).show();
  });

  $('#application-nav a').click(function(){      
    $('#application-edit .section').hide();
    $('#application-nav .active').removeClass('active');
    $('#application-nav .active-and-checked').removeClass('active-and-checked').addClass('checked');
    if($(this).parents('li').hasClass('checked')) {
      $(this).parents('li').removeClass('checked').addClass('active-and-checked');
    } else {
      $(this).parents('li').addClass('active');
    }

    id = $(this).attr('href').split('#')[1];
    $('#application-edit #'+id).show();
    $('#lastTab').val('#'+id);
    return false;
  });
	$('.application-edit a').click(function(){
		$('#application-nav a[href=#'+$(this).attr('href').split('#')[1]+']').trigger('click');
		return false;
	});
	
	$('#application-nav a[href='+window.location.hash+']').trigger('click');

  // if the status is not complete, disable the form
  if ($('#application_status').length && ($('#application_status').val() != 'Incomplete')) {
    disableForm();
  }
	

	/* Default text */
    $('#stay-informed input[rel!=""]').each(function(){
      if($(this).val() == $(this).attr('rel')) {
        $(this).css({'color':'#AAA'});
      }
      if($(this).val() == '') {
        $(this).val($(this).attr('rel'));
        $(this).css({'color':'#AAA'});
      }
			$(this).focus(function(){
				$(this).removeAttr('style');
				if($(this).val() == $(this).attr('rel')) {
					$(this).val('');
				}
			});
			$(this).blur(function(){
				if($(this).val() == $(this).attr('rel')) {
					$(this).css({'color':'#AAA'});
				}
				if($(this).val() == '') {
					$(this).val($(this).attr('rel'));
					$(this).css({'color':'#AAA'});
				}
			});
		});
		
		$('.no-btn').live('click', function(){
			$.fancybox.close();
		});

    $('#browser-box input[rel!=""]').each(function(){
      if($(this).val() == $(this).attr('rel')) {
        $(this).css({'color':'#AAA'});
      }
      if($(this).val() == '') {
        $(this).val($(this).attr('rel'));
        $(this).css({'color':'#AAA'});
      }
			$(this).focus(function(){
				$(this).removeAttr('style');
				if($(this).val() == $(this).attr('rel')) {
					$(this).val('');
				}
			});
			$(this).blur(function(){
				if($(this).val() == $(this).attr('rel')) {
					$(this).css({'color':'#AAA'});
				}
				if($(this).val() == '') {
					$(this).val($(this).attr('rel'));
					$(this).css({'color':'#AAA'});
				}
			});
		});


		$('#application-edit input[rel!=""]').each(function(){
      if($(this).val() == $(this).attr('rel')) {
        $(this).css({'color':'#AAA'});
      }
      if($(this).val() == '') {
        $(this).val($(this).attr('rel'));
        $(this).css({'color':'#AAA'});
      }
			$(this).focus(function(){
				$(this).removeAttr('style');
				if($(this).val() == $(this).attr('rel')) {
					$(this).val('');
				}
			});
			$(this).blur(function(){
				if($(this).val() == $(this).attr('rel')) {
					$(this).css({'color':'#AAA'});
				}
				if($(this).val() == '') {
					$(this).val($(this).attr('rel'));
					$(this).css({'color':'#AAA'});
				}
			});
		});

		$('#application-edit textarea[rel!=""]').each(function(){
			if($(this).val() == $(this).attr('rel')) {
        $(this).css({'color':'#AAA'});
      }
      if($(this).val() == '') {
        $(this).val($(this).attr('rel'));
        $(this).css({'color':'#AAA'});
      }
			$(this).focus(function(){
				$(this).removeAttr('style');
				if($(this).val() == $(this).attr('rel')) {
					$(this).val('');
				}
			});
			$(this).blur(function(){
				if($(this).val() == $(this).attr('rel')) {
					$(this).css({'color':'#AAA'});
				}
				if($(this).val() == '') {
					$(this).val($(this).attr('rel'));
					$(this).css({'color':'#AAA'});
				}
			});
		});

		/* Counter */
			$('.countChar30').counter({
				goal: 30
			});
                        $('.countChar50').counter({
				goal: 50
			});
			$('.countChar60').counter({
				goal: 60
			});
			$('.countChar80').counter({
				goal: 80
			});
                        $('.countChar100').counter({
				goal: 100
			});
			$('.countChar150').counter({
				goal: 150
			});
                        $('.countChar200').counter({
				goal: 200
			});
                        $('.countChar250').counter({
				goal: 250
			});

			$('.countWord25').counter({
				type: 'word',
				goal: 25
			});
			$('.countWord100').counter({
				type: 'word',
				goal: 100
			});
			$('.countWord200').counter({
				type: 'word',
				goal: 200
			});
			$('.countWord250').counter({
				type: 'word',
				goal: 250
			});

		
	/* Area changing */
    $('.changeArea').each(function(){
			$('ul[rel='+$(this).attr('rel')+'] > li').hide();
			$('ul[rel='+$(this).attr('rel')+'] > li[rel='+$(this).val()+']').show();
			$(this).change(function(){
				$('ul[rel='+$(this).attr('rel')+'] > li').hide();
				$('ul[rel='+$(this).attr('rel')+'] > li[rel='+$(this).val()+']').show();
			})
		});

    /* AMP Employee field */
    function showHideAMPEmployeeField() {
      if ($('#application_is_employee_1').is(':checked')) {
        $('#application_amp_employee').fadeIn();
      } else {
        $('#application_amp_employee').fadeOut();
      }
    }
    // Check on load
    showHideAMPEmployeeField();
    // Check on click
    $('#application_is_employee_1').click(function(){
      showHideAMPEmployeeField()
    });
    $('#application_is_employee_0').click(function(){
      showHideAMPEmployeeField()
    });

    $('#browser-box').submit(function(){
        if($('#browser-box #application_filters_first_name').val() == $('#browser-box #application_filters_first_name').attr('rel')){
            $('#browser-box #application_filters_first_name').val('');
        }
        if($('#browser-box #application_filters_last_name').val() == $('#browser-box #application_filters_last_name').attr('rel')){
            $('#browser-box #application_filters_last_name').val('');
        }
    });


	/* Submit application */
	$("#btn-application-submit-top, #btn-application-submit-bottom").fancybox({
          titlePosition : 'over',
          overlayOpacity : '0.8',
          overlayColor : '#000'
	});
	$("#submit_application").fancybox({
          titlePosition : 'over',
          overlayOpacity : '0.8',
          overlayColor : '#000'
	});
	$("#submit_application").click(function(){

          // Submit the form through AJAX
          $.ajax({
            type: "POST",
            cache: false,
            url: '/contender/submit',
            async: false,
            success: function(data) {
              // disabling form
              $('form.application-browser-box').hide();
              disableForm();
            }
          });
          return false;

        });
		$("#back_application").click(function(){
			$.fancybox.close();
			return false;
		});
		
  $(".voteApplicant").bind("click", function() {

    $.fancybox.showActivity();
    
    //console.log($(this).attr('rel'));

    $.ajax({
      type:  "GET",
      cache: false,
      url:   '/vote/' + $(this).attr('rel'),
      data:  { },
      success: function(data) {
        $.fancybox(data);
      }
    });
		
    return false;
			
  });

		
		
    if($.browser.msie && $.browser.version == 6){
      	//internet explorer 6 doesn't get cufon
		$('#header #navigation .subnav').hover(function(){
			$('ul', this).show();
			$(this).find('a:eq(0)').css({'background': 'url(../images/new-header-sprite.gif) no-repeat 0 -377px'});
		}, function(){
			$('ul', this).hide();
			$(this).find('a:eq(0)').css({'background': 'none'});
		});
	}


});

$(function() {
  var regionId         = $("select[name='application[region_id]'] option:selected");
  var regionNone       = $("#region-none");
  var regionSet        = $("#region-set");
  
  var switchRegional = function(rId) {
    setDisabledForRegionalRadios(false);
    if (!rId) {
      regionSet.hide();
      regionNone.show();
      setDisabledForRegionalRadios(true);
    } else {
      $.ajax({
        type:  "GET",
        cache: false,
        url:   '/contender/getRegionalScholarships',
        data:  { region_id: rId },
        success: function(data) {
          regionSet.html(data);
          //console.log($(data));
        }
      });
      regionSet.show();
      regionNone.hide();
    }
  }
  
  switchRegional(regionId.val());
  
  $('#application_region_id').change(function()  {
    var regionId   = $(this).find("option:selected").val();
    switchRegional(regionId);
  });
});

function setDisabledForRegionalRadios(status) {
  var regionalRadio = $("input[name='application[is_regional_application]']:radio");
  regionalRadio.attr('disabled',status);
  
  if (status === true) {
    $("#application_is_regional_application_0").attr('checked', 'checked');
  } else if (status === false)  {
    $("#application_is_regional_application_1").attr('checked', 'checked');
  }
}

jQuery.each(jQuery.browser, function(i) {
   if(jQuery.browser.msie && jQuery.browser.version == 6){
      	//internet explorer 6 doesn't get cufon
   } else {
      	Cufon.replace('h1, .region-header, #content-right h3, #home-container h3');
	Cufon.replace('#application-status-box h3, .application-browser-box h3');
        Cufon.replace('#header #navigation li.paint-stripe a');
        Cufon.replace('#article-content-bottom h3');
        Cufon.replace('.region-block .more-info a');
   }
 });

jQuery.fn.switchClass = function(class1,class2) {
  if (this.hasClass(class1)) {
    this.removeClass(class1).addClass(class2);
  } else {
    this.removeClass(class2).addClass(class1);
  }
};

// swfobject
swfobject.registerObject("myId", "9.0.0", "_assets/_swf/expressInstall.swf");

