/**
 * Global JavaScript initialization file
 * 
 * @author Matthew Dunham <mdunham@brooksidestudios.com>
 * @author Cornell Campbell <ccampbell@brooksidestudios.com>
 */

/* @define URL - Absolute base url */

/**
 * Convience method for logging a string to console
 */
function log() {
	if (! this.console || typeof console.log == "undefined") {return;} // ie fix
    for (var i = 0; i < arguments.length; i++) {
        console.log(arguments[i]);
    }
}

jQuery(function() {
	
	$('input, textarea').placeholder();

	/**
	 * Home Page Sliders Initialization
	 */
	if ($('div.slider').length) {
		setTimeout(function(){ window.location = window.location; }, (60 * 1000) * 30);
		$('div.slider').staggerSlider();
	}
	
	/**
	 * Twitter Widget
	 */
	if ($('#twttr').length) {
		new TWTR.Widget({
		  version: 2,
		  type: 'profile',
		  rpp: 1,
		  id: 'twttr',
		  interval: 30000,
		  width: 190,
		  height: 245,
		  theme: {
			shell: {
			  background: '#ffffff',
			  color: '#936e07'
			},
			tweets: {
			  background: '#ffffff',
			  color: '#000000',
			  links: '#936e07'
			}
		  },
		  features: {
			scrollbar: false,
			loop: false,
			live: false,
			behavior: 'all'
		  }
		}).render().setUser('AmeristarFence').start();
	}
	
	/**
	 * Main Menu Initialization
	 */
	if ($('ul.main-nav-ul').length) {
		$('ul.main-nav-ul li').each(function() {
			var $this = $(this),
				ubermenuid = $this.data('uber'),
				has_menu = ubermenuid !== undefined;

			if (has_menu) {
				var $ubermenu = $(ubermenuid);
				$this.bind('mouseenter', function() {
					$ubermenu.siblings('.ubermenu').hide();
					$ubermenu.show();
				});
				$ubermenu.bind('mouseleave', function() {
					$ubermenu.hide();
				});
			}
		});
	}
	
	/**
	 * Page search validation
	 */
	if ($('#top-search').length) {
		$('#top-search').submit(function(e){
			if ( ! $(this).find('input[type=text]').val() || $(this).find('input[type=text]').val() == 'Search') {
				e.preventDefault();
				var $_item = $(this).find('input[type=text]');
				$_item.unbind('placeholder').val('');
				alert('You must enter a search term first');
				$_item.focus().val('');
				setTimeout(function(){ $_item.val(''); }, 100);
				return false;
			}
		});
	}
	
	/**
	 * Fancy Box Gallery
	 */
	if ($('#photo-gallery a').length) {
		$('#photo-gallery a[rel=group]').fancybox({
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
	}
	
	/**
	 * Generic galleries
	 */
	if ($('.photo-gallery a').length) {
		$('.photo-gallery a[rel=group]').fancybox({
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
	}
	
	/**
	 * FancyBox Tabs
	 */
	if ($('a.ajax_open').length) {
		$('a.ajax_open').fancybox({
			'onComplete' : function() {
				$("div").unbind('mousewheel.fb');
				$("div").unbind('mousewheel')
			},
			'hideOnContentClick': true,
			'autoDimensions': false,
			'width': 800,
			'height': 600,
			'hideOnOverlayClick': true,
			'hideOnContentClick': false
		});
	}
	
	/**
	 * Check for photo gallery hashtags
	 */
	
	var hiddenHashes = $('table[id]:hidden');
	
	if (hiddenHashes.length) {
		$('a').click(function(e){
			if ($(this).attr('href').substr(0, 1) == '#') {
				var hash_value1 = $(this).attr('href').replace('#', '');
				if ($('#'+hash_value1).length && $('#'+hash_value1).is(':visible')) {
					$('#'+hash_value1).fadeOut();
					return false;
				} else {
					$('#'+hash_value1).fadeIn();
					return false;
				}
			}
		});
	}

	if (window.location.hash) {
		//set the value as a variable, and remove the #
		var hash_value = window.location.hash.replace('#', '');

		if ($('a[name='+hash_value+']').length) {
			$('a[name='+hash_value+']').click();
		} else if ($('#'+hash_value).length) {
			$('#'+hash_value).fadeIn();
		}
	}

	$(window).hashchange( function(){
		//set the value as a variable, and remove the #
		var hash_value = window.location.hash.replace('#', '');
		
		

		if ($('a[name='+hash_value+']').length) {
			$('a[name='+hash_value+']').click();
		} else if ($('#'+hash_value).length) {
			hiddenHashes.filter(':visible').fadeOut();
			$('#'+hash_value).fadeIn();
		}
	});

	/**
	 * Fence Applications Slide-up Initialization
	 */
	if ($('#fence-types').length) {
		$('#fence-types article').each(function() {
			var $this = $(this),
				$holder = $this.find('div.holder'),
				$nav = $holder.find('nav'),
				$bg = $holder.find('div.bg');
			
			$holder.data('oheight', $holder.css('height'));
			$nav.data('oheight', $nav.css('height'));
			$bg.data('oheight', $bg.css('height'));

			$this.hover(
				function() {
					$holder.dequeue().stop().animate({'height': '201px'}, 300);
					$nav.dequeue().stop().animate({'height': '141px'}, 300);
					$bg.dequeue().stop().animate({'height': '200px'}, 300);
				},
				function() {
					$holder.dequeue().stop().animate({'height': $holder.data('oheight')}, 300);
					$nav.dequeue().stop().animate({'height': $nav.data('oheight')}, 300);
					$bg.dequeue().stop().animate({'height': $bg.data('oheight')}, 300);
				}
			);
		});
	}
	
	/**
	 * Panel Accordions Initialization
	 */
	if ($('ul.panels').length) {
		var $panels = $('ul.panels');
		$panels.find('article:not(.ajax_open)').hide();
		
		$('a.panel-open:not(.ajax_open):not(.goto)').each(function() {
			var $this = $(this),
				$parent = $this.parents('li'),
				$article = $parent.find('article');
			
			
			
			$this.click(function(e) {
				is_open = $parent.hasClass('open');
				
				if (is_open) {
					$parent.removeClass('open');
					$article.slideUp('fast');
				} else {
					$open = $('li.open');
					if ($open.length) {
						$open.removeClass('open')
						$open.find('article').slideUp('fast');
					}
					$parent.addClass('open')
					$article.slideDown('fast');
				}
				e.preventDefault();
			});
		});
		$('a.panel-close').each(function() {
			var $this = $(this),
				$parent = $this.parents('li'),
				$btn = $parent.find('a.panel-open');
			
			$(this).click(function(e) {
				$btn.trigger('click');
				e.preventDefault();
			});
		});
	}
	
	/**
	 * Iframe Fix for YouTube
	 */
	$('iframe').each(function(){
		var url = $(this).attr("src");
		if (typeof url == 'undefined') return;
		$(this).attr("src", url+"?wmode=transparent");
	});
	
	/**
	 * Header Photostrip Initialization
	 */
	if ($('#strip').length) {
		var $original = $('#strip ul.original'),
			$reverse = $original.clone();
		
		$reverse.removeClass('original').addClass('reverse').insertAfter($original);
	}
});
