/************************************************

	/includes/js/dsGlobal.js

	Copyright � DailyStrength, 2008.

	Use this file for truly global js that has
	a decent chance of being needed on any
	given page

***/
(function($) {

/* plugins */
$.sendAnalyticsEvent = function(url) {
	_trackPageView(url);
}

$.fn.hoverClass = function(c) {
	return $(this).hover(
		function() { $(this).addClass(c); $(this).siblings().removeClass(c); },
		function() { $(this).removeClass(c); }
	);
};

$.fn.hoverClick = function(sel) {
	return $(this).hoverClass('hover').click(function(e) {
		var $a = $(this).find(sel);
		var href = false;

		if ($a.attr('href')) {
			href = $a.attr('href');
		} else if ($(this).find('a:first').attr('href')) {
			href = $(this).find('a:first').attr('href');
		} 

		if (href) {
			if (e.ctrlKey) {
				window.open(href,'_tab');
			} else {
				window.location = href;
			}
		}
	});
};

$.fn.focusClass = function(c) {
	var $this = $(this);
	$this.focus(function() { addClass(c); });
	$this.blur(function() { removeClass(c); });
	return $this;
}
	
$.ds_so_rpc = function(settings) {
	$.ajax({
		'url' : '/_ext/RPC',
		'method' : 'POST',
		'data' : {
			'object_class' : 	settings.object_class,
			'object_id' :		settings.object_id,
			'prop' : 		settings.prop,
			'method' : 		settings.method,
			'args' :		settings.args
		},
		'success' : settings.callback
	});
};

$.fn.getDataFromClass = function() {
	return $(this).each(function() {
		var $this = $(this);
		if ($this.attr('class').match('_')) {
			var classes = $this.attr('class').split(' ');
			var keys = [ 'keys' ];
			$.each(classes, function(i,c) {
				if (c.match('_')) {
					var parts = c.split('_');
					var prefix = parts[0];
					keys.push(prefix);
					var data;
					if (parts[1].match('-')) {
						data = parts[1].split('-');
					} else {
						data = parts[1];
					}
					$this.data(prefix,data);
				}
			});
			$this.data('keys',keys);
		}
	});
};

/* 
 * Auto Expanding Text Area
 * by Chrys Bader (www.chrysbader.com)
 *  modified by jake@hybridstudio.com
 */

$.expandingTA = function(elems) 
{
    // turn each element into an expanding text area
    elems.each( function()  
    {       
        var _interval = null;
        var _dummy = null;
        var _self = this;
        var _prevHeight = 0;
        var _min_height;

        // magically expand the textarea
        $(_self).bind('focus', __checkExpand)
            .bind('blur', __stopExpand)
            .css('overflow', 'hidden');

        function __checkExpand()
        {
            _min_height = $(_self).height();

            _interval = setInterval(function() { 
                __expandUpdate(); 
            }, 150);
        }

        function __stopExpand()
        {
            clearInterval(_interval);   
        }

        function __expandUpdate()
        {
            var line_height = 16;   // 16px assumed

            if ( _dummy == null ) { // create dummy

                _dummy = $('<div></div>')
                _dummy.css( {
                                'font-size':    $(_self).css('font-size'),
                                'font-family':  $(_self).css('font-family'),
                                'width':        $(_self).css('width'),
                                'padding':      $(_self).css('padding'),
                                'xline-height':  line_height,
                                'overflow-x':   'hidden',
                                'display':      'none'
                            }).appendTo('body');
            }

            // update html in dummy div
            var newHtml = $(_self).val().replace(/\n/g, '<br>new');

            if( _dummy.html() != newHtml ) {

                _dummy.html( newHtml );
                var newHeight = Math.max(_min_height, _dummy.height() + line_height);

                if (_prevHeight != newHeight) {

                    //$(_self).height(newHeight);
                    $(_self).animate({height:(newHeight)}, 100);    // animate?

                    _prevHeight = newHeight;
                }
            }
        }
    });
}




/* things that should be done on document ready on all pages */
$().ready(function() {

	/* rounded corners */
	$('h1.lid,h2.lid,div.module div.module_title,div#page_title').
		not('.nocorner').corner("top 5px keep");
	$('div.box, div.module div.module_content').
		not('.nocorner').corner("bottom 5px keep");
	$('ul.tabs li').corner("top 5px");

	/* module content subnav tabs */
	$('div.module_content ul.subnav li').
		corner('8px top').
		not('li.current').
		hoverClick('a:first');

	/* submit/cancel buttons */
	$('input.submit, input.input_submit, input.reset').
		hoverClass('hover');

	$('a.chat_link').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		var $this = $(this);
		var href = $this.attr('href');
		var id = href.split('=')[1];
		window.open(href,id,'width=500,height=570');
	});

});

})(jQuery);

