/*
	Website 23.0 (Scott Bradford: Off on a Tangent)
	Copyright (c) 1995-2011, Scott Bradford Creative Enterprises
*/

// override with correct ThickBox image paths
var tb_pathToImage = '/wp-includes/js/thickbox/loadingAnimation.gif',
    tb_closeImage = '/wp-includes/js/thickbox/tb-close.png';

ooat.status = {
	isMenuPinned: false,
	isQuotesPinned: false,
	isQuotesVisible: false,
	isWindowFocused: false,
	needsPhotoCycle: false,
	needsQuoteCycle: false
};

// Chrome now behaves very weird with window/tab focus, so we do our best to detect the state and pause background execution
// (quotes and header photos) when the tab or browser isn't focused.

// create focusHack object
ooat.focusHack = {};

// function when blurred
ooat.focusHack.blur = function() {
	if (ooat.status.isWindowFocused) {
		ooat.status.isWindowFocused = false;
	}
};

// function when focused
ooat.focusHack.focus = function() {
	if (!ooat.status.isWindowFocused) {
		ooat.status.isWindowFocused = true;
	
		if (ooat.status.needsPhotoCycle) {
			ooat.photos.flipImage();
		} else {
			ooat.photos.setTimer();
		}
		
		if (ooat.status.needsQuotesCycle) {
			ooat.quotescollection.iterate();
		} else {
			ooat.quotescollection.setTimer();
		}
	}
};

// function to randomly rotate one of the photos in the photo bars
ooat.photos.flipImage = function() {
	var i = (Math.floor(5*Math.random())),
	    hold = ooat.photos[i],
	    pos =  ooat.photos[i] + 'px 0px';
	while (jQuery.inArray(hold,ooat.photos) != -1) {
		hold=(Math.floor(ooat.maximg*Math.random())*(-320));
	}
	ooat.photos[i]=hold;
	jQuery('.sphotos .p' + i).fadeTo('slow',0.01,function(){
		jQuery(this).css('backgroundPosition',pos).fadeTo('slow',0.15);
	});
	
	if (ooat.status.isWindowFocused && !ooat.status.isMenuPinned) {
		ooat.status.needsPhotoCycle = false;
		ooat.photos.setTimer();
	} else {
		ooat.status.needsPhotoCycle = true;
	}
}

// function to set the photo bar timer
ooat.photos.setTimer = function() {
	ooat.photos.timer = clearTimeout(ooat.photos.timer);
	ooat.photos.timer = setTimeout( "ooat.photos.flipImage()", 10000 );
}

