//////////////////////////////////////////////////////////////////////
///////JAVASCRIPT HELPERS BY GD#360///////////////////////////////////
//////////////////////////////////////////////////////////////////////
////Code includes:////////////////////////////////////////////////////
////geturlparms///////////////////////////////////////////////////////
////text resize///////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
////fade element on hover and restore/////////////////////////////////
////email regex///////////////////////////////////////////////////////
////animate input/textarea labels/////////////////////////////////////
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
///////////////LIBRARY USAGE//////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
////geturlparms -> objGD360.getUrlParm('value')///////////////////////
////objGD360.textResize(default_size, mid_size, max_size,  target)////
////objGD360.fadeOnHover(elem, onVal, ofVal, speed)///////////////////
////objGD360.SiblingsFadeOnHover(elem, onVal, ofVal, speed)///////////
////objGD360.validateEmail(email)/////////////////////////////////////
////objGD360.inputWrap() -> required for animate Fileds///////////////
////objGD360.animateFields() -> applys to all input&texarea///////////
////objGD360.getDocHeight() -> returns document height////////////////
////objGD360.getDocWidth() -> returns document width//////////////////
////objGD360.setGreyBg(overlay) -> takes div id as parameter//////////
////objGD360.loadAjax('ajax.html', 'ajax') -> takes URL and div ID////
////objGD360.setInitMargin('ajax') -> takes ajax target div id////////
////objGD360.takeDownPopup('grey', 'ajax', 'close', 0) -> takes/////// 
////overlay div ID, ajax div, close div and speed of hide effect//////
////could be easily modified to different effect line 134, 135, 136///
//////////////////////////////////////////////////////////////////////

objGD360 = {
    'vars':{
		'h':null, 'w':null
	},
	//function parse Url
	getUrlParm:function( name ){
		var tmpURL = window.location.href;
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( tmpURL );
			if( results == null )
				return "";
			 else
				return results[1];	
	},
	//function resize text
	textResize:function( default_size, mid_size, max_size,  target ){
		jQuery('#font_select').click(function(){
			jQuery(target).animate({'font-size':default_size + 'px'}, 500);
		});
		
		jQuery('#font_select_mid').click(function(){
			jQuery(target).animate({'font-size':mid_size + 'px'}, 500);
		});
		
		jQuery('#font_select_large').click(function(){
			jQuery(target).animate({'font-size':max_size + 'px'}, 500);
		});
	},
	//fadein effect
	siblingsFadeOnHover:function( target, valOn, valOff, speed ){
		jQuery(target).hover(function(){
			jQuery(this).siblings().animate({'opacity': valOn}, speed);
		}, function(){
			jQuery(this).siblings().animate({'opacity': valOff}, speed);			
		});
	},
	//siblibgs fadein effect
	fadeOnHover:function( target, valOn, valOff, speed ){
		jQuery(target).hover(function(){
			jQuery(this).animate({'opacity': valOn}, speed);
		}, function(){
			jQuery(this).animate({'opacity': valOff}, speed);			
		});
	}
	,
	//email regex
	validateEmail:function(email){
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
		if(emailPattern.test(email)){
			return true;
		}
		return false;	
	},
	//wrap input with span tag
	inputWrap:function(){
		jQuery('input:text, input[type=password], textarea').each(function(){
		    if(jQuery(this).attr('title').length !== 0){
			var value = jQuery(this).attr('title');
			jQuery(this).wrap('<span class="inputWrap" />');
			jQuery(this).parent().prepend('<span class="inptVal">' + value + '</span>');
		   }
		})
	},
	//animate labels
	animateFields:function(){
		jQuery('input:text, textarea').focus( function(){
			jQuery(this).prev('.inptVal').animate({'opacity':0.5}, 300);
		})
		.blur( function(){
		if(! jQuery(this).val()){
			jQuery(this).prev('.inptVal').show('slow').animate({'opacity':1.0}, 300);
		}
	}).keyup( function(){
			jQuery(this).prev('.inptVal').hide('slow');
	})
	},
	//////////////////
	////POPUP CODE////
	//////////////////
	//document height
	getDocHeight:function(){
		return this.vars.h = jQuery(document).height();
	},
	//document width
	getDocWidth:function(){
		return this.vars.w = jQuery(document).width();
	},
	//create Overlay
	setGreyBg:function(bgOverlay){
		jQuery('body').append('<div id=' + bgOverlay + '></div>');
		jQuery('#'+ bgOverlay).css({'z-index':'99999', 'background':'url("https://d8ldk29pm7a3h.cloudfront.net/images/colorbox/example1/images/overlay.png") repeat scroll 0 0 transparent', 'position':'absolute', 'top':'0', 'left':'0', 'width':this.vars.w, 'height': this.vars.h, 'opacity': '0.7'});
	},
	//ajax page load
	loadAjax:function(url, targetDiv){
		jQuery('body').append('<div id=' + targetDiv + '></div>');
		jQuery('#' + targetDiv).css({'z-index':'999999999', 'display':'inline-block', 'position':'fixed'});
		jQuery('#'+ targetDiv).append('<img src="http://www2.guidestar.org/App_Themes/MainSite2/images/loading.gif" />');
		jQuery.ajax({//ajax call to page load
			url: url,
			cache: false,
			dataType: 'html',
			 success: function(data) {
			jQuery('#' + targetDiv).html(data);
			}
		})
	},
	//setting up inital popup position
	setInitMargin:function(targetDiv){
		var targetWidth = jQuery('#' + targetDiv).width();
		var margin = (this.vars.w - targetWidth) / 2;
		jQuery('#' + targetDiv).css({'left':margin});
	},
	//close popup
	takeDownPopup:function(bgOverlay, targetDiv, trigger, speed){
		jQuery(bgOverlay +','+ trigger).live('click', function(){
			jQuery(targetDiv).hide(speed).remove();
			jQuery(bgOverlay).hide(speed).remove();
			jQuery(trigger).hide(speed).remove();
		})
	},
	//on window resize
	windowResize:function(targetDiv, bgOverlay){
		jQuery(window).resize(function(){
			var targetWidth = jQuery('#' + targetDiv).width();
			var w = jQuery(window).width();
			var margin = (w - targetWidth) / 2;
			jQuery('#' + bgOverlay).css('width', w);
			jQuery('#' + targetDiv).css({'left':margin});
		})
    }
}
