var resizeText = function (size) {   
	var myCookie = jQuery.cookie('fontSize', size); 
	redrawFonts(size);
} 

var redrawFonts = function(size) {
	$body = jQuery("body").removeClass('resize1');
	$body.removeClass('resize2');
	$body.removeClass('resize3');
	$body.addClass('resize' + String(size));
}

	initializePage = function (id, primaryid) {
		
		jQuery('a[href$=".pdf"]').addClass("pdflink");
		
		
		var pushpane = function(e,idx){	
			location.hash = 'Pane'+(idx+1);
			//jQuery.bbq.pushState({ pane: (idx) }); 
			//jQuery(".accordionGroup h2[href='#"+ idx +"']").trigger('click'); 
		}
		
		
			
		// Tabs
		jQuery("#tabbedContent > div.tabs").tabs("#tabbedContent > div.panes > div",{ onClick: pushpane }); 
		jQuery("#tabbedContentNews > div.tabs").tabs("#tabbedContentNews > div.panes > div",{ onClick: pushpane }); 
		
		var tcapi = jQuery("#tabbedContent > div.tabs").data('tabs');
		//console.log(tcapi)
		// Attorney Tabs
		jQuery("#attyTabbedContent > div.attyTabs").tabs("#attyTabbedContent > div.attyPanes > div", {
			effect: 'fade',onClick: pushpane
		});
		
		// Accordions
		jQuery(".accordion").tabs(".accordion div.pane", {tabs: 'h2', effect: 'slide', initialIndex: 0, onClick: pushpane});
		
		var hashes = location.hash.split("#");
		for (var i = 1; i < hashes.length; i++) {
			// get your tabs instance
			jQuery(".accordionGroup h2[href='#"+ hashes[i] +"']").trigger('click'); 
		}
		
		/*
  		jQuery(window).bind( "hashchange", function(e) {
  		  var pane = jQuery.bbq.getState("pane");
  		  jQuery('#Pane'+pane).parent().click();
			console.log(pane)
  		});
		*/
  		//jQuery(window).trigger("hashchange");
		
		jQuery("#expandall").bind("click", function(){
			jQuery(".accordion div.pane").css({ 'display': 'block' });
			jQuery("#"+this.id).css({ 'display': 'none' });
			jQuery("#closeall").css({ 'display': 'block' });
		});
		
		jQuery("#closeall").bind("click", function(){
			jQuery(".accordion div.pane").css({ 'display': 'none' });
			jQuery(".accordion").tabs(".accordion div.pane", {tabs: 'h2', effect: 'slide', initialIndex: 0});
			jQuery("#"+this.id).css({ 'display': 'none' });
			jQuery("#expandall").css({ 'display': 'block' });
		});
		
		
		  /* FONT SIZE */
	/*var gp = readCookie('customfontsize');
	var sizeInc = 'user3'; 
	var sizeDec = 'user2';
	if (gp) {
		changeFontSize(gp);
	} else {
		//alert('no cookie exists');
		createCookie('customfontsize','user1',7)
	}
	//alert('gp: '+gp+', sizeInc: '+sizeInc+', sizeDec: '+sizeDec);

	if(jQuery('a#resetlink')) {
		jQuery("a#resetlink").attr('onclick', 'javascript:changeFontSize(\'user1\')');
	}
	if(jQuery('a#declink')) {
		jQuery("a#declink").attr('onclick', 'javascript:changeFontSize(\''+ sizeDec +'\')');
	}
	if(jQuery('a#inclink')) {
		jQuery("a#inclink").attr('onclick', 'javascript:changeFontSize(\''+ sizeInc +'\')');
	}*/
		
		
		
		
		
		
		if (id == 1) {
			jQuery(".scrollable").scrollable({circular: true}).autoscroll({ autoplay: true, interval: 15000 });
			
			jQuery("form#newsletter").validate({
				rules: {
					 email: {
						 required: true,
						 email: true
					 }
				 },
				 messages: {
					 email: {
						 required: "Please enter your email address.",
						 email: "Please enter a valid email address."
					 }
				 }
			});
		}
		
		if (id == 4) {
			/* Shade Button Code */
			/* Shade Button Configurator */
			jQuery(".wsImage").each(function () {
				var $this = jQuery(this), myTarget = $this.parent().attr('href'), myLabel = $this.parent().attr('title'), myDescription = $this.attr('alt'), myWidth = $this.width(), myHeight = $this.height();
				
				$this.parent().wrap('<div class="shadebuttonwrapper" />').before('<div class="shadeclipper"><div class="shadebuttontab"><div class="shadeTop"><img src="/images/shadeTop.png" width="231" height="9"/></div><div class="shadebuttonBG"><div class="shadebuttondesc">' + myDescription + '<p align="right"><a href="'+ myTarget +'">Read more</a></p></div></div></div></div>', '<div onclick="document.location=\'' + myTarget + '\'" class="shadebuttontrigger" title=""><img src="/images/spacer.gif" alt="" /></div>');
				
				$this.after('<div class="shadebuttonlabel hand" onclick="document.location=\'' + myTarget + '\'">' + myLabel + '</div>');
				
				$this.parent().siblings('.shadeclipper, .shadebuttontrigger').css('width', String(myWidth + 'px')).css('height', String(myHeight + 'px')).find('.shadebuttontab').css('width', String(myWidth + 'px'));
				
				$this.parent().siblings('.shadebuttontrigger').find('img').css('width', String(myWidth + 'px')).css('height', String(myHeight + 'px'));
				//console.log($this.parent().siblings('.shadebuttontrigger').find('img').attr('src'));
			});
			
			/* Shadebutton Watcher */
			jQuery('.shadebuttontrigger').hover(function () {
				var $this = jQuery(this);
				$this.siblings('.shadeclipper').find('.shadebuttontab').animate({ marginTop: 57 }, 125, "swing");
			}, function () {
				var $this = jQuery(this);
				$this.siblings('.shadeclipper').find('.shadebuttontab').animate({ marginTop: 148 }, 500, "swing");
			});
		}
		
		if (id == 38) {
			jQuery('.newsTable').dataTable( {
				"bSort": false, 
				"bFilter": false,
				"bLengthChange": false,
				"iDisplayLength": 10,
				"sPaginationType": "full_numbers"
				} );
		}
		
		if (id == 5) {
			/*jQuery('.attyTable').dataTable( {
				"bSort": false, 
				"bFilter": false,
				"aoColumns": [
					null,
					null,
					null,
					null
				],
				"bLengthChange": false,
				"sPaginationType": "full_numbers"
				} );*/
				
			jQuery("form.jqtransform").jqTransform();	
			var srch='', letter='', oloc='';
			jQuery.fn.dataTableExt.oApi.fnFilterAll = function(oSettings, sInput, iColumn, bRegex, bSmart) { 
				if ( typeof bRegex == 'undefined' ) {
					bRegex = false;
				}
				
				if ( typeof bSmart == 'undefined' ) {
					bSmart = true;
				}
				
				for (var i in this.dataTableSettings) {
					 jQuery.fn.dataTableExt.iApiIndex = i;
					 //alert('filtering'+i+' : '+sInput)
					 this.fnFilter(sInput, iColumn, bRegex, bSmart);
				}
				jQuery.fn.dataTableExt.iApiIndex = 0;
			}
			
			var oTable;
			oTable = jQuery(".attyTable").dataTable( {
				"aoColumns": [ 
					{ "bVisible": false, "bSortable": false },
					null,
					{ "bSearchable": false, "bSortable": false },
					{ "bSearchable": false, "bSortable": false },
					null
				],
				"bSort": false,
				"sDom": 'lrtip',
				"bLengthChange": false,
				"sPaginationType": "full_numbers"
				
			});			
			
			jQuery("#attyname").keyup( function () {
				srch = jQuery(this).val();
				doFltr()
				//oTable.fnFilterAll(this.value);
			} );
			
			jQuery('.jqTransformSelectWrapper ul li a').click( function() {
				if (this.text == 'All Locations') {
					oloc = '';
					doFltr()
					//oTable.fnFilterAll("");
				} else {
					oloc = jQuery(this).text()
					doFltr()
					//oTable.fnFilterAll(this.text);
				}
			} );
			
			jQuery('.attyTabs a').click( function() {
				jQuery('.attyTabs a').removeClass("current");
				jQuery(this).addClass("current");
				letter = jQuery(this).text();
				doFltr()
				//oTable.fnFilterAll(this.text, 0);
			} );
			
			jQuery('a.search_go').click( function() {
				jQuery('#attyname').val('');
				jQuery('.jqTransformSelectWrapper ul li a').removeClass('selected');
				jQuery('.jqTransformSelectWrapper ul li a[index="0"]').addClass('selected');
				
				jQuery('.jqTransformSelectWrapper span').text('All Locations');
				jQuery('.attyTabs a').removeClass("current");
				
				
				srch=''; letter=''; oloc=''
				oTable.fnFilterAll("");
			} );
			
			//jQuery('#attysearch select').change( function() { oTable.fnFilter( jQuery(this).text() ); } );
	
			function doFltr(){
			//oTable.fnFilterAll(letter,0)
			oTable.fnFilterAll(srch + ' ^' + letter + ' ' + oloc)
			}
		}
		
		//jQuery('#mainContent').localScroll();
		
		/* Break Secondary Nav tabs to two lines */
		jQuery(".primary4 .secondaryNav ul li a, #tabbedContent .tabs div a").each( function() {
			var thisText = jQuery(this).html();
			if (thisText.indexOf(' ') != -1) {
				var midPoint = Math.floor(thisText.length/2 - 1);
				var spacePos = thisText.indexOf(' ', midPoint);
				if (spacePos == -1) {
					spacePos = thisText.substring(0, midPoint).lastIndexOf(' ');
				}
				thisText = thisText.substring(0,spacePos)+'<br/>'+thisText.substring(spacePos+1);
				jQuery(this).html(thisText);
			} else {
				jQuery(this).html('<span class="oneline">'+thisText+'</span>');
			}
		});
		
	}