// logic to handle the custom quotes-collection widget
ooat.quotescollection_init = function(auto_refresh) {
	
	// set up the runtime variables
	ooat.quotescollection = {
		quotes: [],
		position: 0,
		auto_refresh: auto_refresh,
		topPos: jQuery('.sbot').offset().top
	};
	
	// if auto-refresh isn't turned on at all, just set the 'paused' var to true to disable auto-refresh
	if (auto_refresh <= 0) {
		ooat.settings.qcp = true;
	}

	// function to set the quotes timer
	ooat.quotescollection.setTimer = function() {
		ooat.quotescollection.timer = clearTimeout(ooat.quotescollection.timer);
		ooat.quotescollection.timer = setTimeout( "ooat.quotescollection.iterate()", ooat.quotescollection.auto_refresh*1000 );
	}

	// function to auto-iterate (also used for manual iteration)
	ooat.quotescollection.iterate = function(auto_refresh) {		
		if (ooat.status.isWindowFocused && ooat.status.isQuotesVisible) {
			ooat.status.needsQuoteCycle = false;
			
			// increase position by 1 and display the quote
			ooat.quotescollection.position++;
			ooat.quotescollection.showQuote(ooat.quotescollection.position);
			
			// if we're running out of quotes, fire the ajax request to get more
			if (ooat.quotescollection.position >= (ooat.quotescollection.quotes.length-2)) {
				ooat.quotescollection.morequotes();
			}
			
			ooat.quotescollection.setTimer();
		} else {
			ooat.status.needsQuoteCycle = true;
		}
	}
	
	// function to display the current quote
	ooat.quotescollection.showQuote = function(pos) {
		
		// fade all the controls in
		jQuery('.qc_controls').fadeTo(0,1);

		// if we're at position 0, hide the 'previous' button
		if (pos == 0) {
			jQuery('#qc_back').fadeTo(0,0);
			
		// if we're at the last position (should never happen, since it should keep loading more), hide the 'next' button
		} else if (pos == (ooat.quotescollection.quotes.length-1)) {
			jQuery('#qc_next').fadeTo(0,0);
		}
		
		// hide the old quote, then insert the new one and fade it in
		jQuery(".quotescollection_randomquote").fadeTo(0,0);
		jQuery(".quotescollection_randomquote").html( ooat.quotescollection.quotes[pos] );
		jQuery(".quotescollection_randomquote").fadeTo('slow',1);
	};
	
	// function to load more quotes (initially and as-needed after)
	ooat.quotescollection.morequotes = function(){
		jQuery.ajax({
			type: 'GET',
			url: '/wp-admin/admin-ajax.php',
			data: {action:'ooat_quotescollection_getquotes',rand:ooat.rand()},
			dataType: 'json',
			error: function(err) {
				// if there's an error, output it to the JS console
				console.log('quotes error: ' + err);
			},
			success: function(data) {
				
				// add the new quotes to the existing (though possibly empty) array of quotes
				ooat.quotescollection.quotes = ooat.quotescollection.quotes.concat(data);
				
				// if we're at position 0 (that is, initial page load), display the quote...otherwise wait until the iteration happens on a timer or by click
				if (ooat.quotescollection.position == 0) {
					// show the quote
					ooat.quotescollection.showQuote(ooat.quotescollection.position);
				}
			}
		});
	};
	
	// initial execution of the quote loader (when kicked-off by the PHP)
	ooat.quotescollection.morequotes();
	
	// back button listener
	jQuery('#qc_back').click(function(){
		
		// only execute if the position is not 0 (prevents weird spurious fading if you click the hidden [but still clickable] back button)
		if (ooat.quotescollection.position > 0) {
			
			// clear the existing timer
			clearTimeout(ooat.quotescollection.timer);
			
			// reverse-iterate the position
			ooat.quotescollection.position-=1;
			
			// show the quote
			ooat.quotescollection.showQuote(ooat.quotescollection.position);
			
			// set the timer
			ooat.quotescollection.setTimer();
		}
		return false;
	});
	
	// next button listener
	jQuery('#qc_next').click(function(){
		
		// clear the existing timer
		clearTimeout(ooat.quotescollection.timer);
		
		// run the iterator (same as if it had auto-iterated)
		ooat.quotescollection.iterate(auto_refresh);
		
		// set the timer
		ooat.quotescollection.setTimer();
		
		return false;
	});
	
	// pause/resume button listener
	jQuery('#qc_pause').click(function(){
		
		// clear the existing timer before doing anything
		clearTimeout(ooat.quotescollection.timer);
		
		// if we're paused before the click
		if (ooat.settings.qcp) {
			
			// un-pause us
			ooat.settings.qcp = false;
			
			// set the timer
			ooat.quotescollection.setTimer();
			
			// change the button text
			jQuery(this).html('Pause')
			
		// if we're un-paused before the click
		} else {
			
			// pause us
			ooat.settings.qcp = true;
			
			// change the button text
			jQuery(this).html('Resume')
		}
		
		ooat.saveCookie();
		return false;
	});
	
	// change text to 'resume' if we're in a paused state on load
	if (ooat.settings.qcp) {
		jQuery('#qc_pause').html('Resume');
	}
};

// show the holiday business
ooat.holiday.show = function(holiday) {
	var hout = '<div class="h-pop"><div class="h-img"><img src="/wp-content/themes/sb-web23.0/images/holiday/' + holiday[0] + '.jpg" alt="' + holiday[1] + '" /></div><div class="h-txt"><h4>' + holiday[1] + ':</h4>' + holiday[2] + ' <a href="' + holiday[3] + '" target="_blank">More&hellip;</a></div><div class="clr"></div></div>';
	jQuery('.header .cbase').append(hout);
}

