(function($){
	$.fn.elementlocation = function() {
		var curleft = 0;
		var curtop = 0;
		
		var obj = this;
		
		do {
			curleft += obj.attr('offsetLeft');
			curtop += obj.attr('offsetTop');
			
			obj = obj.offsetParent();
		} while ( obj.attr('tagName') != 'BODY' );
		
		return ( {x:curleft, y:curtop} );
	};
		
	$(document).ready(function(){
    
        // Bind empty function on click - it appears to hide the drop-down menu on iPad
        $('div').bind('click', function(e){})
    
		
		// Bind empty function on click - it appears to hide the drop-down menu on iPad
        $('div').bind('click', function(e){})
        
		$('.fader .item:first').show();
		
		$('#navigation ul > li > ul').wrap('<div class="dd" />').css('display', 'block');
		$('#navigation ul > li > .dd > ul').each(function(){
			$(this).find(' > li:last').addClass('last');
		})
		
		$('#navigation ul > li > .dd > ul > li > .dd').each(function(){
			$(this).find('ul.sub-menu').prepend('<div class="dd-top" />');
			$(this).find('ul.sub-menu').append('<div class="cl">&nbsp;</div><div class="dd-bottom" />');
			if ( $(this).find('ul.sub-menu > li:first').hasClass('last') ) {
				$(this).find('ul.sub-menu > li:first').addClass('first-last');
			} else {
				$(this).find('ul.sub-menu > li:first').addClass('first');
			}
		})
		if($.browser.msie && $.browser.version.substr(0,1) == 6) {
			DD_belatedPNG.fix('.dd-top, .dd-bottom, .dd .last');
		}
		
		$('#navigation > ul > li ul, #navigation > ul > li .dd').css('visibility', 'hidden').css('display', 'block');
		
		$('#navigation > ul > li > .dd > ul > li > .dd > ul > li.last').each(function(){
			var height = $(this).find('a').height();
			if ( height != '' && height != undefined && height != 0 ) {
				var max_height = height - 5;
				max_height = max_height + 'px';
				$(this).find('a').css('max-height', max_height );
			} else{
				$(this).find('a').css('display', 'block');
				height = $(this).find('a').outerHeight();
				if ( height != '' && height != undefined && height != 0 ) {
					var max_height = height - 5;
					max_height = max_height + 'px';
					$(this).find('a').css('max-height', max_height );
				}
			};
		})
		
		$('#navigation ul li ul li').each(function(){
			var height = $(this).height();
			if ( height >= 42 ) {
				$(this).find('a').css('line-height', '22px' );
				if ( !( $.browser.msie && $.browser.version.substr(0,1) == 6 ) ) {
					$(this).find('a').css('margin-top', '9px' );
				} else {
					if( ! ($(this).is('.last')) ) {
						$(this).find('a').css('margin-top', '9px' );
					}
				};
			};
		})
		
		$('#navigation > ul > li ul, #navigation > ul > li .dd').css('visibility', 'visible').css('display', 'none');
		
		$('p').each(function(){
			if ( !$(this).has('img').length && !$(this).has('input').length ) {
				$(this).filter(function(){
					return $.trim($(this).text()) === '';
				}).remove()
			}
		});
		
		$('#footer ul > li > ul > li:last').addClass('last');
		
		$(document).find('#gform_1 .ginput_container textarea').each(function () {
			var val = $(this).val();
			if ( val != '' ) {
				$(this).parent().parent().find('label').after('<small><em>' + val + '</em></small>');
				$(this).val('');
			};
		})
		
		$('div.video').hover(
			function(){
				$(this).find('.videoPopup').stop();
				$(this).find('.videoPopup').animate({opacity: 0.5}, 500)
		}, function() {
				$(this).find('.videoPopup').stop();
				$(this).find('.videoPopup').animate({opacity: 1}, 500)
		})
		$('.videoPopup').colorbox({iframe:true, innerWidth:640, innerHeight:390});
		
		$('.widecolumn .gfield input, .widecolumn .gfield textarea')
			.focus(function(){
				var is_captcha = ( $(this).attr('id') == 'recaptcha_challenge_field' || $(this).attr('id') == 'recaptcha_response_field' );
				if ( is_captcha == false ) {
					$(this).parent().parent().find('label').hide();
				};
			}).blur(function(){
				if ( $(this).val() == '' ) {
					$(this).parent().parent().find('label').show();
				};
			})
		
		tooltip();
		
	    // Drop Down
	    $('#navigation ul li').hover(function(){
	    	$(this).find('.dd').eq(0).css('display', 'block');
	    	$(this).find('.dd .sub-menu').css('display', 'block');
	    }, function(){
	    	$(this).find('.dd').eq(0).css('display', 'none');
	    	$(this).find('.dd .sub-menu').eq(0).css('display', 'none');
	    });
	    
		// Apply PNG Fix for Internet Explorer 6
		if($.browser.msie && $.browser.version.substr(0,1) == 6) {
			DD_belatedPNG.fix('h1#logo a, #search .submit, .fader .read-more-btn, .ribon');
		}
		
		
		// Sidebar Box Textarea Focus
		$('textarea, input').live('focus', function(){ 
			$(this).parent().addClass('textarea-focus');
		 }).blur(function(){ 
		 	$(this).parent().removeClass('textarea-focus');
		})
		
		if ( $('.sidebar .gform_wrapper').length != 0 ) {
			$('.sidebar .gform_wrapper .gform_fields li').each(function(){
				if ( $(this).find('.gfield_required').length != 0 ) {
					$(this).addClass('required');
				};
			})
		};
		
		if ( $('.post .entry .gform_wrapper').length != 0 ) {
			$('.post .entry .gform_wrapper .gform_fields li').each(function(){
				if ( $(this).find('.gfield_required').length != 0 ) {
					$(this).addClass('required');
				};
			})
		};
		
		if ( $('.widecolumn #gform_2 .gfield label').length != 0 ) {
			$('.widecolumn #gform_2 .gfield').each(function(){
				var title = $(this).find('label').text();
				var val = $(this).find('input').val();
				
				$(this).find('label').css('position', 'absolute').css('left', '18px').css('top', '10px').css('width', '100px').css('margin', '0');
				if ( title != undefined && $(this).find('#recaptcha_widget_div').length == 0 ) {
					$(this).find('input').attr('title', title);
				};
				if ( val != undefined && val != '' && $(this).find('#recaptcha_widget_div').length == 0 ) {
					$(this).find('label').hide();
				};
			})
		};
		
		if ( $('.gform_wrapper form').length != 0 ) {
			$('.gform_wrapper input, .gform_wrapper textarea').each(function(){
				var val = $(this).val();
				var title = $(this).attr('title');
				var is_captcha = ( $(this).attr('id') == 'recaptcha_challenge_field' || $(this).attr('id') == 'recaptcha_response_field' );
				if ( val != '' && title == '' && is_captcha == false ) {
					$(this).addClass('blink');
					$(this).attr('title', val);
				};
			})
		};
	})
	
	$(window).load(function(){
		//Blinking Fields
	    $('.blink').
	        focus(function() {
	            if(this.title==this.value) {
	                this.value = '';
	            }
	        }).
	        blur(function(){
	            if(this.value=='') {
	                this.value = this.title;
	            }
	        });
	})
	
	this.tooltip = function(){	
		/* CONFIG */		
			xOffset = -25;
			yOffset = -30;		
			// these 2 variable determine popup's distance from the cursor
			// you might want to adjust to get the right result		
		/* END CONFIG */		
		$("a.tooltip").hover(function(e){											  
			this.t = this.title;
			this.title = "";									  
			$("body").append("<span id='tooltip'>"+ this.t +"</span>");
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");		
	    },
		function(){
			this.title = this.t;		
			$("#tooltip").remove();
	    });	
		$("a.tooltip").mousemove(function(e){
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});			
	};
})(jQuery)