/*function gotoAnchor(anch) {
	 jQuery.scrollTo('#'+anch, 800 );
}*/


var emailpage = function() {
	temploc='http://www.susmangodfrey.com/Email-Page/?myURL=';
	//temploc='http://173.203.65.83:9999/staged/Email_Page/?myURL=';
	temploc = temploc + window.location.href.replace(/\&/, '%26').replace(/\?/, '%3F');
	window.location.href = temploc;
}

var openpane = function(idx) {
	jQuery(".accordionGroup h2[href='#"+ idx +"']").trigger('click');
}

//jQuery(window).scroll($.debounce(1000, moveMe));
//jQuery(window).resize($.debounce(1000, moveMe));

   
 

function moveMe () {
	 var winPos = jQuery(window).scrollTop();
	 //var sideBarTop = jQuery('#leftSidebar').offset().top;
	 var sideBarTop = 220;
	 if (winPos > sideBarTop) {
		 //move to compensate
		 var difference = (winPos-sideBarTop);
		//alert(difference); 
		var totalOffset = (difference+20);
		 jQuery('#leftSidebar').stop().animate({
            marginTop: totalOffset
		}, {
            duration: 500,
            specialEasing: {
                top: 'linear'
            }
        });
		
	 } else {
		 //return home
		  jQuery('#leftSidebar').stop().animate({
            marginTop: 0
		}, {
            duration: 500,
            specialEasing: {
                top: 'linear'
            }
        });
	 }
}

/**
 * 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
 *
 */

// TODO JsDoc

/**
 * Create a cookie with the given key and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String key The key of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given key.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String key The key of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
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 : encodeURIComponent(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;
};
