var gw = gw || {};
gw.core = gw.core || {};
gw.module = {};

gw.core.general = function(){
	jQuery('#sponsors div:nth-child(even)').css( 'margin-left', '10px');
	jQuery.localScroll();	
}

gw.core.initMenuFixes = function(){
	
	//Removed unerline from second to last element if the set is an even number
	jQuery('#menu-main-nav ul').each(function(){
		jQuery(this).children('li:last').css( { 'padding-bottom': '12px', 'margin-bottom' : '-16px',
		'background' : 'url(/wp-content/themes/gerweck/images/nav-sec-bg.png) no-repeat bottom center'
		});
		
		jQuery(this).children('li:last').children('a').css( 'border-bottom', '0');
	});
	
	jQuery('#menu-global-nav').children('li:first a').attr('href',"javascript:window.external.AddFavorite('http://www.gerweck.net', 'Gerweck.net')");
}

gw.core.initTwoColMenu = function(){
	jQuery("li:has(a:contains('TV Ratings'))").addClass('double');
	var numItems = jQuery("li:has(a:contains('TV Ratings')) li").length;
	var counter = numItems/2;
	jQuery("li:has(a:contains('TV Ratings')) li").each(function(){
		jQuery(this).children('a').css({ 'width': '65px', 'text-align':'center' } );
		if( jQuery(this).index() > numItems/2-1 ) {
			counter--;
			jQuery(this).css('margin-left', '75px');
			jQuery(this).css('margin-top', -1.5*(counter+1) + 'em');
		}
	});
	jQuery("li:has(a:contains('TV Ratings')) li:last").css( { 'background': 'none' } );
	jQuery("li:has(a:contains('TV Ratings')) ul").append('<span style="display:block;padding-bottom:12px;margin-bottom:-16px;background:url(/wp-content/themes/gerweck/images/nav-sec-bg.png) no-repeat bottom center;clear:both"></span>');
}

gw.core.initSuperfish = function(){
	// initialise Superfish 
	jQuery("#menu-main-nav").superfish(); 
}

gw.module.SocialBar = function (p_xDOMElement, p_xContainerDOMElement) {
	this._element = jQuery(p_xDOMElement);
	this._container = jQuery(p_xContainerDOMElement);
	this.initialize();
};

gw.module.SocialBar.prototype = {
	initialize: function () {
		var that = this,
			$this = jQuery(this),
			$doc = jQuery(document);
		this.states = {
			PINNED_TOP: "pinnedTop",
			FLOATING: "floating",
			PINNED_BOTTOM: "pinnedBottom"
		};
		this.determine = {
			pinnedTop: function () {
				if (this._offset > this._topheight) {
					return this.setState(this.states.FLOATING);
				}
			},
			floating: function () {
				if (this._offset < this._topheight) {
					return this.setState(this.states.PINNED_TOP);
				}
				if (this._offset >= this._containerBottom + 10 ) {
					return this.setState(this.states.PINNED_BOTTOM);
				}
			},
			pinnedBottom: function () {
				if (this._offset < this._containerBottom + 10 ) {
					return this.setState(this.states.FLOATING);
				}
			}
		};
		this._offset = jQuery(window).scrollTop();
		this._topheight = this._element.offset().top;
		this._elementheight = this._element.outerHeight(true);
		this._containerBottom = this._container.offset().top + this._container.height();
		this.setState(this.states.PINNED_TOP);
		jQuery(window).bind('scroll', function () {
			that.viewportScrolled.apply(that)
		});
	},
	viewportScrolled: function () {
		this._offset = jQuery(window).scrollTop();
		this._elementbottom = this._element.offset().top + this._elementheight - this._offset;
		this._stateSwitcher();
	},
	setState: function (state) {
		if (this._currentState == state) {
			return;
		}
		if (this._currentState === undefined) {
			this._currentState = '';
		}
		this._element.removeClass(this._currentState);
		this._element.addClass(state);
		this._currentState = state;
		this._stateSwitcher = this.determine[state];
	}
};

jQuery(document).ready(function(){
	gw.core.general();
	gw.core.initMenuFixes();
	gw.core.initTwoColMenu();
	gw.core.initSuperfish();
});
