// raw javascript

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
function set_form_text_field_handlers() {
  jQuery('form input[name="zip"]').focus(function() { focus_form_text_field(jQuery(this)) });
  jQuery('form input[name="zip"]').blur( function() {  blur_form_text_field(jQuery(this)) });
  jQuery('form[name="applynow"]').submit( function() { jQuery('form input[name="zip"]').each( function() { blur_form_text_field(jQuery(this)) }); return true; });
}
function focus_form_text_field(field_selector, default_text) {
  if (!default_text) {
    default_text = 'Enter Your Postal Code';
  }
  var field = field_selector[0];
  if (undefined != field && default_text == field.value) {
    field.value = '';
  }
}
function blur_form_text_field(field_selector, default_text) {
  if (!default_text) {
    default_text = 'Enter Your Postal Code';
  }
  var field = field_selector[0];
  if (undefined != field && '' == field.value) {
    field.value = default_text;
  } else {
    field.value = field.value.toUpperCase();
    if (6 == field.value.length) {
      field.value = field.value.substring(0,3) + ' ' + field.value.substring(3,6);
    }
  }
}

// jquery

(function($) {
	$(function() {
	
		$('#apply_now').submit(function (){
			if ( $('#zip2').val() == '' ) { alert( 'You must enter a value.' ); return false; }  
		});
			
		// tips 
		
		$('.tip').tooltip({ effect: 'slide'});
		
		// on zip blur
		
		$('#zip2, #zip').focus(function(){
			$(this).val('');
		});
		
		function checkempty( form ) {
			var val = form.find('input[name=zip]').val();
			if (val == '' || val == 'Enter Your Postal Code' ) {
	  			alert('You must enter a value');
	  			return false;
	  		} else {
	  			return true;
	  		}
		
		}
		$('form[name=applynow]').submit( function() {
			var cform = $(this);
			return checkempty( cform );
		});
		
		$('#apply_btn, #apply_btn_small').click(function () {
			cform = $(this).parent();
			return checkempty( cform ); 
		});
			
		//external links 
		$('.external').attr('target', '_blank');
		
		// nav 
		$('.page-item-1015 a:first, .page-item-11 a:first, .page-item-7 a:first').click(function() { return false; });

		// home tabs
		$('.tabs').tabs("div#home_panes > div");	
		
		// sidebar tabs
		$("#sidebar").tabs("#sidebar .widget", {tabs: '.side_tabs li', effect: 'fade', initialIndex: 0 });
		
		$('.cats_wg li:even, .archives_wg li:even').css('background-color' , '#f4f4f4' );
		
		$('ul.tweets li:even').css('background-color', '#f4f4f4');
		
		$('.fade, .ss_img img').css('opacity', 0.85).hover(function() {  
					
			if ( $.support.opacity ) 
			{
				$(this).stop().animate({'opacity': 1}, 300);
			} 
		}, function() { 
		
			if ( $.support.opacity ) 
			{
				$(this).stop().animate({'opacity': 0.85}, 300);
			} 
		});
				
		// popup
		
		$('.poll_overlay').overlay({			
			top: 'center',
			mask: {
				color: '#EBEBEB',
				loadSpeed: 200,
				opacity: 0.5
			}
		});
		
		$('.overlay').overlay({			
			top: 'center',
			mask: {
				color: '#EBEBEB',
				loadSpeed: 200,
				opacity: 0.5
			}
		});
		
		$('.home_vid_trigger').overlay({			
			top		: 'center',
			mask: {
				color: '#EBEBEB',
				loadSpeed: 200,
				opacity: 0.5
			}
		});
		
		$('.team_img').click(function() {
			$('.overlay', this).click();
			return false;
		});
		
		$('.ss_img').hover(function() { 
			$('.tag, .watch_vid', this).css({'display' : 'inline-block', 'opacity' : 0 }).stop().animate({'opacity' : 0.9},500);
		}, function() {
			$('.tag, .watch_vid', this).stop().animate({'opacity' : 0},500, function () { $(this).css('display', 'none'); });
		});
		
		// got questions accordion
		
		$("#got_questions").tabs("#got_questions div.answer", {tabs: 'h3', effect: 'slide', initialIndex: 0});
		
		$(window).resize(function (){
		
			//console.log(  );
			
			var wW = $(window).width();
			if ( wW < 1150 ) {
				$('#rs_menu').stop().animate({ 'padding-right' : (1100 - wW) + 'px' }, 100);
			} else {
				$('#rs_menu').stop().animate({ 'padding-right' : '1%' }, 100);
			}
						
		});
		
		var wW = $(window).width();
		if ( wW < 1150 ) {
			$('#rs_menu').stop().animate({ 'padding-right' : (1100 - wW) + 'px' }, 100);
		} else {
			$('#rs_menu').stop().animate({ 'padding-right' : '1%' }, 100);
		}

		
		
	}); // end $
	
})(jQuery); // end function($)


/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);
        if (value === null || value === undefined) {
            options.expires = -1;
        }
        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        value = String(value);
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : cookie_encode(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }
    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
function cookie_encode(string){
  //full uri decode not only to encode ",; =" but to save uicode charaters
  var decoded = encodeURIComponent(string);
  //encod back common and allowed charaters {}:"#[] to save space and make the cookies more human readable
  var ns = decoded.replace(/(%7B|%7D|%3A|%22|%23|%5B|%5D)/g,function(charater){return decodeURIComponent(charater);});
  return ns;
}
/* END jQuery Cookie plugin */
function valid_switch_token(token) {
  if ('string' != typeof(token)) return false;
  if ('' == token) return false;
  return (null != token.match(/^vc/));
}
function get_switch_token() {
  var token = window.location.hash;
  if ('' != token) {
    token = token.replace(/^#/, '');
    if (!valid_switch_token(token)) {
      token = '';
    }
  }
  if ('' == token) {
    token = jQuery.cookie('switch');
  }
  return token;
}
function log_switch_token() {
  var token = get_switch_token();
  if (token != jQuery.cookie('switch') && valid_switch_token(token)) {
    var i = new Image;
    var d = new Date;
    i.src = '/wp-content/themes/vm/track_switch.php?switch=' + token + '&' + d.getTime();
    jQuery.cookie('switch', token);
  }
}
function set_hidden_switch() {
  var token = get_switch_token();
  if (!valid_switch_token(token)) {
    token = 'vc';
  }
  jQuery('form input[name="switch"]').each( function() { jQuery(this).attr('value', token) });
}