// do all this stuff when the document is ready
jQuery(document).ready(function(){
	
	// are we supposed to be redirecting to the mobile site?
	// the PHP should have handled this, but, if not, it's our turn.
	ooat.mobile.link = jQuery('.mlink a').attr('href');
	if (ooat.settings.mob == 'mobi') {
		document.location.href = ooat.mobile.link;
	}
	
	// add pretty arrows to menu items that have submenus
	jQuery('#nav li ul li:has(ul)').addClass('exp');

	// delinkify menu links with an href of '#nolink' (these are placeholders)
	jQuery('a[href="#nolink"]').removeAttr("href");

	// Thickboxify images in entries
	jQuery(".entry").each(function(i) {
		jQuery("a:has(img)",this).not(".nopop").addClass("thickbox").prop("rel",i).each(function() {
			linkTitle=jQuery(this).attr("title");
			if ((linkTitle=="") || (typeof linkTitle == 'undefined')) {
				imgTitle=jQuery("img",this).attr("title");
				jQuery(this).attr("title",imgTitle);
			}
		});
	});
	
	// if the tab is blurred, run the focusHack blur function
	jQuery(window).blur(function(){ ooat.focusHack.blur(); });
	
	// if the tab is focused, set the var back to true and reinitiate the timers
	jQuery(window).focus(function(){ ooat.focusHack.focus(); })
	
	// if the mouse is moving, we can assume we're focused (part of the focusHack)
	jQuery(window).mousemove(function(){ ooat.focusHack.focus(); });
	
	// handle page scrolling
	jQuery(window).scroll(function() {
		
		// we can assume we're focused if we're scrolling
		ooat.focusHack.focus();
		
		var scrollPos = jQuery(window).scrollTop();
		if (ooat.settings.pin) {
			
			// handle the menu pinning
			if( (!ooat.status.isMenuPinned) && (scrollPos >= 85) ) {
				jQuery('.container.menu').addClass('pin');
				ooat.status.isMenuPinned = true;
			} else if ( (ooat.status.isMenuPinned) && (scrollPos < 85)) {
				jQuery('.container.menu').removeClass('pin');
				ooat.status.isMenuPinned = false;
				if (ooat.status.needsPhotoCycle) {
					ooat.photos.flipImage();
				}
			}
			
			// handle the quotes
			var docBot = scrollPos + jQuery(window).height() - 80,
			    quoTop = ooat.quotescollection.topPos;
			
			// quotes visible detection
			if ((!ooat.status.isQuotesVisible) && ((ooat.status.isQuotesPinned) || (docBot > quoTop))) {
				ooat.status.isQuotesVisible = true;
				if (ooat.status.needsQuoteCycle) {
					ooat.quotescollection.iterate();
				} else {
					ooat.quotescollection.setTimer();
				}
			} else if ((ooat.status.isQuotesVisible) && (!ooat.status.isQuotesPinned) && (docBot <= quoTop)) {
				ooat.status.isQuotesVisible = false;
			}
			
			// only do the quotes pinning if it's a long enough page
			if(((jQuery(document).height())-quoTop) >= 700) {
				
				// quotes pinning detection
				if ((!ooat.status.isQuotesPinned) && ((scrollPos) > quoTop)) {
					jQuery('.sbot').addClass('float');
					ooat.status.isQuotesPinned = true;
				} else if ((ooat.status.isQuotesPinned) && ((scrollPos) <= quoTop)) {
					jQuery('.sbot').removeClass('float');
					ooat.status.isQuotesPinned = false;
				}
			
			}
			
			
		}
	});
	
	// Low Resolution or Match on Browser String? Encourage the Mobile Site
	if (((jQuery(window).width() < 990) || (jQuery.browser.mobile == true)) && (ooat.settings.mob !== "nevr")) {

		ooat.mobile.warn = '<div class="mwarn"><p>Based on your browser type or window size, we think you might have a better experience on the mobile site.</p><p><a href="' + ooat.mobile.link + '" id="mobileAlways">Always Redirect Me to the Mobile Site</a> | <a href="#" id="mobileNever">Don\'t Ask Again</a></p></div>';
		
		jQuery(ooat.mobile.warn).prependTo('.mainbox').slideDown();
		
		jQuery('#mobileAlways').click(function(){
			ooat.settings.mob = 'mobi';
			ooat.saveCookie();
		});
		
		jQuery('#mobileNever').click(function(){
			ooat.settings.mob = 'nevr';
			ooat.saveCookie();
			jQuery('.mwarn').slideUp();
			return false;
		});
		
	}
	
	// Narrow the site slightly for 1024px displays
	if (jQuery(window).width() < 1050) {
		jQuery('body').addClass('narrow');
	}
});

/**
 * jQuery.browser.mobile (http://detectmobilebrowser.com/)
 *
 * jQuery.browser.mobile will be true if the browser is a mobile device
 *
 **/
(function(a){jQuery.browser.mobile=/android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))})(navigator.userAgent||navigator.vendor||window.opera);