/* * * *
* Function used in Omniture JS to determin aid/cid from URL or cookie and set cookie accordingly
* * * */
/* Handle campaign tracking IDs */
function getCampaignId( cookieDomain )
{
        var campId = "";
        var setCookieName = "hswi_set_dscid";
        var campCookieName = "hswi_aid_cid";
        var campCookieDomain = cookieDomain;
        var campUrlVarKey = (window.location.search.search("dsaid") > -1 )? "dsaid" : (window.location.search.search("dscid") > -1 )? "dscid" : null;
        if (campUrlVarKey != null) // if 'aid' or 'cid' is found as the key to a URL variable
        {
                // get the value of that url variable
                queryString = window.location.search.substr(1,window.location.search.length).split("&");
                for (var i=0;i<queryString.length; i++)
                {
                        if (escape(unescape(queryString[i].split("=")[0])) == campUrlVarKey)
                        {
                                campId = queryString[i].split("=")[1];
                                // set a session cookie with the found cid/aid
                                var today = today=new Date();
                                var expires_date = new Date(today.getTime() + (30 * 1000 * 60 * 60 * 24));
                                document.cookie = campCookieName + "=" + escape(campId) + ";domain=" + cookieDomain + ";";
                                if (typeof(getCampaignIdOnce.writeSetCookie) == "undefined")
								{
									document.cookie = setCookieName + "=1;expires=" + expires_date.toGMTString() + ";domain=" + cookieDomain + ";path=/";
								}
                        }
                        
                }
        } else {
                // if neither 'aid' nor 'cid' is found as key to a URL variable, check for a cookie and get value from there if available
                if (document.cookie && document.cookie != '')
                {
                        var cookies = document.cookie.split(';');
                        for (var i = 0; i < cookies.length; i++)
                        {
                                var cookie = jQuery.trim(cookies[i]);
                                if (cookie.substring(0, campCookieName.length + 1) == (campCookieName + '='))
                                {
                                        campId = decodeURIComponent(cookie.substring(campCookieName.length + 1));
                                        break;
                                }
                        }
                }
        }
        return campId;
}

function getCampaignIdOnce( cookieDomain )
{
   var setCookieName = "hswi_set_dscid";
   if (typeof(getCampaignIdOnce.campId) == "undefined")
   {
       getCampaignIdOnce.campId = "";
       var campId = getCampaignId( cookieDomain );
       if (document.cookie && document.cookie != '')
       {
           var cookies = document.cookie.split(';');
           for(var i = 0; i < cookies.length; i++)
           {
               var cookie = jQuery.trim(cookies[i]);
               if (cookie.substring(0, setCookieName.length + 1) == (setCookieName + '='))
               {
                   if (decodeURIComponent(cookie.substring(setCookieName.length + 1)) == "1")
                   {
                       getCampaignIdOnce.campId = campId;
                   }
               }
           }
       }
   }
   if (typeof(getCampaignIdOnce.writeSetCookie) == "undefined")
   {
       getCampaignIdOnce.writeSetCookie = "no";
   }
   var today = new Date();
   var expires_date = new Date(today.getTime() + (30 * 1000 * 60 * 60 * 24));
   document.cookie = setCookieName + "=0;expires=Thu, 01-Jan-70 00:00:01 GMT;domain=" + cookieDomain + ";path=/";
   return getCampaignIdOnce.campId;
}
function validateStandardDisableSubmits(form,strErrorClass){
            
            $(form).find(':submit').attr("disabled",true);
            
            var myReturn =  validateStandard(form, strErrorClass);

            if(myReturn){
                return true;
            }
            else{
               $(form).find(':submit').removeAttr("disabled"); 
                return false;
            }
}
